LArSoft
v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
|
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 |
Returns a reference to the specified element, throws an exception if not present. More... | |
value_type | operator[] (size_type pos) const |
Returns a copy of the specified element. More... | |
value_type | const_get (size_type pos) const |
Returns a copy of the specified element. More... | |
reference | operator[] (size_type pos) |
Returns a reference to the specified element. More... | |
reference | get (size_type pos) |
Returns a reference to the specified element. More... | |
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... | |
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 |
Returns the data storage. More... | |
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 415 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 422 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 429 of file LazyVector.h.
References util::LazyVector< T, A >::at().
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 455 of file LazyVector.h.
References util::LazyVector< T, A >::check_range(), util::LazyVector< T, A >::data_defvalue(), util::LazyVector< T, A >::data_has_index(), util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< T, A >::at(), util::LazyVector< TF1 >::const_at(), util::LazyVector< TF1 >::data_has_index(), and util::LazyVector< T, A >::LazyVector().
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 438 of file LazyVector.h.
References util::LazyVector< T, A >::at(), util::LazyVector< T, A >::check_range(), util::LazyVector< T, A >::expand(), util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
|
private |
Throws std::out_of_range
if pos
is not contained in the vector.
Definition at line 611 of file LazyVector.h.
References util::LazyVector< T, A >::has_index(), util::LazyVector< T, A >::size(), and util::flags::to_string().
Referenced by util::LazyVector< T, A >::at(), and util::LazyVector< TF1 >::index_of().
void util::LazyVector< T, A >::clear | ( | ) |
Removes all stored data and sets the nominal size to 0.
Definition at line 548 of file LazyVector.h.
References util::LazyVector< T, A >::data_clear(), and util::LazyVector< T, A >::fNominalSize.
Referenced by phot::PhotonLibrary::CreateEmptyLibrary(), phot::PhotonLibrary::LoadLibraryFromFile(), and util::LazyVector< TF1 >::reserve().
|
inline |
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 245 of file LazyVector.h.
|
inline |
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 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 510 of file LazyVector.h.
References util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_size(), util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< TF1 >::data_has_index(), phot::PhotonLibrary::GetCounts(), phot::PhotonLibrary::GetReflCounts(), phot::PhotonLibrary::GetReflT0s(), phot::PhotonLibrary::GetTimingPars(), phot::PhotonLibrary::GetTimingTF1s(), and phot::PhotonLibrary::uncheckedAccessTimingTF1().
|
inline |
Index of the first data element in the storage (undefined if data_empty()
).
Definition at line 176 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::data_address(), util::LazyVector< TF1 >::data_end_index(), util::LazyVector< TF1 >::data_has_index(), util::LazyVector< T, A >::expand(), util::LazyVector< T, A >::expand_back(), util::LazyVector< T, A >::expand_front(), util::LazyVector< T, A >::operator[](), and util::LazyVector< T, A >::resize().
|
private |
Erases all stored data from the container; nominal size is not changed.
Definition at line 603 of file LazyVector.h.
References util::LazyVector< T, A >::fFirstIndex, and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< T, A >::clear(), util::LazyVector< TF1 >::index_of(), and util::LazyVector< T, A >::resize().
|
inline |
Returns the default value.
Definition at line 172 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::at(), util::LazyVector< T, A >::expand_back(), util::LazyVector< T, A >::expand_front(), util::LazyVector< T, A >::init(), and util::LazyVector< T, A >::operator[]().
|
inlinenoexcept |
Returns whether no data is actually stored.
Definition at line 168 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::expand(), and util::LazyVector< T, A >::init().
|
inline |
Index after the last data element in the storage (undefined if data_empty()
).
Definition at line 180 of file LazyVector.h.
Referenced by util::LazyVector< TF1 >::data_has_index(), util::LazyVector< T, A >::expand(), util::LazyVector< T, A >::expand_back(), util::LazyVector< T, A >::fix_size(), and util::LazyVector< T, A >::resize().
|
inline |
Returns the internal storage index for the specified position.
Definition at line 184 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::at(), and util::LazyVector< T, A >::operator[]().
|
inlinenoexcept |
Returns the size of data actually stored.
Definition at line 162 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::data_address(), util::LazyVector< TF1 >::data_end_index(), and util::LazyVector< T, A >::operator[]().
|
inlinestaticprivate |
Returns the class default value (used when user does not specify any).
Definition at line 397 of file LazyVector.h.
|
inlinenoexcept |
|
private |
Expands the storage to include the specified position.
Definition at line 556 of file LazyVector.h.
References util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_empty(), util::LazyVector< T, A >::data_end_index(), util::LazyVector< T, A >::expand_back(), util::LazyVector< T, A >::expand_front(), and util::LazyVector< T, A >::init().
Referenced by util::LazyVector< T, A >::at(), util::LazyVector< TF1 >::index_of(), and util::LazyVector< T, A >::operator[]().
|
private |
Expands the storage to include the specified position ahead of it.
Definition at line 586 of file LazyVector.h.
References util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_defvalue(), util::LazyVector< T, A >::data_end_index(), util::LazyVector< T, A >::fix_size(), and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< T, A >::expand(), and util::LazyVector< TF1 >::index_of().
|
private |
Expands the storage to include the specified position behind it.
Definition at line 576 of file LazyVector.h.
References evd::details::begin(), util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_defvalue(), util::LazyVector< T, A >::fFirstIndex, and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< T, A >::expand(), and util::LazyVector< TF1 >::index_of().
|
private |
Makes sure the nominal size is large enough to include all stored data.
Definition at line 595 of file LazyVector.h.
References util::LazyVector< T, A >::data_end_index(), and util::LazyVector< T, A >::fNominalSize.
Referenced by util::LazyVector< T, A >::expand_back(), util::LazyVector< TF1 >::index_of(), and util::LazyVector< T, A >::init().
|
inline |
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 296 of file LazyVector.h.
|
inlinenoexcept |
Returns whether the specified position is within the vector.
Definition at line 165 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::check_range(), and util::LazyVector< T, A >::operator[]().
|
inlineprivate |
Returns the internal storage index for the specified position.
Definition at line 372 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::at(), util::LazyVector< T, A >::data_address(), util::LazyVector< T, A >::operator[](), and util::LazyVector< T, A >::resize().
|
private |
Makes the first data allocation.
Definition at line 566 of file LazyVector.h.
References util::LazyVector< T, A >::data_defvalue(), util::LazyVector< T, A >::data_empty(), util::LazyVector< T, A >::fFirstIndex, util::LazyVector< T, A >::fix_size(), and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< T, A >::expand(), and util::LazyVector< TF1 >::index_of().
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 491 of file LazyVector.h.
References util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_defvalue(), util::LazyVector< T, A >::data_size(), util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
Referenced by util::LazyVector< TF1 >::const_at(), util::LazyVector< TF1 >::const_get(), and util::LazyVector< TF1 >::get().
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 471 of file LazyVector.h.
References util::LazyVector< T, A >::data_has_index(), util::LazyVector< T, A >::expand(), util::LazyVector< T, A >::has_index(), util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
|
inline |
Allocates enough memory in storage to store n
elements.
n | number of elements to have storage for |
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.
Definition at line 344 of file LazyVector.h.
Referenced by phot::PhotonLibrary::LoadLibraryFromFile().
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 528 of file LazyVector.h.
References evd::details::begin(), util::LazyVector< T, A >::data_begin_index(), util::LazyVector< T, A >::data_clear(), util::LazyVector< T, A >::data_end_index(), evd::details::end(), util::LazyVector< T, A >::fNominalSize, util::LazyVector< T, A >::index_of(), and util::LazyVector< T, A >::storage().
Referenced by phot::PhotonLibrary::CreateEmptyLibrary(), util::LazyVector< TF1 >::get(), and phot::PhotonLibrary::LoadLibraryFromFile().
|
inline |
Reduces memory usage to the amount needed by the elements with storage.
Definition at line 350 of file LazyVector.h.
|
inlinenoexcept |
Returns the size of the vector.
Definition at line 156 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::check_range(), util::LazyVector< TF1 >::has_index(), and phot::PhotonLibrary::StoreLibraryToFile().
|
inlineprivate |
Returns the data storage.
Definition at line 367 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::at(), util::LazyVector< T, A >::data_address(), util::LazyVector< T, A >::data_clear(), util::LazyVector< T, A >::expand_back(), util::LazyVector< T, A >::expand_front(), util::LazyVector< T, A >::init(), util::LazyVector< T, A >::operator[](), util::LazyVector< TF1 >::reserve(), util::LazyVector< T, A >::resize(), and util::LazyVector< TF1 >::shrink_to_fit().
|
inlineprivate |
|
private |
Actual data storage.
Definition at line 356 of file LazyVector.h.
Referenced by util::LazyVector< TF1 >::data_empty(), util::LazyVector< TF1 >::data_size(), and util::LazyVector< TF1 >::storage().
|
staticprivate |
Default-initialised value of type value_type
used as default fallback.
Definition at line 363 of file LazyVector.h.
Referenced by util::LazyVector< TF1 >::defaultValueType().
|
private |
Default value.
Definition at line 360 of file LazyVector.h.
Referenced by util::LazyVector< TF1 >::data_defvalue().
|
private |
First element currently stored.
Definition at line 359 of file LazyVector.h.
Referenced by util::LazyVector< TF1 >::data_begin_index(), util::LazyVector< T, A >::data_clear(), util::LazyVector< T, A >::expand_front(), util::LazyVector< TF1 >::index_of(), and util::LazyVector< T, A >::init().
|
private |
Alleged data size.
Definition at line 358 of file LazyVector.h.
Referenced by util::LazyVector< T, A >::clear(), util::LazyVector< TF1 >::empty(), util::LazyVector< T, A >::fix_size(), util::LazyVector< T, A >::resize(), and util::LazyVector< TF1 >::size().