Skip to content

Commit 9c55fe9

Browse files
author
Pavel Siska
committed
unirec++ - Add missing operators to UnirecArray::Iterator for random access support
1 parent 7031b1c commit 9c55fe9

File tree

1 file changed

+144
-106
lines changed

1 file changed

+144
-106
lines changed
Lines changed: 144 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* @author Pavel Siska <[email protected]>
3+
* @ author Pavel Siska <[email protected]>
44
* @brief Header file containing the definition of the UnirecArray class and its Iterator subclass.
55
*
66
* SPDX-License-Identifier: BSD-3-Clause
@@ -26,115 +26,153 @@ 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:
32-
/**
33-
* @brief An iterator for the UnirecArray class.
34-
*/
35-
class Iterator {
36-
public:
37-
using iterator_category = std::random_access_iterator_tag;
38-
using difference_type = std::ptrdiff_t;
39-
using value_type = T;
40-
using pointer = value_type*;
41-
using reference = value_type&;
42-
43-
Iterator(pointer ptr)
44-
: m_ptr(ptr)
45-
{
46-
}
47-
48-
reference operator*() const { return *m_ptr; }
49-
pointer operator->() { return m_ptr; }
50-
Iterator& operator++()
51-
{
52-
m_ptr++;
53-
return *this;
54-
}
55-
56-
Iterator operator++(int)
57-
{
58-
Iterator tmp = *this;
59-
++(*this);
60-
return tmp;
61-
}
62-
63-
T* data() { return m_ptr; }
64-
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; };
67-
68-
private:
69-
pointer m_ptr;
70-
};
71-
72-
/**
73-
* @brief Constructs a UnirecArray object.
74-
*
75-
* @param dataPointer A pointer to the first element of the value array.
76-
* @param size The number of elements in the value array.
77-
* @param fieldID The unirec fieldID associated with the array.
78-
*/
79-
UnirecArray(T* dataPointer, size_t size, ur_field_id_t fieldID)
80-
: m_data(dataPointer)
81-
, m_size(size)
82-
{
83-
checkDataType(ur_get_type(fieldID));
84-
}
85-
86-
/**
87-
* @brief Returns the number of elements in the UniRec field array.
88-
*/
89-
constexpr size_t size() const noexcept { return m_size; }
90-
91-
/**
92-
* @brief Returns an iterator to the first element of the UniRec field array.
93-
*/
94-
constexpr Iterator begin() const noexcept { return Iterator(m_data); }
95-
96-
/**
97-
* @brief Returns an iterator to the element following the last element of the UniRec field
98-
* array.
99-
*/
100-
constexpr Iterator end() const noexcept { return Iterator(m_data + m_size); }
101-
102-
/**
103-
* @brief Returns a reference to the element at the specified position in the UniRec field
104-
* array.
105-
* @param pos The position of the element to return.
106-
* @return T& The reference to the element at the specified position.
107-
*/
108-
constexpr T& operator[](size_t pos) { return m_data[pos]; }
109-
110-
/**
111-
* @brief Returns a reference to the element at the specified position in the UniRec field
112-
* array, with bounds checking.
113-
* @tparam pos The position of the element to return.
114-
* @throw std::out_of_range If pos is out of range of valid element positions in the UniRec
115-
* field array.
116-
* @return A reference to the element at the specified position in the UniRec field array.
117-
*/
118-
constexpr T& at(size_t pos) const
119-
{
120-
if (pos >= m_size)
121-
throw std::out_of_range(
122-
"UnirecArray::at: pos (which is %zu) "
123-
">= m_size (which is %zu)"),
124-
pos, m_size;
125-
return m_data[pos];
126-
}
32+
/**
33+
* @brief An iterator for the UnirecArray class.
34+
*/
35+
class Iterator {
36+
public:
37+
using iterator_category = std::random_access_iterator_tag;
38+
using difference_type = std::ptrdiff_t;
39+
using value_type = T;
40+
using pointer = value_type*;
41+
using reference = value_type&;
42+
43+
Iterator(pointer ptr)
44+
: m_ptr(ptr)
45+
{
46+
}
47+
48+
// Dereference
49+
reference operator*() const { return *m_ptr; }
50+
pointer operator->() const { return m_ptr; }
51+
52+
// Increment / Decrement
53+
Iterator& operator++()
54+
{
55+
++m_ptr;
56+
return *this;
57+
}
58+
Iterator operator++(int)
59+
{
60+
Iterator tmp = *this;
61+
++(*this);
62+
return tmp;
63+
}
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; }
99+
100+
// Indexing
101+
reference operator[](difference_type n) const { return *(m_ptr + n); }
102+
103+
// Data pointer access
104+
pointer data() { return m_ptr; }
105+
106+
private:
107+
pointer m_ptr;
108+
};
109+
110+
/**
111+
* @brief Constructs a UnirecArray object.
112+
*
113+
* @param dataPointer A pointer to the first element of the value array.
114+
* @param size The number of elements in the value array.
115+
* @param fieldID The unirec fieldID associated with the array.
116+
*/
117+
UnirecArray(T* dataPointer, size_t size, ur_field_id_t fieldID)
118+
: m_data(dataPointer)
119+
, m_size(size)
120+
{
121+
checkDataType(ur_get_type(fieldID));
122+
}
123+
124+
/**
125+
* @brief Returns the number of elements in the UniRec field array.
126+
*/
127+
constexpr size_t size() const noexcept { return m_size; }
128+
129+
/**
130+
* @brief Returns an iterator to the first element of the UniRec field array.
131+
*/
132+
constexpr Iterator begin() const noexcept { return Iterator(m_data); }
133+
134+
/**
135+
* @brief Returns an iterator to the element following the last element of the UniRec field
136+
* array.
137+
*/
138+
constexpr Iterator end() const noexcept { return Iterator(m_data + m_size); }
139+
140+
/**
141+
* @brief Returns a reference to the element at the specified position in the UniRec field
142+
* array.
143+
* @param pos The position of the element to return.
144+
* @return T& The reference to the element at the specified position.
145+
*/
146+
constexpr T& operator[](size_t pos) { return m_data[pos]; }
147+
148+
/**
149+
* @brief Returns a reference to the element at the specified position in the UniRec field
150+
* array, with bounds checking.
151+
* @tparam pos The position of the element to return.
152+
* @throw std::out_of_range If pos is out of range of valid element positions in the UniRec
153+
* field array.
154+
* @return A reference to the element at the specified position in the UniRec field array.
155+
*/
156+
constexpr T& at(size_t pos) const
157+
{
158+
if (pos >= m_size)
159+
throw std::out_of_range(
160+
"UnirecArray::at: pos (which is %zu) "
161+
">= m_size (which is %zu)"),
162+
pos, m_size;
163+
return m_data[pos];
164+
}
127165

128166
private:
129-
void checkDataType(ur_field_type_t fieldDataType) const
130-
{
131-
if (getExpectedUnirecType<T*>() != fieldDataType) {
132-
throw std::runtime_error("Unirec array data type format mismatch");
133-
}
134-
}
135-
136-
size_t m_size;
137-
T* m_data;
167+
void checkDataType(ur_field_type_t fieldDataType) const
168+
{
169+
if (getExpectedUnirecType<T*>() != fieldDataType) {
170+
throw std::runtime_error("Unirec array data type format mismatch");
171+
}
172+
}
173+
174+
size_t m_size;
175+
T* m_data;
138176
};
139177

140178
} // namespace Nemea

0 commit comments

Comments
 (0)