ModErn Text Analysis
META Enumerates Textual Applications
iterator.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_ITERATOR_H_
11 #define META_UTIL_ITERATOR_H_
12 
13 #include <iterator>
14 #include <type_traits>
15 
16 #include "meta/config.h"
17 #include "meta/util/comparable.h"
18 
19 namespace meta
20 {
21 namespace util
22 {
23 
28 template <class Iterator, class UnaryFunction>
30  : public comparable<transform_iterator<Iterator, UnaryFunction>>
31 {
32  public:
33  using traits_type = std::iterator_traits<Iterator>;
34  using difference_type = typename traits_type::difference_type;
35  using value_type = typename std::result_of<UnaryFunction(
36  typename traits_type::reference)>::type;
37  using pointer = typename std::add_pointer<value_type>::type;
38  using reference =
39  typename std::add_lvalue_reference<const value_type>::type;
40  using iterator_category = typename traits_type::iterator_category;
41 
42  transform_iterator(Iterator it, UnaryFunction fun) : it_{it}, fun_(fun)
43  {
44  // nothing
45  }
46 
47  transform_iterator& operator++()
48  {
49  ++it_;
50  return *this;
51  }
52 
53  transform_iterator operator++(int)
54  {
55  auto tmp = *this;
56  ++it_;
57  return tmp;
58  }
59 
60  transform_iterator& operator--()
61  {
62  --it_;
63  return *this;
64  }
65 
66  transform_iterator operator--(int)
67  {
68  auto tmp = *this;
69  --it_;
70  return *tmp;
71  }
72 
73  transform_iterator& operator+=(difference_type diff)
74  {
75  it_ += diff;
76  return *this;
77  }
78 
79  transform_iterator operator+(difference_type diff) const
80  {
81  auto tmp = *this;
82  tmp += diff;
83  return tmp;
84  }
85 
86  transform_iterator& operator-=(difference_type diff)
87  {
88  it_ -= diff;
89  return *this;
90  }
91 
92  transform_iterator operator-(difference_type diff) const
93  {
94  auto tmp = *this;
95  tmp -= diff;
96  return tmp;
97  }
98 
99  difference_type operator-(transform_iterator other) const
100  {
101  return it_ - other.it_;
102  }
103 
104  reference operator[](difference_type diff) const
105  {
106  return fun_(it_[diff]);
107  }
108 
109  bool operator<(transform_iterator other) const
110  {
111  return it_ < other.it_;
112  }
113 
114  value_type operator*() const
115  {
116  return fun_(*it_);
117  }
118 
119  private:
120  Iterator it_;
121  UnaryFunction fun_;
122 };
123 
128 template <class Iterator, class UnaryFunction>
130 make_transform_iterator(Iterator it, UnaryFunction&& fun)
131 {
133  it, std::forward<UnaryFunction>(fun));
134 }
135 }
136 }
137 #endif
Wrapper around an Iterator that, when dereferenced, returns f(*it) where it is the wrapped Iterator a...
Definition: iterator.h:29
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
transform_iterator< Iterator, UnaryFunction > make_transform_iterator(Iterator it, UnaryFunction &&fun)
Helper function to construct a transform_iterator from an Iterator and a UnaryFunction to transform t...
Definition: iterator.h:130
A CRTP base class that allows for inheritance of all comparator operators given that the derived clas...
Definition: comparable.h:28