cxtream  0.5.1
C++17 data pipeline with Python bindings.
pad.hpp
1 /****************************************************************************
2  * cxtream library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Author(s) Filip Matzner
5  *
6  * This file is distributed under the MIT License.
7  * See the accompanying file LICENSE.txt for the complete license agreement.
8  ****************************************************************************/
9 
10 #ifndef CXTREAM_CORE_STREAM_PAD_HPP
11 #define CXTREAM_CORE_STREAM_PAD_HPP
12 
13 #include <cxtream/core/stream/transform.hpp>
14 #include <cxtream/core/utility/vector.hpp>
15 
16 namespace cxtream::stream {
17 namespace detail {
18 
19  // Create a transformation function that can be sent to stream::transform.
20  template<typename FromColumn, typename MaskColumn, typename ValT>
21  struct wrap_pad_fun_for_transform {
22  ValT value;
23 
24  std::tuple<typename FromColumn::batch_type, typename MaskColumn::batch_type>
25  operator()(typename FromColumn::batch_type& source)
26  {
27  using SourceVector = typename FromColumn::batch_type;
28  using MaskVector = typename MaskColumn::batch_type;
29  constexpr long SourceDims = utility::ndims<SourceVector>::value;
30  constexpr long MaskDims = utility::ndims<MaskVector>::value;
31  static_assert(MaskDims <= SourceDims, "stream::pad requires"
32  " the number of padded dimensions (i.e., the number of dimensions"
33  " of the mask) to be at most the number of dimensions of the source column.");
34  // create the positive mask
35  std::vector<std::vector<long>> source_size = utility::ndim_size<MaskDims>(source);
36  MaskVector mask;
37  utility::ndim_resize<MaskDims>(mask, source_size, true);
38  // pad the source
39  utility::ndim_pad<MaskDims>(source, value);
40  // create the negative mask
41  source_size = utility::ndim_size<MaskDims>(source);
42  utility::ndim_resize<MaskDims>(mask, source_size, false);
43  return {std::move(source), std::move(mask)};
44  }
45  };
46 
47 } // namespace detail
48 
81 template<
82  typename FromColumn, typename MaskColumn,
83  // The value type is automatically deduced as the type of the source column
84  // in the dimension of the mask column.
85  typename ValT =
86  typename utility::ndim_type_t<
87  typename FromColumn::batch_type,
88  utility::ndims<typename MaskColumn::batch_type>::value>>
89 constexpr auto pad(from_t<FromColumn> f, mask_t<MaskColumn> m, ValT value = ValT{})
90 {
91  detail::wrap_pad_fun_for_transform<FromColumn, MaskColumn, ValT>
92  trans_fun{std::move(value)};
93  return stream::transform(from<FromColumn>, to<FromColumn, MaskColumn>,
94  std::move(trans_fun), dim<0>);
95 }
96 
97 } // namespace cxtream::stream
98 #endif
constexpr auto pad(from_t< FromColumn > f, mask_t< MaskColumn > m, ValT value=ValT{})
Pad the selected column to a rectangular size.
Definition: pad.hpp:89
constexpr auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of cxtream columns to a different subset of cxtream columns.
Definition: transform.hpp:177