Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::MemArray< T > Class Template Reference

Simple, scalable and fast dynamic array. More...

#include <tpl_memArray.H>

Inherited by Aleph::ArrayQueue< int > [protected], Aleph::ArrayQueue< Aleph::DynList< int > > [protected], and Aleph::ArrayQueue< T > [protected].

Collaboration diagram for Aleph::MemArray< T >:
[legend]

Classes

struct  Iterator
 Simple iterator on elements of array. More...
 

Public Types

using Item_Type = T
 
using Const_Iterator = Aleph::Const_Iterator< Iterator >
 Read-only iterator: the iterator of a constant container.
 

Public Member Functions

T * get_ptr () noexcept
 Return the current base of array.
 
const T * get_ptr () const noexcept
 See get_ptr().
 
const size_t & get_dim () const noexcept
 Return the current dimension of array.
 
constexpr size_t capacity () const noexcept
 The type of element of array.
 
size_t size () const noexcept
 Return the number of elements.
 
bool is_empty () const noexcept
 Return true is the array is empty.
 
 MemArray (size_t _dim=Min_Dim)
 Construct an array con capacity equal or greater than _dim.
 
 ~MemArray ()
 
void swap (MemArray &a) noexcept
 Swap in constant time this with a
 
 MemArray (const MemArray &a)
 Construct a copy of a, with its capacity.
 
 MemArray (MemArray &&a) noexcept
 Transfer storage without allocation.
 
MemArray & operator= (const MemArray &a)
 Assign by copy a to this
 
MemArray & operator= (MemArray &&a) noexcept
 Assign by moving a to this
 
void empty () noexcept
 Empties the container.
 
void clear () noexcept
 Alias for empty().
 
void empty_and_release ()
 Clear the array and reduce allocated storage to Min_Dim.
 
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.
 
T & push (const T &item)
 Push a copy of item at the beginning of the sequence.
 
T & push (T &&item)
 Move item to the beginning of the sequence.
 
T & top ()
 
const T & top () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T remove_first ()
 Remove the first item. Gap is closed.
 
T pop ()
 pop() the most recently pushed item
 
T & append (const T &item)
 See put(const T & item).
 
T & append (T &&item)
 See put(T && item).
 
T & insert (const T &item)
 See push(const T & item).
 
T & insert (T &&item)
 See push(T && item).
 
void putn (const size_t more)
 Reserve more additional logical slots in the array.
 
MemArray & append (const MemArray &a)
 
void reserve (const size_t cap)
 Reserves cap cells into the array.
 
T get (const size_t i=1)
 Remove i elements from the end.
 
T get_ne (const size_t i=1) noexcept
 
T remove_last ()
 See get().
 
T & last ()
 Return a modifiable reference to the last element.
 
const T & last () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T & first ()
 Return a modifiable reference to the first element.
 
const T & first () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T & get_first ()
 See first.
 
const T & get_first () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T & get_last ()
 See last.
 
const T & get_last () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
MemArray & reverse ()
 Reverse the order of items in array.
 
T & access (const size_t i) noexcept
 Return a modifiable reference to the ith element.
 
const T & access (const size_t i) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T & operator[] (const size_t i)
 Return a reference to the ith element.
 
const T & operator[] (const size_t i) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
T & operator() (const size_t i) noexcept
 Unlike access(), which also reaches the reserved slots beyond the logical size (a build with AddressSanitizer and ALEPH_ANNOTATE_CONTAINERS reports that), debug builds assert that i is a live item (i < n).
 
const T & operator() (const size_t i) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, T &>
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.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
 Traverse a constant array; each element is handed out as a constant reference.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &&operation) const noexcept(Aleph::NothrowPredicateWith< Operation &, const T & >)
 See traverse(Operation & operation) const.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, T &>
bool traverse (Operation &&operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
 See traverse(Operation & operation).
 
bool is_valid () const noexcept
 

Public Attributes

size_t contract_threshold
 Number of items at or below which contract() halves the capacity.
 

Static Public Attributes

static constexpr size_t Min_Dim = 4
 

Protected Member Functions

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, new_n): accesses to the slots in [new_n, dim) are then reported as container-overflow.
 
void disable_annotations () noexcept
 Make the whole buffer accessible and stop annotating it.
 
void allocate ()
 Allocate memory for the current dimension.
 
void reallocate (const size_t cap, const size_t first=0)
 Relocate the logical sequence into a new buffer.
 
bool expand (const size_t first=0)
 Grow a full array, including an empty moved-from array.
 
template<class U >
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.
 
template<class U >
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().
 
bool contract (const size_t first=0)
 Test if n is lesser than contract_threshold and eventually contract the array half long and copies its content.
 
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.
 

Static Protected Member Functions

static size_t next_power_of_two (const size_t requested)
 

Protected Attributes

T * ptr = nullptr
 
size_t dim = Min_Dim
 
size_t n = 0
 

Static Protected Attributes

static constexpr bool copied_as_bytes
 Whether memcpy() copies an item as its assignments do: T is trivially copyable, its copy and move assignments are trivial, and it is not volatile, whose pointers memcpy() does not take.
 

Private Member Functions

template<class U >
T & put_item (U &&item)
 Shared body of both put() overloads.
 
template<class U >
T & push_item (U &&item)
 Shared body of both push() overloads.
 
void open_gap (size_t pos=0, size_t num_entries=1)
 
void close_gap (size_t pos, size_t num_entries=1)
 

Detailed Description

template<typename T>
class Aleph::MemArray< T >

Simple, scalable and fast dynamic array.

MemArray implements a totally sequential dynamic array. That is, conditioned to memory availability, the array is stored in a contiguous chunk of memory. So, the access is direct and with exactly the same cost of accessing a normal array.

The array allows inserting and removing elements. These operations are conceived by the ends. The number of stored elements is called n.

The allocation technique obeys to "buddy system"; that is, the exact dimension of array always is an exact two power.

When the array is full, a new chunk twice as long is allocated and the current entries are moved (by move semantic if available).

When the number of entries descends to a contract_threshold the array is half long contracted.

Note
This class is not really conceived for managing a set. Instead, it is conceived as implementation support of single sequentially based set as stacks and queues.

Definition at line 156 of file tpl_memArray.H.

Member Typedef Documentation

◆ Const_Iterator

template<typename T >
using Aleph::MemArray< T >::Const_Iterator = Aleph::Const_Iterator<Iterator>

Read-only iterator: the iterator of a constant container.

Definition at line 1054 of file tpl_memArray.H.

◆ Item_Type

template<typename T >
using Aleph::MemArray< T >::Item_Type = T

Definition at line 421 of file tpl_memArray.H.

Constructor & Destructor Documentation

◆ MemArray() [1/3]

template<typename T >
Aleph::MemArray< T >::MemArray ( size_t  _dim = Min_Dim)
inline

Construct an array con capacity equal or greater than _dim.

Parameters
[in]_dimproposed dimension of array which could be adjusted to the next two power greater than _dim
Exceptions
bad_allocif there is not enough memory.
overflow_errorif that power of two, or its bytes, do not fit in a size_t.

Definition at line 449 of file tpl_memArray.H.

References Aleph::MemArray< T >::allocate(), and Aleph::MemArray< T >::init_dim().

◆ ~MemArray()

◆ MemArray() [2/3]

template<typename T >
Aleph::MemArray< T >::MemArray ( const MemArray< T > &  a)
inline

Construct a copy of a, with its capacity.

Parameters
[in]athe array to copy.
Exceptions
std::bad_allocor an exception from T.
Note
O(n). If memcpy() copies T as its assignments do (copied_as_bytes), the items are copied with it from 5 of them on: item by item, the compilers do not see that the two arrays do not overlap.

Definition at line 487 of file tpl_memArray.H.

References Aleph::MemArray< T >::copied_as_bytes, Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::putn().

◆ MemArray() [3/3]

template<typename T >
Aleph::MemArray< T >::MemArray ( MemArray< T > &&  a)
inlinenoexcept

Transfer storage without allocation.

Parameters
[in,out]aSource; left empty with zero capacity and reusable.
Note
O(1). The next insertion into a allocates storage lazily.
Exceptions
Nothing.

Definition at line 507 of file tpl_memArray.H.

References Aleph::MemArray< T >::swap().

Member Function Documentation

◆ access() [1/2]

template<typename T >
const T & Aleph::MemArray< T >::access ( const size_t  i) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 924 of file tpl_memArray.H.

References Aleph::MemArray< T >::ptr.

◆ access() [2/2]

◆ allocate()

template<typename T >
void Aleph::MemArray< T >::allocate ( )
inlineprotected

◆ annotate()

template<typename T >
void Aleph::MemArray< T >::annotate ( const size_t  old_n,
const size_t  new_n 
) const
inlineprotectednoexcept

Tell AddressSanitizer that the live items, previously the prefix [0, old_n), are now [0, new_n): accesses to the slots in [new_n, dim) are then reported as container-overflow.

The whole buffer must be accessible again before it is deleted. No-op unless the annotations are in effect (ALEPH_ANNOTATE_CONTAINERS with AddressSanitizer).

Parameters
[in]old_nprevious end of the accessible prefix
[in]new_nnew end of the accessible prefix

Definition at line 214 of file tpl_memArray.H.

References Aleph::and, Aleph::MemArray< T >::dim, and Aleph::MemArray< T >::ptr.

Referenced by Aleph::MemArray< T >::~MemArray(), Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::close_gap(), Aleph::MemArray< T >::disable_annotations(), Aleph::MemArray< T >::empty(), Aleph::MemArray< T >::get(), Aleph::MemArray< T >::get_ne(), Aleph::MemArray< T >::put_item(), and Aleph::MemArray< T >::putn().

◆ append() [1/3]

template<typename T >
MemArray & Aleph::MemArray< T >::append ( const MemArray< T > &  a)
inline

◆ append() [2/3]

template<typename T >
T & Aleph::MemArray< T >::append ( const T &  item)
inline

See put(const T & item).

Definition at line 723 of file tpl_memArray.H.

References Aleph::MemArray< T >::put().

Referenced by MemArray_with_30_items::MemArray_with_30_items(), TEST(), and TEST().

◆ append() [3/3]

template<typename T >
T & Aleph::MemArray< T >::append ( T &&  item)
inline

See put(T && item).

Definition at line 730 of file tpl_memArray.H.

References Aleph::MemArray< T >::put().

◆ capacity()

◆ clear()

template<typename T >
void Aleph::MemArray< T >::clear ( )
inlinenoexcept

Alias for empty().

Exceptions
none
See also
empty()

Definition at line 554 of file tpl_memArray.H.

References Aleph::MemArray< T >::empty().

Referenced by Aleph::FlatSet< Key, Compare >::clear().

◆ close_gap()

template<typename T >
void Aleph::MemArray< T >::close_gap ( size_t  pos,
size_t  num_entries = 1 
)
inlineprivate

◆ contract()

template<typename T >
bool Aleph::MemArray< T >::contract ( const size_t  first = 0)
inlineprotected

Test if n is lesser than contract_threshold and eventually contract the array half long and copies its content.

contract(first) first testes n with contract_threshold. If it is lesser, then a new array half as long is allocated and the n elements from first are copied.

Parameters
[in]firstindex of first element
Returns
true if the array is reallocated

Definition at line 388 of file tpl_memArray.H.

References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::first(), Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::reallocate().

Referenced by Aleph::MemArray< T >::close_gap(), Aleph::ArrayQueue< T >::get(), Aleph::MemArray< T >::get(), Aleph::MemArray< T >::get_ne(), Aleph::ArrayQueue< T >::getn(), and Aleph::ArrayQueue< T >::remove_last().

◆ disable_annotations()

template<typename T >
void Aleph::MemArray< T >::disable_annotations ( )
inlineprotectednoexcept

Make the whole buffer accessible and stop annotating it.

For users that keep their items in a circular layout (ArrayQueue), where the live items are not a prefix of the buffer.

Definition at line 229 of file tpl_memArray.H.

References Aleph::MemArray< T >::annotate(), Aleph::MemArray< T >::dim, and Aleph::MemArray< T >::n.

Referenced by Aleph::ArrayQueue< T >::ArrayQueue(), Aleph::ArrayQueue< T >::ArrayQueue(), Aleph::ArrayQueue< T >::ArrayQueue(), Aleph::MemArray< T >::empty_and_release(), Aleph::MemArray< T >::reallocate(), and Aleph::MemArray< T >::relocate_and_insert().

◆ empty()

template<typename T >
void Aleph::MemArray< T >::empty ( )
inlinenoexcept

Empties the container.

The array size is set to zero, but the allocated memory is not released.

Exceptions
none
Note
Unlike std::vector::clear(), the items are not destroyed: the storage is an array of T built with new T[dim], so the objects stay alive, with their last values, until they are overwritten or the array is released. Use empty_and_release() or the destructor to release them.

Definition at line 543 of file tpl_memArray.H.

References Aleph::MemArray< T >::annotate(), and Aleph::MemArray< T >::n.

Referenced by Aleph::MemArray< T >::clear(), Aleph::ArrayQueue< T >::empty(), Aleph::FlatMap< Key, T, Compare >::empty(), Aleph::FlatSet< Key, Compare >::empty(), and Aleph::MemArray< T >::empty_and_release().

◆ empty_and_release()

template<typename T >
void Aleph::MemArray< T >::empty_and_release ( )
inline

Clear the array and reduce allocated storage to Min_Dim.

Exceptions
std::bad_allocor an exception from default construction of T.
Note
Allocation failure leaves the original array unchanged. An already small or moved-from array simply becomes empty.

Definition at line 564 of file tpl_memArray.H.

References Aleph::MemArray< T >::dim, Aleph::MemArray< T >::disable_annotations(), Aleph::MemArray< T >::empty(), Aleph::MemArray< T >::Min_Dim, and Aleph::MemArray< T >::swap().

Referenced by Aleph::ArrayQueue< T >::empty_and_release().

◆ expand()

template<typename T >
bool Aleph::MemArray< T >::expand ( const size_t  first = 0)
inlineprotected

Grow a full array, including an empty moved-from array.

Parameters
[in]firstPhysical index of the first logical element.
Returns
true if storage was allocated and the sequence starts at zero.
Exceptions
std::bad_alloc,std::overflow_erroror an exception from T.
Note
O(dim) on growth; otherwise O(1). Zero capacity grows to Min_Dim.

Definition at line 291 of file tpl_memArray.H.

References ah_overflow_error_if, Aleph::and, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::first(), Aleph::is_power_of_2(), Aleph::MemArray< T >::Min_Dim, Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::reallocate().

Referenced by Aleph::ArrayQueue< T >::putn().

◆ first() [1/2]

◆ first() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::first ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 871 of file tpl_memArray.H.

References ah_underflow_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ get()

◆ get_dim()

template<typename T >
const size_t & Aleph::MemArray< T >::get_dim ( ) const
inlinenoexcept

Return the current dimension of array.

Definition at line 199 of file tpl_memArray.H.

References Aleph::MemArray< T >::dim.

◆ get_first() [1/2]

template<typename T >
T & Aleph::MemArray< T >::get_first ( )
inline

See first.

Definition at line 878 of file tpl_memArray.H.

References Aleph::MemArray< T >::first().

◆ get_first() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::get_first ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 885 of file tpl_memArray.H.

References Aleph::MemArray< T >::first().

◆ get_last() [1/2]

template<typename T >
T & Aleph::MemArray< T >::get_last ( )
inline

See last.

Definition at line 891 of file tpl_memArray.H.

References Aleph::MemArray< T >::last().

◆ get_last() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::get_last ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 898 of file tpl_memArray.H.

References Aleph::MemArray< T >::last().

◆ get_ne()

template<typename T >
T Aleph::MemArray< T >::get_ne ( const size_t  i = 1)
inlinenoexcept

◆ get_ptr() [1/2]

template<typename T >
const T * Aleph::MemArray< T >::get_ptr ( ) const
inlinenoexcept

See get_ptr().

Definition at line 193 of file tpl_memArray.H.

References Aleph::MemArray< T >::ptr.

◆ get_ptr() [2/2]

◆ grow_and_insert()

template<typename T >
template<class U >
T & Aleph::MemArray< T >::grow_and_insert ( const size_t  pos,
U &&  item,
const size_t  first = 0 
)
inlineprotected

Grow a full array and insert one item: relocate_and_insert().

A scalar item goes as a copy. The growth takes its item by reference, and an item whose address leaves its function lives in memory: a loop that appends would store it at each step, also when nothing grows.

Parameters
[in]posLogical position of the new item, at most size().
[in]itemValue to insert; it may be an element of this array.
[in]firstPhysical index of the first logical element.
Returns
Reference to the inserted item, at physical index pos.
Exceptions
std::overflow_error,std::bad_allocor an exception from T.

Definition at line 370 of file tpl_memArray.H.

References Aleph::MemArray< T >::first(), and Aleph::MemArray< T >::relocate_and_insert().

Referenced by Aleph::MemArray< T >::push_item(), Aleph::ArrayQueue< T >::put_item(), and Aleph::MemArray< T >::put_item().

◆ init_dim()

template<typename T >
void Aleph::MemArray< T >::init_dim ( size_t  d)
inlineprotected

Initialize the dimension of the array to d or to the next two power if d is not a two power.

If d is 0 then d is set to Min_Dim.

Parameters
[in]dthe proposed dimension of array

Definition at line 409 of file tpl_memArray.H.

References Aleph::MemArray< T >::dim, Aleph::is_power_of_2(), Aleph::MemArray< T >::Min_Dim, and Aleph::MemArray< T >::next_power_of_two().

Referenced by Aleph::MemArray< T >::MemArray().

◆ insert() [1/2]

template<typename T >
T & Aleph::MemArray< T >::insert ( const T &  item)
inline

See push(const T & item).

Definition at line 736 of file tpl_memArray.H.

References Aleph::MemArray< T >::push().

◆ insert() [2/2]

template<typename T >
T & Aleph::MemArray< T >::insert ( T &&  item)
inline

See push(T && item).

Definition at line 743 of file tpl_memArray.H.

References Aleph::MemArray< T >::push().

◆ is_empty()

template<typename T >
bool Aleph::MemArray< T >::is_empty ( ) const
inlinenoexcept

◆ is_valid()

template<typename T >
bool Aleph::MemArray< T >::is_valid ( ) const
inlinenoexcept

Definition at line 1030 of file tpl_memArray.H.

References Aleph::MemArray< T >::ptr.

◆ last() [1/2]

template<typename T >
T & Aleph::MemArray< T >::last ( )
inline

Return a modifiable reference to the last element.

Definition at line 848 of file tpl_memArray.H.

References ah_underflow_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

Referenced by Aleph::MemArray< T >::get_last(), and Aleph::MemArray< T >::get_last().

◆ last() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::last ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 856 of file tpl_memArray.H.

References ah_underflow_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ next_power_of_two()

template<typename T >
static size_t Aleph::MemArray< T >::next_power_of_two ( const size_t  requested)
inlinestaticprotected

◆ open_gap()

template<typename T >
void Aleph::MemArray< T >::open_gap ( size_t  pos = 0,
size_t  num_entries = 1 
)
inlineprivate

◆ operator()() [1/2]

template<typename T >
const T & Aleph::MemArray< T >::operator() ( const size_t  i) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 963 of file tpl_memArray.H.

References Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ operator()() [2/2]

template<typename T >
T & Aleph::MemArray< T >::operator() ( const size_t  i)
inlinenoexcept

Unlike access(), which also reaches the reserved slots beyond the logical size (a build with AddressSanitizer and ALEPH_ANNOTATE_CONTAINERS reports that), debug builds assert that i is a live item (i < n).

See also
access(const size_t i)

Definition at line 954 of file tpl_memArray.H.

References Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ operator=() [1/2]

template<typename T >
MemArray & Aleph::MemArray< T >::operator= ( const MemArray< T > &  a)
inline

Assign by copy a to this

Definition at line 513 of file tpl_memArray.H.

References Aleph::copy(), and Aleph::MemArray< T >::swap().

◆ operator=() [2/2]

template<typename T >
MemArray & Aleph::MemArray< T >::operator= ( MemArray< T > &&  a)
inlinenoexcept

Assign by moving a to this

Definition at line 526 of file tpl_memArray.H.

References Aleph::MemArray< T >::swap().

◆ operator[]() [1/2]

template<typename T >
T & Aleph::MemArray< T >::operator[] ( const size_t  i)
inline

Return a reference to the ith element.

Throws out_of_range if i is out of range

Definition at line 932 of file tpl_memArray.H.

References ah_out_of_range_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ operator[]() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::operator[] ( const size_t  i) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 941 of file tpl_memArray.H.

References ah_out_of_range_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ pop()

template<typename T >
T Aleph::MemArray< T >::pop ( )
inline

pop() the most recently pushed item

Definition at line 717 of file tpl_memArray.H.

References Aleph::MemArray< T >::remove_first().

◆ push() [1/2]

template<typename T >
T & Aleph::MemArray< T >::push ( const T &  item)
inline

Push a copy of item at the beginning of the sequence.

Parameters
[in]itemValue to insert; it may be an element of this array, as in a.push(a(1)), even when the array must grow.
Returns
Reference to the inserted copy, now the first element.
Exceptions
std::bad_alloc,std::overflow_erroror an exception from T.
Note
O(n): every element shifts one position. The array is unchanged if the allocation or the copy throws.

Definition at line 671 of file tpl_memArray.H.

References Aleph::MemArray< T >::push_item().

Referenced by Aleph::MemArray< T >::insert(), Aleph::MemArray< T >::insert(), TEST(), TEST(), TEST(), and TEST().

◆ push() [2/2]

template<typename T >
T & Aleph::MemArray< T >::push ( T &&  item)
inline

Move item to the beginning of the sequence.

Parameters
[in]itemValue to insert; it may be an element of this array, as in a.push(std::move(a(1))), which leaves that element moved from.
Returns
Reference to the inserted item, now the first element.
Exceptions
std::bad_alloc,std::overflow_erroror an exception from T.
Note
O(n): every element shifts one position.

Definition at line 685 of file tpl_memArray.H.

References Aleph::MemArray< T >::push_item().

◆ push_item()

template<typename T >
template<class U >
T & Aleph::MemArray< T >::push_item ( U &&  item)
inlineprivate

◆ put() [1/2]

template<typename T >
T & Aleph::MemArray< T >::put ( const T &  item)
inline

Put a copy of item at the end of the sequence.

Parameters
[in]itemValue to append; it may be an element of this array, as in a.put(a(0)), even when the array must grow.
Returns
Reference to the appended copy.
Exceptions
std::bad_alloc,std::overflow_erroror an exception from T.
Note
Amortized O(1); O(n) when the array is full and grows. The array is unchanged if the allocation or the copy throws.

Definition at line 588 of file tpl_memArray.H.

References Aleph::MemArray< T >::put_item().

Referenced by Aleph::FlatMap< Key, T, Compare >::FlatMap(), Aleph::FlatSet< Key, Compare >::FlatSet(), Aleph::MemArray< T >::append(), Aleph::MemArray< T >::append(), create_memarray(), main(), Aleph::FlatMap< Key, T, Compare >::sort_and_unique(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ put() [2/2]

template<typename T >
T & Aleph::MemArray< T >::put ( T &&  item)
inline

Move item to the end of the sequence.

Parameters
[in]itemValue to append; it may be an element of this array, as in a.put(std::move(a(0))), which leaves that element moved from.
Returns
Reference to the appended item.
Exceptions
std::bad_alloc,std::overflow_erroror an exception from T.
Note
Amortized O(1); O(n) when the array is full and grows. item is untouched if the allocation throws.

Definition at line 603 of file tpl_memArray.H.

References Aleph::MemArray< T >::put_item().

◆ put_item()

template<typename T >
template<class U >
T & Aleph::MemArray< T >::put_item ( U &&  item)
inlineprivate

◆ putn()

template<typename T >
void Aleph::MemArray< T >::putn ( const size_t  more)
inline

Reserve more additional logical slots in the array.

putn(more) only adjusts the logical size and grows the backing storage if necessary; it does not value-initialize the new entries. Callers must assign to the returned positions before reading them.

Parameters
[in]morenumber of cells to reserve
Exceptions
bad_allocif there is no enough memory

Definition at line 758 of file tpl_memArray.H.

References ah_overflow_error_if, Aleph::MemArray< T >::annotate(), Aleph::MemArray< T >::dim, Aleph::MemArray< T >::Min_Dim, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::reallocate().

Referenced by Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::append(), Aleph::FlatSet< Key, Compare >::grow_to(), Aleph::FlatMap< Key, T, Compare >::make_room(), Aleph::FlatSet< Key, Compare >::make_room(), Aleph::MemArray< T >::open_gap(), Aleph::MemArray< T >::reallocate(), Aleph::FlatMap< Key, T, Compare >::relocate(), Aleph::MemArray< T >::relocate_and_insert(), TEST(), and TEST().

◆ reallocate()

template<typename T >
void Aleph::MemArray< T >::reallocate ( const size_t  cap,
const size_t  first = 0 
)
inlineprotected

Relocate the logical sequence into a new buffer.

Parameters
[in]capRequested capacity, at least size().
[in]firstPhysical index of the first logical element.
Exceptions
std::bad_allocor any exception from constructing/assigning T.
Note
O(cap + n) time. A temporary owns the allocation on all paths; throwing moves may leave some source elements moved from.

Definition at line 271 of file tpl_memArray.H.

References Aleph::MemArray< T >::dim, Aleph::MemArray< T >::disable_annotations(), Aleph::MemArray< T >::first(), Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, Aleph::MemArray< T >::putn(), and Aleph::MemArray< T >::swap().

Referenced by Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::putn(), Aleph::ArrayQueue< T >::reserve(), and Aleph::MemArray< T >::reserve().

◆ relocate_and_insert()

template<typename T >
template<class U >
T & Aleph::MemArray< T >::relocate_and_insert ( const size_t  pos,
U &&  item,
const size_t  first 
)
inlineprotected

Grow a full array and insert one item into the new buffer.

The item is assigned before any element leaves the current buffer, so it may be one of those elements: growth never reads freed memory and inserts the value that item had on entry.

Parameters
[in]posLogical position of the new item, at most size().
[in]itemValue to insert; it may be an element of this array.
[in]firstPhysical index of the first logical element.
Returns
Reference to the inserted item, at physical index pos.
Exceptions
std::overflow_error,std::bad_allocor an exception from T.
Note
O(dim). Requires size() == capacity(). The array is unchanged if the allocation or the assignment of item throws; a throwing move may leave some elements moved from, as in reallocate().
It is out of line: copied into put_item(), it made that function too large for the loops that append to inline it.
If memcpy() copies T as its assignments do (copied_as_bytes), the items that are in a row, from the first slot, are moved with it.

Definition at line 326 of file tpl_memArray.H.

References ah_overflow_error_if, Aleph::and, Aleph::MemArray< T >::copied_as_bytes, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::disable_annotations(), Aleph::MemArray< T >::first(), Aleph::MemArray< T >::Min_Dim, Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, Aleph::MemArray< T >::putn(), and Aleph::MemArray< T >::swap().

Referenced by Aleph::MemArray< T >::grow_and_insert().

◆ remove_first()

template<typename T >
T Aleph::MemArray< T >::remove_first ( )
inline

Remove the first item. Gap is closed.

Definition at line 707 of file tpl_memArray.H.

References ah_underflow_error_if, Aleph::MemArray< T >::close_gap(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

Referenced by Aleph::MemArray< T >::pop().

◆ remove_last()

template<typename T >
T Aleph::MemArray< T >::remove_last ( )
inline

See get().

Definition at line 842 of file tpl_memArray.H.

References Aleph::MemArray< T >::get().

◆ reserve()

template<typename T >
void Aleph::MemArray< T >::reserve ( const size_t  cap)
inline

Reserves cap cells into the array.

Parameters
[in]capnew dimension
Exceptions
bad_allocif there is no enough memory

Definition at line 800 of file tpl_memArray.H.

References Aleph::MemArray< T >::dim, Aleph::is_power_of_2(), Aleph::MemArray< T >::next_power_of_two(), and Aleph::MemArray< T >::reallocate().

Referenced by Aleph::FlatMap< Key, T, Compare >::grow_array(), Aleph::FlatSet< Key, Compare >::grow_to(), TEST(), and TEST().

◆ reverse()

template<typename T >
MemArray & Aleph::MemArray< T >::reverse ( )
inline

Reverse the order of items in array.

Definition at line 904 of file tpl_memArray.H.

References Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ size()

◆ swap()

◆ top() [1/2]

template<typename T >
T & Aleph::MemArray< T >::top ( )
inline

◆ top() [2/2]

template<typename T >
const T & Aleph::MemArray< T >::top ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. A reference to const.

Definition at line 699 of file tpl_memArray.H.

References ah_underflow_error_if, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ traverse() [1/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool Aleph::MemArray< T >::traverse ( Operation &&  operation) const
inlinenoexcept

See traverse(Operation & operation) const.

Definition at line 1016 of file tpl_memArray.H.

◆ traverse() [2/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, T &>
bool Aleph::MemArray< T >::traverse ( Operation &&  operation)
inlinenoexcept

See traverse(Operation & operation).

Definition at line 1025 of file tpl_memArray.H.

◆ traverse() [3/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool Aleph::MemArray< T >::traverse ( Operation &  operation) const
inlinenoexcept

Traverse a constant array; each element is handed out as a constant reference.

Parameters
[in]operationto be performed on each element; it must accept const T &.
Returns
true if operation was executed on all elements; false otherwise.
Exceptions
anythingthat operation throws.

Definition at line 1002 of file tpl_memArray.H.

References Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ traverse() [4/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, T &>
bool Aleph::MemArray< T >::traverse ( Operation &  operation)
inlinenoexcept

Traverse all the elements from index 0 to n - 1 and execute operation on each on them.

Parameters
[in]operationto be performed on each element; it receives a modifiable reference.
Returns
true if operation was executed on all elements; false otherwise.
Exceptions
anythingthat operation throws.

Definition at line 981 of file tpl_memArray.H.

References Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

Referenced by TEST().

Member Data Documentation

◆ contract_threshold

template<typename T >
size_t Aleph::MemArray< T >::contract_threshold
mutable

Number of items at or below which contract() halves the capacity.

It is recomputed as a quarter of the capacity whenever the array is reallocated. Changing it only changes when the array contracts: contract() never shrinks the capacity below the number of items, so no item can be lost. ArrayQueue exports it with a using-declaration.

Definition at line 184 of file tpl_memArray.H.

Referenced by Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::contract(), and Aleph::MemArray< T >::swap().

◆ copied_as_bytes

template<typename T >
constexpr bool Aleph::MemArray< T >::copied_as_bytes
staticconstexprprotected
Initial value:
=
std::is_trivially_copyable_v<T> and std::is_trivially_copy_assignable_v<T>
and std::is_trivially_move_assignable_v<T> and not std::is_volatile_v<T>
and
Check uniqueness with explicit hash + equality functors.

Whether memcpy() copies an item as its assignments do: T is trivially copyable, its copy and move assignments are trivial, and it is not volatile, whose pointers memcpy() does not take.

Definition at line 172 of file tpl_memArray.H.

Referenced by Aleph::MemArray< T >::MemArray(), and Aleph::MemArray< T >::relocate_and_insert().

◆ dim

◆ Min_Dim

◆ n

template<typename T >
size_t Aleph::MemArray< T >::n = 0
protected

Definition at line 164 of file tpl_memArray.H.

Referenced by Aleph::ArrayQueue< T >::ArrayQueue(), Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::~MemArray(), Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::append(), Aleph::MemArray< T >::close_gap(), Aleph::ArrayQueue< T >::complete_put(), Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::disable_annotations(), Aleph::MemArray< T >::empty(), Aleph::MemArray< T >::expand(), Aleph::ArrayQueue< T >::first(), Aleph::MemArray< T >::first(), Aleph::ArrayQueue< T >::first(), Aleph::MemArray< T >::first(), Aleph::ArrayQueue< T >::front(), Aleph::ArrayQueue< T >::front(), Aleph::ArrayQueue< T >::get(), Aleph::MemArray< T >::get(), Aleph::MemArray< T >::get_ne(), Aleph::ArrayQueue< T >::getn(), Aleph::MemArray< T >::is_empty(), Aleph::ArrayQueue< T >::last(), Aleph::MemArray< T >::last(), Aleph::ArrayQueue< T >::last(), Aleph::MemArray< T >::last(), Aleph::MemArray< T >::open_gap(), Aleph::ArrayQueue< T >::operator()(), Aleph::MemArray< T >::operator()(), Aleph::ArrayQueue< T >::operator()(), Aleph::MemArray< T >::operator()(), Aleph::ArrayQueue< T >::operator[](), Aleph::MemArray< T >::operator[](), Aleph::ArrayQueue< T >::operator[](), Aleph::MemArray< T >::operator[](), Aleph::MemArray< T >::push_item(), Aleph::ArrayQueue< T >::put_item(), Aleph::MemArray< T >::put_item(), Aleph::MemArray< T >::putn(), Aleph::ArrayQueue< T >::putn(), Aleph::MemArray< T >::reallocate(), Aleph::ArrayQueue< T >::rear(), Aleph::ArrayQueue< T >::rear(), Aleph::ArrayQueue< T >::recenter_indices(), Aleph::MemArray< T >::relocate_and_insert(), Aleph::MemArray< T >::remove_first(), Aleph::ArrayQueue< T >::remove_last(), Aleph::ArrayQueue< T >::reverse(), Aleph::MemArray< T >::reverse(), Aleph::MemArray< T >::size(), Aleph::MemArray< T >::swap(), Aleph::MemArray< T >::top(), Aleph::MemArray< T >::top(), Aleph::ArrayQueue< T >::traverse(), Aleph::MemArray< T >::traverse(), Aleph::ArrayQueue< T >::traverse(), and Aleph::MemArray< T >::traverse().

◆ ptr


The documentation for this class was generated from the following file: