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