Visual Computing Library
Loading...
Searching...
No Matches
vcl::Vector< T, N > Class Template Reference

The Vector class is a generic container of objects of type T, that could have fixed or dynamic size, depending on the templated size N. More...

#include <vclib/space/core/vector/vector.h>

Inheritance diagram for vcl::Vector< T, N >:

Public Types

using ValueType = Container::value_type
 The type of the elements stored in the Vector.
 
using ConstReference = Container::const_reference
 A const reference to the type of the elements stored in the Vector.
 
using Reference = Container::reference
 A reference to the type of the elements stored in the Vector.
 
using ConstPointer = Container::const_pointer
 A const pointer to the type of the elements stored in the Vector.
 
using Pointer = Container::pointer
 A pointer to the type of the elements stored in the Vector.
 
using Iterator = Container::iterator
 An iterator to the elements of the Vector.
 
using ConstIterator = Container::const_iterator
 A const iterator to the elements of the Vector.
 

Public Member Functions

 Vector ()=default
 Creates an empty Vector object.
 
 Vector (std::size_t size, const T &value=T())
 Creates a Vector object with the specified size.
 
template<typename ItType >
 Vector (ItType first, ItType last)
 Constructs the container with the contents of the range [first, last).
 
template<Range RangeType>
 Vector (RangeType &&rng)
 Constructs the container with the contents of the range rng.
 
 Vector (std::initializer_list< T > list)
 Constructs the container with the contents of the initializer list list.
 
std::size_t size () const
 Returns the size of the container.
 
Reference at (uint i)
 Access the specified element with bounds checking.
 
ConstReference at (uint i) const
 Access the specified element with bounds checking.
 
Reference atMod (int i)
 Access the specified element, computing first the module of the position w.r.t. the size of the container.
 
ConstReference atMod (int i) const
 Access the specified element, computing first the module of the position w.r.t. the size of the container.
 
Reference front ()
 Access the first element of the Vector.
 
ConstReference front () const
 Access the first element of the Vector.
 
Reference back ()
 Access the last element of the Vector.
 
ConstReference back () const
 Access the last element of the Vector.
 
Pointer data ()
 Returns a pointer to the underlying array serving as element storage. The pointer is such that range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).
 
ConstPointer data () const
 Returns a const pointer to the underlying array serving as element storage. The pointer is such that range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).
 
void set (uint i, const T &e)
 Set the value of the element at the specified position.
 
void set (uint i, T &&e)
 Set the value of the element at the specified position.
 
void set (ConstIterator it, const T &e)
 Set the value of the element at the specified position.
 
void set (ConstIterator it, T &&e)
 Set the value of the element at the specified position.
 
template<Range Rng>
requires InputRange<Rng, T>
void set (Rng &&r)
 Set the elements of the Vector using the values from a range.
 
void fill (const T &e)
 Fill all elements of the Vector with the specified value.
 
bool contains (const MakeConstPointerT< T > &e) const
 Check if the Vector contains the specified element.
 
Iterator find (const MakeConstPointerT< T > &e)
 Find the first occurrence of the specified element in the Vector.
 
ConstIterator find (const MakeConstPointerT< T > &e) const
 Find the first occurrence of the specified element in the Vector.
 
uint indexOf (const MakeConstPointerT< T > &e) const
 Get the index of the first occurrence of the specified element in the Vector.
 
void swap (Vector &other)
 Swaps the contents of the container with those of other.
 
void resize (uint n, const T &v=T())
 Resize the Vector to the specified size.
 
void pushBack (const T &v)
 Add an element to the end of the Vector.
 
void pushBack (T &&v)
 Add an element to the end of the Vector.
 
void insert (uint i, const T &v)
 Insert an element at the specified position in the Vector.
 
void insert (uint i, T &&v)
 Insert an element at the specified position in the Vector.
 
template<typename... Args>
requires (N < 0)
void emplace (uint i, Args &&... args)
 Insert an element at the specified position in the Vector.
 
bool empty () const noexcept
 Returns whether the vector is empty (i.e. whether its size is 0).
 
void erase (uint i)
 Remove the element at the specified index from the Vector.
 
void clear ()
 Remove all elements from the Vector.
 
void serialize (std::ostream &os) const
 
void deserialize (std::istream &is)
 
Reference operator[] (uint i)
 Returns a reference to the element at specified location i. No bounds checking is performed.
 
ConstReference operator[] (uint i) const
 Returns a const reference to the element at specified location i. No bounds checking is performed.
 
Reference operator() (uint i)
 Returns a reference to the element at specified location i. No bounds checking is performed.
 
ConstReference operator() (uint i) const
 Returns a const reference to the element at specified location i. No bounds checking is performed.
 
Iterator begin ()
 Return an iterator pointing to the beginning of the Vector.
 
Iterator end ()
 Return an iterator pointing to the end of the Vector.
 
ConstIterator begin () const
 Return a const iterator pointing to the beginning of the Vector.
 
ConstIterator end () const
 Return a const iterator pointing to the end of the Vector.
 

Static Public Attributes

static const int SIZE = N
 Size of the vector at compile time. It will be -1 if the Vector has dynamic size.
 

Protected Attributes

Container mContainer
 

Private Types

using Container = std::conditional_t<(N >=0), typename std::array< T, ARRAY_SIZE >, typename std::vector< T > >
 

Static Private Attributes

static const int ARRAY_SIZE = N >= 0 ? N : 0
 

Friends

void swap (Vector &a, Vector &b)
 

Detailed Description

template<typename T, int N>
class vcl::Vector< T, N >

The Vector class is a generic container of objects of type T, that could have fixed or dynamic size, depending on the templated size N.

The Vector class is a container that can hold objects of type T. The size of the container can be either fixed or dynamic, depending on the value of the template parameter N. If N is greater than or equal to zero, the container will have a fixed size of N elements, and it will use an array to store the elements. If N is less than zero, the container will have a dynamic size, and it will use a vector to store the elements. The Vector class provides several member functions to manipulate the elements of the container, such as getting and setting individual elements, filling the container with a value, checking if the container contains a certain element, and finding the index of a certain element.

Template Parameters
Tthe type of the objects stored in the container.
Nthe size of the container. If N is greater than or equal to zero, the container will have a fixed size of N elements, and it will use an array to store the elements. If N is less than zero, the container will have a dynamic size, and it will use a vector to store the elements.

Constructor & Destructor Documentation

◆ Vector() [1/5]

template<typename T , int N>
vcl::Vector< T, N >::Vector ( )
default

Creates an empty Vector object.

If the container is dynamic, its size is 0. When the container is static, the size is N and its elements are initialized with their empty constructor.

◆ Vector() [2/5]

template<typename T , int N>
vcl::Vector< T, N >::Vector ( std::size_t  size,
const T &  value = T() 
)
inline

Creates a Vector object with the specified size.

If the container is dynamic, its size is set to size. When the container is static, the given size must be equal to N. The elements are initialized with their empty constructor if the argument value is not specified. Otherwise, the elements are initialized with the given value.

Exceptions
WrongSizeExceptionif the given size is not equal to N and the container is static.
Parameters
[in]sizethe size of the container.
[in]valuethe value to initialize the elements with.

◆ Vector() [3/5]

template<typename T , int N>
template<typename ItType >
vcl::Vector< T, N >::Vector ( ItType  first,
ItType  last 
)
inline

Constructs the container with the contents of the range [first, last).

If the container is dynamic, the size of the container is determined by the number of elements in the range [first, last). If the container is static, the elements are initialized with the contents of the first N elements of the range [first, last). If the range contains less than N elements, the remaining elements are initialized with their empty constructor.

Template Parameters
ItTypeThe type of the iterators used to access the elements in the range.
Parameters
[in]firstThe beginning of the range.
[in]lastThe end of the range.

◆ Vector() [4/5]

template<typename T , int N>
template<Range RangeType>
vcl::Vector< T, N >::Vector ( RangeType &&  rng)
inline

Constructs the container with the contents of the range rng.

If the container is dynamic, the size of the container is determined by the number of elements in the input range. If the container is static, the elements are initialized with the contents of the first N elements of the range. If the range contains less than N elements, the remaining elements are initialized with their empty constructor.

Template Parameters
RangeTypeThe type of the range used to access the elements in the range. It must satisfy the vcl::Range concept.
Parameters
[in]rnga range of T elements.

◆ Vector() [5/5]

template<typename T , int N>
vcl::Vector< T, N >::Vector ( std::initializer_list< T >  list)
inlineexplicit

Constructs the container with the contents of the initializer list list.

If the container is dynamic, the size of the container is determined by the number of elements in the initializer list list. If the container is static, the elements are initialized with the contents of the first N elements of the initializer list list. If the initializer list contains less than N elements, the remaining elements are initialized with their empty constructor.

Parameters
[in]listThe initializer list to initialize the elements of the container with.

Member Function Documentation

◆ at() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::at ( uint  i)
inline

Access the specified element with bounds checking.

Returns a reference to the element at position i in the Vector, with bounds checking. If i is not within the range of valid indices for the Vector, an std::out_of_range exception is thrown.

Parameters
[in]iThe position of the element.
Returns
A reference to the element at position i.
Exceptions
std::out_of_rangeIf i is not within the range of valid indices for the Vector.

◆ at() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::at ( uint  i) const
inline

Access the specified element with bounds checking.

Returns a const reference to the element at position i in the Vector, with bounds checking. If i is not within the range of valid indices for the Vector, an std::out_of_range exception is thrown.

Parameters
[in]iThe position of the element.
Returns
A const reference to the element at position i.
Exceptions
std::out_of_rangeIf i is not within the range of valid indices for the Vector.

◆ atMod() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::atMod ( int  i)
inline

Access the specified element, computing first the module of the position w.r.t. the size of the container.

Takes into account negative indices: atMod(-1) will access to the last element of the container.

Parameters
[in]iThe position of the element.
Returns
A reference to the element at position i % size().

◆ atMod() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::atMod ( int  i) const
inline

Access the specified element, computing first the module of the position w.r.t. the size of the container.

Takes into account negative indices: atMod(-1) will access to the last element of the container.

Parameters
[in]iThe position of the element.
Returns
A const reference to the element at position i % size().

◆ back() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::back ( )
inline

Access the last element of the Vector.

Returns a reference to the last element of the Vector. If the Vector is empty, the behavior is undefined.

Returns
A reference to the last element.

◆ back() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::back ( ) const
inline

Access the last element of the Vector.

Returns a const reference to the last element of the Vector. If the Vector is empty, the behavior is undefined.

Returns
A const reference to the last element.

◆ begin() [1/2]

template<typename T , int N>
Iterator vcl::Vector< T, N >::begin ( )
inline

Return an iterator pointing to the beginning of the Vector.

Returns
An iterator pointing to the beginning of the Vector.

◆ begin() [2/2]

template<typename T , int N>
ConstIterator vcl::Vector< T, N >::begin ( ) const
inline

Return a const iterator pointing to the beginning of the Vector.

Returns
A const iterator pointing to the beginning of the Vector.

◆ clear()

template<typename T , int N>
void vcl::Vector< T, N >::clear ( )
inline

Remove all elements from the Vector.

Removes all elements from the Vector by calling the clear() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

◆ contains()

template<typename T , int N>
bool vcl::Vector< T, N >::contains ( const MakeConstPointerT< T > &  e) const
inline

Check if the Vector contains the specified element.

Checks if the Vector contains the element specified by e, using the std::find() algorithm.

Parameters
[in]eThe element to search for in the Vector.
Returns
true if the element is found in the Vector, false otherwise.

◆ data() [1/2]

template<typename T , int N>
Pointer vcl::Vector< T, N >::data ( )
inline

Returns a pointer to the underlying array serving as element storage. The pointer is such that range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).

Returns
A pointer to the underlying element storage. For non-empty containers, the returned pointer compares equal to the address of the first element.

◆ data() [2/2]

template<typename T , int N>
ConstPointer vcl::Vector< T, N >::data ( ) const
inline

Returns a const pointer to the underlying array serving as element storage. The pointer is such that range [data(), data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).

Returns
A const pointer to the underlying element storage. For non-empty containers, the returned pointer compares equal to the address of the first element.

◆ emplace()

template<typename T , int N>
template<typename... Args>
requires (N < 0)
void vcl::Vector< T, N >::emplace ( uint  i,
Args &&...  args 
)
inline

Insert an element at the specified position in the Vector.

Inserts the newly constructed element with the args at the position specified by i in the Vector by calling the emplace() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]iThe index at which to insert the element.
[in]argsArguments to forward to the constructor of the element.

◆ empty()

template<typename T , int N>
bool vcl::Vector< T, N >::empty ( ) const
inlinenoexcept

Returns whether the vector is empty (i.e. whether its size is 0).

Returns
true if the container size is 0, false otherwise.

◆ end() [1/2]

template<typename T , int N>
Iterator vcl::Vector< T, N >::end ( )
inline

Return an iterator pointing to the end of the Vector.

Returns
An iterator pointing to the end of the Vector.

◆ end() [2/2]

template<typename T , int N>
ConstIterator vcl::Vector< T, N >::end ( ) const
inline

Return a const iterator pointing to the end of the Vector.

Returns
A const iterator pointing to the end of the Vector.

◆ erase()

template<typename T , int N>
void vcl::Vector< T, N >::erase ( uint  i)
inline

Remove the element at the specified index from the Vector.

Removes the element at the position specified by i in the Vector by calling the erase() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]iThe index of the element to remove from the Vector.

◆ fill()

template<typename T , int N>
void vcl::Vector< T, N >::fill ( const T &  e)
inline

Fill all elements of the Vector with the specified value.

Sets all elements of the Vector to the value specified by e, using the std::fill() algorithm.

Parameters
[in]eThe value to fill the Vector with.

◆ find() [1/2]

template<typename T , int N>
Iterator vcl::Vector< T, N >::find ( const MakeConstPointerT< T > &  e)
inline

Find the first occurrence of the specified element in the Vector.

Finds the first occurrence of the element specified by e in the Vector, using the std::find() algorithm.

Parameters
[in]eThe element to search for in the Vector.
Returns
An iterator to the first occurrence of the element in the Vector, or end() if the element is not found.

◆ find() [2/2]

template<typename T , int N>
ConstIterator vcl::Vector< T, N >::find ( const MakeConstPointerT< T > &  e) const
inline

Find the first occurrence of the specified element in the Vector.

Finds the first occurrence of the element specified by e in the Vector, using the std::find() algorithm.

Parameters
[in]eThe element to search for in the Vector.
Returns
A const iterator to the first occurrence of the element in the Vector, or end() if the element is not found.

◆ front() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::front ( )
inline

Access the first element of the Vector.

Returns a reference to the first element of the Vector. If the Vector is empty, the behavior is undefined.

Returns
A reference to the first element.

◆ front() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::front ( ) const
inline

Access the first element of the Vector.

Returns a const reference to the first element of the Vector. If the Vector is empty, the behavior is undefined.

Returns
A const reference to the first element.

◆ indexOf()

template<typename T , int N>
uint vcl::Vector< T, N >::indexOf ( const MakeConstPointerT< T > &  e) const
inline

Get the index of the first occurrence of the specified element in the Vector.

Finds the first occurrence of the element specified by e in the Vector, using the std::find() algorithm, and returns its index. If the element is not found in the Vector, the member function returns UINT_NULL.

Parameters
[in]eThe element to search for in the Vector.
Returns
The index of the first occurrence of the element in the Vector, or UINT_NULL if the element is not found.

◆ insert() [1/2]

template<typename T , int N>
void vcl::Vector< T, N >::insert ( uint  i,
const T &  v 
)
inline

Insert an element at the specified position in the Vector.

Inserts the element v at the position specified by i in the Vector by calling the insert() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]iThe index at which to insert the element.
[in]vThe value to insert into the Vector.

◆ insert() [2/2]

template<typename T , int N>
void vcl::Vector< T, N >::insert ( uint  i,
T &&  v 
)
inline

Insert an element at the specified position in the Vector.

Inserts the element v at the position specified by i in the Vector by calling the insert() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]iThe index at which to insert the element.
[in]vThe value to insert into the Vector.

◆ operator()() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::operator() ( uint  i)
inline

Returns a reference to the element at specified location i. No bounds checking is performed.

Parameters
[in]iPosition of the element to return
Returns
A reference to the requested element.

◆ operator()() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::operator() ( uint  i) const
inline

Returns a const reference to the element at specified location i. No bounds checking is performed.

Parameters
[in]iPosition of the element to return
Returns
A const reference to the requested element.

◆ operator[]() [1/2]

template<typename T , int N>
Reference vcl::Vector< T, N >::operator[] ( uint  i)
inline

Returns a reference to the element at specified location i. No bounds checking is performed.

Parameters
[in]iPosition of the element to return
Returns
A reference to the requested element.

◆ operator[]() [2/2]

template<typename T , int N>
ConstReference vcl::Vector< T, N >::operator[] ( uint  i) const
inline

Returns a const reference to the element at specified location i. No bounds checking is performed.

Parameters
[in]iPosition of the element to return
Returns
A const reference to the requested element.

◆ pushBack() [1/2]

template<typename T , int N>
void vcl::Vector< T, N >::pushBack ( const T &  v)
inline

Add an element to the end of the Vector.

Adds the element v to the end of the Vector by calling the push_back() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]vThe value to add to the end of the Vector.

◆ pushBack() [2/2]

template<typename T , int N>
void vcl::Vector< T, N >::pushBack ( T &&  v)
inline

Add an element to the end of the Vector.

Moves the element v to the end of the Vector by calling the push_back() member function of the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]vThe value to add to the end of the Vector.

◆ resize()

template<typename T , int N>
void vcl::Vector< T, N >::resize ( uint  n,
const T &  v = T() 
)
inline

Resize the Vector to the specified size.

Resizes the Vector to the specified size n by resizing the underlying std::vector. This member function is only available if the size of the Vector is not known at compile-time, as specified by the concept requirement requires (N < 0).

Parameters
[in]nThe new size of the Vector.
[in]vThe value to use to fill the new elements of the Vector.

◆ set() [1/5]

template<typename T , int N>
void vcl::Vector< T, N >::set ( ConstIterator  it,
const T &  e 
)
inline

Set the value of the element at the specified position.

Sets the value of the element at position it in the Vector to the specified value e.

Parameters
[in]itThe iterator pointing to the position of the element.
[in]eThe new value of the element.

◆ set() [2/5]

template<typename T , int N>
void vcl::Vector< T, N >::set ( ConstIterator  it,
T &&  e 
)
inline

Set the value of the element at the specified position.

Sets the value of the element at position it in the Vector by moving the specified value e.

Parameters
[in]itThe iterator pointing to the position of the element.
[in]eThe new value of the element.

◆ set() [3/5]

template<typename T , int N>
template<Range Rng>
requires InputRange<Rng, T>
void vcl::Vector< T, N >::set ( Rng &&  r)
inline

Set the elements of the Vector using the values from a range.

Sets the elements of the Vector to the values from the range r. If the size of the Vector is known at compile-time and is not negative, the first N elements from the range r (or all of them, if they are less than N) are copied into the Vector using std::copy_n(). If the size of the Vector is negative or not known at compile-time, the elements of the Vector are set to the values from the range r by constructing a new std::vector from the range r.

Template Parameters
RngThe type of the range, it must satisfy the Range concept and the value type of the range must be convertible to T.
Parameters
[in]rThe range of values to set the elements of the Vector to.

◆ set() [4/5]

template<typename T , int N>
void vcl::Vector< T, N >::set ( uint  i,
const T &  e 
)
inline

Set the value of the element at the specified position.

Sets the value of the element at position i in the Vector to the specified value e.

Parameters
[in]iThe position of the element.
[in]eThe new value of the element.

◆ set() [5/5]

template<typename T , int N>
void vcl::Vector< T, N >::set ( uint  i,
T &&  e 
)
inline

Set the value of the element at the specified position.

Sets the value of the element at position i in the Vector by moving the specified value e.

Parameters
[in]iThe position of the element.
[in]eThe new value of the element.

◆ size()

template<typename T , int N>
std::size_t vcl::Vector< T, N >::size ( ) const
inline

Returns the size of the container.

If the container is static, the size is N. If the container is dynamic, the size is determined by the number of elements currently stored.

Returns
The size of the container.

◆ swap()

template<typename T , int N>
void vcl::Vector< T, N >::swap ( Vector< T, N > &  other)
inline

Swaps the contents of the container with those of other.

Parameters
[in]otherAnother Vector container of the same type.

Friends And Related Symbol Documentation

◆ swap

template<typename T , int N>
void swap ( Vector< T, N > &  a,
Vector< T, N > &  b 
)
friend

Specializes the swap function to allow the swapping of two Vector objects.

Swaps the content of the two Vector objects. Calls a.swap(b).

Parameters
[in]aThe first Vector object.
[in]bThe second Vector object.

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