From 03bab5615207404367bde073369dcb557462cb12 2017-08-17 07:44:44 From: Alexandre Leroux Date: 2017-08-17 07:44:44 Subject: [PATCH] Creates iterator for ArrayData --- diff --git a/core/include/Data/ArrayData.h b/core/include/Data/ArrayData.h index ccca546..9f5a240 100644 --- a/core/include/Data/ArrayData.h +++ b/core/include/Data/ArrayData.h @@ -58,6 +58,67 @@ struct Sort<1> { template class ArrayData { public: + class IteratorValue { + public: + explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} + { + for (auto i = 0; i < container.size(); ++i) { + m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); + } + } + + double at(int index) const { return *m_Its.at(index); } + double first() const { return *m_Its.front(); } + + void next() + { + for (auto &it : m_Its) { + ++it; + } + } + + bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; } + + private: + std::vector m_Its; + }; + + class Iterator { + public: + using iterator_category = std::forward_iterator_tag; + using value_type = const IteratorValue; + using difference_type = std::ptrdiff_t; + using pointer = value_type *; + using reference = value_type &; + + Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {} + + virtual ~Iterator() noexcept = default; + Iterator(const Iterator &) = default; + Iterator(Iterator &&) = default; + Iterator &operator=(const Iterator &) = default; + Iterator &operator=(Iterator &&) = default; + + Iterator &operator++() + { + m_CurrentValue.next(); + return *this; + } + + pointer operator->() const { return &m_CurrentValue; } + reference operator*() const { return m_CurrentValue; } + + bool operator==(const Iterator &other) const + { + return m_CurrentValue == other.m_CurrentValue; + } + + bool operator!=(const Iterator &other) const { return !(*this == other); } + + private: + IteratorValue m_CurrentValue; + }; + // ///// // // Ctors // // ///// // @@ -184,6 +245,13 @@ public: return arraydata_detail::Sort::sort(m_Data, sortPermutation); } + // ///////// // + // Iterators // + // ///////// // + + Iterator cbegin() const { return Iterator{m_Data, true}; } + Iterator cend() const { return Iterator{m_Data, false}; } + // ///////////// // // 1-dim methods // // ///////////// //