cxtream  0.5.1
C++17 data pipeline with Python bindings.
generate.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_GENERATE_HPP
11 #define CXTREAM_CORE_STREAM_GENERATE_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 ToColumn, typename Gen, int Dim>
21  struct wrap_generate_fun_for_transform {
22  Gen gen;
23  long gendims;
24 
25  typename ToColumn::batch_type operator()(typename FromColumn::batch_type& source)
26  {
27  using SourceVector = typename FromColumn::batch_type;
28  using TargetVector = typename ToColumn::batch_type;
29  constexpr long SourceDims = utility::ndims<SourceVector>::value;
30  static_assert(Dim <= SourceDims, "stream::generate requires"
31  " the dimension in which to apply the generator to be at most the number"
32  " of dimensions of the source column (i.e., the column the shape is taken"
33  " from).");
34  // get the size of the source up to the dimension of the target
35  std::vector<std::vector<long>> target_size = utility::ndim_size<Dim>(source);
36  // create, resize, and fill the target using the generator
37  TargetVector target;
38  utility::ndim_resize<Dim>(target, target_size);
39  utility::generate<Dim>(target, gen, gendims);
40  return target;
41  }
42  };
43 
44 } // namespace detail
45 
74 template<typename FromColumn, typename ToColumn, typename Gen,
75  int Dim = utility::ndims<typename ToColumn::batch_type>::value
76  - utility::ndims<std::result_of_t<Gen()>>::value>
77 constexpr auto generate(from_t<FromColumn> size_from,
78  to_t<ToColumn> fill_to,
79  Gen gen,
80  long gendims = std::numeric_limits<long>::max(),
81  dim_t<Dim> d = dim_t<Dim>{})
82 {
83  detail::wrap_generate_fun_for_transform<FromColumn, ToColumn, Gen, Dim>
84  trans_fun{std::move(gen), gendims};
85  return stream::transform(from<FromColumn>, to<ToColumn>, std::move(trans_fun), dim<0>);
86 }
87 
88 } // namespace cxtream::stream
89 #endif
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
constexpr void generate(Rng &&rng, Gen &&gen, long gendims=std::numeric_limits< long >::max())
Fill a multidimensional range with values generated by a nullary function.
Definition: vector.hpp:605