From d1eeea901bd146bb73a6948f72ee82ce4224d082 Mon Sep 17 00:00:00 2001 From: Pavel Siska Date: Mon, 1 Sep 2025 15:40:53 +0200 Subject: [PATCH] unirec++ - Add missing operators to UnirecArray::Iterator for random access support --- unirec/include/unirec++/unirecArray.hpp | 81 ++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 8 deletions(-) diff --git a/unirec/include/unirec++/unirecArray.hpp b/unirec/include/unirec++/unirecArray.hpp index 85b286ca..61147cda 100644 --- a/unirec/include/unirec++/unirecArray.hpp +++ b/unirec/include/unirec++/unirecArray.hpp @@ -26,7 +26,7 @@ namespace Nemea { * * @tparam T The type of the values in the array. */ -template +template class UnirecArray { public: /** @@ -45,25 +45,64 @@ class UnirecArray { { } + // Dereference reference operator*() const { return *m_ptr; } - pointer operator->() { return m_ptr; } + pointer operator->() const { return m_ptr; } + + // Increment / Decrement Iterator& operator++() { - m_ptr++; + ++m_ptr; return *this; } - Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; } + Iterator& operator--() + { + --m_ptr; + return *this; + } + Iterator operator--(int) + { + Iterator tmp = *this; + --(*this); + return tmp; + } + + // Arithmetic + Iterator operator+(difference_type n) const { return Iterator(m_ptr + n); } + Iterator operator-(difference_type n) const { return Iterator(m_ptr - n); } + difference_type operator-(const Iterator& other) const { return m_ptr - other.m_ptr; } + + Iterator& operator+=(difference_type n) + { + m_ptr += n; + return *this; + } + Iterator& operator-=(difference_type n) + { + m_ptr -= n; + return *this; + } + + // Comparison + bool operator==(const Iterator& other) const { return m_ptr == other.m_ptr; } + bool operator!=(const Iterator& other) const { return m_ptr != other.m_ptr; } + bool operator<(const Iterator& other) const { return m_ptr < other.m_ptr; } + bool operator<=(const Iterator& other) const { return m_ptr <= other.m_ptr; } + bool operator>(const Iterator& other) const { return m_ptr > other.m_ptr; } + bool operator>=(const Iterator& other) const { return m_ptr >= other.m_ptr; } - T* data() { return m_ptr; } + // Indexing + reference operator[](difference_type n) const { return *(m_ptr + n); } - bool operator==(const Iterator& other) const { return this->m_ptr == other.m_ptr; }; - bool operator!=(const Iterator& other) const { return this->m_ptr != other.m_ptr; }; + // Data pointer access + pointer data() { return m_ptr; } + pointer data() const { return m_ptr; } private: pointer m_ptr; @@ -107,6 +146,14 @@ class UnirecArray { */ constexpr T& operator[](size_t pos) { return m_data[pos]; } + /** + * @brief Returns a const reference to the element at the specified position in the UniRec field + * array. + * @param pos The position of the element to return. + * @return T& The reference to the element at the specified position. + */ + constexpr const T& operator[](size_t pos) const { return m_data[pos]; } + /** * @brief Returns a reference to the element at the specified position in the UniRec field * array, with bounds checking. @@ -115,7 +162,25 @@ class UnirecArray { * field array. * @return A reference to the element at the specified position in the UniRec field array. */ - constexpr T& at(size_t pos) const + constexpr T& at(size_t pos) + { + if (pos >= m_size) + throw std::out_of_range( + "UnirecArray::at: pos (which is %zu) " + ">= m_size (which is %zu)"), + pos, m_size; + return m_data[pos]; + } + + /** + * @brief Returns a const reference to the element at the specified position in the UniRec field + * array, with bounds checking. + * @tparam pos The position of the element to return. + * @throw std::out_of_range If pos is out of range of valid element positions in the UniRec + * field array. + * @return A reference to the element at the specified position in the UniRec field array. + */ + constexpr const T& at(size_t pos) const { if (pos >= m_size) throw std::out_of_range(