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 std::uint64_t UnderflowIndex = -3;
25 static constexpr std::uint64_t OverflowIndex = -2;
26 static constexpr std::uint64_t InvalidIndex = -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
32 std::uint64_t fIndex = InvalidIndex;
33
34public:
35 /// Construct an invalid bin index.
36 RBinIndex() = default;
37
38 /// Construct a bin index for a normal bin.
39 RBinIndex(std::uint64_t index) : fIndex(index) { assert(IsNormal()); }
40
41 /// Return the index for a normal bin.
42 std::uint64_t GetIndex() const
43 {
45 return fIndex;
46 }
47
48 /// A bin index is normal iff it is not one of the special values.
49 ///
50 /// Note that a normal bin index may not actually be valid for a given axis if it is outside its range.
51 bool IsNormal() const { return fIndex < UnderflowIndex; }
52 bool IsUnderflow() const { return fIndex == UnderflowIndex; }
53 bool IsOverflow() const { return fIndex == OverflowIndex; }
54 bool IsInvalid() const { return fIndex == InvalidIndex; }
55
56 RBinIndex &operator+=(std::uint64_t a)
57 {
58 if (!IsNormal()) {
59 // Arithmetic operations on special values go to InvalidIndex.
61 } else {
62 std::uint64_t old = fIndex;
63 fIndex += a;
64 if (fIndex < old || !IsNormal()) {
65 // The addition wrapped around, go to InvalidIndex.
67 }
68 }
69 return *this;
70 }
71
72 RBinIndex operator+(std::uint64_t a) const
73 {
74 RBinIndex ret = *this;
75 ret += a;
76 return ret;
77 }
78
80 {
81 operator+=(1);
82 return *this;
83 }
84
86 {
87 RBinIndex old = *this;
88 operator++();
89 return old;
90 }
91
92 RBinIndex &operator-=(std::uint64_t a)
93 {
94 if (!IsNormal()) {
95 // Arithmetic operations on special values go to InvalidIndex.
97 } else if (fIndex >= a) {
98 fIndex -= a;
99 } else {
100 // The operation would wrap around, go to InvalidIndex.
102 }
103 return *this;
104 }
105
106 RBinIndex operator-(std::uint64_t a) const
107 {
108 RBinIndex ret = *this;
109 ret -= a;
110 return ret;
111 }
112
114 {
115 operator-=(1);
116 return *this;
117 }
118
120 {
121 RBinIndex old = *this;
122 operator--();
123 return old;
124 }
125
126 friend bool operator==(RBinIndex lhs, RBinIndex rhs) { return lhs.fIndex == rhs.fIndex; }
127 friend bool operator!=(RBinIndex lhs, RBinIndex rhs) { return !(lhs == rhs); }
128
130 {
131 if (lhs.IsNormal() && rhs.IsNormal()) {
132 return lhs.fIndex < rhs.fIndex;
133 }
134 return false;
135 }
136 friend bool operator<=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs < rhs; }
137
139 {
140 if (lhs.IsNormal() && rhs.IsNormal()) {
141 return lhs.fIndex > rhs.fIndex;
142 }
143 return false;
144 }
145 friend bool operator>=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs > rhs; }
146
148 {
150 underflow.fIndex = UnderflowIndex;
151 return underflow;
152 }
153
155 {
156 RBinIndex overflow;
157 overflow.fIndex = OverflowIndex;
158 return overflow;
159 }
160};
161
162} // namespace Experimental
163} // namespace ROOT
164
165#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:56
RBinIndex operator+(std::uint64_t a) const
Definition RBinIndex.hxx:72
static constexpr std::uint64_t OverflowIndex
Definition RBinIndex.hxx:25
RBinIndex()=default
Construct an invalid bin index.
RBinIndex(std::uint64_t index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:39
friend bool operator<=(RBinIndex lhs, RBinIndex rhs)
friend bool operator==(RBinIndex lhs, RBinIndex rhs)
friend bool operator<(RBinIndex lhs, RBinIndex rhs)
static constexpr std::uint64_t InvalidIndex
Definition RBinIndex.hxx:26
RBinIndex & operator-=(std::uint64_t a)
Definition RBinIndex.hxx:92
bool IsNormal() const
A bin index is normal iff it is not one of the special values.
Definition RBinIndex.hxx:51
static RBinIndex Overflow()
static constexpr std::uint64_t UnderflowIndex
Definition RBinIndex.hxx:24
friend bool operator>(RBinIndex lhs, RBinIndex rhs)
RBinIndex operator-(std::uint64_t a) const
std::uint64_t GetIndex() const
Return the index for a normal bin.
Definition RBinIndex.hxx:42
friend bool operator>=(RBinIndex lhs, RBinIndex rhs)
friend bool operator!=(RBinIndex lhs, RBinIndex rhs)
static RBinIndex Underflow()