LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
util::LazyVector< T, A > Class Template Reference

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 LazyVector and the ones of STL vector.

The constant versions of the access methods differ from the corresponding STL vector ones in that they return a copy of the element rather than a reference to it. This happens because the element could not have current storage, and we can't create it because the method is constant.

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: const_at() and const_get(), explicitly constant versions of at() and operator[] respectively. To preserve the naming scheme, a method get() is also provided which is equivalent to the non-constant version of operator[].

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_tstorage ()
 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...
 

Detailed Description

template<typename T, typename A = typename std::vector<T>::allocator_type>
class util::LazyVector< T, A >

A contiguous data container expanded on write.

Template Parameters
Ttype of contained data
Aallocator 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:

// start with a lazy vector of nominal size 6 elements and no actual data:
// add a value `-4.0` at the previous-to-last element `4`:
// the vector will look like: { ... [4] -4.0 ... }
// (nominal size: 6, 1 stored datum)
v[4] = -4.0;
// add a value `-2.0` at the third element:
// the vector will look like: { ... [2] -2.0, [3] def, [4] -4.0 ... }
// (nominal size still 6, 3 stored data, the default value "def" is 0.0)
v[2] = -2.0;
// we want to set element #6 to -6.0: we need to expand the vector first.
v.resize(7U); // barely enough for element #6
// the vector will look like: { ... [2] -2.0, [3] def, [4] -4.0 [5] def [6] -6.0 }
// (nominal size 7, 5 stored data, the default value "def" is 0.0)
v[6] = -6.0;
Note
Special care needs to be used when accessing a non-const 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.

Member Typedef Documentation

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::allocator_type = typename Data_t::allocator_type

Definition at line 88 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::const_pointer = typename Data_t::const_pointer

Definition at line 95 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::const_reference = typename Data_t::const_reference

Definition at line 93 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::Data_t = std::vector<T, A>
private

Actual data storage type.

Definition at line 80 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::difference_type = typename Data_t::difference_type

Definition at line 91 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::pointer = typename Data_t::pointer

Definition at line 94 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::reference = typename Data_t::reference

Definition at line 92 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::size_type = typename Data_t::size_type

Definition at line 90 of file LazyVector.h.

template<typename T, typename A = typename std::vector<T>::allocator_type>
using util::LazyVector< T, A >::value_type = typename Data_t::value_type

Definition at line 89 of file LazyVector.h.

Constructor & Destructor Documentation

template<typename T, typename A = typename std::vector<T>::allocator_type>
util::LazyVector< T, A >::LazyVector ( )
default

— BEGIN Constructors ----------------------------------------------— Default constructor: an empty vector.

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

416  : fData(a)
417  {}
Data_t fData
Actual data storage.
Definition: LazyVector.h:356
template<typename T , typename A >
util::LazyVector< T, A >::LazyVector ( size_type  n)

Constructor: a lazy vector with a specified maximum size.

Parameters
nthe initial maximum size of the vector
Note
This constructor is essentially different from the one of STL vector with the same signature.

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.

424  {}
static value_type const & defaultValueType()
Returns the class default value (used when user does not specify any).
Definition: LazyVector.h:397
LazyVector()=default
Char_t n[5]
template<typename T , typename A >
util::LazyVector< T, A >::LazyVector ( size_type  n,
value_type const &  defValue 
)

Constructor: a lazy vector with a specified maximum size.

Parameters
nthe initial maximum size of the vector
defValuevalue to be used when resizing
Note
This constructor is essentially different from the one of STL vector with the same signature.

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:

v[0] = 0;
v[2] = -2;
std::cout << "Default element [1]: " << v.at(1) << std::endl;

will print something like: "Default element [1]: 5".

Definition at line 429 of file LazyVector.h.

References util::LazyVector< T, A >::at().

430  : fNominalSize(n)
431  , fDefValue(defValue)
432  {}
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
Char_t n[5]
value_type fDefValue
Default value.
Definition: LazyVector.h:360

Member Function Documentation

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

Parameters
posposition to be accessed
Returns
a copy of the value of requested element
Exceptions
std::out_of_rangeif 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.

Note
This method differs from the corresponding STL vector in that it returns a copy of the element rather than a reference to it. This happens because the element could not have current storage, and we can't create it because the method is constant.

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().

456 {
457  /*
458  * Behaviour summary:
459  * * if `pos` is out of vector range, throw an exception
460  * * if element at `pos` has no storage, return a copy of the default value
461  * * otherwise, return a copy of the element at `pos`
462  */
463  check_range(pos); // verify that `pos` is valid, throw otherwise
464  return data_has_index(pos)? storage()[index_of(pos)]: data_defvalue();
465 } // util::LazyVector<T,A>::at() const
void check_range(size_type pos) const
Throws std::out_of_range if pos is not contained in the vector.
Definition: LazyVector.h:611
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
value_type const & data_defvalue() const
Definition: LazyVector.h:172
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
bool data_has_index(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:184
template<typename T , typename A >
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.

Parameters
posposition to be accessed
Returns
the reference to the requested element
Exceptions
std::out_of_rangeif the container is too small to contain pos
See also
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().

439 {
440  /*
441  * Behaviour summary:
442  * * if `pos` is out of vector range, throw an exception
443  * * if element at `pos` has no storage, create storage for it
444  * * return a reference to the element at `pos`
445  */
446  check_range(pos); // verify that `pos` is valid, throw otherwise
447  expand(pos);
448  return storage()[index_of(pos)]; // we already know it's valid
449 }
void check_range(size_type pos) const
Throws std::out_of_range if pos is not contained in the vector.
Definition: LazyVector.h:611
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
void expand(size_type pos)
Expands the storage to include the specified position.
Definition: LazyVector.h:556
template<typename T , typename A >
void util::LazyVector< T, A >::check_range ( size_type  pos) const
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().

611  {
612  if (has_index(pos)) return;
613  throw std::out_of_range(
614  "Index " + std::to_string(pos) + " is out of LazyVector range (size: "
615  + std::to_string(size()) + ")"
616  );
617 } // util::LazyVector<T,A>::check_range()
bool has_index(size_type pos) const noexcept
Returns whether the specified position is within the vector.
Definition: LazyVector.h:165
size_type size() const noexcept
Returns the size of the vector.
Definition: LazyVector.h:156
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
template<typename T , typename A >
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().

548  {
549  data_clear();
550  fNominalSize = 0U;
551 } // util::LazyVector<T,A>::clear()
void data_clear()
Erases all stored data from the container; nominal size is not changed.
Definition: LazyVector.h:603
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
template<typename T, typename A = typename std::vector<T>::allocator_type>
value_type util::LazyVector< T, A >::const_at ( size_type  pos) const
inline

Returns a reference to the specified element, throws an exception if not present.

Parameters
posposition to be accessed
Returns
a copy of the value of requested element
Exceptions
std::out_of_rangeif 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.

Note
This method differs from the corresponding STL vector in that it returns a copy of the element rather than a reference to it. This happens because the element could not have current storage, and we can't create it because the method is constant.

Definition at line 245 of file LazyVector.h.

245 { return at(pos); }
value_type at(size_type pos) const
Returns a reference to the specified element, throws an exception if not present. ...
Definition: LazyVector.h:455
template<typename T, typename A = typename std::vector<T>::allocator_type>
value_type util::LazyVector< T, A >::const_get ( size_type  pos) const
inline

Returns a copy of the specified element.

Parameters
posposition to be accessed
Returns
a copy of the specified element
See also
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.

Note
See 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.

278 { return this->operator[](pos); }
value_type operator[](size_type pos) const
Returns a copy of the specified element.
Definition: LazyVector.h:491
template<typename T , typename A >
util::LazyVector< T, A >::const_pointer util::LazyVector< T, A >::data_address ( size_type  pos) const

Returns a constant pointer to the specified element.

Parameters
posposition of the element
Returns
pointer to storage for specified element, nullptr if not stored

If 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().

511 {
512  /*
513  * Behaviour summary:
514  * * if `pos` is out of vector range, behaviour is undefined
515  * * if element at `pos` has no storage, return nullptr
516  * * otherwise, return the pointer to the specified element
517  */
518  // this implementation will return nullptr if `pos` is out of range;
519  // this is not a requirement, and may change at any time.
520  if (pos < data_begin_index()) return nullptr;
521  auto const index = index_of(pos);
522  return (index < data_size())? storage().data() + index: nullptr;
523 } // util::LazyVector<T,A>::data_address()
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
size_type data_begin_index() const
Definition: LazyVector.h:176
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type data_size() const noexcept
Returns the size of data actually stored.
Definition: LazyVector.h:162
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::data_begin_index ( ) const
inline
template<typename T , typename A >
void util::LazyVector< T, A >::data_clear ( )
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().

603  {
604  storage().clear();
605  fFirstIndex = storage().max_size();
606 } // util::LazyVector<T,A>::data_clear()
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type fFirstIndex
First element currently stored.
Definition: LazyVector.h:359
template<typename T, typename A = typename std::vector<T>::allocator_type>
value_type const& util::LazyVector< T, A >::data_defvalue ( ) const
inline
template<typename T, typename A = typename std::vector<T>::allocator_type>
bool util::LazyVector< T, A >::data_empty ( ) const
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().

168 { return fData.empty(); }
Data_t fData
Actual data storage.
Definition: LazyVector.h:356
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::data_end_index ( ) const
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().

181  { return data_begin_index() + data_size(); }
size_type data_begin_index() const
Definition: LazyVector.h:176
size_type data_size() const noexcept
Returns the size of data actually stored.
Definition: LazyVector.h:162
template<typename T, typename A = typename std::vector<T>::allocator_type>
bool util::LazyVector< T, A >::data_has_index ( size_type  pos) const
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[]().

185  { return (pos >= data_begin_index()) && (pos < data_end_index()); }
size_type data_begin_index() const
Definition: LazyVector.h:176
size_type data_end_index() const
Definition: LazyVector.h:180
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::data_size ( ) const
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[]().

162 { return fData.size(); }
Data_t fData
Actual data storage.
Definition: LazyVector.h:356
template<typename T, typename A = typename std::vector<T>::allocator_type>
static value_type const& util::LazyVector< T, A >::defaultValueType ( )
inlinestaticprivate

Returns the class default value (used when user does not specify any).

Definition at line 397 of file LazyVector.h.

397 { return fDefaultDefaultValue; }
static value_type const fDefaultDefaultValue
Default-initialised value of type value_type used as default fallback.
Definition: LazyVector.h:363
template<typename T, typename A = typename std::vector<T>::allocator_type>
bool util::LazyVector< T, A >::empty ( ) const
inlinenoexcept

Returns whether the vector is empty.

Definition at line 159 of file LazyVector.h.

159 { return fNominalSize == 0U; }
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
template<typename T , typename A >
void util::LazyVector< T, A >::expand ( size_type  pos)
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[]().

556  {
557  // this is just a dispatcher
558  if (data_empty()) init(pos);
559  else if (pos < data_begin_index()) expand_front(pos);
560  else if (pos >= data_end_index()) expand_back(pos);
561 }
void expand_front(size_type pos)
Expands the storage to include the specified position behind it.
Definition: LazyVector.h:576
bool data_empty() const noexcept
Returns whether no data is actually stored.
Definition: LazyVector.h:168
size_type data_begin_index() const
Definition: LazyVector.h:176
void expand_back(size_type pos)
Expands the storage to include the specified position ahead of it.
Definition: LazyVector.h:586
void init(size_type pos, size_type n=1U)
Makes the first data allocation.
Definition: LazyVector.h:566
size_type data_end_index() const
Definition: LazyVector.h:180
template<typename T , typename A >
void util::LazyVector< T, A >::expand_back ( size_type  pos)
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().

586  {
587  assert(pos >= data_end_index());
588  storage().resize(pos + 1U - data_begin_index(), data_defvalue());
589  fix_size();
590 }
value_type const & data_defvalue() const
Definition: LazyVector.h:172
void fix_size()
Makes sure the nominal size is large enough to include all stored data.
Definition: LazyVector.h:595
size_type data_begin_index() const
Definition: LazyVector.h:176
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type data_end_index() const
Definition: LazyVector.h:180
template<typename T , typename A >
void util::LazyVector< T, A >::expand_front ( size_type  pos)
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().

576  {
577  assert(pos < data_begin_index());
578  storage().insert
579  (storage().begin(), data_begin_index() - pos, data_defvalue());
580  fFirstIndex = pos;
581 }
value_type const & data_defvalue() const
Definition: LazyVector.h:172
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
size_type data_begin_index() const
Definition: LazyVector.h:176
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type fFirstIndex
First element currently stored.
Definition: LazyVector.h:359
template<typename T , typename A >
void util::LazyVector< T, A >::fix_size ( )
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().

595  {
596  auto const min_size = data_end_index();
597  if (fNominalSize < min_size) fNominalSize = min_size;
598 }
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
size_type data_end_index() const
Definition: LazyVector.h:180
template<typename T, typename A = typename std::vector<T>::allocator_type>
reference util::LazyVector< T, A >::get ( size_type  pos)
inline

Returns a reference to the specified element.

Parameters
posposition to be accessed
Returns
a reference to the specified element
See also
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.

296 { return this->operator[](pos); }
value_type operator[](size_type pos) const
Returns a copy of the specified element.
Definition: LazyVector.h:491
template<typename T, typename A = typename std::vector<T>::allocator_type>
bool util::LazyVector< T, A >::has_index ( size_type  pos) const
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[]().

165 { return pos < size(); }
size_type size() const noexcept
Returns the size of the vector.
Definition: LazyVector.h:156
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::index_of ( size_type  pos) const
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().

372 { return pos - fFirstIndex; }
size_type fFirstIndex
First element currently stored.
Definition: LazyVector.h:359
template<typename T , typename A >
void util::LazyVector< T, A >::init ( size_type  pos,
size_type  n = 1U 
)
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().

566  {
567  assert(data_empty());
568  storage().assign(n, data_defvalue());
569  fFirstIndex = pos;
570  fix_size();
571 }
value_type const & data_defvalue() const
Definition: LazyVector.h:172
void fix_size()
Makes sure the nominal size is large enough to include all stored data.
Definition: LazyVector.h:595
bool data_empty() const noexcept
Returns whether no data is actually stored.
Definition: LazyVector.h:168
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type fFirstIndex
First element currently stored.
Definition: LazyVector.h:359
Char_t n[5]
template<typename T , typename A >
util::LazyVector< T, A >::value_type util::LazyVector< T, A >::operator[] ( size_type  pos) const

Returns a copy of the specified element.

Parameters
posposition to be accessed
Returns
a copy of the specified element
See also
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.

Note
See 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().

492 {
493  /*
494  * Behaviour summary:
495  * * if `pos` is out of vector range, behaviour is undefined
496  * * if element at `pos` has no storage, return a copy of the default value
497  * * otherwise, return a copy of the element at `pos`
498  */
499  // this implementation will return a default value if `pos` is out of range;
500  // this is not a requirement, and may change at any time.
501  if (pos < data_begin_index()) return data_defvalue();
502  auto const index = index_of(pos);
503  return (index < data_size())? storage()[index]: data_defvalue();
504 } // util::LazyVector<T,A>::operator[] () const
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
value_type const & data_defvalue() const
Definition: LazyVector.h:172
size_type data_begin_index() const
Definition: LazyVector.h:176
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type data_size() const noexcept
Returns the size of data actually stored.
Definition: LazyVector.h:162
template<typename T , typename A >
util::LazyVector< T, A >::reference util::LazyVector< T, A >::operator[] ( size_type  pos)

Returns a reference to the specified element.

Parameters
posposition to be accessed
Returns
a reference to the specified element
See also
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().

472 {
473  /*
474  * Behaviour summary:
475  * * if `pos` is out of vector range, behaviour is undefined
476  * * if element at `pos` has no storage, create storage for it
477  * * return a reference to the element at `pos`
478  */
479  // to have the common case where the requested position has storage be handled
480  // the fastest, we "enforce" the order by nesting (optimiser has last word)
481  if (!data_has_index(pos)) {
482  if (has_index(pos)) expand(pos);
483  }
484  return storage()[index_of(pos)];
485 } // util::LazyVector<T,A>::operator[]()
bool has_index(size_type pos) const noexcept
Returns whether the specified position is within the vector.
Definition: LazyVector.h:165
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
void expand(size_type pos)
Expands the storage to include the specified position.
Definition: LazyVector.h:556
bool data_has_index(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:184
template<typename T, typename A = typename std::vector<T>::allocator_type>
void util::LazyVector< T, A >::reserve ( size_type  n)
inline

Allocates enough memory in storage to store n elements.

Parameters
nnumber 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().

344 { storage().reserve(n); }
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
Char_t n[5]
template<typename T , typename A >
void util::LazyVector< T, A >::resize ( size_type  newSize)

Changes the nominal size of the container.

Parameters
newSizenew 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.

Note
It is not possible to specify a filling value: the "default" value specified on construction is always used.

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().

528  {
529  /*
530  * Behaviour summary:
531  * * when extending, do not change storage
532  * * when shrinking, cut the excess storage
533  */
534  fNominalSize = newSize;
535  // delete any excess data
536  if (data_end_index() > newSize) {
537  if (fNominalSize <= data_begin_index()) data_clear(); // no data is left
538  else {
539  storage().erase
541  }
542  }
543 } // util::LazyVector<T,A>::resize()
void data_clear()
Erases all stored data from the container; nominal size is not changed.
Definition: LazyVector.h:603
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
Definition: LazyVector.h:372
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
size_type data_begin_index() const
Definition: LazyVector.h:176
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
size_type data_end_index() const
Definition: LazyVector.h:180
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
template<typename T, typename A = typename std::vector<T>::allocator_type>
void util::LazyVector< T, A >::shrink_to_fit ( )
inline

Reduces memory usage to the amount needed by the elements with storage.

Definition at line 350 of file LazyVector.h.

350 { storage().shrink_to_fit(); }
Data_t & storage()
Returns the data storage.
Definition: LazyVector.h:367
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::size ( ) const
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().

156 { return fNominalSize; }
size_type fNominalSize
Alleged data size.
Definition: LazyVector.h:358
template<typename T, typename A = typename std::vector<T>::allocator_type>
Data_t const& util::LazyVector< T, A >::storage ( ) const
inlineprivate

Returns the data storage.

Definition at line 368 of file LazyVector.h.

368 { return fData; }
Data_t fData
Actual data storage.
Definition: LazyVector.h:356

Member Data Documentation

template<typename T, typename A = typename std::vector<T>::allocator_type>
Data_t util::LazyVector< T, A >::fData
private
template<typename T, typename A = typename std::vector<T>::allocator_type>
util::LazyVector< T, A >::value_type const util::LazyVector< T, A >::fDefaultDefaultValue {}
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().

template<typename T, typename A = typename std::vector<T>::allocator_type>
value_type util::LazyVector< T, A >::fDefValue = defaultValueType()
private

Default value.

Definition at line 360 of file LazyVector.h.

Referenced by util::LazyVector< TF1 >::data_defvalue().

template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::fFirstIndex = fData.max_size()
private
template<typename T, typename A = typename std::vector<T>::allocator_type>
size_type util::LazyVector< T, A >::fNominalSize = 0U
private

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