Visual Computing Library
|
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>
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) |
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.
T | the type of the objects stored in the container. |
N | the 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. |
|
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.
|
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.
WrongSizeException | if the given size is not equal to N and the container is static. |
[in] | size | the size of the container. |
[in] | value | the value to initialize the elements with. |
|
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.
ItType | The type of the iterators used to access the elements in the range. |
[in] | first | The beginning of the range. |
[in] | last | The end of the range. |
|
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.
RangeType | The type of the range used to access the elements in the range. It must satisfy the vcl::Range concept. |
[in] | rng | a range of T elements. |
|
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.
[in] | list | The initializer list to initialize the elements of the container with. |
|
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.
[in] | i | The position of the element. |
i
.std::out_of_range | If i is not within the range of valid indices for the Vector. |
|
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.
[in] | i | The position of the element. |
i
.std::out_of_range | If i is not within the range of valid indices for the Vector. |
|
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.
[in] | i | The position of the element. |
|
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.
[in] | i | The position of the element. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
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).
|
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).
|
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)
.
[in] | i | The index at which to insert the element. |
[in] | args | Arguments to forward to the constructor of the element. |
|
inlinenoexcept |
Returns whether the vector is empty (i.e. whether its size is 0).
true
if the container size is 0, false
otherwise.
|
inline |
|
inline |
|
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)
.
[in] | i | The index of the element to remove from the Vector. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
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.
[in] | e | The element to search for in the Vector. |
|
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)
.
[in] | i | The index at which to insert the element. |
[in] | v | The value to insert into the Vector. |
|
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)
.
[in] | i | The index at which to insert the element. |
[in] | v | The value to insert into the Vector. |
|
inline |
Returns a reference to the element at specified location i. No bounds checking is performed.
[in] | i | Position of the element to return |
|
inline |
Returns a const reference to the element at specified location i. No bounds checking is performed.
[in] | i | Position of the element to return |
|
inline |
Returns a reference to the element at specified location i. No bounds checking is performed.
[in] | i | Position of the element to return |
|
inline |
Returns a const reference to the element at specified location i. No bounds checking is performed.
[in] | i | Position of the element to return |
|
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)
.
[in] | v | The value to add to the end of the Vector. |
|
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)
.
[in] | v | The value to add to the end of the Vector. |
|
inline |
|
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
.
[in] | it | The iterator pointing to the position of the element. |
[in] | e | The new value of the element. |
|
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
.
[in] | it | The iterator pointing to the position of the element. |
[in] | e | The new value of the element. |
|
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
.
Rng | The type of the range, it must satisfy the Range concept and the value type of the range must be convertible to T. |
[in] | r | The range of values to set the elements of the Vector to. |
|
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
.
[in] | i | The position of the element. |
[in] | e | The new value of the element. |
|
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
.
[in] | i | The position of the element. |
[in] | e | The new value of the element. |
|
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.
|
inline |
Swaps the contents of the container with those of other.
[in] | other | Another Vector container of the same type. |