Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynArray.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
43#ifndef TPL_DYNARRAY_H
44#define TPL_DYNARRAY_H
45
46#include <cmath>
47
48#include <string>
49#include <concepts>
50#include <functional>
51#include <memory>
52#include <type_traits>
53#include <aleph.H>
54#include <ahIterator.H>
55#include <array_utils.H>
56#include <ahDry.H>
57#include <tpl_dynDlist.H>
58#include <ah-args-ctor.H>
59#include <htlist.H>
60#include <ah-concepts.H>
61#include <ah-dry.H>
62#include <ah-errors.H>
63#include <tpl_array.H>
64#include <ah-iterator.H>
65
66namespace Aleph {
67
214template <typename T>
215class DynArray : public LocateFunctions<DynArray<T>, T>,
216 public FunctionalMethods<DynArray<T>, T>,
217 public GenericKeys<DynArray<T>, T>,
218 public EqualSequenceMethod<DynArray<T>>,
219 public StlAlephIterator<DynArray<T>>
220{
221 // look at the end of this file for seeing the values
222public:
223 using Item_Type = T;
224 using Key_Type = T;
225
226 static const size_t Default_Pow_Dir;
227 static const size_t Default_Pow_Seg;
228 static const size_t Default_Pow_Block;
229
230private:
231 static const size_t Max_Bits_Allowed;
232
233public:
235 static const unsigned long long Max_Dim_Allowed;
236
237private:
238 static const size_t Max_Pow_Block;
239
240 mutable size_t pow_dir = Default_Pow_Dir;
241 mutable size_t pow_seg = Default_Pow_Seg;
242 mutable size_t pow_block = std::min(Default_Pow_Block, Max_Pow_Block);
245 mutable size_t dir_size = two_raised(pow_dir); // = 2^pow_dir
246 mutable size_t seg_size = two_raised(pow_seg); // = 2^pow_seg
247 mutable size_t block_size = two_raised(pow_block); // = 2^pow_block
248
249 // 2^(pow_dir + pow_seg + pow_block) - 1
251
257 static size_t two_raised(const size_t n)
258 {
259 ah_overflow_error_if(n >= Max_Bits_Allowed) << "two_raised: exponent " << n << " is too large";
260 return static_cast<size_t>(1) << n;
261 }
262
270 static size_t compute_dim(size_t d, size_t s, size_t b)
271 {
272 return two_raised(d) * two_raised(s) * two_raised(b);
273 }
274
275public:
286 static void compute_sizes(const size_t n, size_t &d, size_t &s, size_t &b)
287 {
288 d = Default_Pow_Dir;
289 s = Default_Pow_Seg;
291 if (compute_dim(d, s, b) >= n)
292 return;
293
294 while (true)
295 {
296 if (compute_dim(++d, s, b) >= n)
297 break;
298
299 if (compute_dim(d, ++s, b) >= n)
300 break;
301
302 if (compute_dim(d, s, ++b) >= n)
303 break;
304 }
305 }
306
315 static std::tuple<size_t, size_t, size_t> compute_sizes(const size_t n)
316 {
317 size_t d, s, b;
318 compute_sizes(n, d, s, b);
319 return std::make_tuple(d, s, b);
320 }
321
322private:
323 size_t mask_seg = seg_size - 1;
324 size_t mask_block = block_size - 1;
325
326 size_t index_in_dir(const size_t i) const noexcept
327 {
328 assert((pow_block + pow_seg) == seg_plus_block_pow);
330 assert(i >> seg_plus_block_pow == i / (seg_size * block_size));
331
332 return i >> seg_plus_block_pow;
333 }
334
335 size_t modulus_from_index_in_dir(const size_t i) const noexcept
336 {
337 assert(mask_seg_plus_block == seg_size * block_size - 1);
338 assert((i & mask_seg_plus_block) == i % (seg_size * block_size));
339
340 return (i & mask_seg_plus_block);
341 }
342
343 size_t index_in_seg(const size_t &i) const noexcept
344 {
345 assert(two_raised(pow_block) == block_size);
347
349 }
350
351 size_t index_in_block(const size_t i) const noexcept
352 {
353 assert(mask_block == block_size - 1);
355 ((i % (seg_size * block_size)) % block_size));
356
358 }
359
361 size_t num_segs = 0;
362 size_t num_blocks = 0;
363 T ***dir = nullptr;
364
365 void fill_dir_to_null() noexcept
366 {
367 assert(dir != nullptr);
368
369 for (size_t i = 0; i < dir_size; ++i)
370 dir[i] = nullptr;
371 }
372
373 void fill_seg_to_null(T **seg) noexcept
374 {
375 assert(seg != nullptr);
376
377 for (size_t i = 0; i < seg_size; ++i)
378 seg[i] = nullptr;
379 }
380
382 {
383 dir = static_cast<T ***>(malloc(dir_size * sizeof(T **)));
384 ah_bad_alloc_unless(dir != nullptr);
385
387 }
388
400 {
401 if (dir == nullptr)
402 allocate_dir();
403 }
404
405 void resize_dir(const size_t i) // resize dir to fit index i
406 {
407 assert(i >= max_dim);
408 // As the constructor does, before anything grows: past that limit the
409 // array would take an index that it does not allow, or its directory
410 // would ask for terabytes
412 << "DynArray: index " << i << " is not less than the maximum dimension allowed "
414 ensure_dir(); // realloc() must extend a fully initialized directory
415
416 size_t new_pow_dir = pow_dir + 1;
417 while (compute_dim(new_pow_dir, pow_seg, pow_block) <= i)
418 ++new_pow_dir;
419
420 const size_t new_dir_sz = two_raised(new_pow_dir);
421 T ***new_dir = static_cast<T ***>(realloc(dir, new_dir_sz * sizeof(T **)));
422 ah_bad_alloc_unless(new_dir != nullptr);
423
424 dir = new_dir;
425 for (size_t k = dir_size; k < new_dir_sz; ++k)
426 dir[k] = nullptr;
427
428 pow_dir = new_pow_dir;
429 dir_size = new_dir_sz;
430
432 }
433
434 void allocate_segment(T **&seg)
435 {
436 assert(seg == nullptr);
437
438 seg = new T *[seg_size];
439 fill_seg_to_null(seg);
440 ++num_segs;
441 }
442
445
457 void allocate_block(T *&block)
458 {
459 assert(block == nullptr);
460
461 std::unique_ptr<T[]> whole(new T[block_size]);
462 if (default_initial_value_ptr != nullptr)
463 for (T *entry = whole.get(), *end = entry + block_size; entry != end; ++entry)
465
466 block = whole.release();
467 ++num_blocks;
468 }
469
470 void release_segment(T **&seg) noexcept
471 {
472 assert(seg != nullptr);
473
474 delete[] seg;
475 seg = nullptr;
476 --num_segs;
477 }
478
479 void release_block(T *&block) noexcept
480 {
481 assert(block != nullptr);
482
483 delete[] block;
484 block = nullptr;
485 --num_blocks;
486 }
487
488 void release_blocks_and_segment(T **&seg) noexcept
489 {
490 assert(seg != nullptr);
491
492 for (size_t i = 0; i < seg_size; ++i)
493 if (seg[i] != nullptr)
494 release_block(seg[i]);
495
496 release_segment(seg);
497 }
498
499 void ensure_not_empty(const char *context) const
500 {
501 ah_underflow_error_if(is_empty()) << context;
502 }
503
505 {
506 if (dir != nullptr) // an array that never wrote, or was moved from, has none
507 for (size_t i = 0; i < dir_size; ++i)
508 if (dir[i] != nullptr)
510
511 current_dim = 0;
512 }
513
514 void release_dir() noexcept
515 {
516 if (dir == nullptr)
517 return;
518
520 if (dir != nullptr)
521 free(dir);
522
523 dir = nullptr;
524 current_dim = 0;
525 }
526
527 static size_t next2Pow(const size_t number) noexcept
528 {
529 return static_cast<size_t>(ceil(log(static_cast<float>(number)) / log(2.0)));
530 }
531
532 size_t divide_by_block_size(const size_t number) const noexcept
533 {
534 assert(number / block_size == number >> pow_block);
535
536 return number >> pow_block;
537 }
538
539 size_t modulus_by_block_size(const size_t number) const noexcept
540 {
541 assert((number % block_size) == (number & mask_block));
542
543 return number & mask_block;
544 }
545
546 void allocate_block(T *&block, T *src_block)
547 {
548 allocate_block(block);
549 for (size_t i = 0; i < block_size; i++)
550 block[i] = src_block[i];
551 }
552
553 void allocate_segment(T **&seg, T **src_seg)
554 {
555 allocate_segment(seg);
556 for (size_t i = 0; i < seg_size; i++)
557 if (src_seg[i] != nullptr)
558 allocate_block(seg[i], src_seg[i]);
559 }
560
561 class Proxy
562 {
563 size_t index;
570
571 public:
572 Proxy(DynArray<T> &_array, const size_t i) noexcept
573 : index(i), pos_in_dir(_array.index_in_dir(index)), pos_in_seg(_array.index_in_seg(index)),
574 pos_in_block(_array.index_in_block(index)), ref_seg(_array.dir[pos_in_dir]), block(nullptr),
575 array(_array)
576 {
577 if (ref_seg != nullptr)
578 block = ref_seg[pos_in_seg]; // Entry block already exists
579 }
580
592 operator T &()
593 {
594 return *operator->();
595 }
596
598 {
599 bool seg_was_allocated_in_current_call = false;
600
601 if (ref_seg == nullptr) // Is there a segment?
602 { // No ==> allocate it!
604 seg_was_allocated_in_current_call = true;
605 }
606
607 if (block == nullptr) // test if block is allocated
608 {
609 try
610 {
613
614 assert(array.dir[pos_in_dir] == ref_seg);
615 }
616 catch (...)
617 {
618 if (seg_was_allocated_in_current_call)
620
621 throw;
622 }
623 }
624
625 if (index >= array.current_dim)
626 array.current_dim = index + 1;
627
628 return &block[pos_in_block];
629 }
630
631 Proxy &operator = (const T &data)
632 {
633 bool seg_was_allocated_in_current_call = false;
634 if (ref_seg == nullptr) // Is there a segment?
635 { // No ==> allocate ii!
637 seg_was_allocated_in_current_call = true;
638 }
639
640 if (block == nullptr) // test if block is allocated
641 {
642 try
643 {
646
647 assert(array.dir[pos_in_dir] == ref_seg);
648 }
649 catch (...)
650 {
651 if (seg_was_allocated_in_current_call)
653
654 throw;
655 }
656 }
657
658 if (index >= array.current_dim)
659 array.current_dim = index + 1;
660
661 block[pos_in_block] = data;
662
663 return *this;
664 }
665
666 Proxy &operator = (const Proxy &proxy)
667 {
668 if (&proxy == this)
669 return *this;
670
671 // An entry that was never written reads as its array's default value.
672 // Allocating blocks never moves existing ones, so this reference stays
673 // valid while the left entry is allocated below.
674 const T &value = proxy.block != nullptr ? proxy.block[proxy.pos_in_block]
676
677 bool seg_was_allocated_in_current_call = false;
678 if (ref_seg == nullptr) // Is there a segment?
679 { // No ==> allocate it!
681 seg_was_allocated_in_current_call = true;
682 }
683
684 if (block == nullptr) // test if block is allocated
685 {
686 try
687 {
690
691 assert(array.dir[pos_in_dir] == ref_seg);
692 }
693 catch (...)
694 {
695 if (seg_was_allocated_in_current_call)
697
698 throw;
699 }
700 }
701
702 if (index >= array.current_dim)
703 array.current_dim = index + 1;
704
706
707 return *this;
708 }
709 };
710
711public:
713 size_t get_dir_size() const noexcept
714 {
715 return dir_size;
716 }
717
719 size_t get_seg_size() const noexcept
720 {
721 return seg_size;
722 }
723
725 size_t get_block_size() const noexcept
726 {
727 return block_size;
728 }
729
733 size_t size() const noexcept
734 {
735 return current_dim;
736 }
737
742 size_t max_size() const noexcept
743 {
744 return max_dim;
745 }
746
748 size_t get_num_blocks() const noexcept
749 {
750 return num_blocks;
751 }
752
766 noexcept(std::is_nothrow_copy_assignable_v<T>)
767 {
770 }
771
775 void set_default_initial_value(T &&value = T()) noexcept(std::is_nothrow_swappable_v<T>)
776 {
777 std::swap(default_initial_value, value);
779 }
780
800 DynArray(const size_t _pow_dir, const size_t _pow_seg, const size_t _pow_block)
801 : pow_dir(_pow_dir), pow_seg(_pow_seg), pow_block(_pow_block),
802 seg_plus_block_pow(_pow_seg + _pow_block),
807 {
808 static_assert(std::is_copy_constructible_v<T>, "No copy constructor for T");
809 static_assert(std::is_move_constructible_v<T>, "No move constructor for T");
810 static_assert(std::is_copy_assignable_v<T>, "No copy assign for T");
811 static_assert(std::is_move_assignable_v<T>, "No move assign for T");
812 assert(Max_Dim_Allowed > 0);
813
814 ah_length_error_if(max_dim > Max_Dim_Allowed) << "Dimension too large";
815 }
816
827 DynArray() : DynArray(size_t{0})
828 { /* empty */
829 }
830
852 explicit DynArray(const size_t dim) : current_dim(dim)
853 {
854 static_assert(std::is_default_constructible_v<T>, "No default constructor for T");
855 static_assert(std::is_copy_constructible_v<T>, "No copy constructor for T");
856 static_assert(std::is_move_constructible_v<T>, "No move constructor for T");
857 static_assert(std::is_copy_assignable_v<T>, "No copy assign for T");
858 static_assert(std::is_move_assignable_v<T>, "No move assign for T");
859 assert(Max_Dim_Allowed > 0);
860
862 << "DynArray: dimension " << dim << " is greater than the maximum allowed "
864
865 // Grow the directory geometry, so that it covers [0, dim). The
866 // directory itself is allocated by the first write.
867 while (max_dim < dim)
868 {
869 ++pow_dir;
872 }
873
874 ah_length_error_if(max_dim > Max_Dim_Allowed) << "Dimension too large";
875 }
876
885 DynArray(const size_t n, const T & value) : DynArray(n)
886 {
887 if (n == 0)
888 return;
889
890 reserve(0, n - 1);
891 for (size_t i = 0; i < n; ++i)
892 access(i) = value;
893 }
894
896
898 {
899 release_dir();
900 }
901
906 void copy_array(const DynArray<T> &src_array)
907 {
908 for (size_t i = 0; i < src_array.current_dim; ++i)
909 if (src_array.exist(i))
910 (*this)[i] = src_array.access(i);
911 }
912
924 DynArray(const DynArray<T> &array)
925 : DynArray(array.pow_dir, array.pow_seg, array.pow_block)
926 {
928 // An array that never wrote, or that was moved from, has no directory:
929 // its copy takes none either
930 if (array.dir != nullptr)
931 {
932 allocate_dir();
933 for (size_t i = 0; i < dir_size; ++i)
934 if (array.dir[i] != nullptr)
935 allocate_segment(dir[i], array.dir[i]);
936 }
937 current_dim = array.current_dim;
938 }
939
951 {
952 if (this != &array)
953 {
954 DynArray<T> copy(array);
955 swap(copy);
956 }
957
958 return *this;
959 }
960
970 void swap(DynArray<T> &array) noexcept(std::is_nothrow_swappable_v<T>)
971 {
972 // The swap that the specification evaluates: that of T found by its
973 // arguments, else std::swap(). First when it may throw; when it may
974 // not, the order cannot be seen, and it goes last, as it always did
975 using std::swap;
976 if constexpr (not std::is_nothrow_swappable_v<T>)
977 swap(default_initial_value, array.default_initial_value);
978 std::swap(dir, array.dir);
979 std::swap(pow_dir, array.pow_dir);
980 std::swap(pow_seg, array.pow_seg);
981 std::swap(pow_block, array.pow_block);
982 std::swap(seg_plus_block_pow, array.seg_plus_block_pow);
983 std::swap(mask_seg_plus_block, array.mask_seg_plus_block);
984 std::swap(dir_size, array.dir_size);
985 std::swap(seg_size, array.seg_size);
986 std::swap(block_size, array.block_size);
987 std::swap(mask_seg, array.mask_seg);
988 std::swap(mask_block, array.mask_block);
989 std::swap(max_dim, array.max_dim);
990 std::swap(current_dim, array.current_dim);
991 std::swap(num_segs, array.num_segs);
992 std::swap(num_blocks, array.num_blocks);
993
994 if constexpr (std::is_nothrow_swappable_v<T>)
995 swap(default_initial_value, array.default_initial_value);
996
998 array.default_initial_value_ptr = &array.default_initial_value;
999 }
1000
1019
1025 DynArray &operator = (DynArray &&other) noexcept(std::is_nothrow_swappable_v<T>)
1026 {
1027 swap(other);
1028 return *this;
1029 }
1030
1042 T &access(const size_t i) noexcept
1043 {
1044 assert(dir[index_in_dir(i)] != nullptr);
1045 assert(dir[index_in_dir(i)][index_in_seg(i)] != nullptr);
1046
1047 return dir[index_in_dir(i)][index_in_seg(i)][index_in_block(i)];
1048 }
1049
1052 const T &access(const size_t i) const noexcept
1053 {
1054 assert(dir[index_in_dir(i)] != nullptr);
1055 assert(dir[index_in_dir(i)][index_in_seg(i)] != nullptr);
1056
1057 return dir[index_in_dir(i)][index_in_seg(i)][index_in_block(i)];
1058 }
1059
1061 T &operator () (const size_t i) noexcept
1062 {
1063 return access(i);
1064 }
1065
1068 const T &operator () (const size_t i) const noexcept
1069 {
1070 return access(i);
1071 }
1072
1080 bool exist(const size_t i) const
1081 {
1082 if (i >= max_dim or dir == nullptr)
1083 return false;
1084
1085 const size_t pos_in_dir = index_in_dir(i);
1086
1087 assert(pos_in_dir < dir_size);
1088
1089 if (dir[pos_in_dir] == nullptr)
1090 return false;
1091
1092 const size_t pos_in_seg = index_in_seg(i);
1093
1094 assert(pos_in_seg < seg_size);
1095
1096 if (dir[pos_in_dir][pos_in_seg] == nullptr)
1097 return false;
1098
1099 return true;
1100 }
1101
1112 T *test(const size_t i) const noexcept
1113 {
1114 if (i >= max_dim or dir == nullptr)
1115 return nullptr;
1116
1117 const size_t pos_in_dir = index_in_dir(i);
1118 if (dir[pos_in_dir] == nullptr)
1119 return nullptr;
1120
1121 const size_t pos_in_seg = index_in_seg(i);
1122 if (dir[pos_in_dir][pos_in_seg] == nullptr)
1123 return nullptr;
1124
1125 return &dir[index_in_dir(i)][index_in_seg(i)][index_in_block(i)];
1126 }
1127
1145 T &touch(const size_t i)
1146 {
1147 // resize_dir() checks the limit before it makes the directory: an index
1148 // that the array does not allow takes no memory
1149 if (i >= max_dim)
1150 resize_dir(i);
1151 else
1152 ensure_dir();
1153
1154 const size_t pos_in_dir = index_in_dir(i);
1155 bool new_segment = false;
1156 if (dir[pos_in_dir] == nullptr)
1157 {
1158 allocate_segment(dir[pos_in_dir]);
1159 new_segment = true;
1160 }
1161
1162 const size_t pos_in_seg = index_in_seg(i);
1163 if (dir[pos_in_dir][pos_in_seg] == nullptr)
1164 {
1165 try
1166 {
1167 allocate_block(dir[pos_in_dir][pos_in_seg]);
1168 }
1169 catch (...)
1170 {
1171 if (new_segment && dir[pos_in_dir] != nullptr)
1172 release_segment(dir[pos_in_dir]);
1173 throw;
1174 }
1175 }
1176
1177 if (i >= current_dim)
1178 current_dim = i + 1;
1179
1180 return dir[pos_in_dir][pos_in_seg][index_in_block(i)];
1181 }
1182
1202 void reserve(const size_t l, const size_t r)
1203 {
1204 ah_domain_error_if(l > r) << "invalid range";
1205
1206 // as touch() does: the limit before the directory
1207 if (r >= max_dim)
1208 resize_dir(r);
1209 else
1210 ensure_dir();
1211
1212 const size_t first_seg = index_in_dir(l);
1213 const size_t last_seg = index_in_dir(r);
1214 const size_t first_block = index_in_seg(l);
1215 const size_t last_block = index_in_seg(r);
1216
1217 // First pass: count the segments and blocks this call must allocate.
1218 // It keeps the common case, where the whole range already exists, free
1219 // of heap allocations, and it lets the rollback bookkeeping be sized
1220 // before anything is allocated. Recording an allocation then never
1221 // allocates, so it cannot fail and leave an allocation out of the
1222 // rollback.
1223 size_t missing_segs = 0;
1224 size_t missing_blocks = 0;
1225 for (size_t seg_idx = first_seg; seg_idx <= last_seg; ++seg_idx)
1226 {
1227 const size_t lo = (seg_idx == first_seg) ? first_block : 0;
1228 const size_t hi = (seg_idx == last_seg) ? last_block : seg_size - 1;
1229 if (dir[seg_idx] == nullptr)
1230 {
1231 ++missing_segs;
1232 missing_blocks += hi - lo + 1;
1233 }
1234 else
1235 for (size_t block_idx = lo; block_idx <= hi; ++block_idx)
1236 if (dir[seg_idx][block_idx] == nullptr)
1237 ++missing_blocks;
1238 }
1239
1240 if (missing_blocks == 0) // a missing segment always implies missing blocks
1241 {
1242 if (r + 1 > current_dim)
1243 current_dim = r + 1;
1244 return;
1245 }
1246
1247 Array<size_t> new_segments;
1248 new_segments.reserve(missing_segs);
1250 new_blocks.reserve(missing_blocks);
1251
1252 try
1253 {
1254 for (size_t seg_idx = first_seg; seg_idx <= last_seg; ++seg_idx)
1255 {
1256 if (dir[seg_idx] == nullptr)
1257 {
1258 allocate_segment(dir[seg_idx]);
1259 new_segments.append(seg_idx);
1260 }
1261
1262 size_t block_idx = (seg_idx == first_seg) ? first_block : 0;
1263 const size_t final_block = (seg_idx == last_seg) ? last_block : seg_size - 1;
1264
1265 while (block_idx <= final_block)
1266 {
1267 if (dir[seg_idx][block_idx] == nullptr)
1268 {
1269 allocate_block(dir[seg_idx][block_idx]);
1270 new_blocks.append(std::make_pair(seg_idx, block_idx));
1271 }
1272
1273 ++block_idx;
1274 }
1275 } // end for (...)
1276 }
1277 catch (...)
1278 {
1279 for (size_t k = new_blocks.size(); k > 0; --k)
1280 {
1281 const auto [seg_idx, block_idx] = new_blocks(k - 1);
1282 if (dir[seg_idx] != nullptr and dir[seg_idx][block_idx] != nullptr)
1283 release_block(dir[seg_idx][block_idx]);
1284 }
1285
1286 for (size_t k = new_segments.size(); k > 0; --k)
1287 if (const size_t seg_idx = new_segments(k - 1); dir[seg_idx] != nullptr)
1288 release_segment(dir[seg_idx]);
1289
1290 throw;
1291 }
1292
1293 if (r + 1 > current_dim)
1294 current_dim = r + 1;
1295 }
1296
1314 void reserve(const size_t dim)
1315 {
1316 if (dim > 0)
1317 reserve(0, dim - 1);
1318 }
1319
1320 void cut_ne(const size_t new_dim = 0)
1321 {
1322 if (new_dim == 0)
1323 {
1325 current_dim = 0;
1326 return;
1327 }
1328
1329 if (dir == nullptr) // never written, or moved from: nothing to release
1330 {
1331 current_dim = std::min(current_dim, new_dim);
1332 return;
1333 }
1334
1335 const size_t old_dim = current_dim; // old dimension
1336
1337 // The entries of the last kept block in [new_dim, old_dim) leave the
1338 // array: reset them to the default value, so that growing it again shows
1339 // unwritten entries instead of stale ones. Entries past old_dim already
1340 // hold the default value, so a pop() costs O(1), not O(block_size).
1341 // This is the only step that can throw (the assignment of T), so it goes
1342 // first: if it does, no block has been released and the dimension is the
1343 // old one.
1344 if (new_dim < old_dim)
1345 if (T * last = test(new_dim - 1); last != nullptr)
1346 {
1347 const size_t pos = index_in_block(new_dim - 1);
1348 const bool same_block = (new_dim - 1) / block_size == (old_dim - 1) / block_size;
1349 const size_t end = same_block ? index_in_block(old_dim - 1) + 1 : block_size;
1350 T * block = last - pos;
1351 for (size_t k = pos + 1; k < end; ++k)
1352 block[k] = default_initial_value;
1353 }
1354
1355 // segment and block first indexes
1356 const long idx_first_seg = index_in_dir(old_dim - 1);
1357 const long idx_first_block = index_in_seg(old_dim - 1);
1358
1359 // segment and block last indexes
1360 const long idx_last_seg = index_in_dir(new_dim - 1);
1361 const long idx_last_block = index_in_seg(new_dim - 1);
1362 for (long idx_seg = index_in_dir(old_dim - 1); idx_seg >= idx_last_seg;
1363 --idx_seg) // recorre descendentemente los segmentos
1364 {
1365 if (dir[idx_seg] == nullptr) // ¿hay un segmento?
1366 continue; // no ==> Advance to the next
1367
1368 long idx_block = // First block to be released
1369 idx_seg == idx_first_seg ? idx_first_block : seg_size - 1;
1370
1371 // Libera descendentemente los bloques reservados del segmento
1372 while ((idx_seg > idx_last_seg and idx_block >= 0) or
1373 (idx_seg == idx_last_seg and idx_block > idx_last_block))
1374 {
1375 if (dir[idx_seg][idx_block] != nullptr) // ¿Hay un bloque aquí?
1376 release_block(dir[idx_seg][idx_block]);
1377
1378 --idx_block;
1379 }
1380
1381 if (idx_block < 0)
1382 release_segment(dir[idx_seg]);
1383 }
1384
1385 current_dim = new_dim; // Updates New Dimension
1386 }
1387
1394 void cut(const size_t new_dim = 0)
1395 {
1396 // Only shrinking is allowed; growing must be done via adjust().
1397 // If new_dim equals current_dim, this is a no-op and we return early.
1398 ah_length_error_if(new_dim > current_dim) << "new dimension greater than current dimension";
1399
1400 if (new_dim == current_dim)
1401 return;
1402
1403 cut_ne(new_dim);
1404 }
1405
1417 void adjust(const size_t dim)
1418 {
1419 if (dim > current_dim)
1420 reserve(dim);
1421 else
1422 cut(dim);
1423 }
1424
1427 void empty() noexcept
1428 {
1429 cut(0);
1430 }
1431
1437 void clear() noexcept
1438 {
1439 empty();
1440 }
1441
1454 [[nodiscard]] const T &read(const size_t i) const
1455 {
1457 << "DynArray::read(" << i << "): index out of range; size() is " << current_dim;
1458
1459 if (const T *entry = test(i); entry != nullptr)
1460 return *entry;
1461
1462 return default_initial_value;
1463 }
1464
1472 [[nodiscard]] const T &operator [] (const size_t i) const
1473 {
1474 return read(i);
1475 }
1476
1490 Proxy operator [] (const size_t i)
1491 {
1492 // as touch() does: the limit before the directory
1493 if (i >= max_dim)
1494 resize_dir(i);
1495 else
1496 ensure_dir();
1497
1498 return Proxy(*this, i);
1499 }
1500
1514 T &nth(const size_t n)
1515 {
1517 << "DynArray::nth(" << n << "): index out of range; size() is " << current_dim;
1518 return touch(n);
1519 }
1520
1528 const T &nth(const size_t n) const
1529 {
1530 return read(n);
1531 }
1532
1538 T &nth_ne(const size_t n)
1539 {
1540 assert(n < current_dim);
1541 return touch(n);
1542 }
1543
1550 const T &nth_ne(const size_t n) const noexcept
1551 {
1552 assert(n < current_dim);
1553 if (const T *entry = test(n); entry != nullptr)
1554 return *entry;
1555
1556 return default_initial_value;
1557 }
1558
1559private:
1560 // Index of the first entry satisfying `operation`, found with the
1561 // constant traversal, which allocates nothing; size() if none.
1562 template <class Operation>
1563 size_t index_of_first(Operation &operation) const
1564 {
1565 size_t i = 0;
1566 auto count_until_match = [&i, &operation](const T &item)
1567 {
1568 if (std::invoke(operation, item))
1569 return false;
1570 ++i;
1571 return true;
1572 };
1573 const_traverse(count_until_match);
1574 return i;
1575 }
1576
1577public:
1594 template <class Operation>
1596 [[nodiscard]] T *find_ptr(Operation &operation)
1597 {
1599 {
1600 const size_t i = index_of_first(operation);
1601 return i < current_dim ? &touch(i) : nullptr;
1602 }
1603 else
1604 return LocateFunctions<DynArray<T>, T>::find_ptr(operation);
1605 }
1606
1613 template <class Operation>
1615 [[nodiscard]] const T *find_ptr(Operation &operation) const
1616 {
1617 const size_t i = index_of_first(operation);
1618 return i < current_dim ? &read(i) : nullptr;
1619 }
1620
1622 template <class Operation>
1624 [[nodiscard]] T *find_ptr(Operation &&operation)
1625 {
1626 return find_ptr<Operation>(operation);
1627 }
1628
1630 template <class Operation>
1632 [[nodiscard]] const T *find_ptr(Operation &&operation) const
1633 {
1634 return find_ptr<Operation>(operation);
1635 }
1636
1640 {
1641 return touch(this->size());
1642 }
1643
1646 T &append(const T &data)
1647 {
1648 T &ref = this->append();
1649 ref = data;
1650 return ref;
1651 }
1652
1655 T &append(T &&data)
1656 {
1657 T &ref = this->append();
1658 ref = std::move(data);
1659 return ref;
1660 }
1661
1671 T &insert(const T &item)
1672 {
1673 T tmp = item; // open_gap() shifts the entry that item may be
1674 this->append();
1675 open_gap(*this, current_dim);
1676 T &ret = touch(0); // the block of entry 0 may not exist in a lazy array
1677 ret = std::move(tmp);
1678 return ret;
1679 }
1680
1690 T &insert(T &&item)
1691 {
1692 this->append(); // allocate before item is moved from
1693 T tmp = std::move(item); // open_gap() shifts the entry that item may be
1694 open_gap(*this, current_dim);
1695 T &ret = touch(0); // the block of entry 0 may not exist in a lazy array
1696 ret = std::move(tmp);
1697 return ret;
1698 }
1699
1701 void push(const T &data)
1702 {
1703 this->append(data);
1704 }
1705
1707 T &push(T &&data)
1708 {
1709 return this->append(std::forward<T>(data));
1710 }
1711
1713 void put(const T &data)
1714 {
1715 this->append(data);
1716 }
1717
1719 T &put(T &&data)
1720 {
1721 return this->append(std::forward<T>(data));
1722 }
1723
1729 void remove(T &item)
1730 {
1731 ensure_not_empty("DynArray::remove(): empty array");
1732 std::swap(item, this->touch(this->size() - 1)); // the last entry may be unwritten
1733 this->cut_ne(this->size() - 1);
1734 }
1735
1737 void erase(T &item)
1738 {
1739 remove(item);
1740 }
1741
1743 bool is_empty() const noexcept
1744 {
1745 return this->size() == 0;
1746 }
1747
1750 requires std::copy_constructible<T>
1751 {
1752 Array<T> ret;
1753 ret.reserve(this->size());
1754 for (size_t i = 0; i < this->size(); ++i)
1755 ret.append(this->read(i)); // unwritten entries read as the default value
1756 return ret;
1757 }
1758
1761 {
1762 if (current_dim < 2)
1763 return *this;
1764
1765 for (size_t i = 0, j = current_dim - 1; i < j; ++i, --j)
1766 std::swap(touch(i), touch(j));
1767
1768 return *this;
1769 }
1770
1773 {
1774 ensure_not_empty("DynArray::pop(): empty array");
1775 const size_t last = this->size() - 1;
1776 T *entry = test(last); // an unwritten last entry pops the default value
1777 T ret_val = entry != nullptr ? std::move(*entry) : default_initial_value;
1778 cut(last);
1779
1780 return ret_val;
1781 }
1782
1785 {
1786 ensure_not_empty("DynArray::top(): empty array");
1787 return touch(size() - 1); // allocates, as a[i] does
1788 }
1789
1792 const T &top() const
1793 {
1794 ensure_not_empty("DynArray::top(): empty array");
1795 return read(size() - 1);
1796 }
1797
1801 {
1802 ensure_not_empty("DynArray::get_first(): empty array");
1803 return touch(0); // allocates, as a[i] does
1804 }
1805
1808 const T &get_first() const
1809 {
1810 ensure_not_empty("DynArray::get_first(): empty array");
1811 return read(0);
1812 }
1813
1817 {
1818 ensure_not_empty("DynArray::get_last(): empty array");
1819 return touch(size() - 1); // allocates, as a[i] does
1820 }
1821
1824 const T &get_last() const
1825 {
1826 ensure_not_empty("DynArray::get_last(): empty array");
1827 return read(size() - 1);
1828 }
1829
1852 {
1853 protected:
1855 long curr_idx = 0;
1856 bool modifiable = false;
1857
1858 public:
1860
1867
1869 Iterator() noexcept = default;
1870
1878 Iterator(DynArray &array) noexcept : array_ptr(&array), modifiable(true)
1879 {
1880 // empty
1881 }
1882
1883 protected:
1893 Iterator(const DynArray &array) noexcept : array_ptr(const_cast<DynArray *>(&array))
1894 {
1895 // empty
1896 }
1897 friend class Aleph::Const_Iterator<Iterator>;
1898
1899 public:
1900
1907 [[nodiscard]] bool has_curr() const noexcept
1908 {
1909 return array_ptr != nullptr and curr_idx >= 0 and
1910 static_cast<size_t>(curr_idx) < array_ptr->size();
1911 }
1912
1918 [[nodiscard]] bool is_last() const noexcept
1919 {
1920 const long n = array_ptr == nullptr ? 0 : static_cast<long>(array_ptr->size());
1921 return has_curr() and curr_idx == n - 1;
1922 }
1923
1936 [[nodiscard]] T &get_curr_ne() const
1937 {
1938 const auto i = static_cast<size_t>(curr_idx);
1939 if (T *entry = array_ptr->test(i); entry != nullptr)
1940 return *entry;
1941
1942 if (modifiable)
1943 return array_ptr->touch(i); // a modifiable reference: allocate, as a[i] does
1944
1946 }
1947
1956 [[nodiscard]] T &get_curr() const
1957 {
1958 ah_underflow_error_if(curr_idx < 0) << "not current item in iterator";
1959 ah_overflow_error_if(array_ptr == nullptr or
1960 static_cast<size_t>(curr_idx) >= array_ptr->size())
1961 << "not current item in iterator";
1962 return get_curr_ne();
1963 }
1964
1966 [[nodiscard]] long get_pos() const noexcept
1967 {
1968 return curr_idx;
1969 }
1970
1973 void next_ne() noexcept
1974 {
1975 ++curr_idx;
1976 }
1977
1983 void next()
1984 {
1985 ah_overflow_error_if(array_ptr == nullptr or
1986 (curr_idx >= 0 and
1987 static_cast<size_t>(curr_idx) >= array_ptr->size()))
1988 << "not current item in iterator";
1989 next_ne();
1990 }
1991
1994 void prev_ne() noexcept
1995 {
1996 --curr_idx;
1997 }
1998
2001 void prev()
2002 {
2003 ah_underflow_error_if(curr_idx == -1) << "not current item in iterator";
2004 prev_ne();
2005 }
2006
2008 void reset_last() noexcept
2009 {
2010 curr_idx = array_ptr == nullptr ? -1 : static_cast<long>(array_ptr->size()) - 1;
2011 }
2012
2014 void end() noexcept
2015 {
2016 curr_idx = array_ptr == nullptr ? 0 : static_cast<long>(array_ptr->size());
2017 }
2018
2020 void reset_first() noexcept
2021 {
2022 curr_idx = 0;
2023 }
2024
2026 void set_pos(const long pos) noexcept
2027 {
2028 curr_idx = pos;
2029 }
2030 };
2031
2034
2038 [[nodiscard]] Iterator get_it()
2039 {
2040 return Iterator(*this);
2041 }
2042
2046 [[nodiscard]] Const_Iterator get_it() const
2047 {
2048 return Const_Iterator(*this);
2049 }
2050
2056 [[nodiscard]] Iterator get_it(const size_t pos)
2057 {
2058 ah_out_of_range_error_if(pos >= size()) << "DynArray::get_it(pos): pos >= size()";
2059 Iterator it(*this);
2060 it.set_pos(static_cast<long>(pos));
2061 return it;
2062 }
2063
2069 [[nodiscard]] Const_Iterator get_it(const size_t pos) const
2070 {
2071 ah_out_of_range_error_if(pos >= size()) << "DynArray::get_it(pos): pos >= size()";
2072 Const_Iterator it(*this);
2073 it.set_pos(static_cast<long>(pos));
2074 return it;
2075 }
2076
2077private:
2078 // Constant traversal, block by block. It never allocates memory: the
2079 // entries of a block that does not exist are handed to `operation` as
2080 // the default value, whose address is stable while the array lives.
2081 template <class Operation>
2082 bool const_traverse(Operation &operation) const
2084 {
2085 size_t i = 0;
2086 while (i < current_dim)
2087 {
2088 const size_t count = std::min(block_size - index_in_block(i), current_dim - i);
2089 if (const T *entry = test(i); entry != nullptr)
2090 {
2091 for (size_t k = 0; k < count; ++k)
2092 if (not static_cast<bool>(operation(entry[k])))
2093 return false;
2094 }
2095 else
2096 for (size_t k = 0; k < count; ++k)
2097 if (not static_cast<bool>(operation(static_cast<const T &>(default_initial_value))))
2098 return false;
2099 i += count;
2100 }
2101 return true;
2102 }
2103
2104 // Modifiable traversal, block by block. `operation` receives modifiable
2105 // references, so the block of an entry never written is allocated before
2106 // it is visited, as a[i] does: whatever `operation` writes stays in the
2107 // array.
2108 template <class Operation>
2109 bool mutable_traverse(Operation &operation)
2110 {
2111 size_t i = 0;
2112 while (i < current_dim)
2113 {
2114 const size_t count = std::min(block_size - index_in_block(i), current_dim - i);
2115 T *entry = &touch(i); // the count entries from i lie in the same block
2116 for (size_t k = 0; k < count; ++k)
2117 if (not static_cast<bool>(operation(entry[k])))
2118 return false;
2119 i += count;
2120 }
2121 return true;
2122 }
2123
2124public:
2141 template <class Operation>
2143 bool traverse(Operation &operation) const
2145 {
2146 return const_traverse(operation);
2147 }
2148
2161 template <class Operation>
2163 bool traverse(Operation &operation)
2164 {
2165 return mutable_traverse(operation);
2166 }
2167
2169 template <class Operation>
2171 bool traverse(Operation &&operation) const
2173 {
2174 return traverse<Operation>(operation);
2175 }
2176
2178 template <class Operation>
2180 bool traverse(Operation &&operation)
2181 {
2182 return traverse<Operation>(operation);
2183 }
2184
2185 // DynArray declares each(pos, slice, op) to allocate only the visited
2186 // entries; that hides every FunctionalMethods::each, so the constant
2187 // ones are forwarded here.
2188
2193 template <class Operation>
2195 void each(Operation &operation) const
2196 {
2197 this->for_each(operation);
2198 }
2199
2201 template <class Operation>
2203 void each(Operation &&operation) const
2204 {
2205 this->for_each(operation);
2206 }
2207
2216 template <class Operation>
2218 void each(const size_t pos, const size_t slice, Operation &operation) const
2219 {
2220 FunctionalMethods<DynArray<T>, T>::each(pos, slice, operation);
2221 }
2222
2224 template <class Operation>
2226 void each(const size_t pos, const size_t slice, Operation &&operation) const
2227 {
2228 FunctionalMethods<DynArray<T>, T>::each(pos, slice, operation);
2229 }
2230
2245 template <class Operation>
2247 void each(const size_t pos, const size_t slice, Operation &operation)
2248 {
2249 if (slice == 0)
2250 return;
2251 for (size_t i = pos; i < current_dim; i += slice)
2252 {
2253 std::invoke(operation, touch(i));
2254 if (current_dim - i <= slice) // the next position is past the end
2255 break;
2256 }
2257 }
2258
2260 template <class Operation>
2262 void each(const size_t pos, const size_t slice, Operation &&operation)
2263 {
2264 each<Operation>(pos, slice, operation);
2265 }
2266};
2267
2268template <typename T>
2269const size_t DynArray<T>::Default_Pow_Dir = 6; /* 64 */
2270
2271template <typename T>
2272const size_t DynArray<T>::Default_Pow_Seg = 8; /* 256 */
2273
2274template <typename T>
2275const size_t DynArray<T>::Default_Pow_Block = 12; /* 4096 */
2276
2277template <typename T>
2278const size_t DynArray<T>::Max_Bits_Allowed = 8 * sizeof(size_t);
2279
2280template <typename T>
2281const unsigned long long DynArray<T>::Max_Dim_Allowed = 256 * 1024 * 1024 * 1024ull; // 256 Gb
2282
2283template <typename T>
2284const size_t DynArray<T>::Max_Pow_Block = (Max_Bits_Allowed - Default_Pow_Dir - Default_Pow_Seg - 1);
2285} // end namespace Aleph
2286
2287#endif /* TPL_DYNARRAY_H */
Variadic constructor macros for containers.
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
Definition ah-errors.H:846
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_bad_alloc_unless(C)
Throws std::bad_alloc if condition does NOT hold.
Definition ah-errors.H:587
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
STL-compatible iterator adapters for Aleph containers.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Iterator traits and STL-compatible iterator wrappers.
Core header for the Aleph-w library.
Utility functions for array manipulation.
size_t size_t int32_t value
Definition ca-c-api.h:116
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
void reserve(size_t cap)
Reserves cap cells into the array.
Definition tpl_array.H:402
Read-only view of an Aleph iterator: the iterator of a constant container.
auto set_pos(const long pos) noexcept(noexcept(std::declval< I & >().set_pos(pos))) -> decltype(std::declval< I & >().set_pos(pos))
Position the view on the item at ordinal position pos.
Iterator on the items of array.
void reset_last() noexcept
Reset the iterator to the last item.
long get_pos() const noexcept
Return the ordinal position of current item.
void set_pos(const long pos) noexcept
Put the iterator on position pos, without checking it.
void reset_first() noexcept
Reset the iterator to the first item.
void prev()
Move the current a position backward.
bool modifiable
built on a modifiable array: reading an unwritten entry allocates it
bool is_last() const noexcept
Check whether the current item is the last item.
void next()
Advance one position, from the last item to the end if needed.
void end() noexcept
Put the iterator in the end state.
T & get_curr_ne() const
Return the current item without checking the position.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
bool has_curr() const noexcept
Check whether the iterator refers to an item.
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
T & get_curr() const
Return the current item.
void prev_ne() noexcept
Move the iterator one position backward guaranteeing no exception.
Proxy(DynArray< T > &_array, const size_t i) noexcept
Proxy & operator=(const T &data)
DynArray(const DynArray< T > &array)
Copy constructor: build an exact copy of array.
void release_block(T *&block) noexcept
void each(Operation &operation) const
Alias of for_each(); see FunctionalMethods::for_each().
const T & nth_ne(const size_t n) const noexcept
Return entry n of a constant array without checking n.
void set_default_initial_value(const T &value) noexcept(std::is_nothrow_copy_assignable_v< T >)
Set the default value.
void ensure_dir()
Allocate the directory if the array has none.
size_t index_of_first(Operation &operation) const
void allocate_block(T *&block, T *src_block)
T * test(const size_t i) const noexcept
Test if the i-th entry es writable,.
size_t get_block_size() const noexcept
Return the block size.
T & append(T &&data)
Move data to the end of array, increase the dimension and return a modifiable reference to the copied...
void swap(DynArray< T > &array) noexcept(std::is_nothrow_swappable_v< T >)
Swap in constant time array with this
size_t index_in_dir(const size_t i) const noexcept
size_t index_in_seg(const size_t &i) const noexcept
T & top()
Return a modifiable reference to the last item of stack.
void push(const T &data)
See append(const T & data).
void each(const size_t pos, const size_t slice, Operation &operation)
Visit, and possibly modify, the entries at positions pos, pos + slice, pos + 2 slice,...
void adjust(const size_t dim)
Set a new dimension.
Iterator get_it()
Iterator on the modifiable array, positioned on entry 0.
void cut(const size_t new_dim=0)
Cut the array to a new dimension; that is, it reduces the dimension of array and frees the remaining ...
const T & operator[](const size_t i) const
Read entry i of a constant array; equivalent to read(i).
DynArray & reverse()
Reverse the order of items in an array.
size_t modulus_from_index_in_dir(const size_t i) const noexcept
static size_t two_raised(const size_t n)
Return 2^n.
const T & access(const size_t i) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
const T & read(const size_t i) const
Read entry i without allocating memory.
Array< T > to_array() const
Copy contents into Aleph::Array (requires copyable elements).
T & put(T &&data)
See append(T && data).
DynArray(const size_t dim)
Construct an array of logical size dim without allocating entries.
const T & get_last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t get_dir_size() const noexcept
Return the directory size.
unsigned long long max_dim
T * find_ptr(Operation &&operation)
See find_ptr(Operation & operation).
void release_dir() noexcept
T Key_Type
The type of element stored in the array.
void each(const size_t pos, const size_t slice, Operation &&operation) const
See each(size_t pos, size_t slice, Operation & operation) const.
T & insert(const T &item)
Insert a copy of item at the beginning of the array.
T & operator()(const size_t i) noexcept
See access(const size_t i).
size_t seg_plus_block_pow
static size_t next2Pow(const size_t number) noexcept
void cut_ne(const size_t new_dim=0)
DynArray(const size_t _pow_dir, const size_t _pow_seg, const size_t _pow_block)
Construct a dynamic array given directory, segment and block sizes.
void reserve(const size_t dim)
Allocate the first dim entries and make size() at least dim.
size_t get_num_blocks() const noexcept
Return the number of blocks consumed by the array.
bool traverse(Operation &&operation)
See traverse(Operation & operation).
Const_Iterator get_it(const size_t pos) const
Iterator on the constant array, positioned on entry pos.
void clear() noexcept
Empties the container.
T * find_ptr(Operation &operation)
Return a pointer to the first entry satisfying a criterion.
void copy_array(const DynArray< T > &src_array)
Copy the items of src_array to this
DynArray(DynArray &&other) noexcept(std::is_nothrow_default_constructible_v< T > and std::is_nothrow_swappable_v< T >)
Move constructor; allocates nothing.
static const size_t Default_Pow_Seg
Default two power for directory size.
void set_default_initial_value(T &&value=T()) noexcept(std::is_nothrow_swappable_v< T >)
See set_default_initial_value(const T & value).
bool traverse(Operation &&operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation) const.
T & touch(const size_t i)
Touch the entry i.
static size_t compute_dim(size_t d, size_t s, size_t b)
Return the number of entries of a d/s/b geometry.
size_t size() const noexcept
Return the current dimension of array.
void release_blocks_and_segment(T **&seg) noexcept
const T & get_first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
void allocate_segment(T **&seg)
T & nth(const size_t n)
Return a modifiable reference to entry n.
bool traverse(Operation &operation)
Traverse a modifiable array; operation receives modifiable references, with the signature bool operat...
T & get_first()
Return a modifiable reference to the first item of array (as if this was a queue)
T & nth_ne(const size_t n)
Return a modifiable reference to entry n without checking n.
static const size_t Max_Pow_Block
T pop()
Remove the last item of array (as if this was a stack)
const T & nth(const size_t n) const
Return entry n of a constant array; equivalent to read(n).
static void compute_sizes(const size_t n, size_t &d, size_t &s, size_t &b)
Given a dimension n, it proposes values for the directory, segment and block sizes.
static const size_t Default_Pow_Dir
The type of element stored in the array.
Const_Iterator get_it() const
Iterator on the constant array, positioned on entry 0.
T * default_initial_value_ptr
void fill_dir_to_null() noexcept
static const size_t Default_Pow_Block
Default two power for segment size.
void each(const size_t pos, const size_t slice, Operation &&operation)
See each(size_t pos, size_t slice, Operation & operation).
size_t max_size() const noexcept
Return the maximum allowed dimension (or the maximum number of elements that could have the array tre...
void resize_dir(const size_t i)
T & push(T &&data)
See append(T && data).
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse a constant array and execute a conditioned operation, which must have the signature:
bool mutable_traverse(Operation &operation)
void release_all_segments_and_blocks() noexcept
bool exist(const size_t i) const
Return true if the i-th entry is accessible.
size_t modulus_by_block_size(const size_t number) const noexcept
size_t divide_by_block_size(const size_t number) const noexcept
DynArray()
Construct an empty array with the default geometry.
T & get_last()
Return a modifiable reference to the last item of array (as if this was a queue)
size_t mask_seg_plus_block
static const unsigned long long Max_Dim_Allowed
Maximum dimension allowed.
size_t get_seg_size() const noexcept
Return the segment size.
const T * find_ptr(Operation &&operation) const
See find_ptr(Operation & operation) const.
void allocate_segment(T **&seg, T **src_seg)
void allocate_block(T *&block)
Allocate a block filled with the default value.
Aleph::Const_Iterator< Iterator > Const_Iterator
Read-only iterator: the iterator of a constant array; it never allocates.
DynArray< T > & operator=(const DynArray< T > &array)
Copy assignment: *this becomes an exact copy of array.
void each(Operation &&operation) const
See each(Operation & operation) const.
void release_segment(T **&seg) noexcept
bool const_traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
T & insert(T &&item)
Move item to the beginning of the array.
Iterator get_it(const size_t pos)
Iterator on the modifiable array, positioned on entry pos.
void ensure_not_empty(const char *context) const
void put(const T &data)
See append(const T & data).
size_t index_in_block(const size_t i) const noexcept
T & append()
Allocate a new entry to the end of array.
static std::tuple< size_t, size_t, size_t > compute_sizes(const size_t n)
Given a dimension n, it proposes values for the directory, segment and block sizes.
static const size_t Max_Bits_Allowed
Default two power for block size.
bool is_empty() const noexcept
Return true if the array is empty.
T & append(const T &data)
Copy data to the end of array, increase the dimension and return a modifiable reference to the copied...
const T * find_ptr(Operation &operation) const
Constant overload of find_ptr(); it never allocates memory.
void fill_seg_to_null(T **seg) noexcept
void empty() noexcept
Empty the array.
const T & top() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void each(const size_t pos, const size_t slice, Operation &operation) const
Visit the entries at positions pos, pos + slice, ... of a constant array; see FunctionalMethods::each...
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
DynArray(const size_t n, const T &value)
Construct an array of n copies of value; every entry is allocated.
void erase(T &item)
See remove(T & item).
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:2945
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
iterator end() noexcept
Return an STL-compatible end iterator.
std::invoke(f, args...) is valid and usable as a condition.
std::invoke(f, args...) is valid.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4141
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_ceil_function > > ceil(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4134
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
void open_gap(Tarray &ptr, size_t n, size_t pos=0, size_t num_entries=1)
Open a gap in an array by shifting elements right.
Definition array_utils.H:96
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Generic list of items stored in a container.
Definition ah-dry.H:2896
void test()
Definition test-comb.C:40
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.
DynList< int > l