|
8 | 8 | #include <tuple> |
9 | 9 |
|
10 | 10 | namespace iter { |
11 | | - |
12 | 11 | namespace detail { |
13 | | - |
14 | 12 | template <std::size_t Index, typename Functor, typename Tup> |
15 | 13 | struct Expander { |
16 | 14 | template <typename... Ts> |
@@ -58,87 +56,86 @@ namespace iter { |
58 | 56 |
|
59 | 57 | } // end detail |
60 | 58 |
|
61 | | - // Forward declarations of IMap and imap |
62 | | - template <typename MapFunc, typename... Containers> |
63 | | - class IMap; |
| 59 | + namespace impl { |
| 60 | + template <typename MapFunc, typename... Containers> |
| 61 | + class IMapper; |
| 62 | + } |
64 | 63 |
|
65 | 64 | template <typename MapFunc, typename... Containers> |
66 | | - IMap<MapFunc, Containers...> imap(MapFunc, Containers&&...); |
| 65 | + impl::IMapper<MapFunc, Containers...> imap(MapFunc, Containers&&...); |
| 66 | +} |
67 | 67 |
|
68 | | - template <typename MapFunc, typename... Containers> |
69 | | - class IMap { |
70 | | - // The imap function is the only thing allowed to create a IMap |
71 | | - friend IMap imap<MapFunc, Containers...>(MapFunc, Containers&&...); |
| 68 | +template <typename MapFunc, typename... Containers> |
| 69 | +class iter::impl::IMapper { |
| 70 | + // The imap function is the only thing allowed to create a IMapper |
| 71 | + friend IMapper iter::imap<MapFunc, Containers...>(MapFunc, Containers&&...); |
72 | 72 |
|
73 | | - using ZippedType = decltype(zip(std::declval<Containers>()...)); |
74 | | - using ZippedIterType = iterator_type<ZippedType>; |
| 73 | + using ZippedType = decltype(zip(std::declval<Containers>()...)); |
| 74 | + using ZippedIterType = iterator_type<ZippedType>; |
75 | 75 |
|
76 | | - private: |
77 | | - MapFunc map_func; |
78 | | - ZippedType zipped; |
| 76 | + private: |
| 77 | + MapFunc map_func; |
| 78 | + ZippedType zipped; |
79 | 79 |
|
80 | | - using IMapIterDeref = |
81 | | - decltype(detail::call_with_tuple(map_func, *std::begin(zipped))); |
| 80 | + using IMapIterDeref = |
| 81 | + decltype(detail::call_with_tuple(map_func, *std::begin(zipped))); |
82 | 82 |
|
83 | | - // Value constructor for use only in the imap function |
84 | | - IMap(MapFunc in_map_func, Containers&&... in_containers) |
85 | | - : map_func(in_map_func), |
86 | | - zipped(zip(std::forward<Containers>(in_containers)...)) {} |
| 83 | + IMapper(MapFunc in_map_func, Containers&&... in_containers) |
| 84 | + : map_func(in_map_func), |
| 85 | + zipped(zip(std::forward<Containers>(in_containers)...)) {} |
87 | 86 |
|
88 | | - public: |
89 | | - class Iterator : public std::iterator<std::input_iterator_tag, |
90 | | - typename std::remove_reference<IMapIterDeref>::type> { |
91 | | - private: |
92 | | - MapFunc* map_func; |
93 | | - ZippedIterType zipiter; |
94 | | - |
95 | | - public: |
96 | | - Iterator(MapFunc& in_map_func, ZippedIterType&& in_zipiter) |
97 | | - : map_func(&in_map_func), zipiter(std::move(in_zipiter)) {} |
98 | | - |
99 | | - IMapIterDeref operator*() { |
100 | | - return detail::call_with_tuple(*this->map_func, *(this->zipiter)); |
101 | | - } |
| 87 | + public: |
| 88 | + class Iterator : public std::iterator<std::input_iterator_tag, |
| 89 | + typename std::remove_reference<IMapIterDeref>::type> { |
| 90 | + private: |
| 91 | + MapFunc* map_func; |
| 92 | + ZippedIterType zipiter; |
102 | 93 |
|
103 | | - ArrowProxy<IMapIterDeref> operator->() { |
104 | | - return {**this}; |
105 | | - } |
| 94 | + public: |
| 95 | + Iterator(MapFunc& in_map_func, ZippedIterType&& in_zipiter) |
| 96 | + : map_func(&in_map_func), zipiter(std::move(in_zipiter)) {} |
106 | 97 |
|
107 | | - Iterator& operator++() { |
108 | | - ++this->zipiter; |
109 | | - return *this; |
110 | | - } |
| 98 | + IMapIterDeref operator*() { |
| 99 | + return detail::call_with_tuple(*this->map_func, *(this->zipiter)); |
| 100 | + } |
111 | 101 |
|
112 | | - Iterator operator++(int) { |
113 | | - auto ret = *this; |
114 | | - ++*this; |
115 | | - return ret; |
116 | | - } |
| 102 | + ArrowProxy<IMapIterDeref> operator->() { |
| 103 | + return {**this}; |
| 104 | + } |
117 | 105 |
|
118 | | - bool operator!=(const Iterator& other) const { |
119 | | - return this->zipiter != other.zipiter; |
120 | | - } |
| 106 | + Iterator& operator++() { |
| 107 | + ++this->zipiter; |
| 108 | + return *this; |
| 109 | + } |
121 | 110 |
|
122 | | - bool operator==(const Iterator& other) const { |
123 | | - return !(*this != other); |
124 | | - } |
125 | | - }; |
| 111 | + Iterator operator++(int) { |
| 112 | + auto ret = *this; |
| 113 | + ++*this; |
| 114 | + return ret; |
| 115 | + } |
126 | 116 |
|
127 | | - Iterator begin() { |
128 | | - return {this->map_func, this->zipped.begin()}; |
| 117 | + bool operator!=(const Iterator& other) const { |
| 118 | + return this->zipiter != other.zipiter; |
129 | 119 | } |
130 | 120 |
|
131 | | - Iterator end() { |
132 | | - return {this->map_func, this->zipped.end()}; |
| 121 | + bool operator==(const Iterator& other) const { |
| 122 | + return !(*this != other); |
133 | 123 | } |
134 | 124 | }; |
135 | 125 |
|
136 | | - // Helper function to instantiate a IMap |
137 | | - template <typename MapFunc, typename... Containers> |
138 | | - IMap<MapFunc, Containers...> imap( |
139 | | - MapFunc map_func, Containers&&... containers) { |
140 | | - return {map_func, std::forward<Containers>(containers)...}; |
| 126 | + Iterator begin() { |
| 127 | + return {this->map_func, this->zipped.begin()}; |
141 | 128 | } |
| 129 | + |
| 130 | + Iterator end() { |
| 131 | + return {this->map_func, this->zipped.end()}; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +template <typename MapFunc, typename... Containers> |
| 136 | +iter::impl::IMapper<MapFunc, Containers...> iter::imap( |
| 137 | + MapFunc map_func, Containers&&... containers) { |
| 138 | + return {map_func, std::forward<Containers>(containers)...}; |
142 | 139 | } |
143 | 140 |
|
144 | | -#endif // #ifndef ITER_IMAP_H_ |
| 141 | +#endif |
0 commit comments