Aleph-w  1.9
General library for algorithms and data structures
Aleph::MemArray< T > Class Template Reference

#include <tpl_memArray.H>

+ Inheritance diagram for Aleph::MemArray< T >:

Classes

struct  Iterator
 

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.
 
size_t capacity () const noexcept
 The type of element of array. More...
 
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)
 
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 a 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.
 
T & put (const T &item)
 
T & put (T &&item)
 
T & push (const T &item)
 
T & push (T &&item)
 
T & top () const
 
remove_first ()
 Remove first item. Gap is closed.
 
pop ()
 pop() the most recently pushed item
 
T & append (T &item)
 
T & append (T &&item)
 
T & insert (T &item)
 
T & insert (T &&item)
 
void putn (const size_t more)
 
MemArrayappend (const MemArray &a)
 
void reserve (const size_t cap)
 
get (size_t i=1)
 
get_ne (size_t i=1) noexcept
 
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
 
MemArrayreverse ()
 Reverse the order of items in array.
 
T & access (const size_t i) const noexcept
 
T & operator[] (const size_t i) const
 
T & operator() (const size_t i) const noexcept
 
template<class Operation >
bool traverse (Operation &operation)
 
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)
 
bool contract (const size_t first=0)
 
void init_dim (size_t d) noexcept
 

Protected Attributes

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

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.

Constructor & Destructor Documentation

◆ MemArray()

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

Construct an array con capacicity equal or greater than __dim.

Parameters
[in]__dimproposed dimension of arraym which could be adjusted to the next two power greater than __dim
Exceptions
bad_allocsi no hay suficiente memoria.

Member Function Documentation

◆ access()

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

Return a modifiable reference to the ith element. No bound check is performed

+ Here is the caller graph for this function:

◆ append() [1/2]

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

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

+ Here is the caller graph for this function:

◆ append() [2/2]

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

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

◆ capacity()

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

The type of element of array.

Return the capacity of array (its dimension)

+ Here is the caller graph for this function:

◆ 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
+ Here is the caller graph for this function:

◆ 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
+ Here is the caller graph for this function:

◆ get()

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

Remove i elements from the end. Return the value of the last removed element

+ Here is the caller graph for this function:

◆ get_first()

template<typename T>
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.

◆ get_last()

template<typename T>
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.

◆ insert() [1/2]

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

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

◆ insert() [2/2]

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

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

◆ operator()()

template<typename T>
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.

◆ 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

◆ push() [1/2]

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

Push a copy of item at the begining of sequence. The array expands if this is already full

+ Here is the caller graph for this function:

◆ push() [2/2]

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

Push a copy of item at the begining of sequence. The array expands if this is already full

◆ put() [1/2]

template<typename T>
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

+ Here is the caller graph for this function:

◆ put() [2/2]

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

Move item at the end of sequence. The array expands if this is already full

◆ putn()

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

Put n cells into the array.

putn(more) is functionally equivalent to perform more puts of T() in constant time. The idea is to reserve enough space for later use.

Parameters
[in]nnumber of cells to reserve
Exceptions
bad_allocif there is no enough memory
+ Here is the caller graph for this function:

◆ remove_last()

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

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

+ Here is the caller graph for this function:

◆ 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
+ Here is the caller graph for this function:

◆ traverse() [1/4]

template<typename T>
template<class Operation >
bool 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.
+ Here is the caller graph for this function:

◆ traverse() [2/4]

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

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

◆ traverse() [3/4]

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

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

◆ traverse() [4/4]

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

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


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

Leandro Rabindranath León