Skip to content

Commit 328b992

Browse files
authored
Merge pull request #237 from CESNET/unirec++-unirecArray-iterator
unirec++ - Add missing operators to UnirecArray::Iterator for random …
2 parents 7031b1c + d1eeea9 commit 328b992

File tree

1 file changed

+73
-8
lines changed

1 file changed

+73
-8
lines changed

unirec/include/unirec++/unirecArray.hpp

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Nemea {
2626
*
2727
* @tparam T The type of the values in the array.
2828
*/
29-
template<typename T>
29+
template <typename T>
3030
class UnirecArray {
3131
public:
3232
/**
@@ -45,25 +45,64 @@ class UnirecArray {
4545
{
4646
}
4747

48+
// Dereference
4849
reference operator*() const { return *m_ptr; }
49-
pointer operator->() { return m_ptr; }
50+
pointer operator->() const { return m_ptr; }
51+
52+
// Increment / Decrement
5053
Iterator& operator++()
5154
{
52-
m_ptr++;
55+
++m_ptr;
5356
return *this;
5457
}
55-
5658
Iterator operator++(int)
5759
{
5860
Iterator tmp = *this;
5961
++(*this);
6062
return tmp;
6163
}
64+
Iterator& operator--()
65+
{
66+
--m_ptr;
67+
return *this;
68+
}
69+
Iterator operator--(int)
70+
{
71+
Iterator tmp = *this;
72+
--(*this);
73+
return tmp;
74+
}
75+
76+
// Arithmetic
77+
Iterator operator+(difference_type n) const { return Iterator(m_ptr + n); }
78+
Iterator operator-(difference_type n) const { return Iterator(m_ptr - n); }
79+
difference_type operator-(const Iterator& other) const { return m_ptr - other.m_ptr; }
80+
81+
Iterator& operator+=(difference_type n)
82+
{
83+
m_ptr += n;
84+
return *this;
85+
}
86+
Iterator& operator-=(difference_type n)
87+
{
88+
m_ptr -= n;
89+
return *this;
90+
}
91+
92+
// Comparison
93+
bool operator==(const Iterator& other) const { return m_ptr == other.m_ptr; }
94+
bool operator!=(const Iterator& other) const { return m_ptr != other.m_ptr; }
95+
bool operator<(const Iterator& other) const { return m_ptr < other.m_ptr; }
96+
bool operator<=(const Iterator& other) const { return m_ptr <= other.m_ptr; }
97+
bool operator>(const Iterator& other) const { return m_ptr > other.m_ptr; }
98+
bool operator>=(const Iterator& other) const { return m_ptr >= other.m_ptr; }
6299

63-
T* data() { return m_ptr; }
100+
// Indexing
101+
reference operator[](difference_type n) const { return *(m_ptr + n); }
64102

65-
bool operator==(const Iterator& other) const { return this->m_ptr == other.m_ptr; };
66-
bool operator!=(const Iterator& other) const { return this->m_ptr != other.m_ptr; };
103+
// Data pointer access
104+
pointer data() { return m_ptr; }
105+
pointer data() const { return m_ptr; }
67106

68107
private:
69108
pointer m_ptr;
@@ -107,6 +146,14 @@ class UnirecArray {
107146
*/
108147
constexpr T& operator[](size_t pos) { return m_data[pos]; }
109148

149+
/**
150+
* @brief Returns a const reference to the element at the specified position in the UniRec field
151+
* array.
152+
* @param pos The position of the element to return.
153+
* @return T& The reference to the element at the specified position.
154+
*/
155+
constexpr const T& operator[](size_t pos) const { return m_data[pos]; }
156+
110157
/**
111158
* @brief Returns a reference to the element at the specified position in the UniRec field
112159
* array, with bounds checking.
@@ -115,7 +162,25 @@ class UnirecArray {
115162
* field array.
116163
* @return A reference to the element at the specified position in the UniRec field array.
117164
*/
118-
constexpr T& at(size_t pos) const
165+
constexpr T& at(size_t pos)
166+
{
167+
if (pos >= m_size)
168+
throw std::out_of_range(
169+
"UnirecArray::at: pos (which is %zu) "
170+
">= m_size (which is %zu)"),
171+
pos, m_size;
172+
return m_data[pos];
173+
}
174+
175+
/**
176+
* @brief Returns a const reference to the element at the specified position in the UniRec field
177+
* array, with bounds checking.
178+
* @tparam pos The position of the element to return.
179+
* @throw std::out_of_range If pos is out of range of valid element positions in the UniRec
180+
* field array.
181+
* @return A reference to the element at the specified position in the UniRec field array.
182+
*/
183+
constexpr const T& at(size_t pos) const
119184
{
120185
if (pos >= m_size)
121186
throw std::out_of_range(

0 commit comments

Comments
 (0)