Visual Computing Library  devel
Loading...
Searching...
No Matches
vector.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_VECTOR_VECTOR_H
24#define VCL_SPACE_CORE_VECTOR_VECTOR_H
25
26#include <vclib/base.h>
27
28#include <algorithm>
29#include <array>
30#include <cassert>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace vcl {
36
60template<typename T, int N>
61class Vector
62{
63 // if we use the vector, the size of the array will be 0
64 // actually the array will never be used and will not use memory, it's just
65 // for declaration
66 static const int ARRAY_SIZE = N >= 0 ? N : 0;
67
68 // the Container type will be array or vector, depending on N value
69 using Container = std::conditional_t<
70 (N >= 0),
71 typename std::array<T, ARRAY_SIZE>,
72 typename std::vector<T>>;
73
74protected:
75 Container mContainer;
76
77public:
79 using ValueType = Container::value_type;
80
85 using ConstReference = Container::const_reference;
86
88 using Reference = Container::reference;
89
93 using ConstPointer = Container::const_pointer;
96 using Pointer = Container::pointer;
99 using Iterator = Container::iterator;
102 using ConstIterator = Container::const_iterator;
108 static const int SIZE = N;
117 Vector() = default;
118
134 Vector(std::size_t size, const T& value = T())
135 {
136 if constexpr (N >= 0) {
137 if (size != N) {
138 throw WrongSizeException(
139 "Vector must have " + std::to_string(N) + " size.");
140 }
141 fill(value);
142 }
143 else {
144 mContainer.resize(size, value);
145 }
146 }
147
165 template<typename ItType>
167 {
168 set(View(first, last));
169 }
170
185 template<Range RangeType>
187 {
188 set(rng);
189 }
190
205 explicit Vector(std::initializer_list<T> list)
206 {
207 set(View(std::begin(list), std::end(list)));
208 }
209
218 std::size_t size() const
219 {
220 if constexpr (N >= 0) {
221 return N;
222 }
223 else {
224 return mContainer.size();
225 }
226 }
227
241 Reference at(uint i) { return mContainer.at(i); }
242
256 ConstReference at(uint i) const { return mContainer.at(i); }
257
269 {
270 int n = size(); // need to save n as int to avoid unwanted casts
271 return mContainer[(i % n + n) % n];
272 }
273
285 {
286 int n = size(); // need to save n as int to avoid unwanted casts
287 return mContainer[(i % n + n) % n];
288 }
289
298 Reference front() { return mContainer.front(); }
299
308 ConstReference front() const { return mContainer.front(); }
309
318 Reference back() { return mContainer.back(); }
319
328 ConstReference back() const { return mContainer.back(); }
329
340 Pointer data() { return mContainer.data(); }
341
352 ConstPointer data() const { return mContainer.data(); }
353
363 void set(uint i, const T& e)
364 {
365 assert(i < size());
366 mContainer[i] = e;
367 }
368
378 void set(uint i, T&& e)
379 {
380 assert(i < size());
381 mContainer[i] = std::move(e);
382 }
383
393 void set(ConstIterator it, const T& e)
394 {
395 assert(it < end());
396 mContainer[it - begin()] = e;
397 }
398
408 void set(ConstIterator it, T&& e)
409 {
410 assert(it < end());
411 mContainer[it - begin()] = std::move(e);
412 }
413
430 template<Range Rng>
431 void set(Rng&& r) requires InputRange<Rng, T>
432 {
433 if constexpr (N >= 0) {
434 uint n = std::min(
435 N,
436 (int) std::distance(
437 std::ranges::begin(r), std::ranges::end(r)));
438 std::copy_n(std::ranges::begin(r), n, mContainer.begin());
439 }
440 else {
441 mContainer =
442 std::vector<T>(std::ranges::begin(r), std::ranges::end(r));
443 }
444 }
445
454 void fill(const T& e)
455 {
456 std::fill(mContainer.begin(), mContainer.end(), e);
457 }
458
468 bool contains(const MakeConstPointerT<T>& e) const
469 {
470 return std::find(mContainer.begin(), mContainer.end(), e) !=
471 mContainer.end();
472 }
473
485 {
486 return std::find(mContainer.begin(), mContainer.end(), e);
487 }
488
500 {
501 return std::find(mContainer.begin(), mContainer.end(), e);
502 }
503
516 uint indexOf(const MakeConstPointerT<T>& e) const
517 {
518 auto it = find(e);
519 if (it == end())
520 return UINT_NULL;
521 else
522 return it - begin();
523 }
524
529 void swap(Vector& other) { mContainer.swap(other.mContainer); }
530
539 friend void swap(Vector& a, Vector& b) { a.swap(b); }
540
541 /* Member functions specific for dynamic vector */
542
554 void resize(uint n, const T& v = T()) requires (N < 0)
555 {
556 mContainer.resize(n, v);
557 }
558
570 void pushBack(const T& v) requires (N < 0) { mContainer.push_back(v); }
571
583 void pushBack(T&& v) requires (N < 0)
584 {
585 mContainer.push_back(std::move(v));
586 }
587
600 void insert(uint i, const T& v) requires (N < 0)
601 {
602 assert(i < size() + 1);
603 mContainer.insert(mContainer.begin() + i, v);
604 }
605
618 void insert(uint i, T&& v) requires (N < 0)
619 {
620 assert(i < size() + 1);
621 mContainer.insert(mContainer.begin() + i, std::move(v));
622 }
623
636 template<typename... Args>
637 void emplace(uint i, Args&&... args) requires (N < 0)
638 {
639 assert(i < size() + 1);
640 mContainer.emplace(mContainer.begin() + i, std::forward<Args>(args)...);
641 }
642
648 bool empty() const noexcept { return mContainer.empty(); }
649
661 void erase(uint i) requires (N < 0)
662 {
663 assert(i < size());
664 mContainer.erase(mContainer.begin() + i);
665 }
666
675 void clear() requires (N < 0) { mContainer.clear(); }
676
677 void serialize(std::ostream& os) const { vcl::serialize(os, mContainer); }
678
679 void deserialize(std::istream& is) { vcl::deserialize(is, mContainer); }
680
681 /* Operators */
682
689 Reference operator[](uint i) { return mContainer[i]; }
690
697 ConstReference operator[](uint i) const { return mContainer[i]; }
698
705 Reference operator()(uint i) { return mContainer[i]; }
706
713 ConstReference operator()(uint i) const { return mContainer[i]; }
714
715 /* Iterator member functions */
716
722 Iterator begin() { return mContainer.begin(); }
723
729 Iterator end() { return mContainer.end(); }
730
736 ConstIterator begin() const { return mContainer.begin(); }
737
743 ConstIterator end() const { return mContainer.end(); }
744};
745
746} // namespace vcl
747
748#endif // VCL_SPACE_CORE_VECTOR_VECTOR_H
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
Utility concept that is evaluated true the Range R is an Input Range and has a value_type that is con...
Definition range.h:89
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