##// END OF EJS Templates
Creates iterator for DataSeries
Alexandre Leroux -
r556:cbc24d18371d
parent child
Show More
@@ -29,6 +29,71 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
29 29 template <int Dim>
30 30 class DataSeries : public IDataSeries {
31 31 public:
32 class IteratorValue {
33 public:
34 explicit IteratorValue(const DataSeries &dataSeries, bool begin)
35 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
36 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
37 : dataSeries.valuesData()->cend())
38 {
39 }
40
41 double x() const { return m_XIt->at(0); }
42 double value() const { return m_ValuesIt->at(0); }
43 double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); }
44
45 void next()
46 {
47 ++m_XIt;
48 ++m_ValuesIt;
49 }
50
51 bool operator==(const IteratorValue &other) const
52 {
53 return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt);
54 }
55
56 private:
57 ArrayData<1>::Iterator m_XIt;
58 typename ArrayData<Dim>::Iterator m_ValuesIt;
59 };
60
61 class Iterator {
62 public:
63 using iterator_category = std::forward_iterator_tag;
64 using value_type = const IteratorValue;
65 using difference_type = std::ptrdiff_t;
66 using pointer = value_type *;
67 using reference = value_type &;
68
69 Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {}
70 virtual ~Iterator() noexcept = default;
71 Iterator(const Iterator &) = default;
72 Iterator(Iterator &&) = default;
73 Iterator &operator=(const Iterator &) = default;
74 Iterator &operator=(Iterator &&) = default;
75
76 Iterator &operator++()
77 {
78 m_CurrentValue.next();
79 return *this;
80 }
81
82 pointer operator->() const { return &m_CurrentValue; }
83
84 reference operator*() const { return m_CurrentValue; }
85
86 bool operator==(const Iterator &other) const
87 {
88 return m_CurrentValue == other.m_CurrentValue;
89 }
90
91 bool operator!=(const Iterator &other) const { return !(*this == other); }
92
93 private:
94 IteratorValue m_CurrentValue;
95 };
96
32 97 /// @sa IDataSeries::xAxisData()
33 98 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
34 99 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
@@ -119,6 +184,18 public:
119 184 dataSeries->unlock();
120 185 }
121 186
187 // ///////// //
188 // Iterators //
189 // ///////// //
190
191 Iterator cbegin() const { return Iterator{*this, true}; }
192
193 Iterator cend() const { return Iterator{*this, false}; }
194
195 // /////// //
196 // Mutexes //
197 // /////// //
198
122 199 virtual void lockRead() { m_Lock.lockForRead(); }
123 200 virtual void lockWrite() { m_Lock.lockForWrite(); }
124 201 virtual void unlock() { m_Lock.unlock(); }
General Comments 0
You need to be logged in to leave comments. Login now