HiPipe  0.6.0
C++17 data pipeline with Python bindings.
keep.hpp
1 /****************************************************************************
2  * hipipe library
3  * Copyright (c) 2018, Iterait a.s.
4  * Author(s) Adam Blazek
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 #pragma once
11 
12 #include <hipipe/core/stream/stream_t.hpp>
13 
14 #include <range/v3/view/transform.hpp>
15 
16 
17 namespace hipipe::stream {
18 
19 namespace detail {
20 
21  template<typename... Columns>
22  class keep_fn {
23  private:
24  friend ranges::view::view_access;
25 
26  static auto bind(keep_fn<Columns...> fun)
27  {
28  return ranges::make_pipeable(std::bind(fun, std::placeholders::_1));
29  }
30 
31  public:
32  template<typename Rng, CONCEPT_REQUIRES_(ranges::InputRange<Rng>())>
33  forward_stream_t operator()(Rng&& rng) const
34  {
35  return ranges::view::transform(std::forward<Rng>(rng),
36  [](batch_t batch) -> batch_t {
37  batch_t result;
38  (result.raw_insert_or_assign<Columns>(std::move(batch.at<Columns>())), ...);
39  return result;
40  });
41  }
42 
44  template<typename Rng, CONCEPT_REQUIRES_(!ranges::InputRange<Rng>())>
45  void operator()(Rng&&) const
46  {
47  CONCEPT_ASSERT_MSG(ranges::InputRange<Rng>(),
48  "stream::keep only works on ranges satisfying the InputRange concept.");
49  }
51  };
52 
53 } // namespace detail
54 
55 
66 template <typename... Columns>
67 ranges::view::view<detail::keep_fn<Columns...>> keep{};
68 
69 } // end namespace hipipe::stream
ranges::view::view< detail::keep_fn< Columns... > > keep
Keep the specified columns in the stream, drop everything else.
Definition: keep.hpp:67
ranges::any_view< batch_t, ranges::category::forward > forward_stream_t
The stream itself, i.e., a range of batches.
Definition: stream_t.hpp:24
auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of hipipe columns to a different subset of hipipe columns.
Definition: transform.hpp:187
Definition: ndim.hpp:144