#include <gtopt/collection.hpp>
template<CopyMove Type, typename VectorType = std::vector<Type>, typename IndexType = ElementIndex<Type>>
Collection class
A container for managing typed elements with efficient lookup by name, UID, or index.
| Template parameters | |
|---|---|
| Type | The element type stored in the collection (must satisfy CopyMove concept) |
| VectorType | The container type used for storage (defaults to std::vector) |
| IndexType | The strongly-typed index used for safe element access |
The Collection class provides a central repository for elements of a specific type, with optimized access through multiple lookup methods. It enforces unique UIDs and names across all contained elements.
Public types
- using value_type = element_type
Constructors, destructors, conversion operators
- Collection() defaulted
- Collection(Collection&&) defaulted noexcept
- Collection(const Collection&) defaulted
- Collection(vector_t pelements) explicit
- ~Collection() defaulted
Public functions
- void build_maps()
-
template<typename Self, typename ID>auto element(this Self&& self, const ID& id) -> auto&&
- Get a reference to an element by its ID.
-
template<typename ID>auto element_index(const ID& id) const -> auto
- Get the index of an element by its ID (name, UID, or compound ID).
-
template<typename Self>auto elements(this Self&& self) -> auto&& noexcept
- Get a reference to the underlying element vector (mutable version).
- auto empty() const -> auto noexcept
- auto operator=(Collection&&) -> Collection& defaulted noexcept
- auto operator=(const Collection&) -> Collection& defaulted
-
template<typename EType>auto push_back(EType&& element) -> auto
- auto size() const -> auto noexcept
- Get the number of elements in the collection.
Typedef documentation
template<CopyMove Type, typename VectorType, typename IndexType>
using gtopt:: Collection<Type, VectorType, IndexType>:: value_type = element_type
Public alias of the element type — used by tuple-iterating helpers (e.g. SystemLP::bind_reservoir_caches) that need to detect, at compile time, which collections hold elements that satisfy a concept (HasUpdateLP, HasBindReservoirCaches).
Function documentation
template<CopyMove Type, typename VectorType, typename IndexType>
gtopt:: Collection<Type, VectorType, IndexType>:: Collection(Collection&&) defaulted noexcept
Special member functions: moves are noexcept so containers of Collections relocate instead of copying.
template<CopyMove Type, typename VectorType, typename IndexType>
gtopt:: Collection<Type, VectorType, IndexType>:: Collection(vector_t pelements) explicit
| Parameters | |
|---|---|
| pelements | Vector containing the elements to store |
Constructor that takes ownership of an existing vector. Uses move semantics to avoid copying the elements.
template<CopyMove Type, typename VectorType, typename IndexType>
void gtopt:: Collection<Type, VectorType, IndexType>:: build_maps()
Builds the mapping structures for efficient element lookup by name or UID. Uses move semantics to efficiently transfer ownership from temporary maps to member maps once they're fully constructed.
template<CopyMove Type, typename VectorType, typename IndexType>
template<typename Self, typename ID>
auto&& gtopt:: Collection<Type, VectorType, IndexType>:: element(this Self&& self,
const ID& id)
Get a reference to an element by its ID.
| Parameters | |
|---|---|
| self | The collection object (deduced; supports const and non-const). |
| id | The identifier to lookup |
| Returns | A reference to the requested element |
| Exceptions | |
| std::out_of_range | if the element is not found |
template<CopyMove Type, typename VectorType, typename IndexType>
template<typename ID>
auto gtopt:: Collection<Type, VectorType, IndexType>:: element_index(const ID& id) const
Get the index of an element by its ID (name, UID, or compound ID).
| Parameters | |
|---|---|
| id | The identifier to lookup |
| Returns | A strongly-typed index to the element |
| Exceptions | |
| std::out_of_range | if the element is not found |
template<CopyMove Type, typename VectorType, typename IndexType>
template<typename Self>
auto&& gtopt:: Collection<Type, VectorType, IndexType>:: elements(this Self&& self) noexcept
Get a reference to the underlying element vector (mutable version).
| Returns | A mutable reference to the element vector |
|---|
template<CopyMove Type, typename VectorType, typename IndexType>
template<typename EType>
auto gtopt:: Collection<Type, VectorType, IndexType>:: push_back(EType&& element)
| Parameters | |
|---|---|
| element | The element to add (can be lvalue or rvalue) |
| Returns | The index of the newly added element |
Adds an element to the collection using perfect forwarding. The template parameter allows both lvalue and rvalue references to be passed efficiently without unnecessary copies.
template<CopyMove Type, typename VectorType, typename IndexType>
auto gtopt:: Collection<Type, VectorType, IndexType>:: size() const noexcept
Get the number of elements in the collection.
| Returns | The element count |
|---|