Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array_it.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33#ifndef ARRAY_IT_H
34#define ARRAY_IT_H
35
48#include <cassert>
49#include <tuple>
50#include <aleph.H>
51#include <ahDefs.H>
52#include <htlist.H>
53#include <ah-errors.H>
54
55namespace Aleph {
56
57template <typename T> class Array_Container;
58
75template <class T>
77{
78 T * ptr = nullptr;
79 long dim = 0;
80 long num_items = 0;
81 long idx = 0;
82 long first = 0;
83 long last = 0;
84 long pos = 0;
85
87 static void ensure_base_pointer(T * p, const size_t sz)
88 {
89 ah_invalid_argument_if(sz > 0 and p == nullptr)
90 << "Array_Iterator(): ptr == nullptr with dim > 0";
91 }
92
93 static void ensure_num_items(size_t sz, size_t n)
94 {
95 ah_domain_error_if(n > sz)
96 << "Array_Iterator(): num_items greater than dim";
97 }
98
99 static void ensure_index_in_range(const size_t sz, const long value, const char * msg)
100 {
101 ah_domain_error_if(value < 0 or static_cast<size_t>(value) >= sz)
102 << msg;
103 }
104
105 static long compute_last_index(const size_t n) noexcept
106 {
107 return n == 0 ? 0 : static_cast<long>(n - 1);
108 }
110
111public:
112
114 using Item_Type = T;
115
119 T * get_base() noexcept { return ptr; }
120
124 [[nodiscard]] constexpr const T * get_base() const noexcept { return ptr; }
125
127 Array_Iterator() = default;
128
140 Array_Iterator(T * p, const size_t sz, const size_t n)
141 : ptr(p), dim(sz), num_items(n), last(compute_last_index(n))
142 {
144 ensure_num_items(sz, n);
145 }
146
157 Array_Iterator(NoExceptionCtor, T * p, const size_t sz, const size_t n)
158 : ptr(p), dim(sz), num_items(n), last(compute_last_index(n))
159 {
160 assert(ptr);
161 assert(num_items <= dim);
162 }
163
179 Array_Iterator(T * p, const size_t sz, const size_t n, const long f, const long l)
180 : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(n == 0 ? f : l)
181 {
183 ensure_num_items(sz, n);
185 << "Array_Iterator(): dim == 0 but num_items > 0";
186 if (dim > 0)
187 {
188 ensure_index_in_range(dim, first, "Array_Iterator(): first >= dim");
189 ensure_index_in_range(dim, last, "Array_Iterator(): last >= dim");
190 }
191 }
192
201 Array_Iterator(NoExceptionCtor, T * p, const size_t sz, const size_t n, const long f, const long l)
202 : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(n == 0 ? f : l)
203 {
204 assert(ptr);
205 assert(num_items <= dim);
206 assert(dim == 0 or (first >= 0 and first < dim));
207 assert(dim == 0 or (last >= 0 and last < dim));
208 }
209
214 : Array_Iterator(c.get_base(), c.capacity(), c.size()) {}
215
219 [[nodiscard]] bool has_curr() const noexcept { return pos >= 0 and pos < num_items; }
220
224 [[nodiscard]] bool is_last() const noexcept { return pos == num_items - 1; }
225
229 [[nodiscard]] constexpr long get_pos() const noexcept { return pos; }
230
236 {
237 return ptr[idx];
238 }
239
245 T & get_curr() const
246 {
248 << "Array_Iterator::get_curr(): no current item";
249
251 << "Array_Iterator::get_curr(): no current item";
252
253 return ptr[idx];
254 }
255
260 {
261 if (++idx == dim)
262 idx = 0;
263 ++pos;
264 }
265
269 void next()
270 {
272 << "Array_Iterator::next(): no current item";
273 next_ne();
274 }
275
280 {
281 if (--idx < 0)
282 idx = dim - 1;
283 --pos;
284 }
285
289 void prev()
290 {
292 << "Array_Iterator::prev(): no current item";
293 prev_ne();
294 }
295
298 {
299 idx = first;
300 pos = 0;
301 }
302
305
308 {
309 idx = last;
310 pos = num_items - 1;
311 }
312
315 {
316 put_itor_at_the_end(*this);
317 }
318};
319
329template <typename T>
330[[nodiscard]] inline Array_Iterator<T> get_array_it(const T * array, size_t n)
331{
332 return Array_Iterator<T>(const_cast<T*>(array), n, n);
333}
334
344template <typename T>
346{
347 return Array_Container<T>(array, n);
348}
349
350
365template <typename T>
367 : public GenericTraverse<Array_Container<T>>,
368 public LocateFunctions<Array_Container<T>, T>,
369 public FunctionalMethods<Array_Container<T>, T>,
370 public GenericKeys<Array_Container<T>, T>,
371 public EqualToMethod<Array_Container<T>>,
372 public StlAlephIterator<Array_Container<T>>
373{
374 T * base = nullptr;
375 const size_t n = 0;
376
377public:
378
380 using Item_Type = T;
381
385 T * get_base() const noexcept { return base; }
386
392 Array_Container(T * base_ptr, const size_t d) : base(base_ptr), n(d)
393 {
394 // empty
395 }
396
400 [[nodiscard]] constexpr bool is_empty() const noexcept { return n == 0; }
401
405 [[nodiscard]] constexpr size_t size() const noexcept { return n; }
406
410 [[nodiscard]] constexpr size_t capacity() const noexcept { return n; }
411
416 T & get_first() const
417 {
419 << "Array_Container::get_first(): n == 0";
420 return base[0];
421 }
422
427 T & get_last() const
428 {
430 << "Array_Container::get_last(): n == 0";
431 return base[n - 1];
432 }
433
435 struct Iterator : public Array_Iterator<T>
436 {
438 Iterator(const Array_Container & c) : Array_Iterator<T>(c.base, c.n, c.n) {}
439 };
440
444 Iterator get_it() const { return Iterator(*this); }
445};
446
447} // end namespace Aleph
448
449#endif // ARRAY_IT_H
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:368
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:463
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
Core definitions, constants, and utility macros for Aleph-w.
NoExceptionCtor
Tag type for no-exception construction.
Definition ahDefs.H:72
Core header for the Aleph-w library.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
Lightweight wrapper that provides Aleph-w container interface for raw arrays.
Definition array_it.H:373
T * get_base() const noexcept
Get the base pointer.
Definition array_it.H:385
T & get_last() const
Get the last element.
Definition array_it.H:427
T Item_Type
The type of elements in the container.
Definition array_it.H:380
constexpr size_t capacity() const noexcept
Get the capacity.
Definition array_it.H:410
T & get_first() const
Get the first element.
Definition array_it.H:416
Array_Container(T *base_ptr, const size_t d)
Construct a container wrapping a raw array.
Definition array_it.H:392
Iterator get_it() const
Get an iterator to the beginning.
Definition array_it.H:444
constexpr size_t size() const noexcept
Get the number of elements.
Definition array_it.H:405
constexpr bool is_empty() const noexcept
Check if the container is empty.
Definition array_it.H:400
Iterator wrapper for C++ raw arrays.
Definition array_it.H:77
void next_ne() noexcept
Advance to the next item without bounds checking.
Definition array_it.H:259
T & get_curr() const
Get the current item with bounds checking.
Definition array_it.H:245
Array_Iterator(NoExceptionCtor, T *p, const size_t sz, const size_t n)
Construct an iterator without exception checking.
Definition array_it.H:157
void reset_last() noexcept
Reset the iterator to the last item.
Definition array_it.H:307
Array_Iterator(const Array_Container< T > &c)
Construct an iterator from an Array_Container.
Definition array_it.H:213
void reset() noexcept
Reset the iterator to the first item.
Definition array_it.H:297
Array_Iterator()=default
Default constructor - creates an invalid iterator.
T Item_Type
The type of elements being iterated.
Definition array_it.H:114
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
Definition array_it.H:235
constexpr const T * get_base() const noexcept
Get the base pointer of the array (const version).
Definition array_it.H:124
Array_Iterator(T *p, const size_t sz, const size_t n)
Construct an iterator over an array.
Definition array_it.H:140
T * get_base() noexcept
Get the base pointer of the array.
Definition array_it.H:119
Array_Iterator(NoExceptionCtor, T *p, const size_t sz, const size_t n, const long f, const long l)
Construct a circular iterator without exception checking.
Definition array_it.H:201
void end() noexcept
Put the iterator at the end (past the last item).
Definition array_it.H:314
Array_Iterator(T *p, const size_t sz, const size_t n, const long f, const long l)
Construct an iterator over a circular array region.
Definition array_it.H:179
void prev()
Move to the previous item with bounds checking.
Definition array_it.H:289
void prev_ne() noexcept
Move to the previous item without bounds checking.
Definition array_it.H:279
void next()
Advance to the next item with bounds checking.
Definition array_it.H:269
void reset_first() noexcept
Reset the iterator to the first item (alias for reset()).
Definition array_it.H:304
bool has_curr() const noexcept
Check if there is a current valid item.
Definition array_it.H:219
bool is_last() const noexcept
Check if positioned at the last item.
Definition array_it.H:224
constexpr long get_pos() const noexcept
Get the current position index.
Definition array_it.H:229
Equality test for containers.
Definition ah-dry.H:1534
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:548
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
Common sequential searching methods on containers.
Definition ah-dry.H:164
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
Array_Container< T > make_array_container(T *array, size_t n)
Create an Array_Container from a raw array.
Definition array_it.H:345
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
Array_Iterator< T > get_array_it(const T *array, size_t n)
Create an iterator for a raw array.
Definition array_it.H:330
DynList< T > maps(const C &c, Op op)
Classic map operation.
Iterator type for Array_Container.
Definition array_it.H:436
Iterator(const Array_Container &c)
Construct an iterator for the given container.
Definition array_it.H:438
Generic list of items stored in a container.
Definition ah-dry.H:1501
Generic traversal of the container through its iterator.
Definition ah-dry.H:65
DynList< int > l