|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Simple, scalable and fast dynamic array. More...
#include <tpl_memArray.H>
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 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 | |
| MemArray & | operator= (const MemArray &a) |
Assign by copy a to this | |
| MemArray & | operator= (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) |
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 first item. Gap is closed. | |
| T | 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) |
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) noexcept |
Initialize the dimension of the array to d or to the next two power if d is not a two power. | |
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) |
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.
Definition at line 138 of file tpl_memArray.H.
Definition at line 262 of file tpl_memArray.H.
Construct an array con capacity equal or gre+ater than __dim.
| [in] | __dim | proposed dimension of array which could be adjusted to the next two power greater than __dim |
| bad_alloc | if 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().
|
inline |
Definition at line 293 of file tpl_memArray.H.
References Aleph::MemArray< T >::ptr.
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.
Construct an array moved of rvalue a
Definition at line 321 of file tpl_memArray.H.
References Aleph::maps(), and Aleph::MemArray< T >::swap().
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().
|
inlineprotected |
Allocate memory for the current dimension.
Definition at line 163 of file tpl_memArray.H.
References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::is_power_of_2(), Aleph::maps(), and Aleph::MemArray< T >::ptr.
Referenced by Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::MemArray(), and Aleph::MemArray< T >::empty_and_release().
Definition at line 521 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::n, Aleph::MemArray< T >::ptr, Aleph::MemArray< T >::putn(), and Aleph::MemArray< T >::size().
Definition at line 466 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::put().
Definition at line 459 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::put().
Referenced by MemArray_with_30_items::MemArray_with_30_items().
|
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.
|
inlineprivate |
Definition at line 406 of file tpl_memArray.H.
References Aleph::close_gap(), Aleph::MemArray< T >::get(), Aleph::maps(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.
Referenced by Aleph::MemArray< T >::remove_first().
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.
| [in] | first | index of first element |
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().
|
inline |
Empty the container. The array is not contracted.
Definition at line 358 of file tpl_memArray.H.
References Aleph::MemArray< T >::n.
|
inline |
Empty the array and release all memory.
Definition at line 361 of file tpl_memArray.H.
References Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::dim, Aleph::maps(), Aleph::MemArray< T >::Min_Dim, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.
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.
| [in] | first | index where is found the first item of array |
true if the array was full and then a new twice as long was allocated | bad_alloc | if 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().
|
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().
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().
Return the current dimension of array.
Definition at line 158 of file tpl_memArray.H.
References Aleph::MemArray< T >::dim.
|
inline |
Definition at line 603 of file tpl_memArray.H.
References Aleph::MemArray< T >::first().
|
inline |
Definition at line 606 of file tpl_memArray.H.
References Aleph::MemArray< T >::last().
Definition at line 573 of file tpl_memArray.H.
References Aleph::MemArray< T >::contract(), Aleph::maps(), Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.
|
inlinenoexcept |
Return the current base of array.
Definition at line 155 of file tpl_memArray.H.
References Aleph::MemArray< T >::ptr.
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.
| [in] | d | the 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().
Definition at line 480 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::push().
Definition at line 473 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::push().
|
inlinenoexcept |
|
inlinenoexcept |
Definition at line 681 of file tpl_memArray.H.
References Aleph::MemArray< T >::ptr.
|
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().
|
inlineprivate |
Definition at line 400 of file tpl_memArray.H.
References Aleph::maps(), Aleph::MemArray< T >::n, Aleph::open_gap(), Aleph::MemArray< T >::ptr, and Aleph::MemArray< T >::putn().
Referenced by Aleph::MemArray< T >::push(), and Aleph::MemArray< T >::push().
|
inlinenoexcept |
Definition at line 635 of file tpl_memArray.H.
References Aleph::MemArray< T >::dim, Aleph::maps(), and Aleph::MemArray< T >::ptr.
|
inline |
Assign by copy a to this
Definition at line 329 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.
|
inlinenoexcept |
Assign by moving a to this
Definition at line 351 of file tpl_memArray.H.
References Aleph::MemArray< T >::swap().
|
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.
|
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 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().
|
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 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().
|
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.
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.
| [in] | more | number of cells to reserve |
| bad_alloc | if 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().
|
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().
|
inline |
Definition at line 586 of file tpl_memArray.H.
References Aleph::MemArray< T >::get().
Reserves cap cells into the array.
| [in] | cap | new dimension |
| bad_alloc | if 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.
|
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.
|
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 in constant time this with a
Definition at line 303 of file tpl_memArray.H.
References Aleph::MemArray< T >::contract_threshold, Aleph::MemArray< T >::dim, Aleph::MemArray< T >::n, and Aleph::MemArray< T >::ptr.
Referenced by Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::operator=(), and Aleph::ArrayQueue< T >::swap().
|
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().
|
inline |
Definition at line 676 of file tpl_memArray.H.
References Aleph::maps().
|
inline |
Definition at line 669 of file tpl_memArray.H.
References Aleph::maps().
|
inline |
Traverse all the elements from index 0 to n - 1 and execute operation on each on them.
| [in] | operation | to be performed on each element |
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().
|
inline |
Definition at line 662 of file tpl_memArray.H.
References Aleph::maps(), and Aleph::MemArray< T >::traverse().
|
mutable |
Definition at line 152 of file tpl_memArray.H.
Referenced by Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::operator=(), Aleph::MemArray< T >::putn(), Aleph::MemArray< T >::reserve(), and Aleph::MemArray< T >::swap().
Definition at line 147 of file tpl_memArray.H.
Referenced by Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::capacity(), Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::empty_and_release(), Aleph::MemArray< T >::expand(), Aleph::ArrayQueue< T >::front(), Aleph::MemArray< T >::get_dim(), Aleph::ArrayQueue< T >::increase_index(), Aleph::MemArray< T >::init_dim(), Aleph::MemArray< T >::operator()(), Aleph::MemArray< T >::operator=(), Aleph::MemArray< T >::putn(), Aleph::ArrayQueue< T >::putn(), Aleph::ArrayQueue< T >::rear_item(), Aleph::MemArray< T >::reserve(), and Aleph::MemArray< T >::swap().
Definition at line 142 of file tpl_memArray.H.
Referenced by Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::empty_and_release(), and Aleph::MemArray< T >::init_dim().
|
protected |
Definition at line 148 of file tpl_memArray.H.
Referenced by Aleph::MemArray< T >::append(), Aleph::MemArray< T >::close_gap(), Aleph::ArrayQueue< T >::complete_put(), Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::empty(), Aleph::MemArray< T >::empty_and_release(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::first(), 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::MemArray< T >::last(), Aleph::MemArray< T >::open_gap(), Aleph::MemArray< T >::operator=(), Aleph::MemArray< T >::operator[](), Aleph::MemArray< T >::put(), Aleph::MemArray< T >::put(), Aleph::MemArray< T >::putn(), Aleph::ArrayQueue< T >::putn(), Aleph::ArrayQueue< T >::rear(), Aleph::ArrayQueue< T >::recenter_indices(), Aleph::MemArray< T >::remove_first(), Aleph::MemArray< T >::reserve(), Aleph::MemArray< T >::reverse(), Aleph::MemArray< T >::size(), Aleph::MemArray< T >::swap(), Aleph::MemArray< T >::top(), Aleph::ArrayQueue< T >::traverse(), and Aleph::MemArray< T >::traverse().
Definition at line 146 of file tpl_memArray.H.
Referenced by Aleph::MemArray< T >::MemArray(), Aleph::MemArray< T >::~MemArray(), Aleph::MemArray< T >::access(), Aleph::MemArray< T >::allocate(), Aleph::MemArray< T >::append(), Aleph::MemArray< T >::append(), Aleph::MemArray< T >::append(), Aleph::MemArray< T >::close_gap(), Aleph::MemArray< T >::contract(), Aleph::MemArray< T >::empty_and_release(), Aleph::MemArray< T >::expand(), Aleph::MemArray< T >::first(), Aleph::MemArray< T >::get(), Aleph::MemArray< T >::get_ne(), Aleph::MemArray< T >::get_ptr(), Aleph::MemArray< T >::insert(), Aleph::MemArray< T >::insert(), Aleph::MemArray< T >::is_valid(), Aleph::MemArray< T >::last(), Aleph::MemArray< T >::open_gap(), Aleph::MemArray< T >::operator()(), Aleph::MemArray< T >::operator=(), Aleph::MemArray< T >::operator[](), Aleph::MemArray< T >::push(), Aleph::MemArray< T >::push(), Aleph::MemArray< T >::put(), Aleph::MemArray< T >::put(), Aleph::MemArray< T >::putn(), Aleph::MemArray< T >::remove_first(), Aleph::MemArray< T >::reserve(), Aleph::MemArray< T >::reverse(), Aleph::MemArray< T >::swap(), Aleph::MemArray< T >::top(), and Aleph::MemArray< T >::traverse().