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