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#ifndef ARRAY_IT_H
33#define ARRAY_IT_H
34
47#include <cassert>
48#include <tuple>
49#include <aleph.H>
50#include <ahDefs.H>
51#include <htlist.H>
52#include <ah-errors.H>
53
54namespace Aleph {
55
56template <typename T>
57class Array_Container;
58
83template <class T>
85{
86 T *ptr = nullptr;
87 long dim = 0;
88 long num_items = 0;
89 long idx = 0;
90 long first = 0;
91 long last = 0;
92 long pos = 0;
93
95 static void ensure_base_pointer(T *p, const size_t sz)
96 {
97 ah_invalid_argument_if(sz > 0 and p == nullptr)
98 << "Array_Iterator(): ptr == nullptr with dim > 0";
99 }
100
101 static void ensure_num_items(size_t sz, size_t n)
102 {
103 ah_domain_error_if(n > sz) << "Array_Iterator(): num_items greater than dim";
104 }
105
106 static void ensure_index_in_range(const size_t sz, const long value, const char *msg)
107 {
108 ah_domain_error_if(value < 0 or static_cast<size_t>(value) >= sz) << msg;
109 }
110
111 static long compute_last_index(const size_t n) noexcept
112 {
113 return n == 0 ? 0 : static_cast<long>(n - 1);
114 }
116
117public:
119 using Item_Type = T;
120
125 {
126 return ptr;
127 }
128
132 [[nodiscard]] constexpr const T *get_base() const noexcept
133 {
134 return ptr;
135 }
136
138 Array_Iterator() = default;
139
151 Array_Iterator(T *p, const size_t sz, const size_t n)
152 : ptr(p), dim(sz), num_items(n), last(compute_last_index(n))
153 {
155 ensure_num_items(sz, n);
156 }
157
168 Array_Iterator(NoExceptionCtor, T *p, const size_t sz, const size_t n)
169 : ptr(p), dim(sz), num_items(n), last(compute_last_index(n))
170 {
171 assert(ptr);
172 assert(num_items <= dim);
173 }
174
190 Array_Iterator(T *p, const size_t sz, const size_t n, const long f, const long l)
191 : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(n == 0 ? f : l)
192 {
194 ensure_num_items(sz, n);
196 << "Array_Iterator(): dim == 0 but num_items > 0";
197 if (dim > 0)
198 {
199 ensure_index_in_range(dim, first, "Array_Iterator(): first >= dim");
200 ensure_index_in_range(dim, last, "Array_Iterator(): last >= dim");
201 }
202 }
203
212 Array_Iterator(NoExceptionCtor, T *p, const size_t sz, const size_t n, const long f, const long l)
213 : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(n == 0 ? f : l)
214 {
215 assert(ptr);
216 assert(num_items <= dim);
217 assert(dim == 0 or (first >= 0 and first < dim));
218 assert(dim == 0 or (last >= 0 and last < dim));
219 }
220
225 : Array_Iterator(c.get_base(), c.capacity(), c.size())
226 {}
227
232 {
233 return pos >= 0 and pos < num_items;
234 }
235
242 {
243 return has_curr() and pos == num_items - 1;
244 }
245
249 [[nodiscard]] constexpr long get_pos() const noexcept
250 {
251 return pos;
252 }
253
259 {
260 return ptr[idx];
261 }
262
268 T &get_curr() const
269 {
270 ah_underflow_error_if(pos < 0) << "Array_Iterator::get_curr(): no current item";
271
272 ah_overflow_error_if(pos >= num_items) << "Array_Iterator::get_curr(): no current item";
273
274 return ptr[idx];
275 }
276
281 {
282 if (++idx == dim)
283 idx = 0;
284 ++pos;
285 }
286
290 void next()
291 {
293 << "Array_Iterator::next(): no current item";
294 next_ne();
295 }
296
301 {
302 if (--idx < 0)
303 idx = dim - 1;
304 --pos;
305 }
306
310 void prev()
311 {
312 ah_underflow_error_if(num_items == 0 or pos < 0) << "Array_Iterator::prev(): no current item";
313 prev_ne();
314 }
315
318 {
319 idx = first;
320 pos = 0;
321 }
322
325 {
326 reset();
327 }
328
331 {
332 idx = last;
333 pos = num_items - 1;
334 }
335
350 void set_pos(const long new_pos) noexcept
351 {
352 pos = new_pos;
353 if (dim > 0)
354 idx = (first + new_pos) % dim;
355 }
356
364 {
366 }
367};
368
378template <typename T>
379[[nodiscard]] inline Array_Iterator<T> get_array_it(const T *array, size_t n)
380{
381 return Array_Iterator<T>(const_cast<T *>(array), n, n);
382}
383
393template <typename T>
395{
396 return Array_Container<T>(array, n);
397}
398
418template <typename T>
419class Array_Container : public GenericTraverse<Array_Container<T>>,
420 public LocateFunctions<Array_Container<T>, T>,
421 public FunctionalMethods<Array_Container<T>, T>,
422 public GenericKeys<Array_Container<T>, T>,
423 public EqualToMethod<Array_Container<T>>,
424 public StlAlephIterator<Array_Container<T>>
425{
426 T *base = nullptr;
427 size_t n = 0;
428
429public:
431 using Item_Type = T;
432
437 {
438 return base;
439 }
440
446 Array_Container(T *base_ptr, const size_t d) : base(base_ptr), n(d)
447 {
448 // empty
449 }
450
454 [[nodiscard]] constexpr bool is_empty() const noexcept
455 {
456 return n == 0;
457 }
458
462 [[nodiscard]] constexpr size_t size() const noexcept
463 {
464 return n;
465 }
466
470 [[nodiscard]] constexpr size_t capacity() const noexcept
471 {
472 return n;
473 }
474
479 T &get_first() const
480 {
481 ah_underflow_error_if(n == 0) << "Array_Container::get_first(): n == 0";
482 return base[0];
483 }
484
489 T &get_last() const
490 {
491 ah_underflow_error_if(n == 0) << "Array_Container::get_last(): n == 0";
492 return base[n - 1];
493 }
494
496 struct Iterator : public Array_Iterator<T>
497 {
499 Iterator(const Array_Container &c) : Array_Iterator<T>(c.base, c.n, c.n) {}
500 };
501
506 {
507 return Iterator(*this);
508 }
509};
510
511} // end namespace Aleph
512
513#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:373
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:468
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:527
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
Core definitions, constants, and utility macros for Aleph-w.
NoExceptionCtor
Tag type for no-exception construction.
Definition ahDefs.H:74
Core header for the Aleph-w library.
size_t size_t int32_t value
Definition ca-c-api.h:116
Lightweight wrapper that provides Aleph-w container interface for raw arrays.
Definition array_it.H:425
T * get_base() const noexcept
Get the base pointer.
Definition array_it.H:436
T & get_last() const
Get the last element.
Definition array_it.H:489
T Item_Type
The type of elements in the container.
Definition array_it.H:431
constexpr size_t capacity() const noexcept
Get the capacity.
Definition array_it.H:470
T & get_first() const
Get the first element.
Definition array_it.H:479
Array_Container(T *base_ptr, const size_t d)
Construct a container wrapping a raw array.
Definition array_it.H:446
Iterator get_it() const
Get an iterator to the beginning.
Definition array_it.H:505
constexpr size_t size() const noexcept
Get the number of elements.
Definition array_it.H:462
constexpr bool is_empty() const noexcept
Check if the container is empty.
Definition array_it.H:454
Iterator wrapper for C++ raw arrays.
Definition array_it.H:85
void next_ne() noexcept
Advance to the next item without bounds checking.
Definition array_it.H:280
T & get_curr() const
Get the current item with bounds checking.
Definition array_it.H:268
Array_Iterator(NoExceptionCtor, T *p, const size_t sz, const size_t n)
Construct an iterator without exception checking.
Definition array_it.H:168
void reset_last() noexcept
Reset the iterator to the last item.
Definition array_it.H:330
Array_Iterator(const Array_Container< T > &c)
Construct an iterator from an Array_Container.
Definition array_it.H:224
void reset() noexcept
Reset the iterator to the first item.
Definition array_it.H:317
Array_Iterator()=default
Default constructor - creates an invalid iterator.
T Item_Type
The type of elements being iterated.
Definition array_it.H:119
T & get_curr_ne() const noexcept
Get the current item without bounds checking.
Definition array_it.H:258
constexpr const T * get_base() const noexcept
Get the base pointer of the array (const version).
Definition array_it.H:132
void set_pos(const long new_pos) noexcept
Move the iterator to the logical position new_pos.
Definition array_it.H:350
Array_Iterator(T *p, const size_t sz, const size_t n)
Construct an iterator over an array.
Definition array_it.H:151
T * get_base() noexcept
Get the base pointer of the array.
Definition array_it.H:124
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:212
void end() noexcept
Put the iterator at the end (past the last item).
Definition array_it.H:363
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:190
void prev()
Move to the previous item with bounds checking.
Definition array_it.H:310
void prev_ne() noexcept
Move to the previous item without bounds checking.
Definition array_it.H:300
void next()
Advance to the next item with bounds checking.
Definition array_it.H:290
void reset_first() noexcept
Reset the iterator to the first item (alias for reset()).
Definition array_it.H:324
bool has_curr() const noexcept
Check if there is a current valid item.
Definition array_it.H:231
bool is_last() const noexcept
Check if positioned at the last item.
Definition array_it.H:241
constexpr long get_pos() const noexcept
Get the current position index.
Definition array_it.H:249
Equality test for containers.
Definition ah-dry.H:1959
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:658
Common sequential searching methods on containers.
Definition ah-dry.H:200
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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:394
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Array_Iterator< T > get_array_it(const T *array, size_t n)
Create an iterator for a raw array.
Definition array_it.H:379
Iterator type for Array_Container.
Definition array_it.H:497
Iterator(const Array_Container &c)
Construct an iterator for the given container.
Definition array_it.H:499
Generic list of items stored in a container.
Definition ah-dry.H:1846
Generic traversal of the container through its iterator.
Definition ah-dry.H:71
DynList< int > l