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 <memory>
22#include <sstream>
23
24namespace cbm::algo::ca
25{
38 template<class T>
39 class Vector : private std::vector<T> {
40 friend class boost::serialization::access;
41
42 public:
43 using Tbase = std::vector<T>;
44 using value_type = T;
45 using allocator_type = typename Tbase::allocator_type;
46 using pointer = typename std::allocator_traits<allocator_type>::pointer;
47 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
50 using size_type = typename Tbase::size_type;
51 using difference_type = typename Tbase::difference_type;
52 using iterator = typename Tbase::iterator;
53 using const_iterator = typename Tbase::const_iterator;
54 using reverse_iterator = std::reverse_iterator<iterator>;
55 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
56
57
59 template<typename... Tinput>
60 Vector(Tinput... value) : Tbase(value...)
61 {
62 }
63
65 template<typename... Tinput>
66 Vector(const char* name, Tinput... value) : Tbase(value...)
67 , fName(name)
68 {
69 }
70
72 Vector(const std::string& name, std::initializer_list<T> init) : Tbase(init), fName(name) {}
73
75 Vector(const Vector& v) : Tbase() { *this = v; }
76
78 Vector(Vector&& v) noexcept : Tbase(std::move(v)), fName(std::move(v.fName)) {}
79
82 {
83 if (this != &v) {
84 fName = v.fName;
85 Tbase::reserve(v.capacity()); // make sure that the capacity is transmitted
86 Tbase::assign(v.begin(), v.end());
87 }
88 return *this;
89 }
90
92 Vector& operator=(Vector&& v) noexcept
93 {
94 if (this != &v) {
95 std::swap(fName, v.fName);
96 Tbase::swap(v);
97 }
98 return *this;
99 }
100
102 void swap(Vector& v) noexcept
103 {
104 if (this != &v) {
105 std::swap(fName, v.fName);
106 Tbase::swap(v);
107 }
108 }
109
112 void SetName(const std::string& s) { fName = s; }
113
116 void SetName(const std::basic_ostream<char>& s)
117 {
118 // helps to set a composed name in a single line via:
119 // SetName(std::stringstream()<<"my name "<<..whatever..);
120 fName = dynamic_cast<const std::stringstream&>(s).str();
121 }
122
124 std::string GetName() const
125 {
126 std::string s = "ca::Vector<";
127 s += fName + "> ";
128 return s;
129 }
130
134 template<typename... Tinput>
135 void reset(std::size_t count, Tinput... value)
136 {
137 // does the same as Tbase::assign(), but works with the default T constructor too
138 // (no second parameter)
139 Tbase::clear();
140 Tbase::resize(count, value...);
141 }
142
146 template<typename... Tinput>
147 void enlarge(std::size_t count, Tinput... value)
148 {
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());
153 }
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.";
158 }
159 Tbase::resize(count, value...);
160 }
161
164 void shrink(std::size_t count)
165 {
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());
170 }
171 Tbase::resize(count);
172 }
173
176 void reserve(std::size_t count)
177 {
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());
182 }
183 Tbase::reserve(count);
184 }
185
189 template<typename Tinput>
190 void push_back(Tinput value)
191 {
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.";
195 }
196 Tbase::push_back(value);
197 }
198
201 template<typename Tinput>
202 void push_back_no_warning(Tinput value)
203 {
204 Tbase::push_back(value);
205 }
206
209 template<typename... Tinput>
210 void emplace_back(Tinput&&... value)
211 {
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.";
215 }
216 Tbase::emplace_back(value...);
217 }
218
221 T& operator[](std::size_t pos)
222 {
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());
227 }
228 return Tbase::operator[](pos);
229 }
230
233 const T& operator[](std::size_t pos) const
234 {
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());
239 }
240 return Tbase::operator[](pos);
241 }
242
244 T& back()
245 {
246 if (Tbase::size() == 0) {
247 LOG(fatal) << "ca::Vector \"" << fName << "\": trying to access element of an empty vector";
248 assert(Tbase::size() > 0);
249 }
250 return Tbase::back();
251 }
252
254 const T& back() const
255 {
256 if (Tbase::size() == 0) {
257 LOG(fatal) << "ca::Vector \"" << fName << "\": trying to access element of an empty vector";
258 assert(Tbase::size() > 0);
259 }
260 return Tbase::back();
261 }
262
263 using Tbase::begin;
264 using Tbase::capacity;
265 using Tbase::cbegin;
266 using Tbase::cend;
267 using Tbase::clear;
268 using Tbase::end;
269 using Tbase::insert; //TODO:: make it private
270 using Tbase::pop_back;
271 using Tbase::rbegin;
272 using Tbase::reserve;
273 using Tbase::shrink_to_fit;
274 using Tbase::size;
275
276
277 private:
278 std::string fName{"no name"};
279 using Tbase::assign; // use reset() instead
280 using Tbase::at;
281 using Tbase::resize;
282
284 template<class Archive>
285 void serialize(Archive& ar, const unsigned int /*version*/)
286 {
287 ar& boost::serialization::base_object<Tbase>(*this);
288 ar& fName;
289 }
290 };
291
297 template<>
298 class Vector<bool> {
299 };
300} // namespace cbm::algo::ca
301
302#endif // CA_CORE_CaVector_h
fscal v[fmask::Size]
Definition KfSimdPseudo.h:4
Vector(Tinput... value)
Definition CaVector.h:60
void swap(Vector &v) noexcept
Swap operator.
Definition CaVector.h:102
const T & back() const
Constant access to the last element of the vector.
Definition CaVector.h:254
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
Definition CaVector.h:47
void SetName(const std::string &s)
Sets the name of the vector.
Definition CaVector.h:112
void shrink(std::size_t count)
Reduces the vector to a given size.
Definition CaVector.h:164
typename Tbase::difference_type difference_type
Definition CaVector.h:51
void push_back(Tinput value)
Pushes back a value to the vector.
Definition CaVector.h:190
Vector(Vector &&v) noexcept
Move constructor.
Definition CaVector.h:78
T & back()
Mutable access to the last element of the vector.
Definition CaVector.h:244
std::reverse_iterator< iterator > reverse_iterator
Definition CaVector.h:54
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition CaVector.h:55
void SetName(const std::basic_ostream< char > &s)
Sets the name of the vector.
Definition CaVector.h:116
Vector & operator=(const Vector &v)
Copy assignment operator.
Definition CaVector.h:81
Vector(const char *name, Tinput... value)
Generic constructor from vairadic parameter list including the name of the vector.
Definition CaVector.h:66
typename Tbase::size_type size_type
Definition CaVector.h:50
const T & operator[](std::size_t pos) const
Constant access to the element by its index.
Definition CaVector.h:233
typename Tbase::iterator iterator
Definition CaVector.h:52
typename Tbase::allocator_type allocator_type
Definition CaVector.h:45
const value_type & const_reference
Definition CaVector.h:49
void serialize(Archive &ar, const unsigned int)
Serialization function for the vector.
Definition CaVector.h:285
void enlarge(std::size_t count, Tinput... value)
Enlarges the vector to the new size.
Definition CaVector.h:147
std::string GetName() const
Gets name of the vector.
Definition CaVector.h:124
Vector(Tinput... value)
Generic constructor from vairadic parameter list.
Definition CaVector.h:60
Vector(const std::string &name, std::initializer_list< T > init)
Constructor to make initializations like ca::Vector<int> myVector {"MyVector", {1,...
Definition CaVector.h:72
typename std::allocator_traits< allocator_type >::pointer pointer
Definition CaVector.h:46
Vector & operator=(Vector &&v) noexcept
Move assignment operator.
Definition CaVector.h:92
std::vector< Iteration > Tbase
Definition CaVector.h:43
void push_back_no_warning(Tinput value)
Pushes back a value to the vector without testing for the memory re-alocation.
Definition CaVector.h:202
T & operator[](std::size_t pos)
Mutable access to the element by its index.
Definition CaVector.h:221
void reserve(std::size_t count)
Reserves a new size for the vector.
Definition CaVector.h:176
Vector(const Vector &v)
Copy constructor.
Definition CaVector.h:75
void reset(std::size_t count, Tinput... value)
Clears vector and resizes it to the selected size with selected values.
Definition CaVector.h:135
typename Tbase::const_iterator const_iterator
Definition CaVector.h:53
void emplace_back(Tinput &&... value)
Creates a parameter in the end of the vector.
Definition CaVector.h:210
TODO: SZh 8.11.2022: add selection of parameterisation.
Definition CaBranch.h:14