Visual Computing Library
Loading...
Searching...
No Matches
polymorphic_object_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_POLYMORPHIC_OBJECT_VECTOR_H
24#define VCL_SPACE_CORE_VECTOR_POLYMORPHIC_OBJECT_VECTOR_H
25
26#include "pointer_vector.h"
27
28#include <vclib/concepts/polymorphism.h>
29
30namespace vcl {
31
64template<Cloneable T, int N = -1>
65class PolymorphicObjectVector : protected PointerVector<std::shared_ptr<T>, N>
66{
68 using BaseVector = Base::Vector;
69
70public:
71 // types
72 using ValueType = Base::ValueType;
73
75
76 // the iterator of this vector is the const iterator of the base Vector
77 // because we don't want to allow to modify the shared pointers contained
78 // in the vector, but allow only to modify the objects pointed by the shared
79 // pointers
80 using Iterator = BaseVector::ConstIterator;
81
82 // the const iterator of this vector is the const iterator of the
83 // PointerVector, which transforms pointers to const pointers
85
86 using Base::SIZE;
87
88 // constructors
89 using Base::Base;
90
91 // exposing members of base class, later we will redefine non-const members
92 // that should return values instead of references, and functions that
93 // allow to set directly shared pointers
94 using Base::at;
95 using Base::atMod;
96 using Base::back;
97 using Base::clear;
98 using Base::contains;
99 using Base::data;
100 using Base::empty;
101 using Base::erase;
102 using Base::fill;
103 using Base::find;
104 using Base::front;
105 using Base::indexOf;
106 using Base::insert;
107 using Base::pushBack;
108 using Base::resize;
109 using Base::set;
110 using Base::size;
111 using Base::swap;
112 using Base::operator[];
113 using Base::operator();
114 using Base::begin;
115 using Base::end;
116
128 {
129 if constexpr (N < 0) {
130 Base::resize(other.size());
131 }
132 std::transform(
133 other.begin(), other.end(), Base::begin(), [](const auto& e) {
134 return e->clone();
135 });
136 }
137
147
163 PolymorphicObjectVector(std::size_t size, const T& value)
164 {
165 if constexpr (N >= 0) {
166 if (size != N) {
167 throw WrongSizeException(
168 "Vector must have " + std::to_string(N) + " size.");
169 }
170 fill(value);
171 }
172 else {
173 resize(size, value);
174 }
175 }
176
194 template<typename ItType>
196 {
197 set(View(first, last));
198 }
199
214 template<Range RangeType>
219
233 ValueType at(uint i) { return Base::at(i); }
234
245 ValueType atMod(int i) { return Base::atMod(i); }
246
255 ValueType front() { return Base::front(); }
256
265 ValueType back() { return Base::back(); }
266
276 void set(uint i, const T& e)
277 {
278 assert(i < Base::size());
279 Base::at(i) = e.clone();
280 }
281
291 void set(Base::ConstIterator it, const T& e)
292 {
293 assert(it < Base::end());
294 Base::at(it - Base::begin()) = e.clone();
295 }
296
313 template<Range Rng>
314 void set(Rng&& r) requires InputRange<Rng, T>
315 {
316 uint n = std::ranges::distance(r);
317
318 if constexpr (N >= 0) {
319 n = std::min((uint) N, n);
320 }
321 else {
323 }
324
325 // for each element in the range, clone it and store it in the
326 // vector
327 std::transform(
328 std::ranges::begin(r),
329 std::ranges::begin(r) + n,
330 Base::begin(),
331 [](const auto& e) {
332 return e.clone();
333 });
334 }
335
345 void fill(const T& e)
346 {
347 for (uint i = 0; i < size(); i++) {
348 Base::at(i) = e.clone();
349 }
350 }
351
352 /* Member functions specific for dynamic vector */
353
368 void resize(uint n, const T& v) requires (N < 0)
369 {
370 if (n > Base::size()) {
371 uint oldSize = Base::size();
373 for (uint i = oldSize; i < n; i++) {
374 Base::at(i) = v.clone();
375 }
376 }
377 else {
379 }
380 }
381
394 void pushBack(const T& v) requires (N < 0) { Base::pushBack(v.clone()); }
395
410 void pushBack(T&& v) requires (N < 0)
411 {
412 Base::pushBack(std::move(v).clone());
413 }
414
429 void insert(uint i, const T& v) requires (N < 0)
430 {
431 assert(i < Base::size() + 1);
432 Base::insert(i, v.clone());
433 }
434
451 void insert(uint i, T&& v) requires (N < 0)
452 {
453 assert(i < Base::size() + 1);
454 Base::insert(i, std::move(v).clone());
455 }
456
463
474 {
475 a.swap(b);
476 }
477
478 /* Operators */
479
486 ValueType operator[](uint i) { return Base::operator[](i); }
487
494 ValueType operator()(uint i) { return Base::operator()(i); }
495
507
508 /* Iterator member functions */
509
515 Iterator begin() { return Base::begin(); }
516
522 Iterator end() { return Base::end(); }
523};
524
525} // namespace vcl
526
527#endif // VCL_SPACE_CORE_VECTOR_POLYMORPHIC_OBJECT_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
The PointerVector class is a utility container that stores a sequence of pointers that preserve their...
Definition pointer_vector.h:76
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
bool contains(const MakeConstPointerT< T > &e) const
Check if the Vector contains the specified element.
Definition vector.h:471
bool empty() const noexcept
Returns whether the vector is empty (i.e. whether its size is 0).
Definition vector.h:651
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition vector.h:366
void fill(const T &e)
Fill all elements of the Vector with the specified value.
Definition vector.h:457
Vector()=default
Creates an empty Vector object.
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
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition vector.h:603
void clear()
Remove all elements from the Vector.
Definition vector.h:678
friend void swap(Vector &a, Vector &b)
Definition vector.h:542
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
The PolymorphicObjectVector class is a container that stores a collection of polymorphic objects,...
Definition polymorphic_object_vector.h:66
std::size_t size() const
Returns the size of the container.
Definition vector.h:221
PolymorphicObjectVector & operator=(PolymorphicObjectVector other)
Assignment operator of the PolymorphicObjectVector.
Definition polymorphic_object_vector.h:502
ValueType atMod(int i)
Access the specified element, computing first the module of the position w.r.t. the size of the conta...
Definition polymorphic_object_vector.h:245
friend void swap(PolymorphicObjectVector &a, PolymorphicObjectVector &b)
Definition polymorphic_object_vector.h:473
void pushBack(T &&v)
Add an element to the end of the Vector.
Definition polymorphic_object_vector.h:410
ValueType at(uint i)
Access the specified element with bounds checking.
Definition polymorphic_object_vector.h:233
Iterator begin()
Return an iterator pointing to the beginning of the Vector.
Definition polymorphic_object_vector.h:515
void resize(uint n, const T &v)
Resize the Vector to the specified size.
Definition polymorphic_object_vector.h:368
void fill(const T &e)
Fill all elements of the Vector with clones of the specified value.
Definition polymorphic_object_vector.h:345
PolymorphicObjectVector(ItType first, ItType last)
Constructs the container with the contents of the range [first, last).
Definition polymorphic_object_vector.h:195
void set(Base::ConstIterator it, const T &e)
Set the value of the element at the specified position.
Definition polymorphic_object_vector.h:291
void swap(PolymorphicObjectVector &other)
Swaps the contents of the container with those of other.
Definition polymorphic_object_vector.h:462
void set(uint i, const T &e)
Set the value of the element at the specified position.
Definition polymorphic_object_vector.h:276
void pushBack(const T &v)
Add an element to the end of the Vector.
Definition polymorphic_object_vector.h:394
PolymorphicObjectVector(PolymorphicObjectVector &&other) noexcept
Move constructor.
Definition polymorphic_object_vector.h:143
ValueType operator[](uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition polymorphic_object_vector.h:486
void set(Rng &&r)
Set the elements of the Vector using the values from a range.
Definition polymorphic_object_vector.h:314
PolymorphicObjectVector(const PolymorphicObjectVector &other)
Creates a Vector object with copied of the elements of the other Vector.
Definition polymorphic_object_vector.h:127
PolymorphicObjectVector(RangeType &&rng)
Constructs the container with the contents of the range rng.
Definition polymorphic_object_vector.h:215
void insert(uint i, const T &v)
Insert an element at the specified position in the Vector.
Definition polymorphic_object_vector.h:429
void insert(uint i, T &&v)
Insert an element at the specified position in the Vector.
Definition polymorphic_object_vector.h:451
ValueType front()
Access the first element of the Vector.
Definition polymorphic_object_vector.h:255
PolymorphicObjectVector(std::size_t size, const T &value)
Creates a Vector object with the specified size.
Definition polymorphic_object_vector.h:163
ValueType back()
Access the last element of the Vector.
Definition polymorphic_object_vector.h:265
Iterator end()
Return an iterator pointing to the end of the Vector.
Definition polymorphic_object_vector.h:522
ValueType operator()(uint i)
Returns a reference to the element at specified location i. No bounds checking is performed.
Definition polymorphic_object_vector.h:494
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
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