GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TPriorityValue.h
Go to the documentation of this file.
1#ifndef TPRIORITYVALUE_H
2#define TPRIORITYVALUE_H
3
4#include <ostream>
5#include <string>
6#include <vector>
7#include <cstdint>
8
9/** \addtogroup Sorting
10 * @{
11 */
12
13/////////////////////////////////////////////////////////////
14///
15/// \class TPriorityValue
16///
17/// The TPriorityValue defines a template of values with
18/// priorities. The priorities are used to over-write values
19/// set by the root-file by those read from an input file,
20/// and those by values set manually by the user
21///
22/////////////////////////////////////////////////////////////
23
24enum class EPriority : std::uint8_t { kDefault,
27 kUser,
28 kForce };
29
30// template specialization requires full duplicate of original class, take care to update all specializations when changing this!
31template <class T>
33public:
35 explicit TPriorityValue(T value, EPriority priority = EPriority::kDefault) : fValue(value), fPriority(priority) {}
36 TPriorityValue(const TPriorityValue& rhs) : fPriority(EPriority::kDefault) { *this = rhs; }
37 TPriorityValue(TPriorityValue&& rhs) noexcept : fPriority(EPriority::kDefault) { *this = rhs; }
38
39 ~TPriorityValue() = default;
40
41 // setter
42 void Set(const T& val, EPriority priority)
43 {
44 if(priority >= fPriority) {
45 fValue = val;
46 SetPriority(priority);
47 }
48 }
49
50 void SetPriority(EPriority priority)
51 {
52 // only allow the priority to be increased, not decreased
53 fPriority = std::max(priority, fPriority);
54 }
55
56 // reset functions
57 void Reset(T val)
58 {
59 fValue = val;
61 }
62
67
68 // getters
69 const T& Value() const { return fValue; }
70 EPriority Priority() const { return fPriority; }
71 T* Address() { return &fValue; }
72 const T* Address() const { return &fValue; }
73
74 // assignment and move assignment operators
76 {
78 fValue = rhs.fValue;
79 fPriority = rhs.fPriority;
80 }
81 return *this;
82 }
83
85 {
86 if(rhs.fPriority >= fPriority) {
87 fValue = std::move(rhs.fValue);
88 fPriority = rhs.fPriority;
89 }
90 return *this;
91 }
92
93 // comparison operators
95 {
96 return fValue == rhs.fValue;
97 }
99 {
100 return fValue != rhs.fValue; // could also be !(this == rhs)
101 }
103 {
104 return fValue < rhs.fValue;
105 }
107 {
108 return fValue > rhs.fValue; // could also be rhs < this
109 }
111 {
112 return fValue <= rhs.fValue; // could also be !(this>rhs)
113 }
115 {
116 return fValue >= rhs.fValue; // could also be !(this<rhs)
117 }
118
119 // comparison operators for base class
120 bool operator==(const T& rhs) const
121 {
122 return fValue == rhs;
123 }
124 bool operator!=(const T& rhs) const
125 {
126 return fValue != rhs; // could also be !(this == rhs)
127 }
128 bool operator<(const T& rhs) const
129 {
130 return fValue < rhs;
131 }
132 bool operator>(const T& rhs) const
133 {
134 return fValue > rhs; // could also be rhs < this
135 }
136 bool operator<=(const T& rhs) const
137 {
138 return fValue <= rhs; // could also be !(this>rhs)
139 }
140 bool operator>=(const T& rhs) const
141 {
142 return fValue >= rhs; // could also be !(this<rhs)
143 }
144
145 // explicit conversion
146 explicit operator T() const { return fValue; }
147
148 // streaming operator
149 template <class U>
150 friend std::ostream& operator<<(std::ostream&, const TPriorityValue<U>&);
151
152private:
155};
156
157// vector specialization
158template <class T>
159class TPriorityValue<std::vector<T>> {
160 // these are the vector specific functions, the rest is just copy-paste of the original class
161public:
162 // we only have specific (const) functions to access the vectors data
163 const T& at(size_t n) const { return fValue.at(n); }
164 const T& back() const { return fValue.back(); }
165 typename std::vector<T>::const_iterator begin() const { return fValue.begin(); }
166 typename std::vector<T>::const_iterator cbegin() const { return fValue.cbegin(); }
167 typename std::vector<T>::const_iterator cend() const { return fValue.cend(); }
168 typename std::vector<T>::const_reverse_iterator crbegin() const { return fValue.crbegin(); }
169 typename std::vector<T>::const_reverse_iterator crend() const { return fValue.crend(); }
170 bool empty() const { return fValue.empty(); }
171 typename std::vector<T>::const_iterator end() const { return fValue.end(); }
172 const T& front() const { return fValue.front(); }
173 size_t max_size() const { return fValue.max_size(); }
174 // T& operator[](size_t n) { return fValue[n]; }
175 const T& operator[](size_t n) const { return fValue[n]; }
176 typename std::vector<T>::const_reverse_iterator rbegin() const { return fValue.rbegin(); }
177 typename std::vector<T>::const_reverse_iterator rend() const { return fValue.rend(); }
178 void reserve(size_t n) { fValue.reserve(n); }
179 void shrink_to_fit() { fValue.shrink_to_fit(); }
180 size_t size() const { return fValue.size(); }
181 void resize(size_t count) { fValue.resize(count); }
182 void resize(size_t count, const T& value) { fValue.resize(count, value); }
183
184 // copy-paste of original class (with 'T ' replace by 'std::vector<T> ', 'T>' by 'std::vector<T> >', 'T& ' by 'std::vector<T>& ', and 'T* ' by 'std::vector<T>* '):
185 // minus the boolean conversion operator
187 explicit TPriorityValue(std::vector<T> value, EPriority priority = EPriority::kDefault) : fValue(value), fPriority(priority) {}
188 TPriorityValue(const TPriorityValue& rhs) : fPriority(EPriority::kDefault) { *this = rhs; }
189 TPriorityValue(TPriorityValue&& rhs) noexcept : fPriority(EPriority::kDefault) { *this = rhs; }
190
191 ~TPriorityValue() = default;
192
193 // setter
194 void Set(const std::vector<T>& val, EPriority priority)
195 {
196 if(priority >= fPriority) {
197 fValue = val;
198 SetPriority(priority);
199 }
200 }
201
202 void SetPriority(EPriority priority)
203 {
204 // only allow the priority to be increased, not decreased
205 fPriority = std::max(priority, fPriority);
206 }
207
208 // reset functions
209 void Reset(const std::vector<T>& val)
210 {
211 fValue = val;
213 }
214
215 void ResetPriority()
216 {
218 }
219
220 // getters
221 const std::vector<T>& Value() const { return fValue; }
222 EPriority Priority() const { return fPriority; }
223 std::vector<T>* Address() { return &fValue; }
224 const std::vector<T>* Address() const { return &fValue; }
225
226 // assignment and move assignment operators
227 TPriorityValue<std::vector<T>>& operator=(const TPriorityValue<std::vector<T>>& rhs)
228 {
229 if(rhs.fPriority != EPriority::kDefault && rhs.fPriority >= fPriority) {
230 fValue = rhs.fValue;
231 fPriority = rhs.fPriority;
232 }
233 return *this;
234 }
235
236 TPriorityValue<std::vector<T>>& operator=(TPriorityValue<std::vector<T>>&& rhs) noexcept
237 {
238 if(rhs.fPriority >= fPriority) {
239 fValue = std::move(rhs.fValue);
240 fPriority = rhs.fPriority; // this is trivially copyable, so std::move has no effect
241 }
242 return *this;
243 }
244
245 // comparison operators
246 bool operator==(const TPriorityValue<std::vector<T>>& rhs)
247 {
248 return fValue == rhs.fValue;
249 }
250 bool operator!=(const TPriorityValue<std::vector<T>>& rhs)
251 {
252 return fValue != rhs.fValue; // could also be !(this == rhs)
253 }
254 bool operator<(const TPriorityValue<std::vector<T>>& rhs)
255 {
256 return fValue < rhs.fValue;
257 }
258 bool operator>(const TPriorityValue<std::vector<T>>& rhs)
259 {
260 return fValue > rhs.fValue; // could also be rhs < this
261 }
262 bool operator<=(const TPriorityValue<std::vector<T>>& rhs)
263 {
264 return fValue <= rhs.fValue; // could also be !(this>rhs)
265 }
266 bool operator>=(const TPriorityValue<std::vector<T>>& rhs)
267 {
268 return fValue >= rhs.fValue; // could also be !(this<rhs)
269 }
270
271 // comparison operators for base class
272 bool operator==(const std::vector<T>& rhs) const
273 {
274 return fValue == rhs;
275 }
276 bool operator!=(const std::vector<T>& rhs) const
277 {
278 return fValue != rhs; // could also be !(this == rhs)
279 }
280 bool operator<(const std::vector<T>& rhs) const
281 {
282 return fValue < rhs;
283 }
284 bool operator>(const std::vector<T>& rhs) const
285 {
286 return fValue > rhs; // could also be rhs < this
287 }
288 bool operator<=(const std::vector<T>& rhs) const
289 {
290 return fValue <= rhs; // could also be !(this>rhs)
291 }
292 bool operator>=(const std::vector<T>& rhs) const
293 {
294 return fValue >= rhs; // could also be !(this<rhs)
295 }
296
297 // explicit conversion
298 explicit operator std::vector<T>() const { return fValue; }
299
300 // streaming operator
301 template <class U>
302 friend std::ostream& operator<<(std::ostream&, const TPriorityValue<U>&);
303
304private:
305 std::vector<T> fValue;
307};
308
309// string specialization
310template <>
311class TPriorityValue<std::string> {
312 // these are the string specific functions, the rest is just copy-paste of the original class
313public:
314 const char& at(size_t pos) const { return fValue.at(pos); }
315 const char& back() const { return fValue.back(); }
316 std::string::const_iterator begin() const { return fValue.begin(); }
317 size_t capacity() const { return fValue.capacity(); }
318 std::string::const_iterator cbegin() const { return fValue.cbegin(); }
319 std::string::const_iterator cend() const { return fValue.cend(); }
320 int compare(const std::string& str) const noexcept { return fValue.compare(str); }
321 int compare(size_t pos, size_t len, const std::string& str) const { return fValue.compare(pos, len, str); }
322 int compare(size_t pos, size_t len, const std::string& str, size_t subpos, size_t sublen) const { return fValue.compare(pos, len, str, subpos, sublen); }
323 int compare(const char* s) const { return fValue.compare(s); }
324 int compare(size_t pos, size_t len, const char* s) const { return fValue.compare(pos, len, s); }
325 int compare(size_t pos, size_t len, const char* s, size_t n) const { return fValue.compare(pos, len, s, n); }
326 size_t copy(char* s, size_t len, size_t pos = 0) const { return fValue.copy(s, len, pos); }
327 std::string::const_reverse_iterator crbegin() const { return fValue.crbegin(); }
328 std::string::const_reverse_iterator crend() const { return fValue.crend(); }
329 const char* c_str() const noexcept { return fValue.c_str(); }
330 const char* data() const noexcept { return fValue.data(); }
331 bool empty() const { return fValue.empty(); }
332 std::string::const_iterator end() const { return fValue.end(); }
333 // find
334 size_t find(const std::string& str, size_t pos = 0) const noexcept { return fValue.find(str, pos); }
335 size_t find(const char* s, size_t pos = 0) const { return fValue.find(s, pos); }
336 size_t find(const char* s, size_t pos, size_t n) const { return fValue.find(s, pos, n); }
337 size_t find(char c, size_t pos = 0) const noexcept { return fValue.find(c, pos); }
338 size_t find_first_not_of(const std::string& str, size_t pos = 0) const noexcept { return fValue.find_first_not_of(str, pos); }
339 size_t find_first_not_of(const char* s, size_t pos = 0) const { return fValue.find_first_not_of(s, pos); }
340 size_t find_first_not_of(const char* s, size_t pos, size_t n) const { return fValue.find_first_not_of(s, pos, n); }
341 size_t find_first_not_of(char c, size_t pos = 0) const noexcept { return fValue.find_first_not_of(c, pos); }
342 size_t find_first_of(const std::string& str, size_t pos = 0) const noexcept { return fValue.find_first_of(str, pos); }
343 size_t find_first_of(const char* s, size_t pos = 0) const { return fValue.find_first_of(s, pos); }
344 size_t find_first_of(const char* s, size_t pos, size_t n) const { return fValue.find_first_of(s, pos, n); }
345 size_t find_first_of(char c, size_t pos = 0) const noexcept { return fValue.find_first_of(c, pos); }
346 size_t find_last_not_of(const std::string& str, size_t pos = std::string::npos) const noexcept { return fValue.find_last_not_of(str, pos); }
347 size_t find_last_not_of(const char* s, size_t pos = std::string::npos) const { return fValue.find_last_not_of(s, pos); }
348 size_t find_last_not_of(const char* s, size_t pos, size_t n) const { return fValue.find_last_not_of(s, pos, n); }
349 size_t find_last_not_of(char c, size_t pos = std::string::npos) const noexcept { return fValue.find_last_not_of(c, pos); }
350 size_t find_last_of(const std::string& str, size_t pos = std::string::npos) const noexcept { return fValue.find_last_of(str, pos); }
351 size_t find_last_of(const char* s, size_t pos = std::string::npos) const { return fValue.find_last_of(s, pos); }
352 size_t find_last_of(const char* s, size_t pos, size_t n) const { return fValue.find_last_of(s, pos, n); }
353 size_t find_last_of(char c, size_t pos = std::string::npos) const noexcept { return fValue.find_last_of(c, pos); }
354 const char& front() const { return fValue.front(); }
355 size_t length() const noexcept { return fValue.length(); }
356 size_t max_size() const noexcept { return fValue.max_size(); }
357 const char& operator[](size_t pos) const { return fValue[pos]; }
358 std::string::const_reverse_iterator rbegin() const noexcept { return fValue.rbegin(); }
359 std::string::const_reverse_iterator rend() const noexcept { return fValue.rend(); }
360 void reserve(size_t n = 0) { fValue.reserve(n); }
361 size_t rfind(const std::string& str, size_t pos = std::string::npos) const noexcept { return fValue.rfind(str, pos); }
362 size_t rfind(const char* s, size_t pos = std::string::npos) const { return fValue.rfind(s, pos); }
363 size_t rfind(const char* s, size_t pos, size_t n) const { return fValue.rfind(s, pos, n); }
364 size_t rfind(char c, size_t pos = std::string::npos) const noexcept { return fValue.rfind(c, pos); }
365 void shrink_to_fit() { fValue.shrink_to_fit(); }
366 size_t size() const noexcept { return fValue.size(); }
367 std::string substr(size_t pos = 0, size_t len = std::string::npos) const { return fValue.substr(pos, len); }
368
369 // copy-paste of original class (with 'T ' replace by 'std::string ', 'T>' by 'std::string>', 'T& ' by 'std::string& ', and 'T* ' by 'std::string* '):
370 // minus the boolean conversion operator
372 explicit TPriorityValue(std::string value, EPriority priority = EPriority::kDefault) : fValue(std::move(value)), fPriority(priority) {}
373 TPriorityValue(const TPriorityValue& rhs) : fPriority(EPriority::kDefault) { *this = rhs; }
374 TPriorityValue(TPriorityValue&& rhs) noexcept : fPriority(EPriority::kDefault) { *this = rhs; }
375
376 ~TPriorityValue() = default;
377
378 // setter
379 void Set(const std::string& val, EPriority priority)
380 {
381 if(priority >= fPriority) {
382 fValue = val;
383 SetPriority(priority);
384 }
385 }
386
387 void SetPriority(EPriority priority)
388 {
389 // only allow the priority to be increased, not decreased
390 fPriority = std::max(priority, fPriority);
391 }
392
393 // reset functions
394 void Reset(const std::string& val)
395 {
396 fValue = val;
398 }
399
400 void ResetPriority()
401 {
403 }
404
405 // getters
406 const std::string& Value() const { return fValue; }
407 EPriority Priority() const { return fPriority; }
408 std::string* Address() { return &fValue; }
409 const std::string* Address() const { return &fValue; }
410
411 // assignment and move assignment operators
413 {
414 if(rhs.fPriority != EPriority::kDefault && rhs.fPriority >= fPriority) {
415 fValue = rhs.fValue;
416 fPriority = rhs.fPriority;
417 }
418 return *this;
419 }
420
422 {
423 if(rhs.fPriority >= fPriority) {
424 fValue = std::move(rhs.fValue);
425 fPriority = rhs.fPriority; // this is trivially copyable, so std::move has no effect
426 }
427 return *this;
428 }
429
430 // comparison operators
432 {
433 return fValue == rhs.fValue;
434 }
436 {
437 return fValue != rhs.fValue; // could also be !(this == rhs)
438 }
440 {
441 return fValue < rhs.fValue;
442 }
444 {
445 return fValue > rhs.fValue; // could also be rhs < this
446 }
448 {
449 return fValue <= rhs.fValue; // could also be !(this>rhs)
450 }
452 {
453 return fValue >= rhs.fValue; // could also be !(this<rhs)
454 }
455
456 // comparison operators for base class
457 bool operator==(const std::string& rhs) const
458 {
459 return fValue == rhs;
460 }
461 bool operator!=(const std::string& rhs) const
462 {
463 return fValue != rhs; // could also be !(this == rhs)
464 }
465 bool operator<(const std::string& rhs) const
466 {
467 return fValue < rhs;
468 }
469 bool operator>(const std::string& rhs) const
470 {
471 return fValue > rhs; // could also be rhs < this
472 }
473 bool operator<=(const std::string& rhs) const
474 {
475 return fValue <= rhs; // could also be !(this>rhs)
476 }
477 bool operator>=(const std::string& rhs) const
478 {
479 return fValue >= rhs; // could also be !(this<rhs)
480 }
481
482 // explicit conversion
483 explicit operator std::string() const { return fValue; }
484
485 // streaming operator
486 template <class U>
487 friend std::ostream& operator<<(std::ostream&, const TPriorityValue<U>&);
488
489private:
490 std::string fValue;
492};
493
494// streaming operator
495template <class T>
496std::ostream& operator<<(std::ostream& out, const TPriorityValue<T>& val)
497{
498 out << val.fValue;
499 return out;
500}
501
502/*! @} */
503#endif
TPriorityValue(TPriorityValue &&rhs) noexcept
bool operator==(const T &rhs) const
TPriorityValue(T value, EPriority priority=EPriority::kDefault)
bool operator<(const TPriorityValue< T > &rhs)
friend std::ostream & operator<<(std::ostream &, const TPriorityValue< U > &)
bool operator>=(const TPriorityValue< T > &rhs)
EPriority fPriority
void Set(const T &val, EPriority priority)
TPriorityValue(const TPriorityValue &rhs)
~TPriorityValue()=default
bool operator==(const TPriorityValue< T > &rhs)
bool operator>=(const T &rhs) const
EPriority Priority() const
bool operator<(const T &rhs) const
bool operator>(const TPriorityValue< T > &rhs)
bool operator<=(const T &rhs) const
bool operator<=(const TPriorityValue< T > &rhs)
const T & Value() const
bool operator>(const T &rhs) const
TPriorityValue< T > & operator=(TPriorityValue< T > &&rhs) noexcept
const T * Address() const
TPriorityValue< T > & operator=(const TPriorityValue< T > &rhs)
void Reset(T val)
bool operator!=(const TPriorityValue< T > &rhs)
bool operator!=(const T &rhs) const
void SetPriority(EPriority priority)
EPriority
std::ostream & operator<<(std::ostream &out, const TPriorityValue< T > &val)