range.hh 4.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * @file: range.hh
 * @author: Gregory G. Davidson
 *
 * Created on July 28, 2021
 *
 * Provides the ability to iterate over ranges in a ranged-for construct
 */

#ifndef RADIX_RADIXCORE_RANGE_HH_
#define RADIX_RADIXCORE_RANGE_HH_

#include <iterator>
#include <type_traits>

namespace radix
{
namespace detail
{
// Forward declaration of iterator classes
template <class T>
class RangeIterator;

template <class T>
class StridedRangeIterator;
}  // namespace detail

/**
 * @brief Standard range class for iterating over a range
 */
template <class T>
class Range
{
 public:
  using value_type = T;
  using iterator   = detail::RangeIterator<T>;
  using size_type  = std::size_t;

  // >>> CONSTRUCTORS
  // Default constructor for an empty range
  inline Range();

  // Construct with a range ending (begins at zero)
  inline explicit Range(value_type end);

  // Construct from a given beginning and ending
  inline Range(value_type begin, value_type end);

  // >>> BASIC CONTAINER FUNCTIONALITY
  //! Return beginning of range
  iterator begin() const { return d_begin; }

  //! Return ending of range
  iterator end() const { return d_end; }

  //! Return size of range
  size_type size() const { return *d_end - *d_begin; }

  //! Return whether range is empty
  bool empty() const { return d_begin == d_end; }

 private:
  iterator d_begin;
  iterator d_end;
};

/**
 * @brief Strided range class for iterating over a range by some stride
 */
template <class T>
class StridedRange
{
 public:
  using value_type = T;
  using iterator   = detail::StridedRangeIterator<T>;
  using size_type  = std::size_t;

  // >>> CONSTRUCTORS
  // Construct with a range ending and stride
  inline StridedRange(value_type end, value_type stride);

  // Construct with a range beginning, ending, and stride
  inline StridedRange(value_type begin, value_type end, value_type stride);

  // >>> BASIC CONTAINER FUNCTIONALITY
  //! Return beginning of range
  iterator begin() const { return d_begin; }

  //! Return ending of range
  iterator end() const { return d_end; }

  //! Return size of range
  size_type size() const { return *d_end - *d_begin; }

  //! Return whether range is empty
  bool empty() const { return d_begin == d_end; }

 private:
  iterator d_begin;
  iterator d_end;
};

//*****************************************************************************
// HELPER FUNCTIONS
//*****************************************************************************
// Build an iterable range with a beginning and ending
template <class T>
Range<T> range(T begin, T end);

// Build an iterable range with just an ending
template <class T>
Range<T> range(T begin, T end);

// Build a strided range with a beginning, ending, and stride
template <class T>
StridedRange<T> range(T begin, T end, T stride);

namespace detail
{
/**
 * @brief Range iterator class
 */
template <class T>
class RangeIterator
{
 public:
  //@{
  //! Public types
  using iterator_category = std::input_iterator_tag;
  using value_type        = T;
  using difference_type   = typename std::make_signed<T>::type;
  using pointer           = const T*;
  using reference         = const T&;
  using This              = RangeIterator<T>;
  //@}

 public:
  // Construct with value
  inline explicit RangeIterator(value_type value);

  // >>> ITERATOR INTERFACE
  //! Dereference operator
  value_type operator*() const { return d_value; }

  //! Indirection operator
  pointer operator->() const { return &d_value; }

  // Prefix increment operator
  inline This& operator++();

  // Postfix increment operator
  inline This operator++(int);

  //! Equality operator
  inline bool operator==(const This& iter) const;

  //! Inequality operator
  inline bool operator!=(const This& iter) const;

 protected:
  value_type d_value;
};

//*****************************************************************************
/**
 * @brief Strided range iterator class
 */
template <class T>
class StridedRangeIterator : public RangeIterator<T>
{
  using Base = RangeIterator<T>;

 public:
  using typename Base::value_type;
  using This = StridedRangeIterator<T>;

 public:
  // Construct with value and stride
  inline StridedRangeIterator(value_type value, value_type stride);

  // >>> OVERWRITTEN ITERATOR INTERFACE
  // Prefix increment operator
  inline This& operator++();

  //! Equality operator
  inline bool operator==(const This& iter) const;

  // >>> CLASS INTERFACE
  value_type stride() const { return d_stride; }

 private:
  value_type d_stride;
};

}  // namespace detail

}  // namespace radix

//*****************************************************************************
// Inline function definitions
#include "range.i.hh"
//*****************************************************************************

#endif /** RADIX_RADIXCORE_RANGE_HH_ */