23#ifndef VCL_SPACE_CORE_VECTOR_VECTOR_H
24#define VCL_SPACE_CORE_VECTOR_VECTOR_H
26#include <vclib/concepts/ranges/range.h>
27#include <vclib/exceptions.h>
28#include <vclib/io/serialization.h>
29#include <vclib/types.h>
63template<
typename T,
int N>
69 static const int ARRAY_SIZE = N >= 0 ? N : 0;
72 using Container = std::conditional_t<
74 typename std::array<T, ARRAY_SIZE>,
75 typename std::vector<T>>;
99 using Pointer = Container::pointer;
102 using Iterator = Container::iterator;
139 if constexpr (N >= 0) {
142 "Vector must have " + std::to_string(N) +
" size.");
147 mContainer.resize(
size, value);
168 template<
typename ItType>
188 template<Range RangeType>
208 explicit Vector(std::initializer_list<T> list)
210 set(
View(std::begin(list), std::end(list)));
223 if constexpr (N >= 0) {
227 return mContainer.size();
274 return mContainer[(
i %
n +
n) %
n];
290 return mContainer[(
i %
n +
n) %
n];
384 mContainer[
i] = std::move(e);
414 mContainer[
it -
begin()] = std::move(e);
436 if constexpr (N >= 0) {
440 std::ranges::begin(
r), std::ranges::end(
r)));
441 std::copy_n(std::ranges::begin(
r),
n, mContainer.begin());
445 std::vector<T>(std::ranges::begin(
r), std::ranges::end(
r));
459 std::fill(mContainer.begin(), mContainer.end(), e);
473 return std::find(mContainer.begin(), mContainer.end(), e) !=
489 return std::find(mContainer.begin(), mContainer.end(), e);
504 return std::find(mContainer.begin(), mContainer.end(), e);
557 void resize(uint
n,
const T& v = T())
requires (N < 0)
559 mContainer.resize(
n, v);
573 void pushBack(
const T& v)
requires (N < 0) { mContainer.push_back(v); }
588 mContainer.push_back(std::move(v));
603 void insert(uint
i,
const T& v)
requires (N < 0)
606 mContainer.insert(mContainer.begin() +
i, v);
624 mContainer.insert(mContainer.begin() +
i, std::move(v));
639 template<
typename... Args>
643 mContainer.emplace(mContainer.begin() +
i, std::forward<Args>(
args)...);
667 mContainer.erase(mContainer.begin() +
i);
678 void clear()
requires (N < 0) { mContainer.clear(); }
680 void serialize(std::ostream&
os)
const { vcl::serialize(
os, mContainer); }
682 void deserialize(std::istream& is) { vcl::deserialize(is, mContainer); }
The ConstPointerIterator class is a utility class that wraps an iterator of a container of [shared] p...
Definition const_pointer_iterator.h:57
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The Vector class is a generic container of objects of type T, that could have fixed or dynamic size,...
Definition vector.h:65
ConstReference atMod(int i) const
Access the specified element, computing first the module of the position w.r.t. the size of the conta...
Definition vector.h:287
ConstReference operator[](uint i) const
Returns a const reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:700
Reference operator()(uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:708
Reference atMod(int i)
Access the specified element, computing first the module of the position w.r.t. the size of the conta...
Definition vector.h:271
Container::reference Reference
A reference to the type of the elements stored in the Vector.
Definition vector.h:91
std::size_t size() const
Returns the size of the container.
Definition vector.h:221
void resize(uint n, const T &v=T())
Resize the Vector to the specified size.
Definition vector.h:557
void erase(uint i)
Remove the element at the specified index from the Vector.
Definition vector.h:664
void set(ConstIterator it, const T &e)
Set the value of the element at the specified position.
Definition vector.h:396
Container::iterator Iterator
An iterator to the elements of the Vector.
Definition vector.h:102
bool contains(const MakeConstPointerT< T > &e) const
Check if the Vector contains the specified element.
Definition vector.h:471
ConstReference operator()(uint i) const
Returns a const reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:716
Reference back()
Access the last element of the Vector.
Definition vector.h:321
Vector(ItType first, ItType last)
Constructs the container with the contents of the range [first, last).
Definition vector.h:169
Container::const_iterator ConstIterator
A const iterator to the elements of the Vector.
Definition vector.h:105
void pushBack(T &&v)
Add an element to the end of the Vector.
Definition vector.h:586
bool empty() const noexcept
Returns whether the vector is empty (i.e. whether its size is 0).
Definition vector.h:651
ConstReference at(uint i) const
Access the specified element with bounds checking.
Definition vector.h:259
Container::value_type ValueType
The type of the elements stored in the Vector.
Definition vector.h:82
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition vector.h:366
Container::pointer Pointer
A pointer to the type of the elements stored in the Vector.
Definition vector.h:99
Container::const_reference ConstReference
A const reference to the type of the elements stored in the Vector.
Definition vector.h:88
void fill(const T &e)
Fill all elements of the Vector with the specified value.
Definition vector.h:457
Reference at(uint i)
Access the specified element with bounds checking.
Definition vector.h:244
Vector()=default
Creates an empty Vector object.
ConstPointer data() const
Returns a const pointer to the underlying array serving as element storage. The pointer is such that ...
Definition vector.h:355
Iterator find(const MakeConstPointerT< T > &e)
Find the first occurrence of the specified element in the Vector.
Definition vector.h:487
void emplace(uint i, Args &&... args)
Insert an element at the specified position in the Vector.
Definition vector.h:640
Pointer data()
Returns a pointer to the underlying array serving as element storage. The pointer is such that range ...
Definition vector.h:343
void swap(Vector &other)
Swaps the contents of the container with those of other.
Definition vector.h:532
Reference operator[](uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:692
uint indexOf(const MakeConstPointerT< T > &e) const
Get the index of the first occurrence of the specified element in the Vector.
Definition vector.h:519
void pushBack(const T &v)
Add an element to the end of the Vector.
Definition vector.h:573
ConstReference front() const
Access the first element of the Vector.
Definition vector.h:311
ConstIterator find(const MakeConstPointerT< T > &e) const
Find the first occurrence of the specified element in the Vector.
Definition vector.h:502
void insert(uint i, T &&v)
Insert an element at the specified position in the Vector.
Definition vector.h:621
Vector(std::size_t size, const T &value=T())
Creates a Vector object with the specified size.
Definition vector.h:137
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition vector.h:603
void set(ConstIterator it, T &&e)
Set the value of the element at the specified position.
Definition vector.h:411
ConstReference back() const
Access the last element of the Vector.
Definition vector.h:331
void set(Rng &&r)
Set the elements of the Vector using the values from a range.
Definition vector.h:434
Vector(std::initializer_list< T > list)
Constructs the container with the contents of the initializer list list.
Definition vector.h:208
ConstIterator begin() const
Return a const iterator pointing to the beginning of the Vector.
Definition vector.h:739
void clear()
Remove all elements from the Vector.
Definition vector.h:678
Vector(RangeType &&rng)
Constructs the container with the contents of the range rng.
Definition vector.h:189
Iterator end()
Return an iterator pointing to the end of the Vector.
Definition vector.h:732
void set(uint i, T &&e)
Set the value of the element at the specified position.
Definition vector.h:381
ConstIterator end() const
Return a const iterator pointing to the end of the Vector.
Definition vector.h:746
friend void swap(Vector &a, Vector &b)
Definition vector.h:542
Reference front()
Access the first element of the Vector.
Definition vector.h:301
Iterator begin()
Return an iterator pointing to the beginning of the Vector.
Definition vector.h:725
static const int SIZE
Size of the vector at compile time. It will be -1 if the Vector has dynamic size.
Definition vector.h:111
Container::const_pointer ConstPointer
A const pointer to the type of the elements stored in the Vector.
Definition vector.h:96
The View class is a simple class that stores and exposes two iterators begin and end.
Definition view.h:67
Exception thrown when the size (generally of a container) is not the expected one.
Definition misc.h:38
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48