-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[hist] Implement RBinIndexRange
#19694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/// \file | ||
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback | ||
/// is welcome! | ||
|
||
#ifndef ROOT_RBinIndexRange | ||
#define ROOT_RBinIndexRange | ||
|
||
#include "RBinIndex.hxx" | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <iterator> | ||
|
||
namespace ROOT { | ||
namespace Experimental { | ||
|
||
// forward declarations for friend declaration | ||
class RBinIndexRange; | ||
namespace Internal { | ||
RBinIndexRange CreateBinIndexRange(RBinIndex begin, RBinIndex end, std::size_t nNormalBins); | ||
} // namespace Internal | ||
|
||
/** | ||
A range of bin indices \f$[fBegin, fEnd)\f$. | ||
|
||
The interface allows convenient iteration over RBinIndex. If included, RBinIndex::Underflow() is encountered before the | ||
normal bins and RBinIndex::Overflow() is the last value. | ||
|
||
\code | ||
ROOT::Experimental::RRegularAxis axis(10, 0, 1); | ||
for (auto index : axis.GetNormalRange(2, 5)) { | ||
// Will iterate over [2, 3, 4] | ||
} | ||
for (auto index : axis.GetFullRange()) { | ||
// Will iterate over all bins, starting with the underflow and ending with the overflow bin | ||
} | ||
\endcode | ||
|
||
\warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is | ||
welcome! | ||
*/ | ||
class RBinIndexRange final { | ||
friend RBinIndexRange Internal::CreateBinIndexRange(RBinIndex, RBinIndex, std::size_t); | ||
|
||
/// The begin of the range (inclusive) | ||
RBinIndex fBegin; | ||
/// The end of the range (exclusive) | ||
RBinIndex fEnd; | ||
/// The number of normal bins, after which iteration advances to RBinIndex::Overflow() | ||
std::size_t fNNormalBins = 0; | ||
|
||
public: | ||
/// Construct an invalid bin index range. | ||
RBinIndexRange() = default; | ||
|
||
RBinIndex GetBegin() const { return fBegin; } | ||
RBinIndex GetEnd() const { return fEnd; } | ||
// fNNormalBins is not exposed because it might be confusing for partial ranges. | ||
|
||
friend bool operator==(const RBinIndexRange &lhs, const RBinIndexRange &rhs) | ||
{ | ||
return lhs.fBegin == rhs.fBegin && lhs.fEnd == rhs.fEnd && lhs.fNNormalBins == rhs.fNNormalBins; | ||
} | ||
|
||
friend bool operator!=(const RBinIndexRange &lhs, const RBinIndexRange &rhs) { return !(lhs == rhs); } | ||
|
||
/// %Iterator over RBinIndex. | ||
class Iterator final { | ||
/// The current bin index | ||
RBinIndex fIndex; | ||
/// The number of normal bins, after which iteration advances to RBinIndex::Overflow() | ||
std::size_t fNNormalBins = 0; | ||
|
||
public: | ||
using difference_type = std::ptrdiff_t; | ||
using value_type = RBinIndex; | ||
using pointer = const RBinIndex *; | ||
using reference = RBinIndex; | ||
using iterator_category = std::input_iterator_tag; | ||
|
||
Iterator() = default; | ||
Iterator(RBinIndex index, std::size_t nNormalBins) : fIndex(index), fNNormalBins(nNormalBins) {} | ||
|
||
Iterator &operator++() | ||
{ | ||
if (fIndex.IsUnderflow()) { | ||
fIndex = 0; | ||
} else if (fIndex.IsOverflow()) { | ||
fIndex = RBinIndex(); | ||
} else if (fIndex.IsInvalid()) { | ||
// This should never happen! In the worst case, when built with NDEBUG, the iterator stays at Invalid. | ||
assert(0); | ||
} else { | ||
fIndex++; | ||
if (fIndex.GetIndex() == fNNormalBins) { | ||
fIndex = RBinIndex::Overflow(); | ||
} | ||
} | ||
return *this; | ||
} | ||
Iterator operator++(int) | ||
{ | ||
Iterator old = *this; | ||
operator++(); | ||
return old; | ||
} | ||
|
||
RBinIndex operator*() const { return fIndex; } | ||
const RBinIndex *operator->() const { return &fIndex; } | ||
|
||
friend bool operator==(const Iterator &lhs, const Iterator &rhs) | ||
{ | ||
return lhs.fIndex == rhs.fIndex && lhs.fNNormalBins == rhs.fNNormalBins; | ||
} | ||
friend bool operator!=(const Iterator &lhs, const Iterator &rhs) { return !(lhs == rhs); } | ||
}; | ||
|
||
Iterator begin() const { return Iterator(fBegin, fNNormalBins); } | ||
Iterator end() const { return Iterator(fEnd, fNNormalBins); } | ||
}; | ||
|
||
namespace Internal { | ||
|
||
/// %Internal function to create RBinIndexRange. | ||
/// | ||
/// Users are strongly advised to create bin index ranges via the respective axis types, for example with | ||
/// \ref RRegularAxis::GetNormalRange(RBinIndex, RBinIndex) const "RRegularAxis::GetNormalRange(RBinIndex, RBinIndex)" | ||
/// or RRegularAxis::GetFullRange(). | ||
/// | ||
/// \param[in] begin the begin of the bin index range (inclusive) | ||
/// \param[in] end the end of the bin index range (exclusive) | ||
/// \param[in] nNormalBins the number of normal bins, after which iteration advances to RBinIndex::Overflow() | ||
RBinIndexRange CreateBinIndexRange(RBinIndex begin, RBinIndex end, std::size_t nNormalBins) | ||
{ | ||
RBinIndexRange range; | ||
range.fBegin = begin; | ||
range.fEnd = end; | ||
range.fNNormalBins = nNormalBins; | ||
return range; | ||
} | ||
|
||
} // namespace Internal | ||
|
||
} // namespace Experimental | ||
} // namespace ROOT | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.