mdds
Loading...
Searching...
No Matches
soa/iterator.hpp
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3// SPDX-FileCopyrightText: 2021 - 2025 Kohei Yoshida
4//
5// SPDX-License-Identifier: MIT
6
7#pragma once
8
9#include "../iterator_node.hpp"
10
11#include <ostream>
12
13namespace mdds { namespace mtv { namespace soa { namespace detail {
14
31template<typename Traits>
32class iterator_updater
33{
34protected:
35 using parent_type = typename Traits::parent;
36 using positions_type = typename Traits::positions_type;
37 using sizes_type = typename Traits::sizes_type;
38 using element_blocks_type = typename Traits::element_blocks_type;
39 using size_type = typename Traits::parent::size_type;
40
42
43 using positions_iterator_type = typename Traits::positions_iterator_type;
44 using sizes_iterator_type = typename Traits::sizes_iterator_type;
45 using element_blocks_iterator_type = typename Traits::element_blocks_iterator_type;
46
51 struct grouped_iterator_type
52 {
53 positions_iterator_type position_iterator;
54 sizes_iterator_type size_iterator;
55 element_blocks_iterator_type element_block_iterator;
56
57 void inc()
58 {
59 ++position_iterator;
60 ++size_iterator;
61 ++element_block_iterator;
62 }
63
64 void dec()
65 {
66 --position_iterator;
67 --size_iterator;
68 --element_block_iterator;
69 }
70
71 bool operator==(const grouped_iterator_type& other) const
72 {
73 return position_iterator == other.position_iterator && size_iterator == other.size_iterator &&
74 element_block_iterator == other.element_block_iterator;
75 }
76
77 grouped_iterator_type() = default;
78
79 grouped_iterator_type(
80 const positions_iterator_type& itr_pos, const sizes_iterator_type& itr_size,
81 const element_blocks_iterator_type& itr_elem_blocks)
82 : position_iterator(itr_pos), size_iterator(itr_size), element_block_iterator(itr_elem_blocks)
83 {}
84 };
85
86 node m_cur_node;
89
90 iterator_updater() : m_cur_node(nullptr, 0, 0)
91 {}
92
94 const grouped_iterator_type& pos, const grouped_iterator_type& end, const parent_type* parent,
95 size_type block_index, container_id_t container_id)
96 : m_cur_node(parent, block_index, container_id), m_pos(pos), m_end(end)
97 {
98 if (m_pos != m_end)
99 update_node();
100 }
101
102 iterator_updater(
103 const positions_iterator_type& positions_pos, const sizes_iterator_type& sizes_pos,
104 const element_blocks_iterator_type& eb_pos, const positions_iterator_type& positions_end,
105 const sizes_iterator_type& sizes_end, const element_blocks_iterator_type& eb_end, const parent_type* parent,
106 size_type block_index, container_id_t container_id)
107 : iterator_updater(
108 {positions_pos, sizes_pos, eb_pos}, {positions_end, sizes_end, eb_end}, parent, block_index, container_id)
109 {}
110
111 iterator_updater(const iterator_updater& other)
112 : m_cur_node(other.m_cur_node), m_pos(other.m_pos), m_end(other.m_end)
113 {}
114
115 void update_node()
116 {
117#ifdef MDDS_MULTI_TYPE_VECTOR_DEBUG
118 if (m_pos == m_end)
119 throw general_error("Current node position should never equal the end position during node update.");
120#endif
121
122 m_cur_node.position = *m_pos.position_iterator;
123 m_cur_node.size = *m_pos.size_iterator;
124 m_cur_node.data = *m_pos.element_block_iterator;
125
126 if (m_cur_node.data)
127 m_cur_node.type = mdds::mtv::get_block_type(*m_cur_node.data);
128 else
129 m_cur_node.type = mdds::mtv::element_type_empty;
130 }
131
132 node* inc()
133 {
134 m_pos.inc();
135 if (m_pos == m_end)
136 return nullptr;
137
138 update_node();
139 return &m_cur_node;
140 }
141
142 node* dec()
143 {
144 m_pos.dec();
145 update_node();
146 return &m_cur_node;
147 }
148
149 void _print_state(std::ostream& os) const
150 {
151 auto prev_flags = os.flags();
152 os << "parent=" << std::hex << m_cur_node.__private_data.parent
153 << "; block-index=" << m_cur_node.__private_data.block_index << "; position=" << m_cur_node.position
154 << "; size=" << m_cur_node.size << "; type=" << m_cur_node.type << "; data=" << m_cur_node.data;
155 os.flags(prev_flags);
156 }
157
158public:
159 bool operator==(const iterator_updater& other) const
160 {
161 if (m_pos != m_end && other.m_pos != other.m_end)
162 {
163 // TODO: Set hard-coded values to the current node for the end
164 // position nodes to remove this if block.
165 if (m_cur_node != other.m_cur_node)
166 return false;
167 }
168 return m_pos == other.m_pos && m_end == other.m_end;
169 }
170
171 iterator_updater& operator=(const iterator_updater& other)
172 {
173 m_cur_node = other.m_cur_node;
174 m_pos = other.m_pos;
175 m_end = other.m_end;
176 return *this;
177 }
178
179 void swap(iterator_updater& other)
180 {
181 m_cur_node.swap(other.m_cur_node);
182 std::swap(m_pos, other.m_pos);
183 std::swap(m_end, other.m_end);
184 }
185
186 const node& get_node() const
187 {
188 return m_cur_node;
189 }
190 const grouped_iterator_type& get_pos() const
191 {
192 return m_pos;
193 }
194 const grouped_iterator_type& get_end() const
195 {
196 return m_end;
197 }
198};
199
200template<typename Traits>
201class iterator_base : public iterator_updater<Traits>
202{
203 using parent_type = typename Traits::parent;
204 using node_update_func = typename Traits::private_data_update;
205 using updater = iterator_updater<Traits>;
206
207 using grouped_iterator_type = typename updater::grouped_iterator_type;
208 using size_type = typename updater::size_type;
209
210 using updater::dec;
211 using updater::inc;
212 using updater::m_cur_node;
213
214public:
215 using updater::get_end;
216 using updater::get_pos;
217
218 // iterator traits
219 using value_type = typename updater::node;
220 using pointer = value_type*;
221 using reference = value_type&;
222 using difference_type = ptrdiff_t;
223 using iterator_category = std::bidirectional_iterator_tag;
224
225public:
226 iterator_base()
227 {}
228 iterator_base(
229 const grouped_iterator_type& pos, const grouped_iterator_type& end, const parent_type* parent,
230 size_type block_index, container_id_t container_id)
231 : updater(pos, end, parent, block_index, container_id)
232 {}
233
234 value_type& operator*()
235 {
236 return m_cur_node;
237 }
238
239 const value_type& operator*() const
240 {
241 return m_cur_node;
242 }
243
244 value_type* operator->()
245 {
246 return &m_cur_node;
247 }
248
249 const value_type* operator->() const
250 {
251 return &m_cur_node;
252 }
253
254 iterator_base& operator++()
255 {
256 node_update_func::inc(m_cur_node);
257 inc();
258 return *this;
259 }
260
261 iterator_base& operator--()
262 {
263 dec();
264 node_update_func::dec(m_cur_node);
265 return *this;
266 }
267
268 void _print_state(std::ostream& os) const
269 {
270 os << "{iterator: ";
271 updater::_print_state(os);
272 os << "}";
273 }
274};
275
276template<typename Traits, typename NonConstItrBase>
277class const_iterator_base : public iterator_updater<Traits>
278{
279 using parent_type = typename Traits::parent;
280 using node_update_func = typename Traits::private_data_update;
281 using updater = iterator_updater<Traits>;
282
283 using grouped_iterator_type = typename updater::grouped_iterator_type;
284 using size_type = typename updater::size_type;
285
286 using updater::dec;
287 using updater::inc;
288 using updater::m_cur_node;
289
290public:
291 using updater::get_end;
292 using updater::get_pos;
293
294 using iterator_base = NonConstItrBase;
295
296 // iterator traits
297 using value_type = typename updater::node;
298 using pointer = value_type*;
299 using reference = value_type&;
300 using difference_type = ptrdiff_t;
301 using iterator_category = std::bidirectional_iterator_tag;
302
303public:
304 const_iterator_base() : updater()
305 {}
306 const_iterator_base(
307 const grouped_iterator_type& pos, const grouped_iterator_type& end, const parent_type* parent,
308 size_type block_index, container_id_t container_id)
309 : updater(pos, end, parent, block_index, container_id)
310 {}
311
315 const_iterator_base(const iterator_base& other)
316 : updater(
317 other.get_pos().position_iterator, other.get_pos().size_iterator, other.get_pos().element_block_iterator,
318 other.get_end().position_iterator, other.get_end().size_iterator, other.get_end().element_block_iterator,
319 other.get_node().__private_data.parent, other.get_node().__private_data.block_index,
320 other.get_node().__private_data.container_id)
321 {}
322
323 const value_type& operator*() const
324 {
325 return m_cur_node;
326 }
327
328 const value_type* operator->() const
329 {
330 return &m_cur_node;
331 }
332
333 const_iterator_base& operator++()
334 {
335 node_update_func::inc(m_cur_node);
336 inc();
337 return *this;
338 }
339
340 const_iterator_base& operator--()
341 {
342 dec();
343 node_update_func::dec(m_cur_node);
344 return *this;
345 }
346
347 bool operator==(const const_iterator_base& other) const
348 {
349 return updater::operator==(other);
350 }
351
352 void _print_state(std::ostream& os) const
353 {
354 os << "(const-iterator: ";
355 updater::_print_state(os);
356 os << ")";
357 }
358};
359
360template<typename Traits>
361std::ostream& operator<<(std::ostream& os, const iterator_base<Traits>& it)
362{
363 it._print_state(os);
364 return os;
365}
366
367template<typename Traits, typename NonConstItrBase>
368std::ostream& operator<<(std::ostream& os, const const_iterator_base<Traits, NonConstItrBase>& it)
369{
370 it._print_state(os);
371 return os;
372}
373
374}}}} // namespace mdds::mtv::soa::detail
375
376/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition soa/iterator.hpp:278
const_iterator_base(const iterator_base &other)
Definition soa/iterator.hpp:315
Definition soa/iterator.hpp:202
Definition soa/iterator.hpp:33
Definition iterator_node.hpp:19