100#include <type_traits>
115#if defined(ALEPH_ANNOTATE_CONTAINERS)
116# if defined(__SANITIZE_ADDRESS__)
117# define ALEPH_MEMARRAY_ANNOTATIONS 1
118# elif defined(__has_feature)
119# if __has_feature(address_sanitizer)
120# define ALEPH_MEMARRAY_ANNOTATIONS 1
124#ifdef ALEPH_MEMARRAY_ANNOTATIONS
125# include <sanitizer/common_interface_defs.h>
165#ifdef ALEPH_MEMARRAY_ANNOTATIONS
166 bool annotated =
true;
173 std::is_trivially_copyable_v<T>
and std::is_trivially_copy_assignable_v<T>
174 and std::is_trivially_move_assignable_v<T>
and not std::is_volatile_v<T>;
214 void annotate(
const size_t old_n,
const size_t new_n)
const noexcept
216#ifdef ALEPH_MEMARRAY_ANNOTATIONS
217 if (annotated
and ptr !=
nullptr and old_n != new_n)
218 __sanitizer_annotate_contiguous_container(
ptr,
ptr +
dim,
ptr + old_n,
ptr + new_n);
232#ifdef ALEPH_MEMARRAY_ANNOTATIONS
240 while (ret < requested)
243 <<
"MemArray capacity overflow";
252 static_assert(std::is_default_constructible_v<T>,
253 "MemArray<T> requires a default-constructible T (storage is new T[dim])");
258 <<
"MemArray capacity overflow";
275#ifdef ALEPH_MEMARRAY_ANNOTATIONS
280 for (
size_t i = 0; i <
n; ++i)
298 <<
"MemArray::expand(): capacity overflow";
330 <<
"MemArray::grow_and_insert(): capacity overflow";
333#ifdef ALEPH_MEMARRAY_ANNOTATIONS
337 replacement.
putn(
n + 1);
338 replacement.
ptr[pos] = std::forward<U>(item);
342 std::memcpy(
static_cast<void *
>(replacement.
ptr),
ptr, pos *
sizeof(
T));
343 std::memcpy(
static_cast<void *
>(replacement.
ptr + pos + 1),
ptr + pos,
344 (
n - pos) *
sizeof(
T));
348 for (
size_t i = 0; i < pos; ++i)
350 for (
size_t i = pos; i <
n; ++i)
372 if constexpr (std::is_scalar_v<T>)
394 const size_t newsz =
dim >> 1;
396 if (newsz <= Min_Dim or n > newsz)
424 [[nodiscard]]
constexpr size_t capacity() const noexcept
451 static_assert(std::is_move_constructible_v<T>,
"T must be move constructible");
452 static_assert(std::is_move_assignable_v<T>,
"T must be move assignable");
470 std::swap(
ptr, a.ptr);
471 std::swap(
dim, a.dim);
474#ifdef ALEPH_MEMARRAY_ANNOTATIONS
475 std::swap(annotated, a.annotated);
488 requires(std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)
495 std::memcpy(
static_cast<void *
>(
ptr), a.ptr,
n *
sizeof(
T));
498 for (
size_t i = 0; i <
n; ++i)
514 requires(std::is_copy_constructible_v<T> && std::is_copy_assignable_v<T>)
573#ifdef ALEPH_MEMARRAY_ANNOTATIONS
589 requires std::is_copy_assignable_v<T>
619 ptr[
n] = std::forward<U>(item);
637 T tmp(std::forward<U>(item));
639 ptr[0] = std::move(tmp);
644 void open_gap(
size_t pos = 0,
size_t num_entries = 1)
656 const size_t old_n =
n;
672 requires std::is_copy_assignable_v<T>
711 T ret = std::move(
ptr[0]);
724 requires std::is_copy_assignable_v<T>
732 return put(std::forward<T>(item));
737 requires std::is_copy_assignable_v<T>
745 return push(std::forward<T>(item));
761 <<
"MemArray::putn(): size overflow";
762 const size_t new_n =
n + more;
771 while (new_dim < new_n)
774 <<
"MemArray::putn(): capacity overflow";
784 requires std::is_copy_assignable_v<T>
786 const size_t old_n =
n;
787 const size_t num_entries = a.
size();
789 for (
size_t i = 0; i < num_entries; ++i)
790 ptr[old_n + i] = a.ptr[i];
814 <<
"MemArray::get(): invalid number of entries";
816 const size_t old_n =
n;
817 T ret = std::move(
ptr[
n - i]);
831 const size_t old_n =
n;
832 T ret = std::move(
ptr[
n - i]);
909 for (
size_t i = 0, j =
n - 1; i < j; ++i, --j)
910 std::swap(
ptr[i],
ptr[j]);
924 const T &
access(
const size_t i)
const noexcept
979 template <
class Operation>
983 assert(
ptr or
n == 0);
984 for (
size_t i = 0; i <
n; i++)
985 if (not
static_cast<bool>(operation(
ptr[i])))
1000 template <
class Operation>
1005 assert(
ptr or
n == 0);
1006 for (
size_t i = 0; i <
n; i++)
1007 if (not
static_cast<bool>(operation(
static_cast<const T &
>(
ptr[i]))))
1014 template <
class Operation>
1019 return traverse<Operation>(operation);
1023 template <
class Operation>
1027 return traverse<Operation>(operation);
Exception handling system with formatted messages for Aleph-w.
#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_overflow_error_if(C)
Throws std::overflow_error if condition holds.
STL-compatible iterator adapters for Aleph containers.
DRY (Don't Repeat Yourself) utilities and macros.
Iterator traits and STL-compatible iterator wrappers.
General utility functions and helpers.
Iterator wrapper for C++ raw arrays and circular buffers.
Utility functions for array manipulation.
Iterator wrapper for C++ raw arrays.
Read-only view of an Aleph iterator: the iterator of a constant container.
Simple, scalable and fast dynamic array.
T & put(const T &item)
Put a copy of item at the end of the sequence.
T & put(T &&item)
Move item to the end of the sequence.
void reserve(const size_t cap)
Reserves cap cells into the array.
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all the elements from index 0 to n - 1 and execute operation on each on them.
const T & top() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
const T & get_first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void open_gap(size_t pos=0, size_t num_entries=1)
bool contract(const size_t first=0)
Test if n is lesser than contract_threshold and eventually contract the array half long and copies it...
size_t size() const noexcept
Return the number of elements.
void putn(const size_t more)
Reserve more additional logical slots in the array.
T get_ne(const size_t i=1) noexcept
T & get_first()
See first.
bool traverse(Operation &&operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
See traverse(Operation & operation).
void clear() noexcept
Alias for empty().
const T & operator()(const size_t i) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
const T * get_ptr() const noexcept
See get_ptr().
T & last()
Return a modifiable reference to the last element.
T & grow_and_insert(const size_t pos, U &&item, const size_t first=0)
Grow a full array and insert one item: relocate_and_insert().
T & append(const T &item)
See put(const T & item).
size_t contract_threshold
Number of items at or below which contract() halves the capacity.
T & push_item(U &&item)
Shared body of both push() overloads.
static constexpr bool copied_as_bytes
Whether memcpy() copies an item as its assignments do: T is trivially copyable, its copy and move ass...
void empty_and_release()
Clear the array and reduce allocated storage to Min_Dim.
static constexpr size_t Min_Dim
MemArray(MemArray &&a) noexcept
Transfer storage without allocation.
void allocate()
Allocate memory for the current dimension.
bool expand(const size_t first=0)
Grow a full array, including an empty moved-from array.
T get(const size_t i=1)
Remove i elements from the end.
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...
MemArray(const MemArray &a)
Construct a copy of a, with its capacity.
void disable_annotations() noexcept
Make the whole buffer accessible and stop annotating it.
T & operator[](const size_t i)
Return a reference to the ith element.
T & push(T &&item)
Move item to the beginning of the sequence.
T & append(T &&item)
See put(T && item).
T & push(const T &item)
Push a copy of item at the beginning of the sequence.
MemArray & append(const MemArray &a)
MemArray(size_t _dim=Min_Dim)
Construct an array con capacity equal or greater than _dim.
T & insert(T &&item)
See push(T && item).
constexpr size_t capacity() const noexcept
The type of element of array.
MemArray & operator=(const MemArray &a)
Assign by copy a to this
T & relocate_and_insert(const size_t pos, U &&item, const size_t first)
Grow a full array and insert one item into the new buffer.
T & insert(const T &item)
See push(const T & item).
const T & last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static size_t next_power_of_two(const size_t requested)
void init_dim(size_t d)
Initialize the dimension of the array to d or to the next two power if d is not a two power.
bool is_valid() const noexcept
T & operator()(const size_t i) noexcept
Unlike access(), which also reaches the reserved slots beyond the logical size (a build with AddressS...
void close_gap(size_t pos, size_t num_entries=1)
bool is_empty() const noexcept
Return true is the array is empty.
void reallocate(const size_t cap, const size_t first=0)
Relocate the logical sequence into a new buffer.
T pop()
pop() the most recently pushed item
void empty() noexcept
Empties the container.
const T & operator[](const size_t i) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & access(const size_t i) noexcept
Return a modifiable reference to the ith element.
T remove_last()
See get().
MemArray & reverse()
Reverse the order of items in array.
void annotate(const size_t old_n, const size_t new_n) const noexcept
Tell AddressSanitizer that the live items, previously the prefix [0, old_n), are now [0,...
const size_t & get_dim() const noexcept
Return the current dimension of array.
T * get_ptr() noexcept
Return the current base of array.
const T & first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool traverse(Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
Traverse a constant array; each element is handed out as a constant reference.
void swap(MemArray &a) noexcept
Swap in constant time this with a
bool traverse(Operation &&operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
See traverse(Operation & operation) const.
T & put_item(U &&item)
Shared body of both put() overloads.
const T & get_last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & first()
Return a modifiable reference to the first element.
T remove_first()
Remove the first item. Gap is closed.
MemArray & operator=(MemArray &&a) noexcept
Assign by moving a to this
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.
Main namespace for Aleph-w library functions.
bool is_power_of_2(size_t x)
Taken from http://stackoverflow.com/questions/3638431/determine-if-an-int-is-a-power-of-2-or-not-in-a...
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
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.
void close_gap(T *ptr, size_t n, size_t pos, size_t num_entries=1)
Close a gap in an array by shifting elements left.
Simple iterator on elements of array.
Iterator(MemArray< T > &a) noexcept
Iterator on the modifiable array a.
Iterator(const MemArray< T > &)=delete
A constant array is iterated through Const_Iterator.