Aleph-w 3.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 >, Aleph::ArrayQueue< Aleph::DynList< int > >, and Aleph::ArrayQueue< T >.

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

Classes

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

Public Types

using Item_Type = T
 

Public Member Functions

T * get_ptr () const noexcept
 Return the current base of array.
 
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
 
 MemArray (MemArray &&a) noexcept
 Construct an array moved of rvalue a
 
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 ()
 Empty the array and release all memory.
 
T & put (const T &item)
 Put a copy of item at the end of sequence.
 
T & put (T &&item)
 Move item at the end of sequence.
 
T & push (const T &item)
 Push a copy of item at the beginning of sequence.
 
T & push (T &&item)
 Push a copy of item at the beginning of sequence.
 
T & top () const
 
T remove_first ()
 Remove the first item. Gap is closed.
 
T pop ()
 pop() the most recently pushed item
 
T & append (const T &item)
 
T & append (T &&item)
 
T & insert (const T &item)
 
T & insert (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 ()
 
T & last () const
 Return a modifiable reference to the last element.
 
T & first () const
 Return a modifiable reference to the first element.
 
T & get_first () const
 
T & get_last () const
 
MemArray & reverse ()
 Reverse the order of items in array.
 
T & access (const size_t i) const noexcept
 Return a modifiable reference to the ith element.
 
T & operator[] (const size_t i) const
 Return a reference to the ith element.
 
T & operator() (const size_t i) const noexcept
 
template<class Operation >
bool traverse (Operation &operation)
 Traverse all the elements from index 0 to n - 1 and execute operation on each on them.
 
template<class Operation >
bool traverse (Operation &operation) const
 
template<class Operation >
bool traverse (Operation &&operation) const
 
template<class Operation >
bool traverse (Operation &&operation)
 
bool is_valid () const noexcept
 

Public Attributes

size_t contract_threshold
 

Static Public Attributes

static constexpr size_t Min_Dim = 4
 

Protected Member Functions

void allocate ()
 Allocate memory for the current dimension.
 
bool expand (const size_t first=0)
 Test is array is full and if affrimative, then expand the array twice as long and copy the content by swapping.
 
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
 

Private Member Functions

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 135 of file tpl_memArray.H.

Member Typedef Documentation

◆ Item_Type

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

Definition at line 279 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.

Definition at line 305 of file tpl_memArray.H.

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

◆ ~MemArray()

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

Definition at line 313 of file tpl_memArray.H.

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

◆ MemArray() [2/3]

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

Construct a copy of a

Definition at line 332 of file tpl_memArray.H.

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

◆ MemArray() [3/3]

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

Construct an array moved of rvalue a

Definition at line 342 of file tpl_memArray.H.

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

Member Function Documentation

◆ access()

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

Return a modifiable reference to the ith element.

No bound_statics check is performed

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

Definition at line 680 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), and Aleph::MemArray< T >::ptr.

Referenced by Aleph::ArrayQueue< T >::complete_put(), Aleph::ArrayQueue< T >::front(), Aleph::ArrayQueue< T >::get(), Aleph::ArrayQueue< T >::getn(), Aleph::ArrayQueue< T >::put(), Aleph::ArrayQueue< T >::put(), Aleph::ArrayQueue< T >::rear_item(), and Aleph::ArrayQueue< T >::traverse().

◆ allocate()

◆ append() [1/3]

◆ append() [2/3]

◆ append() [3/3]

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

◆ capacity()

template<typename T >
constexpr size_t Aleph::MemArray< T >::capacity ( ) const
inlineconstexprnoexcept

The type of element of array.

Return the capacity of array (its dimension)

Definition at line 282 of file tpl_memArray.H.

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

Referenced by Aleph::FlatMap< Key, T, Compare >::capacity(), Aleph::FlatSet< Key, Compare >::capacity(), Aleph::FlatMap< Key, T, Compare >::make_room(), print(), print(), TEST(), TEST(), and TEST_F().

◆ clear()

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

Alias for empty().

Exceptions
none
See also
empty()

Definition at line 394 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 232 of file tpl_memArray.H.

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

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

◆ 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

Definition at line 384 of file tpl_memArray.H.

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

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

◆ empty_and_release()

◆ expand()

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

Test is array is full and if affrimative, then expand the array twice as long and copy the content by swapping.

This method first allocates a chunck of 2*dim and then copies from first index the n contained entries to the new chunck.

Parameters
[in]firstindex where is found the first item of array
Returns
true if the array was full and then a new twice as long was allocated
Exceptions
bad_allocif there is no enough memory

Definition at line 192 of file tpl_memArray.H.

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

Referenced by Aleph::ArrayQueue< T >::put(), Aleph::MemArray< T >::put(), Aleph::ArrayQueue< T >::put(), Aleph::MemArray< T >::put(), and Aleph::ArrayQueue< T >::putn().

◆ first()

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

Return a modifiable reference to the first element.

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

Definition at line 649 of file tpl_memArray.H.

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

Referenced by Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::expand(), and Aleph::MemArray< T >::get_first().

◆ get()

template<typename T >
Aleph::MemArray< T >::get ( const size_t  i = 1)
inline

Remove i elements from the end.

Return the value of the last removed element

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

Definition at line 608 of file tpl_memArray.H.

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

Referenced by Aleph::MemArray< T >::close_gap(), main(), Aleph::FlatMap< Key, T, Compare >::remove_at(), Aleph::FlatSet< Key, Compare >::remove_at(), Aleph::MemArray< T >::remove_last(), Aleph::FlatMap< Key, T, Compare >::sort_and_unique(), and Aleph::FlatSet< Key, Compare >::sort_and_unique().

◆ get_dim()

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

Return the current dimension of array.

Definition at line 155 of file tpl_memArray.H.

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

◆ get_first()

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

Definition at line 656 of file tpl_memArray.H.

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

◆ get_last()

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

Definition at line 662 of file tpl_memArray.H.

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

◆ get_ne()

◆ get_ptr()

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

◆ 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 267 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), 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

◆ insert() [2/2]

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

◆ is_empty()

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

Return true is the array is empty.

Definition at line 294 of file tpl_memArray.H.

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

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

◆ is_valid()

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

Definition at line 743 of file tpl_memArray.H.

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

◆ last()

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

Return a modifiable reference to the last element.

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

Definition at line 642 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().

◆ 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()()

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

◆ operator=() [1/2]

◆ operator=() [2/2]

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

Assign by moving a to this

Definition at line 372 of file tpl_memArray.H.

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

◆ operator[]()

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

Return a reference to the ith element.

Throws out_of_range if i is out of range

Definition at line 688 of file tpl_memArray.H.

References ah_out_of_range_error_if, Aleph::blossom_maximum_cardinality_matching(), 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 495 of file tpl_memArray.H.

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

◆ push() [1/2]

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

Push a copy of item at the beginning of sequence.

The array expands if this is already full

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

Definition at line 454 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::MemArray< T >::open_gap(), and Aleph::MemArray< T >::ptr.

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

◆ push() [2/2]

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

Push a copy of item at the beginning of sequence.

The array expands if this is already full

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

Definition at line 467 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::MemArray< T >::open_gap(), and Aleph::MemArray< T >::ptr.

◆ put() [1/2]

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

Put a copy of item at the end of sequence.

The array expands if this is already full

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

Definition at line 415 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

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

◆ put() [2/2]

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

Move item at the end of sequence.

The array expands if this is already full

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

Definition at line 428 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ 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 540 of file tpl_memArray.H.

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

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

◆ remove_first()

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

◆ remove_last()

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

Definition at line 636 of file tpl_memArray.H.

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

◆ reserve()

◆ reverse()

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

Reverse the order of items in array.

Definition at line 668 of file tpl_memArray.H.

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

◆ size()

◆ swap()

◆ top()

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

◆ traverse() [1/4]

template<typename T >
template<class Operation >
bool Aleph::MemArray< T >::traverse ( Operation &&  operation)
inline

Definition at line 738 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching().

◆ traverse() [2/4]

template<typename T >
template<class Operation >
bool Aleph::MemArray< T >::traverse ( Operation &&  operation) const
inline

Definition at line 731 of file tpl_memArray.H.

References Aleph::blossom_maximum_cardinality_matching().

◆ traverse() [3/4]

template<typename T >
template<class Operation >
Aleph::MemArray< T >::traverse ( Operation &  operation)
inline

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
Returns
true if operation was executed on all elements; false otherwise.

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

Definition at line 712 of file tpl_memArray.H.

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

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

◆ traverse() [4/4]

template<typename T >
template<class Operation >
bool Aleph::MemArray< T >::traverse ( Operation &  operation) const
inline

Member Data Documentation

◆ contract_threshold

◆ dim

◆ Min_Dim

template<typename T >
constexpr size_t Aleph::MemArray< T >::Min_Dim = 4
staticconstexpr

◆ n

◆ ptr


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