23#ifndef VCL_SPACE_CORE_VECTOR_VECTOR_H
24#define VCL_SPACE_CORE_VECTOR_VECTOR_H
26#include <vclib/base.h>
60template<
typename T,
int N>
66 static const int ARRAY_SIZE = N >= 0 ? N : 0;
69 using Container = std::conditional_t<
71 typename std::array<T, ARRAY_SIZE>,
72 typename std::vector<T>>;
96 using Pointer = Container::pointer;
99 using Iterator = Container::iterator;
136 if constexpr (N >= 0) {
139 "Vector must have " + std::to_string(N) +
" size.");
144 mContainer.resize(
size, value);
165 template<
typename ItType>
185 template<Range RangeType>
205 explicit Vector(std::initializer_list<T> list)
207 set(
View(std::begin(list), std::end(list)));
220 if constexpr (N >= 0) {
224 return mContainer.size();
271 return mContainer[(
i %
n +
n) %
n];
287 return mContainer[(
i %
n +
n) %
n];
381 mContainer[
i] = std::move(e);
411 mContainer[
it -
begin()] = std::move(e);
433 if constexpr (N >= 0) {
437 std::ranges::begin(
r), std::ranges::end(
r)));
438 std::copy_n(std::ranges::begin(
r),
n, mContainer.begin());
442 std::vector<T>(std::ranges::begin(
r), std::ranges::end(
r));
456 std::fill(mContainer.begin(), mContainer.end(), e);
470 return std::find(mContainer.begin(), mContainer.end(), e) !=
486 return std::find(mContainer.begin(), mContainer.end(), e);
501 return std::find(mContainer.begin(), mContainer.end(), e);
554 void resize(uint
n,
const T& v = T())
requires (N < 0)
556 mContainer.resize(
n, v);
570 void pushBack(
const T& v)
requires (N < 0) { mContainer.push_back(v); }
585 mContainer.push_back(std::move(v));
600 void insert(uint
i,
const T& v)
requires (N < 0)
603 mContainer.insert(mContainer.begin() +
i, v);
621 mContainer.insert(mContainer.begin() +
i, std::move(v));
636 template<
typename... Args>
640 mContainer.emplace(mContainer.begin() +
i, std::forward<Args>(
args)...);
664 mContainer.erase(mContainer.begin() +
i);
675 void clear()
requires (N < 0) { mContainer.clear(); }
677 void serialize(std::ostream&
os)
const { vcl::serialize(
os, mContainer); }
679 void deserialize(std::istream& is) { vcl::deserialize(is, mContainer); }
A class representing a box in N-dimensional space.
Definition box.h:46
The ConstPointerIterator class is a utility class that wraps an iterator of a container of [shared] p...
Definition const_pointer_iterator.h:57
The Vector class is a generic container of objects of type T, that could have fixed or dynamic size,...
Definition vector.h:62
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:284
ConstReference operator[](uint i) const
Returns a const reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:697
Reference operator()(uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:705
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:268
Container::reference Reference
A reference to the type of the elements stored in the Vector.
Definition vector.h:88
std::size_t size() const
Returns the size of the container.
Definition vector.h:218
void resize(uint n, const T &v=T())
Resize the Vector to the specified size.
Definition vector.h:554
void erase(uint i)
Remove the element at the specified index from the Vector.
Definition vector.h:661
void set(ConstIterator it, const T &e)
Set the value of the element at the specified position.
Definition vector.h:393
Container::iterator Iterator
An iterator to the elements of the Vector.
Definition vector.h:99
bool contains(const MakeConstPointerT< T > &e) const
Check if the Vector contains the specified element.
Definition vector.h:468
ConstReference operator()(uint i) const
Returns a const reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:713
Reference back()
Access the last element of the Vector.
Definition vector.h:318
Vector(ItType first, ItType last)
Constructs the container with the contents of the range [first, last).
Definition vector.h:166
Container::const_iterator ConstIterator
A const iterator to the elements of the Vector.
Definition vector.h:102
void pushBack(T &&v)
Add an element to the end of the Vector.
Definition vector.h:583
bool empty() const noexcept
Returns whether the vector is empty (i.e. whether its size is 0).
Definition vector.h:648
ConstReference at(uint i) const
Access the specified element with bounds checking.
Definition vector.h:256
Container::value_type ValueType
The type of the elements stored in the Vector.
Definition vector.h:79
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition vector.h:363
Container::pointer Pointer
A pointer to the type of the elements stored in the Vector.
Definition vector.h:96
Container::const_reference ConstReference
A const reference to the type of the elements stored in the Vector.
Definition vector.h:85
void fill(const T &e)
Fill all elements of the Vector with the specified value.
Definition vector.h:454
Reference at(uint i)
Access the specified element with bounds checking.
Definition vector.h:241
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:352
Iterator find(const MakeConstPointerT< T > &e)
Find the first occurrence of the specified element in the Vector.
Definition vector.h:484
void emplace(uint i, Args &&... args)
Insert an element at the specified position in the Vector.
Definition vector.h:637
Pointer data()
Returns a pointer to the underlying array serving as element storage. The pointer is such that range ...
Definition vector.h:340
void swap(Vector &other)
Swaps the contents of the container with those of other.
Definition vector.h:529
Reference operator[](uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition vector.h:689
uint indexOf(const MakeConstPointerT< T > &e) const
Get the index of the first occurrence of the specified element in the Vector.
Definition vector.h:516
void pushBack(const T &v)
Add an element to the end of the Vector.
Definition vector.h:570
ConstReference front() const
Access the first element of the Vector.
Definition vector.h:308
ConstIterator find(const MakeConstPointerT< T > &e) const
Find the first occurrence of the specified element in the Vector.
Definition vector.h:499
void insert(uint i, T &&v)
Insert an element at the specified position in the Vector.
Definition vector.h:618
Vector(std::size_t size, const T &value=T())
Creates a Vector object with the specified size.
Definition vector.h:134
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition vector.h:600
void set(ConstIterator it, T &&e)
Set the value of the element at the specified position.
Definition vector.h:408
ConstReference back() const
Access the last element of the Vector.
Definition vector.h:328
void set(Rng &&r)
Set the elements of the Vector using the values from a range.
Definition vector.h:431
Vector(std::initializer_list< T > list)
Constructs the container with the contents of the initializer list list.
Definition vector.h:205
ConstIterator begin() const
Return a const iterator pointing to the beginning of the Vector.
Definition vector.h:736
void clear()
Remove all elements from the Vector.
Definition vector.h:675
Vector(RangeType &&rng)
Constructs the container with the contents of the range rng.
Definition vector.h:186
Iterator end()
Return an iterator pointing to the end of the Vector.
Definition vector.h:729
void set(uint i, T &&e)
Set the value of the element at the specified position.
Definition vector.h:378
ConstIterator end() const
Return a const iterator pointing to the end of the Vector.
Definition vector.h:743
friend void swap(Vector &a, Vector &b)
Definition vector.h:539
Reference front()
Access the first element of the Vector.
Definition vector.h:298
Iterator begin()
Return an iterator pointing to the beginning of the Vector.
Definition vector.h:722
static const int SIZE
Size of the vector at compile time. It will be -1 if the Vector has dynamic size.
Definition vector.h:108
Container::const_pointer ConstPointer
A const pointer to the type of the elements stored in the Vector.
Definition vector.h:93
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 exceptions.h:45
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