HiPipe  0.7.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 rgv = ranges::views;
20 
21 namespace detail {
22 
23  template<typename... Columns>
24  class keep_fn {
25  private:
26  friend rgv::view_access;
27 
28  static auto bind(keep_fn<Columns...> fun)
29  {
30  return ranges::make_pipeable(std::bind(fun, std::placeholders::_1));
31  }
32 
33  public:
34  CPP_template(typename Rng)(requires ranges::input_range<Rng>)
35  forward_stream_t operator()(Rng&& rng) const
36  {
37  return rgv::transform(std::forward<Rng>(rng),
38  [](batch_t batch) -> batch_t {
39  batch_t result;
40  (result.raw_insert_or_assign<Columns>(std::move(batch.at<Columns>())), ...);
41  return result;
42  });
43  }
44 
46  CPP_template(typename Rng)(requires !ranges::input_range<Rng>)
47  void operator()(Rng&&) const
48  {
49  CONCEPT_ASSERT_MSG(ranges::input_range<Rng>(),
50  "stream::keep only works on ranges satisfying the input_range concept.");
51  }
53  };
54 
55 } // namespace detail
56 
57 
68 template <typename... Columns>
69 rgv::view<detail::keep_fn<Columns...>> keep{};
70 
71 } // end namespace hipipe::stream
hipipe::utility::CPP_template
CPP_template(class Rng)(requires ranges
Unzips a range of tuples to a tuple of std::vectors.
Definition: tuple.hpp:255
hipipe::stream::forward_stream_t
ranges::any_view< batch_t, ranges::category::forward > forward_stream_t
The stream itself, i.e., a range of batches.
Definition: stream_t.hpp:29
hipipe::stream::keep
rgv::view< detail::keep_fn< Columns... > > keep
Keep the specified columns in the stream, drop everything else.
Definition: keep.hpp:74
hipipe::stream::transform
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:218