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>

Inheritance 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

Tget_ptr () const noexcept
 Return the current base of array.
 
const size_tget_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 gre+ater 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
 
MemArrayoperator= (const MemArray &a)
 Assign by copy a to this
 
MemArrayoperator= (MemArray &&a) noexcept
 Assign by moving a to this
 
void empty ()
 Empty the container. The array is not contracted.
 
void empty_and_release ()
 Empty the array and release all memory.
 
Tput (const T &item)
 Put a copy of item at the end of sequence.
 
Tput (T &&item)
 Move item at the end of sequence.
 
Tpush (const T &item)
 Push a copy of item at the beginning of sequence.
 
Tpush (T &&item)
 Push a copy of item at the beginning of sequence.
 
Ttop () const
 
T remove_first ()
 Remove first item. Gap is closed.
 
T pop ()
 pop() the most recently pushed item
 
Tappend (T &item)
 
Tappend (T &&item)
 
Tinsert (T &item)
 
Tinsert (T &&item)
 
void putn (const size_t more)
 Reserve more additional logical slots in the array.
 
MemArrayappend (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 ()
 
Tlast () const
 Return a modifiable reference to the last element.
 
Tfirst () const
 Return a modifiable reference to the first element.
 
Tget_first () const
 
Tget_last () const
 
MemArrayreverse ()
 Reverse the order of items in array.
 
Taccess (const size_t i) const noexcept
 Return a modifiable reference to the ith element.
 
Toperator[] (const size_t i) const
 Return a reference to the ith element.
 
Toperator() (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) noexcept
 Initialize the dimension of the array to d or to the next two power if d is not a two power.
 

Protected Attributes

Tptr = 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 implement a totally sequential dynamic array. That is, conditioned to memory availability, all 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 to insert and remove elements. These operation 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 descend 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 138 of file tpl_memArray.H.

Member Typedef Documentation

◆ Item_Type

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

Definition at line 262 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 gre+ater 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 279 of file tpl_memArray.H.

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

◆ ~MemArray()

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

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

References Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::dim, 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 321 of file tpl_memArray.H.

References Aleph::maps(), 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 618 of file tpl_memArray.H.

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

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

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

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

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST_F().

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

References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::first(), Aleph::maps(), 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 ( )
inline

Empty the container. The array is not contracted.

Definition at line 358 of file tpl_memArray.H.

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

◆ empty_and_release()

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

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

References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::first(), Aleph::is_power_of_2(), Aleph::maps(), 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 596 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 558 of file tpl_memArray.H.

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

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

◆ get_dim()

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

Return the current dimension of array.

Definition at line 158 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 603 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 606 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()

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

Return the current base of array.

Definition at line 155 of file tpl_memArray.H.

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

Referenced by TEST(), and TEST_F().

◆ init_dim()

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

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

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

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

◆ insert() [1/2]

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

◆ insert() [2/2]

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

Definition at line 473 of file tpl_memArray.H.

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

Referenced by TEST(), and TEST().

◆ is_empty()

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

Return true is the array is empty.

Definition at line 271 of file tpl_memArray.H.

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

Referenced by TEST(), TEST(), TEST(), 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 681 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 589 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().

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

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

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

Referenced by TEST().

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

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

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

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

References Aleph::maps(), 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 376 of file tpl_memArray.H.

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

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

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

References Aleph::MemArray< T >::expand(), Aleph::maps(), 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 496 of file tpl_memArray.H.

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

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

◆ remove_first()

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

Remove first item. Gap is closed.

Definition at line 446 of file tpl_memArray.H.

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

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

◆ remove_last()

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

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

References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::is_power_of_2(), log2(), Aleph::maps(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.

◆ reverse()

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

Reverse the order of items in array.

Definition at line 609 of file tpl_memArray.H.

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

◆ size()

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

Return the number of elements.

Definition at line 268 of file tpl_memArray.H.

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

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

◆ swap()

◆ top()

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

Definition at line 438 of file tpl_memArray.H.

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

Referenced by TEST().

◆ traverse() [1/4]

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

Definition at line 676 of file tpl_memArray.H.

References Aleph::maps().

◆ traverse() [2/4]

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

Definition at line 669 of file tpl_memArray.H.

References Aleph::maps().

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

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

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

◆ traverse() [4/4]

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

Definition at line 662 of file tpl_memArray.H.

References Aleph::maps(), and Aleph::MemArray< T >::traverse().

Member Data Documentation

◆ contract_threshold

◆ dim

◆ Min_Dim

◆ n

◆ ptr


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