Template Class ResourceLimitedVector
Defined in File ResourceLimitedVector.hpp
Class Documentation
-
template<typename _Ty, typename _KeepOrderEnabler = std::false_type, typename _LimitsConfig = ResourceLimitedContainerConfig, typename _Alloc = std::allocator<_Ty>, typename _Collection = std::vector<_Ty, _Alloc>>
class ResourceLimitedVector Resource limited wrapper of std::vector.
This template class holds an unordered collection of elements using a std::vector or a replacement. It makes use of a ResourceLimitedContainerConfig to setup the allocation behaviour regarding the number of elements in the collection.
It features linear increment of the capacity, initial preallocation, and maximum number of elements control.
- Template Parameters
_Ty – Element type.
_KeepOrderEnabler – Indicates if element order should be kept when removing items, defaults to std::false_type.
_LimitsConfig – Type defining the resource limits configuration, defaults to ResourceLimitedContainerConfig
_Alloc – Allocator to use on the underlying collection type, defaults to std::allocator<_Ty>.
_Collection – Type used to store the collection of items, defaults to std::vector<_Ty, _Alloc>.
Unnamed Group
-
inline reference at(size_type pos)
Wrappers to other basic vector methods. Please refer to https://en.cppreference.com/w/cpp/container/vector
-
inline const_reference at(size_type pos) const
-
inline const_reference operator[](size_type pos) const
-
inline const_reference front() const
-
inline const_reference back() const
-
inline const_iterator begin() const noexcept
-
inline const_iterator cbegin() const noexcept
-
inline const_iterator end() const noexcept
-
inline const_iterator cend() const noexcept
-
inline reverse_iterator rbegin() noexcept
-
inline const_reverse_iterator rbegin() const noexcept
-
inline const_reverse_iterator crbegin() const noexcept
-
inline reverse_iterator rend() noexcept
-
inline const_reverse_iterator rend() const noexcept
-
inline const_reverse_iterator crend() const noexcept
-
inline bool empty() const noexcept
-
inline void clear()
-
inline iterator erase(const_iterator pos)
-
inline iterator erase(const_iterator first, const_iterator last)
-
inline void pop_back()
-
inline value_type *data()
-
inline const value_type *data() const
Public Types
-
using configuration_type = _LimitsConfig
-
using collection_type = _Collection
-
using pointer = typename collection_type::pointer
-
using const_pointer = typename collection_type::const_pointer
-
using reference = typename collection_type::reference
-
using const_reference = typename collection_type::const_reference
-
using size_type = typename collection_type::size_type
-
using difference_type = typename collection_type::difference_type
-
using iterator = typename collection_type::iterator
-
using const_iterator = typename collection_type::const_iterator
-
using reverse_iterator = typename collection_type::reverse_iterator
-
using const_reverse_iterator = typename collection_type::const_reverse_iterator
Public Functions
-
inline ResourceLimitedVector(configuration_type cfg = configuration_type(), const allocator_type &alloc = allocator_type())
Construct a ResourceLimitedVector.
This constructor receives a ResourceLimitedContainerConfig to setup the allocation behaviour regarding the number of elements in the collection.
The cfg parameter indicates the initial number to be reserved, the maximum number of items allowed, and the capacity increment value.
- Parameters
cfg – Resource limits configuration to use.
alloc – Allocator object. Forwarded to collection constructor.
-
inline ResourceLimitedVector(const ResourceLimitedVector &other)
-
virtual ~ResourceLimitedVector() = default
-
inline ResourceLimitedVector &operator=(const ResourceLimitedVector &other)
-
inline iterator insert(const_iterator pos, const value_type &value)
Insert value before pos.
- Parameters
pos – iterator before which the content will be inserted. pos may be the end() iterator.
value – element value to insert.
- Returns
Iterator pointing to the inserted value. end() if insertion couldn’t be done due to collection limits.
-
inline iterator insert(const_iterator pos, value_type &&value)
Insert value before pos.
- Parameters
pos – iterator before which the content will be inserted. pos may be the end() iterator.
value – element value to insert.
- Returns
Iterator pointing to the inserted value. end() if insertion couldn’t be done due to collection limits.
-
inline pointer push_back(const value_type &val)
Add element at the end.
Adds a new element at the end of the vector, after its current last element. The content of val is copied to the new element.
- Parameters
val – Value to be copied to the new element.
- Returns
pointer to the new element, nullptr if resource limit is reached.
-
inline pointer push_back(value_type &&val)
Add element at the end.
Adds a new element at the end of the vector, after its current last element. The content of val is moved to the new element.
- Parameters
val – Value to be moved to the new element.
- Returns
pointer to the new element, nullptr if resource limit is reached.
-
template<typename ...Args>
inline pointer emplace_back(Args&&... args) Construct and insert element at the end.
Inserts a new element at the end of the vector, right after its current last element. This new element is constructed in place using args as the arguments for its constructor.
- Parameters
args – Arguments forwarded to construct the new element.
- Returns
pointer to the new element, nullptr if resource limit is reached.
-
inline bool remove(const value_type &val)
Remove element.
Removes the first element in the vector that compares equal to val. All iterators may become invalidated if this method returns true.
- Parameters
val – Value to be compared.
- Returns
true if an element was removed, false otherwise.
-
template<class UnaryPredicate>
inline bool remove_if(UnaryPredicate pred) Remove element.
Removes the first element in the vector for which pred returns true. All iterators may become invalidated if this method returns true.
- Parameters
pred – Unary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element is considered a match in the context of this function. The function shall not modify its argument. This can either be a function pointer or a function object.
- Returns
true if an element was removed, false otherwise.
-
template<class InputIterator>
inline void assign(InputIterator first, InputIterator last) Assign vector content.
Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.
- Parameters
first, last – Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last. The function template argument InputIterator shall be an input iterator type that points to elements of a type from which value_type objects can be constructed. If the size of this range is greater than the maximum number of elements allowed on the resource limits configuration, the elements exceeding that maximum will be silently discarded.
-
inline void assign(size_type n, const value_type &val)
Assign vector content.
Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.
- Parameters
n – New size for the container. Will be truncated if greater than the maximum allowed on the resource limits configuration.
val – Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value.
-
inline void assign(std::initializer_list<value_type> il)
Assign vector content.
Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.
- Parameters
il – An initializer_list object. The compiler will automatically construct such objects from initializer list declarators. Member type value_type is the type of the elements in the container. If the size of this list is greater than the maximum number of elements allowed on the resource limits configuration, the elements exceeding that maximum will be silently discarded.
-
inline operator const collection_type&() const noexcept
Const cast to underlying collection.
Useful to easy integration on old APIs where a traditional container was used.
- Returns
const reference to the underlying collection.
Protected Functions
-
inline bool ensure_capacity()
Make room for one item.
Tries to ensure that a new item can be added to the container.
- Returns
true if there is room for a new item, false if resource limit is reached.
-
template<typename Enabler = _KeepOrderEnabler>
inline std::enable_if<!Enabler::value, void>::type do_remove(iterator it) Remove element.
Removes the element pointed to by it. All iterators may become invalidated. This version doesn’t keep the order of insertion, optimizing the number of copies performed.
- Parameters
it – Iterator pointing to the item to be removed.
-
template<typename Enabler = _KeepOrderEnabler>
inline std::enable_if<Enabler::value, void>::type do_remove(iterator it) Remove element.
Removes the element pointed to by it. All iterators may become invalidated. This version keeps the order of insertion, so when removing an item different from the last one, part of the collection will be copied.
- Parameters
it – Iterator pointing to the item to be removed.