260 return static_cast<size_t>(1) << n;
319 return std::make_tuple(d, s, b);
367 assert(
dir !=
nullptr);
369 for (
size_t i = 0; i <
dir_size; ++i)
375 assert(seg !=
nullptr);
377 for (
size_t i = 0; i <
seg_size; ++i)
412 <<
"DynArray: index " << i <<
" is not less than the maximum dimension allowed "
416 size_t new_pow_dir =
pow_dir + 1;
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 **)));
436 assert(seg ==
nullptr);
459 assert(block ==
nullptr);
466 block = whole.release();
472 assert(seg !=
nullptr);
481 assert(block !=
nullptr);
490 assert(seg !=
nullptr);
492 for (
size_t i = 0; i <
seg_size; ++i)
493 if (seg[i] !=
nullptr)
507 for (
size_t i = 0; i <
dir_size; ++i)
508 if (
dir[i] !=
nullptr)
527 static size_t next2Pow(
const size_t number)
noexcept
529 return static_cast<size_t>(
ceil(
log(
static_cast<float>(number)) /
log(2.0)));
550 block[i] = src_block[i];
556 for (
size_t i = 0; i <
seg_size; i++)
557 if (src_seg[i] !=
nullptr)
599 bool seg_was_allocated_in_current_call =
false;
604 seg_was_allocated_in_current_call =
true;
607 if (
block ==
nullptr)
618 if (seg_was_allocated_in_current_call)
633 bool seg_was_allocated_in_current_call =
false;
637 seg_was_allocated_in_current_call =
true;
640 if (
block ==
nullptr)
651 if (seg_was_allocated_in_current_call)
677 bool seg_was_allocated_in_current_call =
false;
681 seg_was_allocated_in_current_call =
true;
684 if (
block ==
nullptr)
695 if (seg_was_allocated_in_current_call)
766 noexcept(std::is_nothrow_copy_assignable_v<T>)
800 DynArray(
const size_t _pow_dir,
const size_t _pow_seg,
const size_t _pow_block)
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");
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");
862 <<
"DynArray: dimension " <<
dim <<
" is greater than the maximum allowed "
891 for (
size_t i = 0; i < n; ++i)
909 if (src_array.
exist(i))
910 (*this)[i] = src_array.
access(i);
930 if (array.
dir !=
nullptr)
933 for (
size_t i = 0; i <
dir_size; ++i)
934 if (array.
dir[i] !=
nullptr)
976 if constexpr (not std::is_nothrow_swappable_v<T>)
978 std::swap(
dir, array.dir);
979 std::swap(
pow_dir, array.pow_dir);
980 std::swap(
pow_seg, array.pow_seg);
984 std::swap(
dir_size, array.dir_size);
985 std::swap(
seg_size, array.seg_size);
987 std::swap(
mask_seg, array.mask_seg);
989 std::swap(
max_dim, array.max_dim);
991 std::swap(
num_segs, array.num_segs);
994 if constexpr (std::is_nothrow_swappable_v<T>)
998 array.default_initial_value_ptr = &array.default_initial_value;
1008 noexcept(std::is_nothrow_default_constructible_v<T>
and std::is_nothrow_swappable_v<T>)
1089 if (
dir[pos_in_dir] ==
nullptr)
1096 if (
dir[pos_in_dir][pos_in_seg] ==
nullptr)
1118 if (
dir[pos_in_dir] ==
nullptr)
1122 if (
dir[pos_in_dir][pos_in_seg] ==
nullptr)
1155 bool new_segment =
false;
1156 if (
dir[pos_in_dir] ==
nullptr)
1163 if (
dir[pos_in_dir][pos_in_seg] ==
nullptr)
1171 if (new_segment &&
dir[pos_in_dir] !=
nullptr)
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)
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)
1232 missing_blocks += hi - lo + 1;
1235 for (
size_t block_idx = lo; block_idx <= hi; ++block_idx)
1236 if (
dir[seg_idx][block_idx] ==
nullptr)
1240 if (missing_blocks == 0)
1248 new_segments.
reserve(missing_segs);
1250 new_blocks.
reserve(missing_blocks);
1254 for (
size_t seg_idx = first_seg; seg_idx <= last_seg; ++seg_idx)
1256 if (
dir[seg_idx] ==
nullptr)
1259 new_segments.
append(seg_idx);
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;
1265 while (block_idx <= final_block)
1267 if (
dir[seg_idx][block_idx] ==
nullptr)
1270 new_blocks.
append(std::make_pair(seg_idx, block_idx));
1279 for (
size_t k = new_blocks.
size();
k > 0; --
k)
1281 const auto [seg_idx, block_idx] = new_blocks(
k - 1);
1282 if (
dir[seg_idx] !=
nullptr and dir[seg_idx][block_idx] !=
nullptr)
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)
1344 if (new_dim < old_dim)
1345 if (
T * last =
test(new_dim - 1); last !=
nullptr)
1350 T * block = last - pos;
1351 for (
size_t k = pos + 1;
k <
end; ++
k)
1357 const long idx_first_block =
index_in_seg(old_dim - 1);
1362 for (
long idx_seg =
index_in_dir(old_dim - 1); idx_seg >= idx_last_seg;
1365 if (
dir[idx_seg] ==
nullptr)
1369 idx_seg == idx_first_seg ? idx_first_block :
seg_size - 1;
1372 while ((idx_seg > idx_last_seg
and idx_block >= 0) or
1373 (idx_seg == idx_last_seg
and idx_block > idx_last_block))
1375 if (
dir[idx_seg][idx_block] !=
nullptr)
1394 void cut(
const size_t new_dim = 0)
1454 [[nodiscard]]
const T &
read(
const size_t i)
const
1457 <<
"DynArray::read(" << i <<
"): index out of range; size() is " <<
current_dim;
1459 if (
const T *entry =
test(i); entry !=
nullptr)
1498 return Proxy(*
this, i);
1517 <<
"DynArray::nth(" << n <<
"): index out of range; size() is " <<
current_dim;
1553 if (
const T *entry =
test(n); entry !=
nullptr)
1562 template <
class Operation>
1566 auto count_until_match = [&i, &operation](
const T &item)
1568 if (std::invoke(operation, item))
1594 template <
class Operation>
1613 template <
class Operation>
1622 template <
class Operation>
1626 return find_ptr<Operation>(operation);
1630 template <
class Operation>
1632 [[nodiscard]]
const T *
find_ptr(Operation &&operation)
const
1634 return find_ptr<Operation>(operation);
1658 ref = std::move(data);
1677 ret = std::move(tmp);
1693 T tmp = std::move(item);
1696 ret = std::move(tmp);
1709 return this->
append(std::forward<T>(data));
1721 return this->
append(std::forward<T>(data));
1732 std::swap(item, this->
touch(this->
size() - 1));
1745 return this->
size() == 0;
1750 requires
std::copy_constructible<
T>
1754 for (
size_t i = 0; i < this->
size(); ++i)
1755 ret.
append(this->read(i));
1765 for (
size_t i = 0, j =
current_dim - 1; i < j; ++i, --j)
1775 const size_t last = this->
size() - 1;
1776 T *entry =
test(last);
1938 const auto i =
static_cast<size_t>(
curr_idx);
1961 <<
"not current item in iterator";
1988 <<
"not current item in iterator";
2060 it.
set_pos(
static_cast<long>(pos));
2073 it.
set_pos(
static_cast<long>(pos));
2081 template <
class Operation>
2089 if (
const T *entry =
test(i); entry !=
nullptr)
2092 if (not
static_cast<bool>(operation(entry[
k])))
2108 template <
class Operation>
2117 if (not
static_cast<bool>(operation(entry[
k])))
2141 template <
class Operation>
2161 template <
class Operation>
2169 template <
class Operation>
2174 return traverse<Operation>(operation);
2178 template <
class Operation>
2182 return traverse<Operation>(operation);
2193 template <
class Operation>
2195 void each(Operation &operation)
const
2201 template <
class Operation>
2203 void each(Operation &&operation)
const
2216 template <
class Operation>
2218 void each(
const size_t pos,
const size_t slice, Operation &operation)
const
2224 template <
class Operation>
2226 void each(
const size_t pos,
const size_t slice, Operation &&operation)
const
2245 template <
class Operation>
2247 void each(
const size_t pos,
const size_t slice, Operation &operation)
2253 std::invoke(operation,
touch(i));
2260 template <
class Operation>
2262 void each(
const size_t pos,
const size_t slice, Operation &&operation)
2264 each<Operation>(pos, slice, operation);
2268template <
typename T>
2271template <
typename T>
2274template <
typename T>
2277template <
typename T>
2280template <
typename T>
2283template <
typename T>
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.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_bad_alloc_unless(C)
Throws std::bad_alloc if condition does NOT hold.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
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.
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
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
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.
Common methods to the Aleph-w ( ) containers.
and
Insert a new element into the container by constructing it from the given args.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Common sequential searching methods on containers.
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)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
__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)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_ceil_function > > ceil(const __gmp_expr< T, U > &expr)
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.
std::decay_t< typename HeadC::Item_Type > T
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.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Generic list of items stored in a container.
Dynamic array container with automatic resizing.
Dynamic doubly linked list implementation.