cxtream  0.5.1
C++17 data pipeline with Python bindings.
create.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_CREATE_HPP
11 #define CXTREAM_CORE_STREAM_CREATE_HPP
12 
13 #include <cxtream/core/utility/tuple.hpp>
14 
15 #include <range/v3/view/transform.hpp>
16 #include <range/v3/view/chunk.hpp>
17 
18 namespace cxtream::stream {
19 
20 namespace detail {
21 
22  template<typename... Columns>
23  struct create_impl {
24 
25  template<typename Source>
26  constexpr std::tuple<Columns...> operator()(Source&& source) const
27  {
28  return std::tuple<Columns...>{
29  utility::unzip_if<(sizeof...(Columns) > 1)>(std::forward<Source>(source))};
30  }
31 
32  };
33 
34  template<typename... Columns>
35  class create_fn {
36  private:
37  friend ranges::view::view_access;
38 
39  static auto bind(create_fn<Columns...> fun, std::size_t batch_size = 1)
40  {
41  return ranges::make_pipeable(std::bind(fun, std::placeholders::_1, batch_size));
42  }
43 
44  public:
45  template<typename Rng, CONCEPT_REQUIRES_(ranges::ForwardRange<Rng>())>
46  constexpr auto operator()(Rng&& rng, std::size_t batch_size = 1) const
47  {
49  ranges::view::chunk(std::forward<Rng>(rng), batch_size),
50  create_impl<Columns...>{});
51  }
52 
54  template<typename Rng, CONCEPT_REQUIRES_(!ranges::ForwardRange<Rng>())>
55  void operator()(Rng&&, std::size_t batch_size = 1) const
56  {
57  CONCEPT_ASSERT_MSG(ranges::ForwardRange<Rng>(),
58  "stream::create only works on ranges satisfying the ForwardRange concept.");
59  }
61  };
62 
63 } // namespace detail
64 
88 template<typename... Columns>
89 constexpr ranges::view::view<detail::create_fn<Columns...>> create{};
90 
91 } // end namespace cxtream::stream
92 #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
decltype(auto) unzip_if(RangeT &&range)
Unzips a range of tuples to a tuple of ranges if a constexpr condition holds.
Definition: tuple.hpp:468
constexpr ranges::view::view< detail::create_fn< Columns... > > create
Converts a range to a stream (i.e., to a range of tuples of columns).
Definition: create.hpp:89