A contiguous data container expanded on write. More...
#include <LazyVector.h>
Public Types | |
STL vector types | |
using | allocator_type = typename Data_t::allocator_type |
using | value_type = typename Data_t::value_type |
using | size_type = typename Data_t::size_type |
using | difference_type = typename Data_t::difference_type |
using | reference = typename Data_t::reference |
using | const_reference = typename Data_t::const_reference |
using | pointer = typename Data_t::pointer |
using | const_pointer = typename Data_t::const_pointer |
Public Member Functions | |
LazyVector ()=default | |
LazyVector (allocator_type const &a) | |
Constructor: like default, but using the specified allocator. More... | |
LazyVector (size_type n) | |
Constructor: a lazy vector with a specified maximum size. More... | |
LazyVector (size_type n, value_type const &defValue) | |
Constructor: a lazy vector with a specified maximum size. More... | |
reference | at (size_type pos) |
Returns a reference to the specified element, throws an exception if not present. More... | |
Container information | |
— END Constructors -------------------------------------------—— | |
size_type | size () const noexcept |
Returns the size of the vector. More... | |
bool | empty () const noexcept |
Returns whether the vector is empty. More... | |
size_type | data_size () const noexcept |
Returns the size of data actually stored. More... | |
bool | has_index (size_type pos) const noexcept |
Returns whether the specified position is within the vector. More... | |
bool | data_empty () const noexcept |
Returns whether no data is actually stored. More... | |
value_type const & | data_defvalue () const |
size_type | data_begin_index () const |
size_type | data_end_index () const |
bool | data_has_index (size_type pos) const |
Returns the internal storage index for the specified position. More... | |
const_pointer | data_address (size_type pos) const |
Returns a constant pointer to the specified element. More... | |
Access to data elements | |
There are important differences between the access methods of The constant versions of the access methods differ from the corresponding STL Also, the non-constant versions of the access methods always ensure storage for the accessed element. If such behaviour needs to be avoided (e.g. for performance reasons), use the methods explicitly named constant: | |
value_type | at (size_type pos) const |
Returns a reference to the specified element, throws an exception if not present. More... | |
value_type | const_at (size_type pos) const |
value_type | operator[] (size_type pos) const |
Returns a copy of the specified element. More... | |
value_type | const_get (size_type pos) const |
reference | operator[] (size_type pos) |
Returns a reference to the specified element. More... | |
reference | get (size_type pos) |
Container operations | |
void | resize (size_type newSize) |
Changes the nominal size of the container. More... | |
void | reserve (size_type n) |
Allocates enough memory in storage to store n elements. More... | |
void | clear () |
Removes all stored data and sets the nominal size to 0. More... | |
void | shrink_to_fit () |
Reduces memory usage to the amount needed by the elements with storage. More... | |
void | data_prepare (size_type startIndex, size_type endIndex) |
Prepares the vector to store elements in the specified range. More... | |
void | data_prepare (size_type n) |
Prepares the vector to store n elements from 0 . More... | |
void | data_init (size_type startIndex, size_type endIndex) |
Allocates the specified range and stores default values for it. More... | |
void | data_init (size_type n) |
Allocates and initializes n elements starting from index 0. More... | |
Private Types | |
using | Data_t = std::vector< T, A > |
Actual data storage type. More... | |
Private Member Functions | |
size_type | index_of (size_type pos) const |
Returns the internal storage index for the specified position. More... | |
void | expand (size_type pos) |
Expands the storage to include the specified position. More... | |
void | init (size_type pos, size_type n=1U) |
Makes the first data allocation. More... | |
void | expand_front (size_type pos) |
Expands the storage to include the specified position behind it. More... | |
void | expand_back (size_type pos) |
Expands the storage to include the specified position ahead of it. More... | |
void | fix_size () |
Makes sure the nominal size is large enough to include all stored data. More... | |
void | data_clear () |
Erases all stored data from the container; nominal size is not changed. More... | |
void | check_range (size_type pos) const |
Throws std::out_of_range if pos is not contained in the vector. More... | |
Data_t & | storage () |
Returns the data storage. More... | |
Data_t const & | storage () const |
Static Private Member Functions | |
static value_type const & | defaultValueType () |
Returns the class default value (used when user does not specify any). More... | |
Private Attributes | |
Data_t | fData |
Actual data storage. More... | |
size_type | fNominalSize = 0U |
Alleged data size. More... | |
size_type | fFirstIndex = fData.max_size() |
First element currently stored. More... | |
value_type | fDefValue = defaultValueType() |
Default value. More... | |
Static Private Attributes | |
static value_type const | fDefaultDefaultValue {} |
Default-initialised value of type value_type used as default fallback. More... | |
A contiguous data container expanded on write.
T | type of contained data |
A | allocator for the data (default: STL vector's default allocator) |
This container class represents a number of data elements contiguously allocated in memory. It mimics the behaviour and interface of STL vector, but the actual data allocation is lazy, that is it happens only when writing to an element is requested. The internal allocation is always contiguous, including as little data as it can accommodate elements from the first to the last index written at any point.
The interface is also a partial replica of STL vector, with the addition of members specific to this class, whose names start with data_
. Among the relevant features missing from this object there is iterators.
For some internal resizing operations, a default value is used to construct the new values. This default value can be specified in some constructors, Otherwise, a value equivalent to 0
(i.e. value_type(0)
) is used for arithmetic value types, and the default-constructed value is used for all other types.
Example of usage:
LazyVector
, since every access will create storage for the specified element (like in STL map operator[]
). For this reason, the special methods const_at(size_type)
and const_get(size_type)
are provided, which never create storage. Definition at line 78 of file LazyVector.h.
using util::LazyVector< T, A >::allocator_type = typename Data_t::allocator_type |
Definition at line 88 of file LazyVector.h.
using util::LazyVector< T, A >::const_pointer = typename Data_t::const_pointer |
Definition at line 95 of file LazyVector.h.
using util::LazyVector< T, A >::const_reference = typename Data_t::const_reference |
Definition at line 93 of file LazyVector.h.
|
private |
Actual data storage type.
Definition at line 80 of file LazyVector.h.
using util::LazyVector< T, A >::difference_type = typename Data_t::difference_type |
Definition at line 91 of file LazyVector.h.
using util::LazyVector< T, A >::pointer = typename Data_t::pointer |
Definition at line 94 of file LazyVector.h.
using util::LazyVector< T, A >::reference = typename Data_t::reference |
Definition at line 92 of file LazyVector.h.
using util::LazyVector< T, A >::size_type = typename Data_t::size_type |
Definition at line 90 of file LazyVector.h.
using util::LazyVector< T, A >::value_type = typename Data_t::value_type |
Definition at line 89 of file LazyVector.h.
|
default |
— BEGIN Constructors ----------------------------------------------— Default constructor: an empty vector.
util::LazyVector< T, A >::LazyVector | ( | allocator_type const & | a | ) |
Constructor: like default, but using the specified allocator.
Definition at line 489 of file LazyVector.h.
util::LazyVector< T, A >::LazyVector | ( | size_type | n | ) |
Constructor: a lazy vector with a specified maximum size.
n | the initial maximum size of the vector |
The vector is set to a nominal size of n
, with no stored data.
The default value of vector elements is the default-constructed T
value, as returned by defaultValueType()
.
Definition at line 496 of file LazyVector.h.
util::LazyVector< T, A >::LazyVector | ( | size_type | n, |
value_type const & | defValue | ||
) |
Constructor: a lazy vector with a specified maximum size.
n | the initial maximum size of the vector |
defValue | value to be used when resizing |
The vector is set to a nominal size of n
, with no stored data. A default value defValue
is registered, so that it can be used when actual storage is needed for elements whose value is not explicitly specified by the user:
will print something like: "Default element [1]: 5".
Definition at line 503 of file LazyVector.h.
util::LazyVector< T, A >::value_type util::LazyVector< T, A >::at | ( | size_type | pos | ) | const |
Returns a reference to the specified element, throws an exception if not present.
pos | position to be accessed |
std::out_of_range | if the container is too small to contain pos |
Returns a copy of the specified element. If the requested element is beyond the size of the container, an exception std::out_of_range
is thrown.
Definition at line 529 of file LazyVector.h.
util::LazyVector< T, A >::reference util::LazyVector< T, A >::at | ( | size_type | pos | ) |
Returns a reference to the specified element, throws an exception if not present.
pos | position to be accessed |
std::out_of_range | if the container is too small to contain pos |
data_defvalue()
Returns a reference to the specified element. If the element is not stored yet, it's created with the default value, and returned. If the requested element is beyond the size of the container, an exception std::out_of_range
is thrown.
Definition at line 512 of file LazyVector.h.
|
private |
Throws std::out_of_range
if pos
is not contained in the vector.
Definition at line 717 of file LazyVector.h.
void util::LazyVector< T, A >::clear | ( | ) |
Removes all stored data and sets the nominal size to 0.
Definition at line 622 of file LazyVector.h.
|
inline |
Definition at line 245 of file LazyVector.h.
|
inline |
Definition at line 278 of file LazyVector.h.
util::LazyVector< T, A >::const_pointer util::LazyVector< T, A >::data_address | ( | size_type | pos | ) | const |
Returns a constant pointer to the specified element.
pos | position of the element |
nullptr
if not storedIf pos
represents an element that has storage, the pointer to that element is returned. If instead pos
represents a valid element with no storage (default value), nullptr
is returned. If pos
does not represent a valid element, the result is undefined.
Definition at line 584 of file LazyVector.h.
|
inline |
Index of the first data element in the storage (undefined if data_empty()
).
Definition at line 176 of file LazyVector.h.
|
private |
Erases all stored data from the container; nominal size is not changed.
Definition at line 709 of file LazyVector.h.
|
inline |
|
inlinenoexcept |
|
inline |
Index after the last data element in the storage (undefined if data_empty()
).
Definition at line 180 of file LazyVector.h.
|
inline |
Returns the internal storage index for the specified position.
Definition at line 184 of file LazyVector.h.
void util::LazyVector< T, A >::data_init | ( | size_type | startIndex, |
size_type | endIndex | ||
) |
Allocates the specified range and stores default values for it.
startIndex | index of the first element to be initialized |
endIndex | index after the last element to be initialized |
Each element in the range from startIndex
to endIndex
is stored and the default value is assigned to it.
Old data is lost.
resize()
, resize()
should be called first. Definition at line 647 of file LazyVector.h.
|
inline |
Allocates and initializes n
elements starting from index 0.
n | number of elements to be initialized |
Each element in the range from 0
to n
is stored and the default value is assigned to it. This is semantically similar to std::vector::resize(n, data_defvalue()
, except that this method does not change the nominal size of the vector.
Old data is lost.
resize()
, resize()
should be called first. Definition at line 422 of file LazyVector.h.
void util::LazyVector< T, A >::data_prepare | ( | size_type | startIndex, |
size_type | endIndex | ||
) |
Prepares the vector to store elements in the specified range.
startIndex | index of the first element to be stored |
endIndex | index after the last element to be stored |
data_prepare(size_type)
This method sets the lower index to startIndex
, and allocates enough storage to store the whole requested range. The elements are not initialized or constructed, but Following access to elements in the specified range will not cause memory reallocation, and that holds until an access outside that range happens, after which all bets are off.
Old data is lost.
resize()
, resize()
should be called first. Definition at line 631 of file LazyVector.h.
|
inline |
Prepares the vector to store n
elements from 0
.
n | number of elements to prepare storage for |
This method reserves storage for n
elements starting with the element #0.
Old data is lost.
See data_prepare(size_type, size_type)
for more information.
Definition at line 389 of file LazyVector.h.
|
inlinenoexcept |
|
inlinestaticprivate |
Returns the class default value (used when user does not specify any).
Definition at line 471 of file LazyVector.h.
|
inlinenoexcept |
|
private |
Expands the storage to include the specified position.
Definition at line 662 of file LazyVector.h.
|
private |
Expands the storage to include the specified position ahead of it.
Definition at line 692 of file LazyVector.h.
|
private |
Expands the storage to include the specified position behind it.
Definition at line 682 of file LazyVector.h.
|
private |
Makes sure the nominal size is large enough to include all stored data.
Definition at line 701 of file LazyVector.h.
|
inline |
Definition at line 296 of file LazyVector.h.
|
inlinenoexcept |
Returns whether the specified position is within the vector.
Definition at line 165 of file LazyVector.h.
|
inlineprivate |
Returns the internal storage index for the specified position.
Definition at line 446 of file LazyVector.h.
|
private |
Makes the first data allocation.
Definition at line 672 of file LazyVector.h.
util::LazyVector< T, A >::value_type util::LazyVector< T, A >::operator[] | ( | size_type | pos | ) | const |
Returns a copy of the specified element.
pos | position to be accessed |
at(size_type) const
Returns a copy of the specified element. If the requested element is beyond the size of the container, the result is undefined.
at(value_type) const
for an explanation of why a value is returned rather than a reference. Definition at line 565 of file LazyVector.h.
util::LazyVector< T, A >::reference util::LazyVector< T, A >::operator[] | ( | size_type | pos | ) |
Returns a reference to the specified element.
pos | position to be accessed |
data_defvalue()
Returns a reference to the specified element. If that element is not stored, it is allocated first; all missing elements, including the required one, are initialised by copying into them the default value stored at construction. Like for STL vector, this method does not expand the vector: if pos
is beyond the vector size, the result is undefined.
Definition at line 545 of file LazyVector.h.
|
inline |
Allocates enough memory in storage to store n
elements.
n | number of elements to have storage for |
data_prepare()
Storage allocation is resized to be able to host at least n
elements (it is not reduced). Note that the use of reserve()
for LazyVector
is more subtle than for a STL vector. The common use of reserve()
is to avoid reallocations when extending the vector. In this case, after a call to reserve(n)
the reallocation is avoided only as long as only the elements from data_begin_index()
to data_begin_index() + n
(excluded) are written.
Note that data_prepare()
has a similar purpose and might be more effective.
Definition at line 348 of file LazyVector.h.
void util::LazyVector< T, A >::resize | ( | size_type | newSize | ) |
Changes the nominal size of the container.
newSize | new container size |
The nominal size of the vector is set to newSize. Even when newSize
is larger than the current nominal size, no additional data is stored. If the new nominal size is smaller, actual data may be released (i.e. erased) so that no stored data is beyond the new nominal size.
Definition at line 602 of file LazyVector.h.
|
inline |
Reduces memory usage to the amount needed by the elements with storage.
Definition at line 354 of file LazyVector.h.
|
inlinenoexcept |
|
inlineprivate |
|
inlineprivate |
Definition at line 442 of file LazyVector.h.
|
private |
Actual data storage.
Definition at line 430 of file LazyVector.h.
|
staticprivate |
Default-initialised value of type value_type
used as default fallback.
Definition at line 437 of file LazyVector.h.
|
private |
Default value.
Definition at line 434 of file LazyVector.h.
|
private |
First element currently stored.
Definition at line 433 of file LazyVector.h.
|
private |
Alleged data size.
Definition at line 432 of file LazyVector.h.