2 // Copyright 2013 Francisco Jerez
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef CLOVER_UTIL_ADAPTOR_HPP
24 #define CLOVER_UTIL_ADAPTOR_HPP
28 #include "util/tuple.hpp"
29 #include "util/pointer.hpp"
30 #include "util/functional.hpp"
35 /// Implementation of the iterator concept that transforms the
36 /// value of the source iterators \a Is on dereference by use of
39 /// The exact category of the resulting iterator should be the
40 /// least common denominator of the source iterator categories.
42 template<typename F, typename... Is>
43 class iterator_adaptor {
45 typedef std::forward_iterator_tag iterator_category;
46 typedef typename std::result_of<
47 F(typename std::iterator_traits<Is>::reference...)
49 typedef typename std::remove_reference<reference>::type value_type;
50 typedef pseudo_ptr<value_type> pointer;
51 typedef std::ptrdiff_t difference_type;
56 iterator_adaptor(F f, std::tuple<Is...> &&its) :
57 f(f), its(std::move(its)) {
62 return tuple::apply(f, tuple::map(derefs(), its));
67 tuple::map(preincs(), its);
79 operator==(const iterator_adaptor &jt) const {
84 operator!=(const iterator_adaptor &jt) const {
95 tuple::map(predecs(), its);
107 operator+=(difference_type n) {
108 tuple::map(advances_by(n), its);
113 operator-=(difference_type n) {
114 tuple::map(advances_by(-n), its);
119 operator+(difference_type n) const {
126 operator-(difference_type n) const {
133 operator-(const iterator_adaptor &jt) const {
134 return std::get<0>(its) - std::get<0>(jt.its);
138 operator[](difference_type n) const {
143 operator<(iterator_adaptor &jt) const {
144 return *this - jt < 0;
148 operator>(iterator_adaptor &jt) const {
149 return *this - jt > 0;
153 operator>=(iterator_adaptor &jt) const {
154 return !(*this < jt);
158 operator<=(iterator_adaptor &jt) const {
159 return !(*this > jt);
164 std::tuple<Is...> its;
167 template<typename F, typename... Is>
168 iterator_adaptor<F, Is...>
169 operator+(typename iterator_adaptor<F, Is...>::difference_type n,
170 const iterator_adaptor<F, Is...> &jt) {
174 template<typename F, typename... Is>
175 iterator_adaptor<F, Is...>
176 operator-(typename iterator_adaptor<F, Is...>::difference_type n,
177 const iterator_adaptor<F, Is...> &jt) {