11#ifndef CA_CORE_CaVector_h
12#define CA_CORE_CaVector_h 1
16#include <boost/serialization/access.hpp>
17#include <boost/serialization/base_object.hpp>
18#include <boost/serialization/string.hpp>
19#include <boost/serialization/vector.hpp>
38 class Vector :
private std::vector<T> {
45 template<
typename... Tinput>
51 template<
typename... Tinput>
58 Vector(
const std::string& name, std::initializer_list<T> init) :
Tbase(init),
fName(name) {}
71 Tbase::reserve(
v.capacity());
72 Tbase::assign(
v.begin(),
v.end());
102 void SetName(
const std::basic_ostream<char>& s)
106 fName =
dynamic_cast<const std::stringstream&
>(s).str();
112 std::string s =
"ca::Vector<";
120 template<
typename... Tinput>
121 void reset(std::size_t count, Tinput... value)
126 Tbase::resize(count, value...);
132 template<
typename... Tinput>
133 void enlarge(std::size_t count, Tinput... value)
135 if (count < Tbase::size()) {
136 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::enlarge(" << count
137 <<
"): the new size is smaller than the current one " << Tbase::size() <<
", something goes wrong.";
138 assert(count >= Tbase::size());
140 if ((!Tbase::empty()) && (count > Tbase::capacity())) {
141 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::enlarge(" << count <<
"): allocated capacity of "
142 << Tbase::capacity() <<
" is reached, the vector of size " << Tbase::size()
143 <<
" will be copied to the new place.";
145 Tbase::resize(count, value...);
152 if (count > Tbase::size()) {
153 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::shrink(" << count
154 <<
"): the new size is bigger than the current one " << Tbase::size() <<
", something goes wrong.";
155 assert(count < Tbase::size());
157 Tbase::resize(count);
164 if (!Tbase::empty()) {
165 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::reserve(" << count <<
"): the vector is not empty; "
166 <<
" it will be copied to the new place.";
167 assert(Tbase::empty());
169 Tbase::reserve(count);
175 template<
typename Tinput>
178 if (Tbase::size() >= Tbase::capacity()) {
179 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::push_back(): allocated capacity of " << Tbase::capacity()
180 <<
" is reached, re-allocate and copy.";
182 Tbase::push_back(value);
187 template<
typename Tinput>
190 Tbase::push_back(value);
195 template<
typename... Tinput>
198 if (Tbase::size() >= Tbase::capacity()) {
199 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::emplace_back(): allocated capacity of " << Tbase::capacity()
200 <<
" is reached, re-allocate and copy.";
202 Tbase::emplace_back(value...);
209 if (
pos >= Tbase::size()) {
210 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element " <<
pos
211 <<
" outside of the vector of the size of " << Tbase::size();
212 assert(
pos < Tbase::size());
214 return Tbase::operator[](
pos);
221 if (
pos >= Tbase::size()) {
222 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element " <<
pos
223 <<
" outside of the vector of the size of " << Tbase::size();
224 assert(
pos < Tbase::size());
226 return Tbase::operator[](
pos);
232 if (Tbase::size() == 0) {
233 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element of an empty vector";
234 assert(Tbase::size() > 0);
236 return Tbase::back();
242 if (Tbase::size() == 0) {
243 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element of an empty vector";
244 assert(Tbase::size() > 0);
246 return Tbase::back();
250 using Tbase::capacity;
256 using Tbase::pop_back;
258 using Tbase::reserve;
259 using Tbase::shrink_to_fit;
261 using typename Tbase::iterator;
270 template<
class Archive>
273 ar& boost::serialization::base_object<Tbase>(*
this);
void swap(Vector &v) noexcept
Swap operator.
const T & back() const
Constant access to the last element of the vector.
void SetName(const std::string &s)
Sets the name of the vector.
void shrink(std::size_t count)
Reduces the vector to a given size.
void push_back(Tinput value)
Pushes back a value to the vector.
Vector(Vector &&v) noexcept
Move constructor.
T & back()
Mutable access to the last element of the vector.
void SetName(const std::basic_ostream< char > &s)
Sets the name of the vector.
Vector & operator=(const Vector &v)
Copy assignment operator.
std::string fName
Name of the vector.
Vector(const char *name, Tinput... value)
Generic constructor from vairadic parameter list including the name of the vector.
const T & operator[](std::size_t pos) const
Constant access to the element by its index.
void serialize(Archive &ar, const unsigned int)
Serialization function for the vector.
void enlarge(std::size_t count, Tinput... value)
Enlarges the vector to the new size.
std::string GetName() const
Gets name of the vector.
Vector(Tinput... value)
Generic constructor from vairadic parameter list.
Vector(const std::string &name, std::initializer_list< T > init)
Constructor to make initializations like ca::Vector<int> myVector {"MyVector", {1,...
Vector & operator=(Vector &&v) noexcept
Move assignment operator.
void push_back_no_warning(Tinput value)
Pushes back a value to the vector without testing for the memory re-alocation.
friend class boost::serialization::access
T & operator[](std::size_t pos)
Mutable access to the element by its index.
void reserve(std::size_t count)
Reserves a new size for the vector.
Vector(const Vector &v)
Copy constructor.
void reset(std::size_t count, Tinput... value)
Clears vector and resizes it to the selected size with selected values.
void emplace_back(Tinput &&... value)
Creates a parameter in the end of the vector.
TODO: SZh 8.11.2022: add selection of parameterisation.