CbmRoot
Loading...
Searching...
No Matches
CaVector.h
Go to the documentation of this file.
1/* Copyright (C) 2021-2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergey Gorbunov [committer], Sergei Zharko */
4
8
9//#pragma once // include this header only once per compilation unit (not robust)
10
11#ifndef CA_CORE_CaVector_h
12#define CA_CORE_CaVector_h 1
13
15
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>
20
21#include <sstream>
22
23namespace cbm::algo::ca
24{
37 template<class T>
38 class Vector : private std::vector<T> {
40
41 public:
42 typedef std::vector<T> Tbase;
43
45 template<typename... Tinput>
46 Vector(Tinput... value) : Tbase(value...)
47 {
48 }
49
51 template<typename... Tinput>
52 Vector(const char* name, Tinput... value) : Tbase(value...)
53 , fName(name)
54 {
55 }
56
58 Vector(const std::string& name, std::initializer_list<T> init) : Tbase(init), fName(name) {}
59
61 Vector(const Vector& v) : Tbase() { *this = v; }
62
64 Vector(Vector&& v) noexcept : Tbase(std::move(v)), fName(std::move(v.fName)) {}
65
68 {
69 if (this != &v) {
70 fName = v.fName;
71 Tbase::reserve(v.capacity()); // make sure that the capacity is transmitted
72 Tbase::assign(v.begin(), v.end());
73 }
74 return *this;
75 }
76
78 Vector& operator=(Vector&& v) noexcept
79 {
80 if (this != &v) {
81 std::swap(fName, v.fName);
82 Tbase::swap(v);
83 }
84 return *this;
85 }
86
88 void swap(Vector& v) noexcept
89 {
90 if (this != &v) {
91 std::swap(fName, v.fName);
92 Tbase::swap(v);
93 }
94 }
95
98 void SetName(const std::string& s) { fName = s; }
99
102 void SetName(const std::basic_ostream<char>& s)
103 {
104 // helps to set a composed name in a single line via:
105 // SetName(std::stringstream()<<"my name "<<..whatever..);
106 fName = dynamic_cast<const std::stringstream&>(s).str();
107 }
108
110 std::string GetName() const
111 {
112 std::string s = "ca::Vector<";
113 s += fName + "> ";
114 return s;
115 }
116
120 template<typename... Tinput>
121 void reset(std::size_t count, Tinput... value)
122 {
123 // does the same as Tbase::assign(), but works with the default T constructor too
124 // (no second parameter)
125 Tbase::clear();
126 Tbase::resize(count, value...);
127 }
128
132 template<typename... Tinput>
133 void enlarge(std::size_t count, Tinput... value)
134 {
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());
139 }
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.";
144 }
145 Tbase::resize(count, value...);
146 }
147
150 void shrink(std::size_t count)
151 {
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());
156 }
157 Tbase::resize(count);
158 }
159
162 void reserve(std::size_t count)
163 {
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());
168 }
169 Tbase::reserve(count);
170 }
171
175 template<typename Tinput>
176 void push_back(Tinput value)
177 {
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.";
181 }
182 Tbase::push_back(value);
183 }
184
187 template<typename Tinput>
188 void push_back_no_warning(Tinput value)
189 {
190 Tbase::push_back(value);
191 }
192
195 template<typename... Tinput>
196 void emplace_back(Tinput&&... value)
197 {
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.";
201 }
202 Tbase::emplace_back(value...);
203 }
204
207 T& operator[](std::size_t pos)
208 {
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());
213 }
214 return Tbase::operator[](pos);
215 }
216
219 const T& operator[](std::size_t pos) const
220 {
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());
225 }
226 return Tbase::operator[](pos);
227 }
228
230 T& back()
231 {
232 if (Tbase::size() == 0) {
233 LOG(fatal) << "ca::Vector \"" << fName << "\": trying to access element of an empty vector";
234 assert(Tbase::size() > 0);
235 }
236 return Tbase::back();
237 }
238
240 const T& back() const
241 {
242 if (Tbase::size() == 0) {
243 LOG(fatal) << "ca::Vector \"" << fName << "\": trying to access element of an empty vector";
244 assert(Tbase::size() > 0);
245 }
246 return Tbase::back();
247 }
248
249 using Tbase::begin;
250 using Tbase::capacity;
251 using Tbase::cbegin;
252 using Tbase::cend;
253 using Tbase::clear;
254 using Tbase::end;
255 using Tbase::insert; //TODO:: make it private
256 using Tbase::pop_back;
257 using Tbase::rbegin;
258 using Tbase::reserve;
259 using Tbase::shrink_to_fit;
260 using Tbase::size;
261 using typename Tbase::iterator;
262
263 private:
264 std::string fName{"no name"};
265 using Tbase::assign; // use reset() instead
266 using Tbase::at;
267 using Tbase::resize;
268
270 template<class Archive>
271 void serialize(Archive& ar, const unsigned int /*version*/)
272 {
273 ar& boost::serialization::base_object<Tbase>(*this);
274 ar& fName;
275 }
276 };
277
283 template<>
284 class Vector<bool> {
285 };
286} // namespace cbm::algo::ca
287
288#endif // CA_CORE_CaVector_h
fscal v[fmask::Size]
Definition KfSimdPseudo.h:4
void swap(Vector &v) noexcept
Swap operator.
Definition CaVector.h:88
std::vector< T > Tbase
Definition CaVector.h:42
const T & back() const
Constant access to the last element of the vector.
Definition CaVector.h:240
void SetName(const std::string &s)
Sets the name of the vector.
Definition CaVector.h:98
void shrink(std::size_t count)
Reduces the vector to a given size.
Definition CaVector.h:150
void push_back(Tinput value)
Pushes back a value to the vector.
Definition CaVector.h:176
Vector(Vector &&v) noexcept
Move constructor.
Definition CaVector.h:64
T & back()
Mutable access to the last element of the vector.
Definition CaVector.h:230
void SetName(const std::basic_ostream< char > &s)
Sets the name of the vector.
Definition CaVector.h:102
Vector & operator=(const Vector &v)
Copy assignment operator.
Definition CaVector.h:67
std::string fName
Name of the vector.
Definition CaVector.h:264
Vector(const char *name, Tinput... value)
Generic constructor from vairadic parameter list including the name of the vector.
Definition CaVector.h:52
const T & operator[](std::size_t pos) const
Constant access to the element by its index.
Definition CaVector.h:219
void serialize(Archive &ar, const unsigned int)
Serialization function for the vector.
Definition CaVector.h:271
void enlarge(std::size_t count, Tinput... value)
Enlarges the vector to the new size.
Definition CaVector.h:133
std::string GetName() const
Gets name of the vector.
Definition CaVector.h:110
Vector(Tinput... value)
Generic constructor from vairadic parameter list.
Definition CaVector.h:46
Vector(const std::string &name, std::initializer_list< T > init)
Constructor to make initializations like ca::Vector<int> myVector {"MyVector", {1,...
Definition CaVector.h:58
Vector & operator=(Vector &&v) noexcept
Move assignment operator.
Definition CaVector.h:78
void push_back_no_warning(Tinput value)
Pushes back a value to the vector without testing for the memory re-alocation.
Definition CaVector.h:188
friend class boost::serialization::access
Definition CaVector.h:39
T & operator[](std::size_t pos)
Mutable access to the element by its index.
Definition CaVector.h:207
void reserve(std::size_t count)
Reserves a new size for the vector.
Definition CaVector.h:162
Vector(const Vector &v)
Copy constructor.
Definition CaVector.h:61
void reset(std::size_t count, Tinput... value)
Clears vector and resizes it to the selected size with selected values.
Definition CaVector.h:121
void emplace_back(Tinput &&... value)
Creates a parameter in the end of the vector.
Definition CaVector.h:196
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14