11 #ifndef LARDATAOBJ_UTILITIES_LAZYVECTOR_H
12 #define LARDATAOBJ_UTILITIES_LAZYVECTOR_H
77 template <typename T, typename A = typename std::vector<T>::allocator_type>
482 template <
typename T,
typename A >
488 template <
typename T,
typename A >
495 template <
typename T,
typename A >
502 template <
typename T,
typename A >
505 , fDefValue(defValue)
510 template <
typename T,
typename A >
522 return storage()[index_of(pos)];
527 template <
typename T,
typename A >
538 return data_has_index(pos)? storage()[index_of(pos)]: data_defvalue();
543 template <
typename T,
typename A >
555 if (!data_has_index(pos)) {
556 if (has_index(pos)) expand(pos);
558 return storage()[index_of(pos)];
563 template <
typename T,
typename A >
575 if (pos < data_begin_index())
return data_defvalue();
576 auto const index = index_of(pos);
577 return (index < data_size())? storage()[index]: data_defvalue();
582 template <
typename T,
typename A >
594 if (pos < data_begin_index())
return nullptr;
595 auto const index = index_of(pos);
596 return (index < data_size())? storage().data() + index:
nullptr;
601 template <
typename T,
typename A >
608 fNominalSize = newSize;
610 if (data_end_index() > newSize) {
611 if (fNominalSize <= data_begin_index()) data_clear();
614 (storage().
begin() + index_of(fNominalSize), storage().
end());
621 template <
typename T,
typename A >
629 template <
typename T,
typename A >
635 if (startIndex >= e)
return;
638 storage().reserve(e - startIndex);
639 fFirstIndex = startIndex;
645 template <
typename T,
typename A >
651 if (startIndex >= e)
return;
654 storage().resize(e - startIndex, data_defvalue());
655 fFirstIndex = startIndex;
661 template <
typename T,
typename A >
664 if (data_empty()) init(pos);
665 else if (pos < data_begin_index()) expand_front(pos);
666 else if (pos >= data_end_index()) expand_back(pos);
671 template <
typename T,
typename A >
673 assert(data_empty());
674 storage().assign(n, data_defvalue());
681 template <
typename T,
typename A >
683 assert(pos < data_begin_index());
685 (storage().
begin(), data_begin_index() - pos, data_defvalue());
691 template <
typename T,
typename A >
693 assert(pos >= data_end_index());
694 storage().resize(pos + 1U - data_begin_index(), data_defvalue());
700 template <
typename T,
typename A >
702 auto const min_size = data_end_index();
703 if (fNominalSize < min_size) fNominalSize = min_size;
708 template <
typename T,
typename A >
711 fFirstIndex = storage().max_size();
716 template <
typename T,
typename A >
718 if (has_index(pos))
return;
719 throw std::out_of_range(
720 "Index " +
std::to_string(pos) +
" is out of LazyVector range (size: "
728 #endif // LARDATAOBJ_UTILITIES_LAZYVECTOR_H
const_pointer data_address(size_type pos) const
Returns a constant pointer to the specified element.
value_type operator[](size_type pos) const
Returns a copy of the specified element.
size_type data_size() const noexcept
Returns the size of data actually stored.
static value_type const & defaultValueType()
Returns the class default value (used when user does not specify any).
std::vector< TF1, typename std::vector< TF1 >::allocator_type > Data_t
Actual data storage type.
void data_prepare(size_type startIndex, size_type endIndex)
Prepares the vector to store elements in the specified range.
value_type const_get(size_type pos) const
void data_clear()
Erases all stored data from the container; nominal size is not changed.
typename Data_t::reference reference
size_type size() const noexcept
Returns the size of the vector.
void data_init(size_type startIndex, size_type endIndex)
Allocates the specified range and stores default values for it.
void clear()
Removes all stored data and sets the nominal size to 0.
void check_range(size_type pos) const
Throws std::out_of_range if pos is not contained in the vector.
size_type index_of(size_type pos) const
Returns the internal storage index for the specified position.
void reserve(size_type n)
Allocates enough memory in storage to store n elements.
void expand_front(size_type pos)
Expands the storage to include the specified position behind it.
void shrink_to_fit()
Reduces memory usage to the amount needed by the elements with storage.
value_type const & data_defvalue() const
void fix_size()
Makes sure the nominal size is large enough to include all stored data.
static value_type const fDefaultDefaultValue
Default-initialised value of type value_type used as default fallback.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
typename Data_t::size_type size_type
Data_t fData
Actual data storage.
size_type data_begin_index() const
bool has_index(size_type pos) const noexcept
Returns whether the specified position is within the vector.
Data_t & storage()
Returns the data storage.
typename Data_t::allocator_type allocator_type
void expand_back(size_type pos)
Expands the storage to include the specified position ahead of it.
typename Data_t::const_pointer const_pointer
void data_prepare(size_type n)
Prepares the vector to store n elements from 0.
value_type at(size_type pos) const
Returns a reference to the specified element, throws an exception if not present. ...
Data_t const & storage() const
typename Data_t::difference_type difference_type
size_type fNominalSize
Alleged data size.
void data_init(size_type n)
Allocates and initializes n elements starting from index 0.
value_type const_at(size_type pos) const
bool data_empty() const noexcept
Returns whether no data is actually stored.
void init(size_type pos, size_type n=1U)
Makes the first data allocation.
size_type fFirstIndex
First element currently stored.
size_type data_end_index() const
std::string to_string(WindowPattern const &pattern)
typename Data_t::value_type value_type
typename Data_t::pointer pointer
bool empty() const noexcept
Returns whether the vector is empty.
void expand(size_type pos)
Expands the storage to include the specified position.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
A contiguous data container expanded on write.
void resize(size_type newSize)
Changes the nominal size of the container.
bool data_has_index(size_type pos) const
Returns the internal storage index for the specified position.
typename Data_t::const_reference const_reference
value_type fDefValue
Default value.