6#ifndef YARP_SIG_VECTORBASE_H
7#define YARP_SIG_VECTORBASE_H
11#include <initializer_list>
24template <
typename Derived,
typename Scalar>
30 using iterator =
typename std::vector<Scalar>::iterator;
37 auto& v =
static_cast<Derived&
>(*this);
38 v.privVec().resize(
size);
47 auto& v =
static_cast<Derived&
>(*this);
48 v.privVec().assign(values.begin(), values.end());
59 auto& v =
static_cast<Derived&
>(*this);
60 v.privVec().assign(s, def);
70 auto& v =
static_cast<Derived&
>(*this);
71 v.privVec().assign(p, p + s);
88 const auto& v =
static_cast<const Derived&
>(*this);
89 return v.privVec().capacity();
98 auto& v =
static_cast<Derived&
>(*this);
99 v.privVec().reserve(
size);
107 auto& v =
static_cast<Derived&
>(*this);
108 v.privVec().resize(
size);
116 auto& v =
static_cast<Derived&
>(*this);
117 v.privVec().pop_back();
127 auto& v =
static_cast<Derived&
>(*this);
128 return v.privVec()[i];
138 const auto& v =
static_cast<const Derived&
>(*this);
139 return v.privVec()[i];
149 auto& v =
static_cast<Derived&
>(*this);
150 return v.privVec()[i];
160 const auto& v =
static_cast<const Derived&
>(*this);
161 return v.privVec()[i];
169 const auto& v =
static_cast<const Derived&
>(*this);
170 return v.privVec().size();
175 auto& v =
static_cast<Derived&
>(*this);
176 if (!v.privVec().empty()) {
177 std::fill(v.privVec().begin(), v.privVec().end(), val);
186 Derived&
operator=(std::initializer_list<Scalar> values)
188 auto& v =
static_cast<Derived&
>(*this);
189 v.privVec().assign(values.begin(), values.end());
206 if (l.size() != r.size())
return false;
207 for (
size_t i = 0; i < l.size(); ++i) {
208 if (l[i] != r[i])
return false;
218 auto& v =
static_cast<Derived&
>(*this);
227 auto& v =
static_cast<Derived&
>(*this);
228 v.privVec().push_back(elem);
237 auto& v =
static_cast<Derived&
>(*this);
238 v.privVec().push_back(std::move(elem));
247 auto& v =
static_cast<Derived&
>(*this);
248 return v.privVec().empty() ? nullptr : &(v.privVec().at(0));
256 inline const Scalar*
data()
const
258 auto& v =
static_cast<const Derived&
>(*this);
259 return v.privVec().empty() ? nullptr : &(v.privVec().at(0));
268 Derived
subVector(
unsigned int first,
unsigned int last)
const
271 if ((first<=last)&&((
int)last<(
int)this->
size()))
273 ret.resize(last-first+1);
274 for (
unsigned int k = first; k <= last; k++) {
275 ret[k - first] = (*this)[k];
288 auto& v =
static_cast<Derived&
>(*this);
289 v.privVec().resize(
size);
290 std::fill(v.privVec().begin(), v.privVec().end(), def);
298 auto& v =
static_cast<Derived&
>(*this);
299 std::fill(v.privVec().begin(), v.privVec().end(), Scalar(0));
307 auto& v =
static_cast<Derived&
>(*this);
308 return v.privVec().begin();
313 auto& v =
static_cast<Derived&
>(*this);
314 return v.privVec().end();
319 auto& v =
static_cast<const Derived&
>(*this);
320 return v.privVec().begin();
325 auto& v =
static_cast<const Derived&
>(*this);
326 return v.privVec().end();
331 auto& v =
static_cast<const Derived&
>(*this);
332 return v.privVec().cbegin();
341 auto& v =
static_cast<Derived&
>(*this);
342 v.privVec().erase(pos);
352 auto& v =
static_cast<Derived&
>(*this);
353 v.privVec().erase(first, last);
358 return sizeof(Scalar);
typename std::vector< Scalar >::iterator iterator
typename std::vector< Scalar >::const_iterator const_iterator
Derived subVector(unsigned int first, unsigned int last) const
Creates and returns a new vector, being the portion of the original vector defined by the first and l...
const_iterator begin() const noexcept
void resize(size_t size, const Scalar &def)
Resize the vector and initialize the element to a default value.
Scalar * data()
Return a pointer to the first element of the vector.
VectorBase(VectorBase< Derived, Scalar > &&other) noexcept=default
const Scalar & operator[](size_t i) const
Single element access, no range check, const version.
void zero()
Set to zero the elements of the vector.
VectorBase< Derived, Scalar > & operator=(const VectorBase< Derived, Scalar > &r)=default
void reserve(size_t size)
Reserve space in the vector.
size_t size() const
Returns the number of elements in the vector.
iterator begin() noexcept
Returns an iterator to the beginning of the Vector.
VectorBase(const VectorBase &r)=default
const Scalar & operator()(size_t i) const
Single element access, no range check, const version.
void erase(iterator first, iterator last)
Remove one or more elements from the vector.
Derived & operator=(const Scalar &val)
size_t capacity() const
Returns the capacity of the vector.
const Scalar * data() const
Return a pointer to the first element of the vector, const version.
void push_back(Scalar &&elem)
Move a new element in the vector: size is changed.
const_iterator end() const noexcept
const_iterator cbegin() const noexcept
void resize(size_t size)
Resize the vector.
Scalar & operator()(size_t i)
Single element access, no range check.
VectorBase(std::initializer_list< Scalar > values)
Initializer list constructor.
void push_back(const Scalar &elem)
Push a new element in the vector: size is changed.
void erase(iterator pos)
Remove an element from the vector.
VectorBase & operator=(VectorBase< Derived, Scalar > &&other) noexcept=default
VectorBase(size_t s, const Scalar &def)
Build a vector and initialize it with def.
friend bool operator==(const Derived &l, const Derived &r)
VectorBase(size_t s, const Scalar *p)
Builds a vector and initialize it with values from 'p'.
size_t getElementSize() const
Derived & operator=(std::initializer_list< Scalar > values)
Initializer list assignment.
Scalar & operator[](size_t i)
Single element access, no range check.
void clear()
Clear (removes all elements) the vector.
void pop_back()
Pop an element out of the vector: size is changed.