Visual Computing Library
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/concepts/ranges/range.h>
27#include <vclib/exceptions.h>
28#include <vclib/io/serialization.h>
29#include <vclib/types.h>
30
31#include <algorithm>
32#include <array>
33#include <cassert>
34#include <string>
35#include <utility>
36#include <vector>
37
38namespace vcl {
39
63template<typename T, int N>
64class Vector
65{
66 // if we use the vector, the size of the array will be 0
67 // actually the array will never be used and will not use memory, it's just
68 // for declaration
69 static const int ARRAY_SIZE = N >= 0 ? N : 0;
70
71 // the Container type will be array or vector, depending on N value
72 using Container = std::conditional_t<
73 (N >= 0),
74 typename std::array<T, ARRAY_SIZE>,
75 typename std::vector<T>>;
76
77protected:
78 Container mContainer;
79
80public:
82 using ValueType = Container::value_type;
83
88 using ConstReference = Container::const_reference;
89
91 using Reference = Container::reference;
92
96 using ConstPointer = Container::const_pointer;
99 using Pointer = Container::pointer;
102 using Iterator = Container::iterator;
105 using ConstIterator = Container::const_iterator;
111 static const int SIZE = N;
120 Vector() = default;
121
137 Vector(std::size_t size, const T& value = T())
138 {
139 if constexpr (N >= 0) {
140 if (size != N) {
141 throw WrongSizeException(
142 "Vector must have " + std::to_string(N) + " size.");
143 }
144 fill(value);
145 }
146 else {
147 mContainer.resize(size, value);
148 }
149 }
150
168 template<typename ItType>
170 {
171 set(View(first, last));
172 }
173
188 template<Range RangeType>
190 {
191 set(rng);
192 }
193
208 explicit Vector(std::initializer_list<T> list)
209 {
210 set(View(std::begin(list), std::end(list)));
211 }
212
221 std::size_t size() const
222 {
223 if constexpr (N >= 0) {
224 return N;
225 }
226 else {
227 return mContainer.size();
228 }
229 }
230
244 Reference at(uint i) { return mContainer.at(i); }
245
259 ConstReference at(uint i) const { return mContainer.at(i); }
260
272 {
273 int n = size(); // need to save n as int to avoid unwanted casts
274 return mContainer[(i % n + n) % n];
275 }
276
288 {
289 int n = size(); // need to save n as int to avoid unwanted casts
290 return mContainer[(i % n + n) % n];
291 }
292
301 Reference front() { return mContainer.front(); }
302
311 ConstReference front() const { return mContainer.front(); }
312
321 Reference back() { return mContainer.back(); }
322
331 ConstReference back() const { return mContainer.back(); }
332
343 Pointer data() { return mContainer.data(); }
344
355 ConstPointer data() const { return mContainer.data(); }
356
366 void set(uint i, const T& e)
367 {
368 assert(i < size());
369 mContainer[i] = e;
370 }
371
381 void set(uint i, T&& e)
382 {
383 assert(i < size());
384 mContainer[i] = std::move(e);
385 }
386
396 void set(ConstIterator it, const T& e)
397 {
398 assert(it < end());
399 mContainer[it - begin()] = e;
400 }
401
411 void set(ConstIterator it, T&& e)
412 {
413 assert(it < end());
414 mContainer[it - begin()] = std::move(e);
415 }
416
433 template<Range Rng>
434 void set(Rng&& r) requires InputRange<Rng, T>
435 {
436 if constexpr (N >= 0) {
437 uint n = std::min(
438 N,
439 (int) std::distance(
440 std::ranges::begin(r), std::ranges::end(r)));
441 std::copy_n(std::ranges::begin(r), n, mContainer.begin());
442 }
443 else {
444 mContainer =
445 std::vector<T>(std::ranges::begin(r), std::ranges::end(r));
446 }
447 }
448
457 void fill(const T& e)
458 {
459 std::fill(mContainer.begin(), mContainer.end(), e);
460 }
461
471 bool contains(const MakeConstPointerT<T>& e) const
472 {
473 return std::find(mContainer.begin(), mContainer.end(), e) !=
474 mContainer.end();
475 }
476
488 {
489 return std::find(mContainer.begin(), mContainer.end(), e);
490 }
491
503 {
504 return std::find(mContainer.begin(), mContainer.end(), e);
505 }
506
519 uint indexOf(const MakeConstPointerT<T>& e) const
520 {
521 auto it = find(e);
522 if (it == end())
523 return UINT_NULL;
524 else
525 return it - begin();
526 }
527
532 void swap(Vector& other) { mContainer.swap(other.mContainer); }
533
542 friend void swap(Vector& a, Vector& b) { a.swap(b); }
543
544 /* Member functions specific for dynamic vector */
545
557 void resize(uint n, const T& v = T()) requires (N < 0)
558 {
559 mContainer.resize(n, v);
560 }
561
573 void pushBack(const T& v) requires (N < 0) { mContainer.push_back(v); }
574
586 void pushBack(T&& v) requires (N < 0)
587 {
588 mContainer.push_back(std::move(v));
589 }
590
603 void insert(uint i, const T& v) requires (N < 0)
604 {
605 assert(i < size() + 1);
606 mContainer.insert(mContainer.begin() + i, v);
607 }
608
621 void insert(uint i, T&& v) requires (N < 0)
622 {
623 assert(i < size() + 1);
624 mContainer.insert(mContainer.begin() + i, std::move(v));
625 }
626
639 template<typename... Args>
640 void emplace(uint i, Args&&... args) requires (N < 0)
641 {
642 assert(i < size() + 1);
643 mContainer.emplace(mContainer.begin() + i, std::forward<Args>(args)...);
644 }
645
651 bool empty() const noexcept { return mContainer.empty(); }
652
664 void erase(uint i) requires (N < 0)
665 {
666 assert(i < size());
667 mContainer.erase(mContainer.begin() + i);
668 }
669
678 void clear() requires (N < 0) { mContainer.clear(); }
679
680 void serialize(std::ostream& os) const { vcl::serialize(os, mContainer); }
681
682 void deserialize(std::istream& is) { vcl::deserialize(is, mContainer); }
683
684 /* Operators */
685
692 Reference operator[](uint i) { return mContainer[i]; }
693
700 ConstReference operator[](uint i) const { return mContainer[i]; }
701
708 Reference operator()(uint i) { return mContainer[i]; }
709
716 ConstReference operator()(uint i) const { return mContainer[i]; }
717
718 /* Iterator member functions */
719
725 Iterator begin() { return mContainer.begin(); }
726
732 Iterator end() { return mContainer.end(); }
733
739 ConstIterator begin() const { return mContainer.begin(); }
740
746 ConstIterator end() const { return mContainer.end(); }
747};
748
749} // namespace vcl
750
751#endif // VCL_SPACE_CORE_VECTOR_VECTOR_H
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
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