mdds
Loading...
Searching...
No Matches
aos/iterator.hpp
1// SPDX-FileCopyrightText: 2012 - 2025 Kohei Yoshida
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include "../iterator_node.hpp"
8
9#include <cstddef>
10
11namespace mdds { namespace mtv { namespace aos { namespace detail {
12
18template<typename Traits>
19class iterator_common_base
20{
21protected:
22 typedef typename Traits::parent parent_type;
23 typedef typename Traits::blocks blocks_type;
24 typedef typename Traits::base_iterator base_iterator_type;
25
26 typedef typename parent_type::size_type size_type;
28
29 iterator_common_base() : m_cur_node(nullptr, 0, 0)
30 {}
31
32 iterator_common_base(
33 const base_iterator_type& pos, const base_iterator_type& end, const parent_type* parent, size_type block_index,
34 container_id_t container_id)
35 : m_cur_node(parent, block_index, container_id), m_pos(pos), m_end(end)
36 {
37 if (m_pos != m_end)
38 update_node();
39 }
40
41 iterator_common_base(const iterator_common_base& other)
42 : m_cur_node(other.m_cur_node), m_pos(other.m_pos), m_end(other.m_end)
43 {}
44
45 void update_node()
46 {
47#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
48 if (m_pos == m_end)
49 throw general_error("Current node position should never equal the end position during node update.");
50#endif
51 // blocks_type::value_type is a pointer to multi_type_vector::block.
52 const typename blocks_type::value_type& blk = *m_pos;
53 if (blk.data)
54 m_cur_node.type = mdds::mtv::get_block_type(*blk.data);
55 else
56 m_cur_node.type = mdds::mtv::element_type_empty;
57
58 m_cur_node.position = blk.position;
59 m_cur_node.size = blk.size;
60 m_cur_node.data = blk.data;
61 }
62
63 node* inc()
64 {
65 ++m_pos;
66 if (m_pos == m_end)
67 return nullptr;
68
69 update_node();
70 return &m_cur_node;
71 }
72
73 node* dec()
74 {
75 --m_pos;
76 update_node();
77 return &m_cur_node;
78 }
79
80 node m_cur_node;
81 base_iterator_type m_pos;
82 base_iterator_type m_end;
83
84public:
85 bool operator==(const iterator_common_base& other) const
86 {
87 if (m_pos != m_end && other.m_pos != other.m_end)
88 {
89 // TODO: Set hard-coded values to the current node for the end
90 // position nodes to remove this if block.
91 if (m_cur_node != other.m_cur_node)
92 return false;
93 }
94 return m_pos == other.m_pos && m_end == other.m_end;
95 }
96
97 iterator_common_base& operator=(const iterator_common_base& other)
98 {
99 m_cur_node = other.m_cur_node;
100 m_pos = other.m_pos;
101 m_end = other.m_end;
102 return *this;
103 }
104
105 void swap(iterator_common_base& other)
106 {
107 m_cur_node.swap(other.m_cur_node);
108 std::swap(m_pos, other.m_pos);
109 std::swap(m_end, other.m_end);
110 }
111
112 const node& get_node() const
113 {
114 return m_cur_node;
115 }
116 const base_iterator_type& get_pos() const
117 {
118 return m_pos;
119 }
120 const base_iterator_type& get_end() const
121 {
122 return m_end;
123 }
124};
125
126template<typename Traits, typename NodeUpdateFunc>
127class iterator_base : public iterator_common_base<Traits>
128{
129 using parent_type = typename Traits::parent;
130 typedef NodeUpdateFunc node_update_func;
131 typedef iterator_common_base<Traits> common_base;
132
133 typedef typename Traits::base_iterator base_iterator_type;
134 typedef typename common_base::size_type size_type;
135
136 using common_base::dec;
137 using common_base::inc;
138 using common_base::m_cur_node;
139
140public:
141 using common_base::get_end;
142 using common_base::get_pos;
143
144 // iterator traits
145 typedef typename common_base::node value_type;
146 typedef value_type* pointer;
147 typedef value_type& reference;
148 typedef ptrdiff_t difference_type;
149 typedef std::bidirectional_iterator_tag iterator_category;
150
151public:
152 iterator_base()
153 {}
154 iterator_base(
155 const base_iterator_type& pos, const base_iterator_type& end, const parent_type* parent, size_type block_index,
156 container_id_t container_id)
157 : common_base(pos, end, parent, block_index, container_id)
158 {}
159
160 value_type& operator*()
161 {
162 return m_cur_node;
163 }
164
165 const value_type& operator*() const
166 {
167 return m_cur_node;
168 }
169
170 value_type* operator->()
171 {
172 return &m_cur_node;
173 }
174
175 const value_type* operator->() const
176 {
177 return &m_cur_node;
178 }
179
180 iterator_base& operator++()
181 {
182 node_update_func::inc(m_cur_node);
183 inc();
184 return *this;
185 }
186
187 iterator_base& operator--()
188 {
189 dec();
190 node_update_func::dec(m_cur_node);
191 return *this;
192 }
193};
194
195template<typename Traits, typename NodeUpdateFunc, typename NonConstItrBase>
196class const_iterator_base : public iterator_common_base<Traits>
197{
198 using parent_type = typename Traits::parent;
199 typedef NodeUpdateFunc node_update_func;
200 typedef iterator_common_base<Traits> common_base;
201
202 typedef typename Traits::base_iterator base_iterator_type;
203 typedef typename common_base::size_type size_type;
204
205 using common_base::dec;
206 using common_base::inc;
207 using common_base::m_cur_node;
208
209public:
210 using common_base::get_end;
211 using common_base::get_pos;
212
213 typedef NonConstItrBase iterator_base;
214
215 // iterator traits
216 typedef typename common_base::node value_type;
217 typedef value_type* pointer;
218 typedef value_type& reference;
219 typedef ptrdiff_t difference_type;
220 typedef std::bidirectional_iterator_tag iterator_category;
221
222public:
223 const_iterator_base() : common_base()
224 {}
225 const_iterator_base(
226 const base_iterator_type& pos, const base_iterator_type& end, const parent_type* parent, size_type block_index,
227 container_id_t container_id)
228 : common_base(pos, end, parent, block_index, container_id)
229 {}
230
234 const_iterator_base(const iterator_base& other)
235 : common_base(
236 other.get_pos(), other.get_end(), other.get_node().__private_data.parent,
237 other.get_node().__private_data.block_index, other.get_node().__private_data.container_id)
238 {}
239
240 const value_type& operator*() const
241 {
242 return m_cur_node;
243 }
244
245 const value_type* operator->() const
246 {
247 return &m_cur_node;
248 }
249
250 const_iterator_base& operator++()
251 {
252 node_update_func::inc(m_cur_node);
253 inc();
254 return *this;
255 }
256
257 const_iterator_base& operator--()
258 {
259 dec();
260 node_update_func::dec(m_cur_node);
261 return *this;
262 }
263
264 bool operator==(const const_iterator_base& other) const
265 {
266 return iterator_common_base<Traits>::operator==(other);
267 }
268};
269
270}}}} // namespace mdds::mtv::aos::detail
Definition global.hpp:54
const_iterator_base(const iterator_base &other)
Definition aos/iterator.hpp:234
Definition iterator_node.hpp:19