density
C++11 library for paged memory management, function queues, heterogeneous queues and lifo memory management
Public Types | Public Member Functions | List of all members
lifo_array< TYPE > Class Template Referencefinal

#include <lifo.h>

Public Types

using value_type = TYPE
 
using reference = TYPE &
 
using const_reference = TYPE &
 
using pointer = TYPE *
 
using const_pointer = const TYPE *
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 

Public Member Functions

 lifo_array (size_t i_size)
 
template<typename... CONSTRUCTION_PARAMS>
 lifo_array (size_t i_size, const CONSTRUCTION_PARAMS &...i_construction_params)
 
template<typename INPUT_ITERATOR >
 lifo_array (INPUT_ITERATOR i_begin, INPUT_ITERATOR i_end, typename std::iterator_traits< INPUT_ITERATOR >::iterator_category=typename std::iterator_traits< INPUT_ITERATOR >::iterator_category())
 
 lifo_array (const lifo_array &)=delete
 
lifo_arrayoperator= (const lifo_array &)=delete
 
 ~lifo_array ()
 
size_t size () const noexcept
 
TYPE & operator[] (size_t i_index) noexcept
 
const TYPE & operator[] (size_t i_index) const noexcept
 
pointer data () noexcept
 
const_pointer data () const noexcept
 
iterator begin () noexcept
 
iterator end () noexcept
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 

Detailed Description

template<typename TYPE>
class density::lifo_array< TYPE >

Array container that allocates its elements from the thread-local data stack, and owns it. The block is deallocated by the destructor. This class should be used only on the automatic storage.

Template Parameters
TYPEElement type.

The data stack is a pool in which a thread can allocate and deallocate memory in LIFO order. It is handled by an internal lifo_allocator, which in turn in built upon an instance of default_allocator. lifo_array and lifo_buffer allocate on the data stack, so they must respect the LIFO order: only the most recently allocated block can be deallocated or reallocated. Instantiating lifo_array and lifo_buffer on the automatic storage (locally in a function) is always safe, as the language guarantees destruction in reverse order, even inside arrays or aggregate types.

Exactly like raw arrays and std::array, the default constructor of lifo_array default-constructs the elements. So if elements have POD type, they are not initialized. In contrast, std::vector value-initializes its elements, so POD types are initialized to zero.

// uninitialized array of doubles
lifo_array<double> numbers(7);
// initialize the array
for (auto & num : numbers)
num = 1.;
// compute the sum
auto const sum = std::accumulate(numbers.begin(), numbers.end(), 0.);
assert(sum == 7.);
// initialized array
lifo_array<double> other_numbers(7, 1.);
auto const other_sum = std::accumulate(other_numbers.begin(), other_numbers.end(), 0.);
assert(other_sum == 7.);
// array of class objects - they are initialized by the default constructor
lifo_array<std::string> strings(10);
bool all_empty =
std::all_of(strings.begin(), strings.end(), [](const std::string & i_str) {
return i_str.empty();
});
assert(all_empty);

The size of a lifo_array is fixed at construction time and is immutable. Elements are constructed in positional order and destroyed in reverse order.

void concat_and_print(const char * i_str_1, const char * i_str_2)
{
using namespace density;
auto const len_1 = strlen(i_str_1);
auto const len_2 = strlen(i_str_2);
lifo_array<char> string(len_1 + len_2 + 1);
memcpy(string.data(), i_str_1, len_1);
memcpy(string.data() + len_1, i_str_2, len_2);
string[len_1 + len_2] = 0;
std::cout << string.data() << std::endl;
}

Constructor & Destructor Documentation

lifo_array ( size_t  i_size)
inline

Constructs a lifo_array and all its elements. If elements are POD types, they are not initialized.

Parameters
i_sizenumber of elements of the array
lifo_array ( size_t  i_size,
const CONSTRUCTION_PARAMS &...  i_construction_params 
)
inline

Constructs all the elements with the provided parameter pack.

Parameters
i_sizenumber of elements of the array
i_construction_paramsconstruction parameters to use for every element of the array
lifo_array<std::string> strings(10, 4, '*');
assert(strings.size() == 10);
bool all_stars =
std::all_of(strings.begin(), strings.end(), [](const std::string & i_str) {
return i_str == "****";
});
assert(all_stars);
lifo_array ( INPUT_ITERATOR  i_begin,
INPUT_ITERATOR  i_end,
typename std::iterator_traits< INPUT_ITERATOR >::iterator_category  = typename std::iterator_traits<INPUT_ITERATOR>::iterator_category() 
)
inline

Initializes the array from an input range. The size of the array is computed with std::distance(i_begin, i_end).

Parameters
i_beginiterator pointing to the first source value
i_enditerator pointing to the first value that is not copied in the array.
std::vector<int> vect{1, 2, 3};
lifo_array<int> array(vect.cbegin(), vect.cend());
auto int_sum = std::accumulate(array.begin(), array.end(), 0);
assert(int_sum == 6);
lifo_array ( const lifo_array< TYPE > &  )
delete

Copy construction not allowed

~lifo_array ( )
inline

Destroys a lifo_array and all its elements. Elements are destroyed in reverse positional order.

Member Function Documentation

lifo_array& operator= ( const lifo_array< TYPE > &  )
delete

Copy assignment not allowed

size_t size ( ) const
inlinenoexcept

Returns the number of elements

TYPE& operator[] ( size_t  i_index)
inlinenoexcept

Returns a reference to the i-th element.

Precondition
The behavior is undefined if either:
const TYPE& operator[] ( size_t  i_index) const
inlinenoexcept

Returns a const reference to the i-th element.

Precondition
The behavior is undefined if either:
pointer data ( )
inlinenoexcept

Returns a pointer to the first element.

const_pointer data ( ) const
inlinenoexcept

Returns a pointer to the first element.


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