Template Class BoundedVector

Inheritance Relationships

Base Type

  • protected std::vector< Tp, std::allocator< Tp > >

Class Documentation

template<typename Tp, std::size_t UpperBound, typename Alloc = std::allocator<Tp>>
class BoundedVector : protected std::vector<Tp, std::allocator<Tp>>

A container based on std::vector but with an upper bound.

Meets the same requirements as std::vector.

Param Tp:

Type of element

Param UpperBound:

The upper bound for the number of elements

Param Alloc:

Allocator type, defaults to std::allocator<Tp>

Public Functions

inline BoundedVector() noexcept(std::is_nothrow_default_constructible<Alloc>::value)

Create a BoundedVector with no elements.

inline explicit BoundedVector(const typename Base::allocator_type &a) noexcept

Creates a BoundedVector with no elements.

Parameters:

a – An allocator object

inline explicit BoundedVector(typename Base::size_type n, const typename Base::allocator_type &a = allocator_type())

Create a BoundedVector with default constructed elements.

This constructor fills the BoundedVector with n default constructed elements.

Parameters:
  • n – The number of elements to initially create

  • a – An allocator

inline BoundedVector(typename Base::size_type n, const typename Base::value_type &value, const typename Base::allocator_type &a = allocator_type())

Create a BoundedVector with copies of an exemplar element.

This constructor fills the BoundedVector with n copies of value.

Parameters:
  • n – The number of elements to initially create

  • value – An element to copy

  • a – An allocator

inline BoundedVector(const BoundedVector &x)

BoundedVector copy constructor.

The newly-created BoundedVector uses a copy of the allocation object used by x. All the elements of x are copied, but any extra memory in x (for fast expansion) will not be copied.

Parameters:

x – A BoundedVector of identical element and allocator types

inline BoundedVector(BoundedVector &&x) noexcept

BoundedVector move constructor.

The newly-created BoundedVector contains the exact contents of x. The contents of x are a valid, but unspecified BoundedVector.

Parameters:

x – A BoundedVector of identical element and allocator types

inline BoundedVector(const BoundedVector &x, const typename Base::allocator_type &a)

Copy constructor with alternative allocator.

inline BoundedVector(std::initializer_list<typename Base::value_type> l, const typename Base::allocator_type &a = typename Base::allocator_type())

Build a BoundedVector from an initializer list.

Create a BoundedVector consisting of copies of the elements in the initializer_list l.

This will call the element type’s copy constructor N times (where N is l.size()) and do no memory reallocation.

Parameters:
  • l – An initializer_list

  • a – An allocator

template<typename InputIterator>
inline BoundedVector(InputIterator first, InputIterator last, const typename Base::allocator_type &a = allocator_type())

Build a BoundedVector from a range.

Create a BoundedVector consisting of copies of the elements from [first,last).

If the iterators are forward, bidirectional, or random-access, then this will call the elements’ copy constructor N times (where N is distance(first,last)) and do no memory reallocation. But if only input iterators are used, then this will do at most 2N calls to the copy constructor, and logN memory reallocations.

Parameters:
  • first – An input iterator

  • last – An input iterator

  • a – An allocator

inline ~BoundedVector() noexcept

The dtor only erases the elements.

Note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user’s responsibility.

inline BoundedVector &operator=(const BoundedVector &x)

BoundedVector assignment operator.

All the elements of x are copied, but any extra memory in x (for fast expansion) will not be copied. Unlike the copy constructor, the allocator object is not copied.

Parameters:

x – A BoundedVector of identical element and allocator types

inline BoundedVector &operator=(BoundedVector &&x)

BoundedVector move assignment operator

Parameters:

x – A BoundedVector of identical element and allocator types.

inline BoundedVector &operator=(std::initializer_list<typename Base::value_type> l)

BoundedVector list assignment operator.

This function fills a BoundedVector with copies of the elements in the initializer list l.

Note that the assignment completely changes the BoundedVector and that the resulting BoundedVector’s size is the same as the number of elements assigned. Old data may be lost.

Parameters:

l – An initializer_list

inline void assign(typename Base::size_type n, const typename Base::value_type &val)

Assign a given value to a BoundedVector.

This function fills a BoundedVector with n copies of the given value. Note that the assignment completely changes the BoundedVector and that the resulting BoundedVector’s size is the same as the number of elements assigned. Old data may be lost.

Parameters:
  • n – Number of elements to be assigned

  • val – Value to be assigned

template<typename InputIterator>
inline void assign(InputIterator first, InputIterator last)

Assign a range to a BoundedVector.

This function fills a BoundedVector with copies of the elements in the range [first,last).

Note that the assignment completely changes the BoundedVector and that the resulting BoundedVector’s size is the same as the number of elements assigned. Old data may be lost.

Parameters:
  • first – An input iterator

  • last – An input iterator

inline void assign(std::initializer_list<typename Base::value_type> l)

Assign an initializer list to a BoundedVector.

This function fills a BoundedVector with copies of the elements in the initializer list l.

Note that the assignment completely changes the BoundedVector and that the resulting BoundedVector’s size is the same as the number of elements assigned. Old data may be lost.

Parameters:

l – An initializer_list

inline Base::size_type max_size() const noexcept

Returns the size() of the largest possible BoundedVector.

inline void resize(typename Base::size_type new_size)

Resize the BoundedVector to the specified number of elements.

This function will resize the BoundedVector to the specified number of elements. If the number is smaller than the BoundedVector’s current size the BoundedVector is truncated, otherwise default constructed elements are appended.

Parameters:

new_size – Number of elements the BoundedVector should contain

inline void resize(typename Base::size_type new_size, const typename Base::value_type &x)

Resize the BoundedVector to the specified number of elements.

This function will resize the BoundedVector to the specified number of elements. If the number is smaller than the BoundedVector’s current size the BoundedVector is truncated, otherwise the BoundedVector is extended and new elements are populated with given data.

Parameters:
  • new_size – Number of elements the BoundedVector should contain

  • x – Data with which new elements should be populated

inline void reserve(typename Base::size_type n)

Attempt to preallocate enough memory for specified number of elements.

This function attempts to reserve enough memory for the BoundedVector to hold the specified number of elements. If the number requested is more than max_size(), length_error is thrown.

The advantage of this function is that if optimal code is a necessity and the user can determine the number of elements that will be required, the user can reserve the memory in advance, and thus prevent a possible reallocation of memory and copying of BoundedVector data.

Parameters:

n – Number of elements required

Throws:

std::length_error – If n exceeds max_size()

template<typename T, typename std::enable_if<!std::is_same<T, Tp>::value && !std::is_same<T, bool>::value>::type* = nullptr>
inline T *data() noexcept

Return a pointer such that [data(), data() + size()) is a valid range.

For a non-empty BoundedVector, data() == &front().

template<typename T, typename std::enable_if<!std::is_same<T, Tp>::value && !std::is_same<T, bool>::value>::type* = nullptr>
inline const T *data() const noexcept
inline void push_back(const typename Base::value_type &x)

Add data to the end of the BoundedVector.

This is a typical stack operation. The function creates an element at the end of the BoundedVector and assigns the given data to it. Due to the nature of a BoundedVector this operation can be done in constant time if the BoundedVector has preallocated space available.

Parameters:

x – Data to be added

inline void push_back(typename Base::value_type &&x)
template<typename ...Args>
inline auto emplace_back(Args&&... args)

Add data to the end of the BoundedVector.

This is a typical stack operation. The function creates an element at the end of the BoundedVector and assigns the given data to it. Due to the nature of a BoundedVector this operation can be done in constant time if the BoundedVector has preallocated space available.

Parameters:

args – Arguments to be forwarded to the constructor of Tp

template<typename ...Args>
inline Base::iterator emplace(typename Base::const_iterator position, Args&&... args)

Insert an object in BoundedVector before specified iterator.

This function will insert an object of type T constructed with T(std::forward<Args>(args)…) before the specified location. Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – A const_iterator into the BoundedVector

  • args – Arguments

Returns:

An iterator that points to the inserted data

inline Base::iterator insert(typename Base::const_iterator position, const typename Base::value_type &x)

Insert given value into BoundedVector before specified iterator.

This function will insert a copy of the given value before the specified location. Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – A const_iterator into the BoundedVector

  • x – Data to be inserted

Returns:

An iterator that points to the inserted data

inline Base::iterator insert(typename Base::const_iterator position, typename Base::value_type &&x)

Insert given rvalue into BoundedVector before specified iterator.

This function will insert a copy of the given rvalue before the specified location. Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – A const_iterator into the BoundedVector

  • x – Data to be inserted

Returns:

An iterator that points to the inserted data

inline Base::iterator insert(typename Base::const_iterator position, std::initializer_list<typename Base::value_type> l)

Insert an initializer_list into the BoundedVector.

This function will insert copies of the data in the initializer_list l into the BoundedVector before the location specified by position.

Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – An iterator into the BoundedVector

  • l – An initializer_list

inline Base::iterator insert(typename Base::const_iterator position, typename Base::size_type n, const typename Base::value_type &x)

Insert a number of copies of given data into the BoundedVector.

This function will insert a specified number of copies of the given data before the location specified by position.

Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – A const_iterator into the BoundedVector

  • n – Number of elements to be inserted

  • x – Data to be inserted

Returns:

An iterator that points to the inserted data

template<typename InputIterator>
inline Base::iterator insert(typename Base::const_iterator position, InputIterator first, InputIterator last)

Insert a range into the BoundedVector.

This function will insert copies of the data in the range [first,last) into the BoundedVector before the location specified by pos.

Note that this kind of operation could be expensive for a BoundedVector and if it is frequently used the user should consider using std::list.

Parameters:
  • position – A const_iterator into the BoundedVector

  • first – An input iterator

  • last – An input iterator

Returns:

An iterator that points to the inserted data