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>
39 class Vector :
private std::vector<T> {
40 friend class boost::serialization::access;
46 using pointer =
typename std::allocator_traits<allocator_type>::pointer;
47 using const_pointer =
typename std::allocator_traits<allocator_type>::const_pointer;
59 template<
typename... Tinput>
65 template<
typename... Tinput>
72 Vector(
const std::string& name, std::initializer_list<T> init) :
Tbase(init),
fName(name) {}
85 Tbase::reserve(
v.capacity());
86 Tbase::assign(
v.begin(),
v.end());
105 std::swap(
fName,
v.fName);
116 void SetName(
const std::basic_ostream<char>& s)
120 fName =
dynamic_cast<const std::stringstream&
>(s).str();
126 std::string s =
"ca::Vector<";
134 template<
typename... Tinput>
135 void reset(std::size_t count, Tinput... value)
140 Tbase::resize(count, value...);
146 template<
typename... Tinput>
147 void enlarge(std::size_t count, Tinput... value)
149 if (count < Tbase::size()) {
150 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::enlarge(" << count
151 <<
"): the new size is smaller than the current one " << Tbase::size() <<
", something goes wrong.";
152 assert(count >= Tbase::size());
154 if ((!Tbase::empty()) && (count > Tbase::capacity())) {
155 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::enlarge(" << count <<
"): allocated capacity of "
156 << Tbase::capacity() <<
" is reached, the vector of size " << Tbase::size()
157 <<
" will be copied to the new place.";
159 Tbase::resize(count, value...);
166 if (count > Tbase::size()) {
167 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::shrink(" << count
168 <<
"): the new size is bigger than the current one " << Tbase::size() <<
", something goes wrong.";
169 assert(count < Tbase::size());
171 Tbase::resize(count);
178 if (!Tbase::empty()) {
179 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\"::reserve(" << count <<
"): the vector is not empty; "
180 <<
" it will be copied to the new place.";
181 assert(Tbase::empty());
183 Tbase::reserve(count);
189 template<
typename Tinput>
192 if (Tbase::size() >= Tbase::capacity()) {
193 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::push_back(): allocated capacity of " << Tbase::capacity()
194 <<
" is reached, re-allocate and copy.";
196 Tbase::push_back(value);
201 template<
typename Tinput>
204 Tbase::push_back(value);
209 template<
typename... Tinput>
212 if (Tbase::size() >= Tbase::capacity()) {
213 LOG(warning) <<
"ca::Vector \"" <<
fName <<
"\"::emplace_back(): allocated capacity of " << Tbase::capacity()
214 <<
" is reached, re-allocate and copy.";
216 Tbase::emplace_back(value...);
223 if (
pos >= Tbase::size()) {
224 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element " <<
pos
225 <<
" outside of the vector of the size of " << Tbase::size();
226 assert(
pos < Tbase::size());
228 return Tbase::operator[](
pos);
235 if (
pos >= Tbase::size()) {
236 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element " <<
pos
237 <<
" outside of the vector of the size of " << Tbase::size();
238 assert(
pos < Tbase::size());
240 return Tbase::operator[](
pos);
246 if (Tbase::size() == 0) {
247 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element of an empty vector";
248 assert(Tbase::size() > 0);
250 return Tbase::back();
256 if (Tbase::size() == 0) {
257 LOG(fatal) <<
"ca::Vector \"" <<
fName <<
"\": trying to access element of an empty vector";
258 assert(Tbase::size() > 0);
260 return Tbase::back();
264 using Tbase::capacity;
270 using Tbase::pop_back;
272 using Tbase::reserve;
273 using Tbase::shrink_to_fit;
284 template<
class Archive>
287 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.
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
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.
typename Tbase::difference_type difference_type
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.
std::reverse_iterator< iterator > reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
void SetName(const std::basic_ostream< char > &s)
Sets the name of the vector.
Vector & operator=(const Vector &v)
Copy assignment operator.
Vector(const char *name, Tinput... value)
Generic constructor from vairadic parameter list including the name of the vector.
typename Tbase::size_type size_type
const T & operator[](std::size_t pos) const
Constant access to the element by its index.
typename Tbase::iterator iterator
typename Tbase::allocator_type allocator_type
const value_type & const_reference
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,...
typename std::allocator_traits< allocator_type >::pointer pointer
Vector & operator=(Vector &&v) noexcept
Move assignment operator.
std::vector< Iteration > Tbase
void push_back_no_warning(Tinput value)
Pushes back a value to the vector without testing for the memory re-alocation.
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.
typename Tbase::const_iterator const_iterator
void emplace_back(Tinput &&... value)
Creates a parameter in the end of the vector.
TODO: SZh 8.11.2022: add selection of parameterisation.