Support Class Library
A set of tools providing classes and utility
uniqueBy.h
Go to the documentation of this file.
1 #pragma once
4 #include <functional>
5 #include <memory>
8 #include <unordered_set>
9 #include <scl/stream/Stream.h>
10 
11 namespace scl{
12  namespace stream{
13  namespace operators{
14  namespace details{
20  template <class T, class U>
22  public:
26 
28  using parent_value_type = typename parent_iterator_type::value_type;
29  using parent_payload_type = typename parent_iterator_type::payload_type;
31 
36  using key_type = U;
37 
42  using mapper_type = std::function<key_type(const value_type&)>;
43 
50  }
51 
52  payload_type next() override{
53  bool condition = true;
54 
55  while(condition && this->parent()->hasNext()){
56  const auto& alt = this->parent()->next().value();
57  if(!alt.hasValue())
58  continue;
59 
60  const auto& value = *alt;
61  auto id = this->key(value);
62  condition = !this->tagged.count(id); //count(id) == 0 => not found
64 
65  if(condition){
66  this->tagged.insert(id);
67  return payload_type::withValue(value);
68  }
69  }
70 
71  return payload_type::withoutValue();
72  }
73 
74  protected:
80 
85  std::unordered_set<key_type> tagged;
86  };
87 
93  template <class T, class U>
100 
106  };
107  }
108 
118  template <
119  class F,
120  class Fn = decltype(META::as_fn(std::declval<F>())),
121  class T = META::decay_t<META::arg_t<Fn, 0>>,
122  class U = META::decay_t<META::return_t<Fn>>
123  >
125  return {META::as_fn(mapper)};
126  }
127 
135  template <class T, class U>
137  return {mapper};
138  }
139 
147  template <class T, class U>
149  using namespace scl::tools;
150  return Stream<T>{
151  make::ptr<details::UniqueByOperator<T, U>>(lhs.it(), rhs.mapper)
152  };
153  }
154  }
155  }
156 }
map_t mapper
the key computation function
Definition: uniqueBy.h:105
Abstract base class for stream iterators defining the required behavior.
Definition: StlAdapter.h:11
typename iterator_type::parent_iterator_type parent_iterator_type
Definition: uniqueBy.h:27
parent_type parent() const
Getter for the parent iterator.
Iterator powering an intermediate operation.
typename UniqueByOperator< T, U >::mapper_type map_t
The key computation function type.
Definition: uniqueBy.h:99
typename parent_iterator_type::payload_type parent_payload_type
Definition: uniqueBy.h:29
StreamIteratorPayload< T > payload_type
Type alias for the payload used.
typename payload_type::value_type value_type
Type alias for the data type manipulated.
Global namespace of the SCL.
Definition: alias.hpp:3
it_t it() const
Get a reference to the underlying iterator.
Definition: Stream.h:48
details::uniqueBy_toolbox< T, U > uniqueBy(F &&mapper)
Remove duplicates from a stream.
Definition: uniqueBy.h:124
details::uniqueBy_toolbox< T, U > uniqueBy_(typename details::uniqueBy_toolbox< T, U >::map_t mapper)
Remove duplicates from a stream by explicitly providing the type arguments.
Definition: uniqueBy.h:136
std::function< key_type(const value_type &)> mapper_type
The function type that computes a key from a value.
Definition: uniqueBy.h:42
std_fn_type< R(*)(Args...)> as_fn(R(*f)(Args...))
Definition: fn_introspect.h:89
std::unordered_set< key_type > tagged
the state that keeps track of all encountered keys
Definition: uniqueBy.h:85
mapper_type key
the function that computes a key from a value
Definition: uniqueBy.h:79
typename parent_iterator_type::value_type parent_value_type
Definition: uniqueBy.h:28
std::shared_ptr< parent_iterator_type > parent_type
BaseStreamIterator< T > parent_iterator_type
Type alias for the "parent" iterator.
payload_type next() override
Retrieve the next iterator payload.
Definition: uniqueBy.h:52
General purpose tooling.
Definition: iostream.hpp:4
Stream< T > operator|(const Stream< T > &lhs, const details::filter_toolbox< T > &rhs)
Pipe operator overload for filter operations.
Definition: filter.h:117
UniqueByOperator(parent_type p, mapper_type key)
Construct an operator from its parent iterator and the mapper function.
Definition: uniqueBy.h:49
Toolbox tag type for unique by operations.
Definition: uniqueBy.h:94
typename iterator_type::parent_type parent_type
Definition: uniqueBy.h:30
U key_type
The type of keys (unique by key)
Definition: uniqueBy.h:36
A class representing a stream iterator&#39;s payload.
Definition: payload.h:16
Class representing a stream of data.
Definition: Stream.h:15