Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBinIndex.hxx
Go to the documentation of this file.
1/// \file
2/// \warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
3/// Feedback is welcome!
4
5#ifndef ROOT_RBinIndex
6#define ROOT_RBinIndex
7
8#include <cassert>
9#include <cstddef>
10#include <cstdint>
11
12namespace ROOT {
13namespace Experimental {
14
15/**
16A bin index with special values for underflow and overflow bins.
17
18Objects of this type should be passed by value.
19
20\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
21Feedback is welcome!
22*/
24 static constexpr auto kUnderflowIndex = static_cast<std::uint64_t>(-3);
25 static constexpr auto kOverflowIndex = static_cast<std::uint64_t>(-2);
26 static constexpr auto kInvalidIndex = static_cast<std::uint64_t>(-1);
27
28 // We use std::uint64_t instead of std::size_t for the index because for sparse histograms, not all bins have to be
29 // allocated in memory. However, we require that the index has at least that size.
30 static_assert(sizeof(std::uint64_t) >= sizeof(std::size_t), "index type not large enough to address all bins");
31 // During construction, we expect that any standard integer fits in std::uint64_t.
32 static_assert(sizeof(std::uint64_t) >= sizeof(unsigned long long),
33 "index type not large enough to store any standard integer");
34
35 std::uint64_t fIndex = kInvalidIndex;
36
37public:
38 /// Construct an invalid bin index.
39 RBinIndex() = default;
40
41 /// Construct a bin index for a normal bin.
42 RBinIndex(unsigned int index) : RBinIndex(static_cast<unsigned long long>(index)) {}
43
44 /// Construct a bin index for a normal bin.
45 RBinIndex(unsigned long index) : RBinIndex(static_cast<unsigned long long>(index)) {}
46
47 /// Construct a bin index for a normal bin.
48 RBinIndex(unsigned long long index) : fIndex(index) { assert(IsNormal()); }
49
50 /// Construct a bin index for a normal bin.
51 ///
52 /// \param[in] index signed integer that must not be negative
53 RBinIndex(int index) : RBinIndex(static_cast<long long>(index)) {}
54
55 /// Construct a bin index for a normal bin.
56 ///
57 /// \param[in] index signed integer that must not be negative
58 RBinIndex(long index) : RBinIndex(static_cast<long long>(index)) {}
59
60 /// Construct a bin index for a normal bin.
61 ///
62 /// \param[in] index signed integer that must not be negative
63 RBinIndex(long long index)
64 {
65 assert(index >= 0);
66 fIndex = static_cast<std::uint64_t>(index);
68 }
69
70 /// Return the index for a normal bin.
71 std::uint64_t GetIndex() const
72 {
74 return fIndex;
75 }
76
77 /// A bin index is normal iff it is not one of the special values.
78 ///
79 /// Note that a normal bin index may not actually be valid for a given axis if it is outside its range.
80 bool IsNormal() const { return fIndex < kUnderflowIndex; }
81 bool IsUnderflow() const { return fIndex == kUnderflowIndex; }
82 bool IsOverflow() const { return fIndex == kOverflowIndex; }
83 bool IsInvalid() const { return fIndex == kInvalidIndex; }
84
85 RBinIndex &operator+=(std::uint64_t a)
86 {
87 if (!IsNormal()) {
88 // Arithmetic operations on special values go to InvalidIndex.
90 } else {
91 std::uint64_t old = fIndex;
92 fIndex += a;
93 if (fIndex < old || !IsNormal()) {
94 // The addition wrapped around, go to InvalidIndex.
96 }
97 }
98 return *this;
99 }
100
101 RBinIndex operator+(std::uint64_t a) const
102 {
103 RBinIndex ret = *this;
104 ret += a;
105 return ret;
106 }
107
109 {
110 operator+=(1);
111 return *this;
112 }
113
115 {
116 RBinIndex old = *this;
117 operator++();
118 return old;
119 }
120
121 RBinIndex &operator-=(std::uint64_t a)
122 {
123 if (!IsNormal()) {
124 // Arithmetic operations on special values go to InvalidIndex.
126 } else if (fIndex >= a) {
127 fIndex -= a;
128 } else {
129 // The operation would wrap around, go to InvalidIndex.
131 }
132 return *this;
133 }
134
135 RBinIndex operator-(std::uint64_t a) const
136 {
137 RBinIndex ret = *this;
138 ret -= a;
139 return ret;
140 }
141
143 {
144 operator-=(1);
145 return *this;
146 }
147
149 {
150 RBinIndex old = *this;
151 operator--();
152 return old;
153 }
154
155 friend bool operator==(RBinIndex lhs, RBinIndex rhs) { return lhs.fIndex == rhs.fIndex; }
156 friend bool operator!=(RBinIndex lhs, RBinIndex rhs) { return !(lhs == rhs); }
157
159 {
160 if (lhs.IsNormal() && rhs.IsNormal()) {
161 return lhs.fIndex < rhs.fIndex;
162 }
163 return false;
164 }
165 friend bool operator<=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs < rhs; }
166
168 {
169 if (lhs.IsNormal() && rhs.IsNormal()) {
170 return lhs.fIndex > rhs.fIndex;
171 }
172 return false;
173 }
174 friend bool operator>=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs > rhs; }
175
177 {
179 underflow.fIndex = kUnderflowIndex;
180 return underflow;
181 }
182
184 {
185 RBinIndex overflow;
186 overflow.fIndex = kOverflowIndex;
187 return overflow;
188 }
189};
190
191} // namespace Experimental
192} // namespace ROOT
193
194#endif
#define a(i)
Definition RSha256.hxx:99
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:23
RBinIndex & operator+=(std::uint64_t a)
Definition RBinIndex.hxx:85
RBinIndex operator+(std::uint64_t a) const
RBinIndex()=default
Construct an invalid bin index.
RBinIndex(unsigned long index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:45
RBinIndex(unsigned int index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:42
static constexpr auto kInvalidIndex
Definition RBinIndex.hxx:26
friend bool operator<=(RBinIndex lhs, RBinIndex rhs)
friend bool operator==(RBinIndex lhs, RBinIndex rhs)
static constexpr auto kUnderflowIndex
Definition RBinIndex.hxx:24
friend bool operator<(RBinIndex lhs, RBinIndex rhs)
RBinIndex & operator-=(std::uint64_t a)
bool IsNormal() const
A bin index is normal iff it is not one of the special values.
Definition RBinIndex.hxx:80
static RBinIndex Overflow()
static constexpr auto kOverflowIndex
Definition RBinIndex.hxx:25
RBinIndex(long long index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:63
friend bool operator>(RBinIndex lhs, RBinIndex rhs)
RBinIndex operator-(std::uint64_t a) const
RBinIndex(unsigned long long index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:48
std::uint64_t GetIndex() const
Return the index for a normal bin.
Definition RBinIndex.hxx:71
RBinIndex(long index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:58
friend bool operator>=(RBinIndex lhs, RBinIndex rhs)
friend bool operator!=(RBinIndex lhs, RBinIndex rhs)
static RBinIndex Underflow()
RBinIndex(int index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:53