ModErn Text Analysis
META Enumerates Textual Applications
range.h
Go to the documentation of this file.
1 
10 #ifndef META_UTIL_RANGE_H_
11 #define META_UTIL_RANGE_H_
12 
13 #include <cmath>
14 #include <iterator>
15 
16 namespace meta
17 {
18 namespace util
19 {
20 
26 template <class T>
28 {
29  public:
33  template <class Plus>
34  class iterator_t
35  {
36  public:
40  using value_type = T;
42  using reference = const T&;
44  using pointer = const T*;
46  using iterator_category = std::forward_iterator_tag;
48  using difference_type = std::ptrdiff_t;
49 
50  friend class basic_range<T>;
51 
57  {
58  _curr = _plus(_curr, _range->_step);
59  ++_idx;
60  return *this;
61  }
62 
68  {
69  self_type temp = *this;
70  ++(*this);
71  return temp;
72  }
73 
79  {
80  return _curr;
81  }
82 
88  {
89  return &_curr;
90  }
91 
98  friend bool operator==(const iterator_t& lhs, const iterator_t& rhs)
99  {
100  return lhs._range == rhs._range && lhs._idx == rhs._idx;
101  }
102 
110  friend bool operator!=(const iterator_t& lhs, const iterator_t& rhs)
111  {
112  return !(lhs == rhs);
113  }
114 
115  private:
123  iterator_t(const basic_range<T>* range, const T& start, size_t idx)
124  : _curr(start), _idx(idx), _range(range)
125  {
126  // nothing
127  }
128 
130  T _curr;
132  size_t _idx;
134  Plus _plus;
137  };
138 
143 
148 
149  friend iterator;
150  friend reverse_iterator;
151 
155  iterator begin() const
156  {
157  return iterator(this, _begin, 0);
158  }
159 
163  iterator end() const
164  {
165  return iterator(this, _end, _num);
166  }
167 
172  {
173  return reverse_iterator(this, _end, 0);
174  }
175 
180  {
181  return reverse_iterator(this, _begin, _num);
182  }
183 
191  basic_range(const T& begin, const T& end, const T& step)
192  : _begin(begin), _end(end), _num((end - begin) / step + 1), _step(step)
193  {
194  // nothing
195  }
196 
197  basic_range(const basic_range&) = default;
198  basic_range(basic_range&&) = default;
199  basic_range& operator=(const basic_range&) = default;
200  basic_range& operator=(basic_range&&) = default;
201 
205  ~basic_range() = default;
206 
207  private:
211  T _end;
213  size_t _num;
215  T _step;
216 };
217 
224 template <class T>
225 basic_range<T> range(const T& begin, const T& end)
226 {
227  return basic_range<T>(begin, end, T{1});
228 }
229 
239 template <class T>
240 basic_range<T> range(const T& begin, const T& end, const T& step)
241 {
242  return basic_range<T>(begin, end, step);
243 }
244 }
245 }
246 
247 #endif
self_type operator++(int)
Postfix increment.
Definition: range.h:67
T _begin
the beginning value for the range
Definition: range.h:209
reverse_iterator rend() const
Definition: range.h:179
T _curr
the current value
Definition: range.h:130
Plus _plus
the functor to be used for incrementing the current value
Definition: range.h:134
iterator begin() const
Definition: range.h:155
size_t _num
the number of values contained in the range
Definition: range.h:213
basic_range(const T &begin, const T &end, const T &step)
Constructs a range.
Definition: range.h:191
const basic_range< T > * _range
a back-pointer to the range this iterator is operating over
Definition: range.h:136
iterator_t< std::plus< T >> iterator
the iterator for the range class
Definition: range.h:140
pointer operator->()
Member access operator.
Definition: range.h:87
reference operator*()
Dereference operator.
Definition: range.h:78
basic_range< T > range(const T &begin, const T &end)
Constructs a range based on a beginning and ending value.
Definition: range.h:225
std::ptrdiff_t difference_type
the difference type for this iterator
Definition: range.h:48
~basic_range()=default
Defaulted destructor.
friend bool operator!=(const iterator_t &lhs, const iterator_t &rhs)
Inequality operator.
Definition: range.h:110
iterator_t(const basic_range< T > *range, const T &start, size_t idx)
Constructs a new iterator.
Definition: range.h:123
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
const T & reference
a reference to the contained type
Definition: range.h:42
friend bool operator==(const iterator_t &lhs, const iterator_t &rhs)
Equality operator.
Definition: range.h:98
Implements a range that spans a loop's extension and termination conditions, most useful for iteratin...
Definition: range.h:27
reverse_iterator rbegin() const
Definition: range.h:171
size_t _idx
the current index into the ragne
Definition: range.h:132
iterator end() const
Definition: range.h:163
T _step
the step size of the range
Definition: range.h:215
T _end
the ending value for the range
Definition: range.h:211
std::forward_iterator_tag iterator_category
the category for this iterator
Definition: range.h:46
self_type & operator++()
Prefix increment.
Definition: range.h:56
T value_type
convenience typedef for the contained value of the range
Definition: range.h:40
const T * pointer
a pointer to the contained type
Definition: range.h:44
iterator_t< std::minus< T >> reverse_iterator
the reverse_iterator for the range class
Definition: range.h:145
Iterator to traverse the generic range.
Definition: range.h:34