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
11namespace ROOT {
12namespace Experimental {
13
14/**
15A bin index with special values for underflow and overflow bins.
16
17Objects of this type should be passed by value.
18
19\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
20Feedback is welcome!
21*/
23 static constexpr std::size_t UnderflowIndex = -3;
24 static constexpr std::size_t OverflowIndex = -2;
25 static constexpr std::size_t InvalidIndex = -1;
26
27 std::size_t fIndex = InvalidIndex;
28
29public:
30 /// Construct an invalid bin index.
31 RBinIndex() = default;
32
33 /// Construct a bin index for a normal bin.
34 RBinIndex(std::size_t index) : fIndex(index) { assert(IsNormal()); }
35
36 /// Return the index for a normal bin.
37 std::size_t GetIndex() const
38 {
40 return fIndex;
41 }
42
43 /// A bin index is normal iff it is not one of the special values.
44 ///
45 /// Note that a normal bin index may not actually be valid for a given axis if it is outside its range.
46 bool IsNormal() const { return fIndex < UnderflowIndex; }
47 bool IsUnderflow() const { return fIndex == UnderflowIndex; }
48 bool IsOverflow() const { return fIndex == OverflowIndex; }
49 bool IsInvalid() const { return fIndex == InvalidIndex; }
50
51 RBinIndex &operator+=(std::size_t a)
52 {
53 if (!IsNormal()) {
54 // Arithmetic operations on special values go to InvalidIndex.
56 } else {
57 std::size_t old = fIndex;
58 fIndex += a;
59 if (fIndex < old || !IsNormal()) {
60 // The addition wrapped around, go to InvalidIndex.
62 }
63 }
64 return *this;
65 }
66
67 RBinIndex operator+(std::size_t a) const
68 {
69 RBinIndex ret = *this;
70 ret += a;
71 return ret;
72 }
73
75 {
76 operator+=(1);
77 return *this;
78 }
79
81 {
82 RBinIndex old = *this;
83 operator++();
84 return old;
85 }
86
87 RBinIndex &operator-=(std::size_t a)
88 {
89 if (!IsNormal()) {
90 // Arithmetic operations on special values go to InvalidIndex.
92 } else if (fIndex >= a) {
93 fIndex -= a;
94 } else {
95 // The operation would wrap around, go to InvalidIndex.
97 }
98 return *this;
99 }
100
101 RBinIndex operator-(std::size_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 friend bool operator==(RBinIndex lhs, RBinIndex rhs) { return lhs.fIndex == rhs.fIndex; }
122 friend bool operator!=(RBinIndex lhs, RBinIndex rhs) { return !(lhs == rhs); }
123
125 {
126 if (lhs.IsNormal() && rhs.IsNormal()) {
127 return lhs.fIndex < rhs.fIndex;
128 }
129 return false;
130 }
131 friend bool operator<=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs < rhs; }
132
134 {
135 if (lhs.IsNormal() && rhs.IsNormal()) {
136 return lhs.fIndex > rhs.fIndex;
137 }
138 return false;
139 }
140 friend bool operator>=(RBinIndex lhs, RBinIndex rhs) { return lhs == rhs || lhs > rhs; }
141
143 {
145 underflow.fIndex = UnderflowIndex;
146 return underflow;
147 }
148
150 {
151 RBinIndex overflow;
152 overflow.fIndex = OverflowIndex;
153 return overflow;
154 }
155};
156
157} // namespace Experimental
158} // namespace ROOT
159
160#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:22
std::size_t GetIndex() const
Return the index for a normal bin.
Definition RBinIndex.hxx:37
RBinIndex(std::size_t index)
Construct a bin index for a normal bin.
Definition RBinIndex.hxx:34
RBinIndex()=default
Construct an invalid bin index.
static constexpr std::size_t OverflowIndex
Definition RBinIndex.hxx:24
friend bool operator<=(RBinIndex lhs, RBinIndex rhs)
friend bool operator==(RBinIndex lhs, RBinIndex rhs)
RBinIndex & operator-=(std::size_t a)
Definition RBinIndex.hxx:87
friend bool operator<(RBinIndex lhs, RBinIndex rhs)
RBinIndex operator+(std::size_t a) const
Definition RBinIndex.hxx:67
bool IsNormal() const
A bin index is normal iff it is not one of the special values.
Definition RBinIndex.hxx:46
static constexpr std::size_t InvalidIndex
Definition RBinIndex.hxx:25
static RBinIndex Overflow()
RBinIndex & operator+=(std::size_t a)
Definition RBinIndex.hxx:51
static constexpr std::size_t UnderflowIndex
Definition RBinIndex.hxx:23
friend bool operator>(RBinIndex lhs, RBinIndex rhs)
RBinIndex operator-(std::size_t a) const
friend bool operator>=(RBinIndex lhs, RBinIndex rhs)
friend bool operator!=(RBinIndex lhs, RBinIndex rhs)
static RBinIndex Underflow()
Namespace for new ROOT classes and functions.