1
+ #pragma once
2
+ #include < scl/stream/details/deprecation.h>
3
+ #include < unordered_set>
4
+ #include < scl/stream/Stream.h>
5
+ #include < scl/stream/details/iterator/EndStreamIterator.h>
6
+
7
+ namespace scl {
8
+ namespace stream {
9
+ namespace terminators {
10
+ namespace pack {
11
+ namespace details {
12
+ /* *
13
+ * Class that allows packing to a std::unordered_set
14
+ * @tparam T being the stream value type
15
+ * @tparam Hash being the type of hash function
16
+ * @tparam KeyEqual being the type of the key comparator
17
+ * @tparam Allocator being the type of the allocator used for std::unordered_set
18
+ */
19
+ template <class T , class Hash == std::hash<T>, class KeyEqual = std::equal_to<T>, class Allocator = std::allocator<T>>
20
+ class UnorderedSetPacker : public scl ::stream::details::iterator::EndStreamIterator<std::unordered_set<T, Hash, KeyEqual, Allocator>, T>{
21
+ public:
22
+ using iterator_type = stream::details::iterator::EndStreamIterator<std::unordered_set<T, Hash, KeyEqual, Allocator>, T>;
23
+ using value_type = typename iterator_type::value_type;
24
+ using payload_type = typename iterator_type::payload_type;
25
+ using result_type = typename iterator_type::result_type;
26
+ using parent_iterator_type = typename iterator_type::parent_iterator_type;
27
+ using parent_type = typename iterator_type::parent_type;
28
+
29
+ using hash_type = Hash;
30
+ using key_equal_type = KeyEqual;
31
+ using allocator_type = Allocator;
32
+
33
+ explicit UnorderedSetPacker (parent_type p) : iterator_type{std::move (p)} {};
34
+
35
+ result_type process (){
36
+ result_type ret;
37
+
38
+ for (auto && payload : *this ){
39
+ if (payload.isValid ())
40
+ ret.insert (*payload.value ());
41
+ }
42
+
43
+ return ret;
44
+ }
45
+ };
46
+
47
+ /* *
48
+ * Tag type to pack into a std::set w/ default template parameters
49
+ */
50
+ struct to_unordered_set_toolbox {};
51
+
52
+ /* *
53
+ * Tag type to pack into a std::set w/ control over template parameters
54
+ * @tparam Hash being the type of the hash function
55
+ * @tparam KeyEqual being the type of the key comparator
56
+ * @tparam Allocator being the type of allocator used to allocate memory
57
+ */
58
+ template <class Hash , class KeyEqual , class Allocator >
59
+ struct to_unordered_set_toolbox_alloc {};
60
+ }
61
+
62
+ details::to_unordered_set_toolbox toUnorderedSet (){ return {}; }
63
+
64
+ /* *
65
+ * Tag type to pack into a std::set w/ control over template parameters
66
+ * @tparam Hash being the type of the hash function
67
+ * @tparam KeyEqual being the type of the key comparator
68
+ * @tparam Allocator being the type of allocator used to allocate memory
69
+ */
70
+ template <class Hash , class KeyEqual , class Allocator >
71
+ details::to_unordered_set_toolbox_alloc<Hash, KeyEqual, Allocator> toUnorderedSet (){
72
+ return {};
73
+ }
74
+ }
75
+
76
+ /* *
77
+ * Pipe operator for packing into an unordered set
78
+ * @tparam T being the value type of the stream
79
+ * @param lhs being the stream to pack
80
+ * @param _ being the packer type tag
81
+ * @return an unordered set containing the elements from the stream
82
+ */
83
+ template <class T >
84
+ typename pack::details::UnorderedSetPacker<T>::result_type operator |(const Stream<T>& lhs, const pack::details::to_unordered_set_toolbox& _){
85
+ auto packer = pack::details::UnorderedSetPacker<T>{lhs.it ()};
86
+ return packer.process ();
87
+ }
88
+
89
+ /* *
90
+ * Pipe operator or packing into an unordered set using a specific allocator and comparator
91
+ * @tparam T being the value type of the stream
92
+ * @tparam Hash being the type of the hash function
93
+ * @tparam KeyEqual being the type of the key comparator
94
+ * @tparam Allocator being the type of allocator used to allocate memory
95
+ * @param lhs being the stream to pack
96
+ * @param _ being the packer type tag
97
+ * @return an unordered set containing the elements from the stream
98
+ */
99
+ template <class T , class Compare , class Allocator >
100
+ typename pack::details::UnorderedSetPacker<T, Hash, KeyEqual, Allocator>::result_type operator |(const Stream<T>& lhs, const pack::details::to_unordered_set_toolbox_alloc<Hash, KeyEqual, Allocator>& _){
101
+ auto packer = pack::details::UnorderedSetPacker<T, Hash, KeyEqual, Allocator>{lhs.it ()};
102
+ return packer.process ();
103
+ }
104
+ }
105
+ }
106
+ }
0 commit comments