Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBinIndexRange.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_RBinIndexRange
6#define ROOT_RBinIndexRange
7
8#include "RBinIndex.hxx"
9
10#include <cassert>
11#include <cstddef> // for std::ptrdiff_t
12#include <cstdint>
13#include <iterator>
14
15namespace ROOT {
16namespace Experimental {
17
18// forward declarations for friend declaration
19class RBinIndexRange;
20namespace Internal {
21static RBinIndexRange CreateBinIndexRange(RBinIndex begin, RBinIndex end, std::uint64_t nNormalBins);
22} // namespace Internal
23
24/**
25A range of bin indices \f$[fBegin, fEnd)\f$.
26
27The interface allows convenient iteration over RBinIndex. If included, RBinIndex::Underflow() is encountered before the
28normal bins and RBinIndex::Overflow() is the last value.
29
30\code
31ROOT::Experimental::RRegularAxis axis(10, {5, 15});
32for (auto index : axis.GetNormalRange(2, 5)) {
33 // Will iterate over [2, 3, 4]
34}
35for (auto index : axis.GetFullRange()) {
36 // Will iterate over all bins, starting with the underflow and ending with the overflow bin
37}
38\endcode
39
40\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
41Feedback is welcome!
42*/
45
46 /// The begin of the range (inclusive)
48 /// The end of the range (exclusive)
50 /// The number of normal bins, after which iteration advances to RBinIndex::Overflow()
51 std::uint64_t fNNormalBins = 0;
52
53public:
54 /// Construct an invalid bin index range.
55 RBinIndexRange() = default;
56
57 RBinIndex GetBegin() const { return fBegin; }
58 RBinIndex GetEnd() const { return fEnd; }
59 // fNNormalBins is not exposed because it might be confusing for partial ranges.
60
61 bool IsInvalid() const
62 {
63 // fEnd can legally be invalid for full ranges including the overflow bin.
64 return fBegin.IsInvalid();
65 }
66
67 friend bool operator==(const RBinIndexRange &lhs, const RBinIndexRange &rhs)
68 {
69 return lhs.fBegin == rhs.fBegin && lhs.fEnd == rhs.fEnd && lhs.fNNormalBins == rhs.fNNormalBins;
70 }
71
72 friend bool operator!=(const RBinIndexRange &lhs, const RBinIndexRange &rhs) { return !(lhs == rhs); }
73
74 /// Iterator over RBinIndex.
76 /// The current bin index
78 /// The number of normal bins, after which iteration advances to RBinIndex::Overflow()
79 std::uint64_t fNNormalBins = 0;
80
81 public:
82 using difference_type = std::ptrdiff_t;
84 using pointer = const RBinIndex *;
86 using iterator_category = std::input_iterator_tag;
87
88 RIterator() = default;
90
92 {
93 if (fIndex.IsUnderflow()) {
94 fIndex = 0;
95 } else if (fIndex.IsOverflow()) {
96 fIndex = RBinIndex();
97 } else if (fIndex.IsInvalid()) {
98 // This should never happen! In the worst case, when built with NDEBUG, the iterator stays at Invalid.
99 assert(0); // GCOVR_EXCL_LINE
100 } else {
101 fIndex++;
102 if (fIndex.GetIndex() == fNNormalBins) {
104 }
105 }
106 return *this;
107 }
109 {
110 RIterator old = *this;
111 operator++();
112 return old;
113 }
114
115 RBinIndex operator*() const { return fIndex; }
116 const RBinIndex *operator->() const { return &fIndex; }
117
118 friend bool operator==(const RIterator &lhs, const RIterator &rhs)
119 {
120 return lhs.fIndex == rhs.fIndex && lhs.fNNormalBins == rhs.fNNormalBins;
121 }
122 friend bool operator!=(const RIterator &lhs, const RIterator &rhs) { return !(lhs == rhs); }
123 };
124
127};
128
129namespace Internal {
130
131/// %Internal function to create RBinIndexRange.
132///
133/// Users are strongly advised to create bin index ranges via the respective axis types, for example with
134/// \ref RRegularAxis::GetNormalRange(RBinIndex, RBinIndex) const "RRegularAxis::GetNormalRange(RBinIndex, RBinIndex)"
135/// or RRegularAxis::GetFullRange().
136///
137/// \param[in] begin the begin of the bin index range (inclusive)
138/// \param[in] end the end of the bin index range (exclusive)
139/// \param[in] nNormalBins the number of normal bins, after which iteration advances to RBinIndex::Overflow()
141{
143 range.fBegin = begin;
144 range.fEnd = end;
145 range.fNNormalBins = nNormalBins;
146 return range;
147}
148
149} // namespace Internal
150
151} // namespace Experimental
152} // namespace ROOT
153
154#endif
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
std::uint64_t fNNormalBins
The number of normal bins, after which iteration advances to RBinIndex::Overflow()
friend bool operator!=(const RIterator &lhs, const RIterator &rhs)
RIterator(RBinIndex index, std::uint64_t nNormalBins)
friend bool operator==(const RIterator &lhs, const RIterator &rhs)
RBinIndex fBegin
The begin of the range (inclusive)
RBinIndex fEnd
The end of the range (exclusive)
friend bool operator!=(const RBinIndexRange &lhs, const RBinIndexRange &rhs)
friend bool operator==(const RBinIndexRange &lhs, const RBinIndexRange &rhs)
RBinIndexRange()=default
Construct an invalid bin index range.
std::uint64_t fNNormalBins
The number of normal bins, after which iteration advances to RBinIndex::Overflow()
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:23
static RBinIndex Overflow()
std::uint64_t GetIndex() const
Return the index for a normal bin.
Definition RBinIndex.hxx:71
static RBinIndexRange CreateBinIndexRange(RBinIndex begin, RBinIndex end, std::uint64_t nNormalBins)
Internal function to create RBinIndexRange.