-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Move MultiVectorManager to DataFormats/Common and address discussion from #48826 and rename it to MultiSpan #48857
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
Open
Electricks94
wants to merge
8
commits into
cms-sw:master
Choose a base branch
from
Electricks94:MultiVectorManager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+377
−211
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
85bfd90
Improve MultiVectorManager
Electricks94 5d6d485
Move MultiSpan to DataFormats/Common
Electricks94 eea910d
Rebase master
Electricks94 4511c5a
Minor Improvements to MultiSpan
Electricks94 d8831ba
Small fixes and code-format
Electricks94 f1e23e4
Fix TICLCandidateProducer
Electricks94 1f3a041
Improve Multispan
Electricks94 c076be7
Add comment about iteration complexity
Electricks94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Author: Felice Pantaleo (CERN), 2023, [email protected] | ||
#ifndef DataFormats_Common_MultiSpan_h | ||
#define DataFormats_Common_MultiSpan_h | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <span> | ||
#include <stdexcept> | ||
#include <vector> | ||
|
||
namespace edm { | ||
|
||
/** | ||
* @brief A view-like container that provides a contiguous indexing interface over multiple disjoint spans. | ||
* | ||
* MultiSpan allows to append multiple `std::vector<T>` as std::span<const T> instances and access them through a | ||
* single global index as if they formed one continuous sequence. | ||
* | ||
* This class is read-only and does not take ownership of the underlying data. | ||
* It is intended for iteration over heterogeneous but logically connected data ranges without copying | ||
* or merging them into a single container. | ||
* | ||
* To find a span that corresponds to a global index, a binary search is used, making the access time logarithmic in the number of spans. | ||
* This means when iterating over the elements the binary search over spans is repeated for every element. | ||
* | ||
*/ | ||
template <typename T> | ||
class MultiSpan { | ||
public: | ||
MultiSpan() = default; | ||
|
||
void add(std::span<const T> sp) { | ||
spans_.emplace_back(sp); | ||
offsets_.push_back(totalSize_); | ||
totalSize_ += sp.size(); | ||
} | ||
|
||
const T& operator[](const std::size_t globalIndex) const { | ||
#ifndef NDEBUG | ||
if (globalIndex >= totalSize_) { | ||
throw std::out_of_range("Global index out of range"); | ||
} | ||
#endif | ||
const auto [spanIndex, indexWithinSpan] = spanAndLocalIndex(globalIndex); | ||
return spans_[spanIndex][indexWithinSpan]; | ||
} | ||
|
||
std::size_t globalIndex(const std::size_t spanIndex, const std::size_t indexWithinSpan) const { | ||
#ifndef NDEBUG | ||
if (spanIndex >= spans_.size()) { | ||
throw std::out_of_range("spanIndex index out of range"); | ||
} | ||
if (indexWithinSpan >= spans_[spanIndex].size()) { | ||
throw std::out_of_range("indexWithinSpan index out of range"); | ||
} | ||
#endif | ||
|
||
return offsets_[spanIndex] + indexWithinSpan; | ||
} | ||
|
||
std::pair<std::size_t, std::size_t> spanAndLocalIndex(const std::size_t globalIndex) const { | ||
#ifndef NDEBUG | ||
if (globalIndex >= totalSize_) { | ||
throw std::out_of_range("Global index out of range"); | ||
} | ||
#endif | ||
auto it = std::upper_bound(offsets_.begin(), offsets_.end(), globalIndex); | ||
std::size_t spanIndex = std::distance(offsets_.begin(), it) - 1; | ||
std::size_t indexWithinSpan = globalIndex - offsets_[spanIndex]; | ||
|
||
return {spanIndex, indexWithinSpan}; | ||
} | ||
|
||
std::size_t size() const { return totalSize_; } | ||
|
||
class ConstRandomAccessIterator { | ||
public: | ||
using iterator_category = std::random_access_iterator_tag; | ||
using difference_type = std::ptrdiff_t; | ||
using value_type = T; | ||
using pointer = const T*; | ||
using reference = const T&; | ||
|
||
ConstRandomAccessIterator(const MultiSpan& ms, const std::size_t index) : ms_(&ms), currentIndex_(index) {} | ||
|
||
reference operator*() const { return (*ms_)[currentIndex_]; } | ||
pointer operator->() const { return &(*ms_)[currentIndex_]; } | ||
|
||
reference operator[](difference_type n) const { return (*ms_)[currentIndex_ + n]; } | ||
|
||
ConstRandomAccessIterator& operator++() { | ||
++currentIndex_; | ||
return *this; | ||
} | ||
ConstRandomAccessIterator operator++(int) { | ||
auto tmp = *this; | ||
++(*this); | ||
return tmp; | ||
} | ||
ConstRandomAccessIterator& operator--() { | ||
--currentIndex_; | ||
return *this; | ||
} | ||
ConstRandomAccessIterator operator--(int) { | ||
auto tmp = *this; | ||
--(*this); | ||
return tmp; | ||
} | ||
|
||
ConstRandomAccessIterator& operator+=(difference_type n) { | ||
currentIndex_ += n; | ||
return *this; | ||
} | ||
ConstRandomAccessIterator& operator-=(difference_type n) { | ||
currentIndex_ -= n; | ||
return *this; | ||
} | ||
|
||
ConstRandomAccessIterator operator+(difference_type n) const { | ||
return ConstRandomAccessIterator(*ms_, currentIndex_ + n); | ||
} | ||
|
||
ConstRandomAccessIterator operator-(difference_type n) const { | ||
return ConstRandomAccessIterator(*ms_, currentIndex_ - n); | ||
} | ||
|
||
difference_type operator-(const ConstRandomAccessIterator& other) const { | ||
return currentIndex_ - other.currentIndex_; | ||
} | ||
|
||
bool operator==(const ConstRandomAccessIterator& other) const { return currentIndex_ == other.currentIndex_; } | ||
bool operator!=(const ConstRandomAccessIterator& other) const { return currentIndex_ != other.currentIndex_; } | ||
auto operator<=>(const ConstRandomAccessIterator& other) const { return currentIndex_ <=> other.currentIndex_; } | ||
|
||
private: | ||
const MultiSpan* ms_; | ||
std::size_t currentIndex_; | ||
}; | ||
|
||
using const_iterator = ConstRandomAccessIterator; | ||
|
||
const_iterator begin() const { return const_iterator(*this, 0); } | ||
const_iterator end() const { return const_iterator(*this, totalSize_); } | ||
|
||
private: | ||
std::vector<std::span<const T>> spans_; | ||
std::vector<std::size_t> offsets_; | ||
std::size_t totalSize_{0}; | ||
}; | ||
|
||
} // namespace edm | ||
|
||
#endif // DataFormats_Common_MultiSpan_h |
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,103 @@ | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <cmath> | ||
#include <numeric> | ||
#include <vector> | ||
#include <catch.hpp> | ||
|
||
#include "DataFormats/Common/interface/MultiSpan.h" | ||
|
||
TEST_CASE("MultiSpan basic indexing", "[MultiSpan]") { | ||
edm::MultiSpan<int> emptyMultiSpan; | ||
edm::MultiSpan<int> ms; | ||
|
||
std::vector<int> a = {1, 2, 3}; | ||
std::vector<int> b = {4, 5}; | ||
|
||
ms.add(a); | ||
ms.add(b); | ||
|
||
using ElementType = decltype(ms[0]); | ||
// Check that the const-correctness of the MultiSpan | ||
static_assert(!std::is_assignable<ElementType, int>::value, | ||
"It should not be possible to assign to an element of MultiSpan; See PR #48826"); | ||
|
||
SECTION("Empty MultiSpan") { | ||
REQUIRE(emptyMultiSpan.size() == 0); | ||
REQUIRE(emptyMultiSpan.begin() == emptyMultiSpan.end()); | ||
REQUIRE_THROWS_AS(emptyMultiSpan[0], std::out_of_range); | ||
REQUIRE_THROWS_AS(emptyMultiSpan.globalIndex(0, 0), std::out_of_range); | ||
} | ||
|
||
SECTION("Size is correct") { REQUIRE(ms.size() == 5); } | ||
|
||
SECTION("Range check") { | ||
REQUIRE_THROWS_AS(ms[5], std::out_of_range); | ||
REQUIRE_THROWS_AS(ms.globalIndex(2, 0), std::out_of_range); | ||
REQUIRE_THROWS_AS(ms.globalIndex(1, 2), std::out_of_range); | ||
REQUIRE_THROWS_AS(ms.spanAndLocalIndex(5), std::out_of_range); | ||
} | ||
|
||
SECTION("Indexing returns correct values") { | ||
REQUIRE(ms[0] == 1); | ||
REQUIRE(ms[1] == 2); | ||
REQUIRE(ms[2] == 3); | ||
REQUIRE(ms[3] == 4); | ||
REQUIRE(ms[4] == 5); | ||
} | ||
|
||
SECTION("Global index from span index and local index") { | ||
REQUIRE(ms.globalIndex(0, 0) == 0); | ||
REQUIRE(ms.globalIndex(0, 2) == 2); | ||
REQUIRE(ms.globalIndex(1, 0) == 3); | ||
REQUIRE(ms.globalIndex(1, 1) == 4); | ||
} | ||
|
||
SECTION("Span and local index from global index") { | ||
auto [span0, local0] = ms.spanAndLocalIndex(0); | ||
REQUIRE(span0 == 0); | ||
REQUIRE(local0 == 0); | ||
|
||
auto [span1, local1] = ms.spanAndLocalIndex(4); | ||
REQUIRE(span1 == 1); | ||
REQUIRE(local1 == 1); | ||
} | ||
|
||
SECTION("Iterators work with range-based for") { | ||
std::vector<int> collected; | ||
for (auto val : ms) { | ||
collected.push_back(val); | ||
} | ||
REQUIRE(collected == std::vector<int>{1, 2, 3, 4, 5}); | ||
} | ||
|
||
SECTION("Random access iterator supports arithmetic") { | ||
auto it = ms.begin(); | ||
REQUIRE(*(it + 2) == 3); | ||
REQUIRE(*(ms.end() - 2) == 4); | ||
REQUIRE((ms.end() - ms.begin()) == 5); | ||
} | ||
|
||
SECTION("std::find works") { | ||
auto it = std::find(ms.begin(), ms.end(), 4); | ||
REQUIRE(it != ms.end()); | ||
REQUIRE(*it == 4); | ||
REQUIRE((it - ms.begin()) == 3); | ||
} | ||
|
||
SECTION("std::distance returns correct result") { | ||
auto dist = std::distance(ms.begin(), ms.end()); | ||
REQUIRE(dist == 5); | ||
} | ||
|
||
SECTION("std::copy copies all values") { | ||
std::vector<int> out(5); | ||
std::copy(ms.begin(), ms.end(), out.begin()); | ||
|
||
REQUIRE(ms[0] == out[0]); | ||
REQUIRE(ms[1] == out[1]); | ||
REQUIRE(ms[2] == out[2]); | ||
REQUIRE(ms[3] == out[3]); | ||
REQUIRE(ms[4] == out[4]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to document explicitly that each global index access has
O(log(S))
time complexity, whereS
is the number ofspan
s being held.It then follows that the iteration through the
MultiSpan
hasO(log(S) * N)
, whereN
is the total number of entries. At least "on the average", I could also think of a pathological case whose complexity would be closer toO(S * N)
.I think the time complexity of the iteration should either be documented as well, or improved to be
O(log(N))
(that could be done by holding two indices or iterators, one for thespan
inspans_
and another for the element in thatspan
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the complexity to the documentation of the class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Could you add a comment about the complexity of the iteration as well? (e.g. "when iterating over the elements the binary search over spans is repeated for every element")