Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions DataFormats/Common/interface/MultiSpan.h
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]; }
Comment on lines +86 to +89
Copy link
Contributor

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, where S is the number of spans being held.

It then follows that the iteration through the MultiSpan has O(log(S) * N), where N is the total number of entries. At least "on the average", I could also think of a pathological case whose complexity would be closer to O(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 the span in spans_ and another for the element in that span).

Copy link
Author

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.

Copy link
Contributor

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")


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
103 changes: 103 additions & 0 deletions DataFormats/Common/test/test_catch2_MultiSpan.cpp
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]);
}
}
85 changes: 0 additions & 85 deletions DataFormats/HGCalReco/interface/MultiVectorManager.h

This file was deleted.

4 changes: 2 additions & 2 deletions DataFormats/HGCalReco/interface/Trackster.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "DataFormats/Provenance/interface/ProductID.h"
#include "DataFormats/Math/interface/Vector3D.h"
#include "DataFormats/HGCalReco/interface/MultiVectorManager.h"
#include "DataFormats/Common/interface/MultiSpan.h"

#include <Eigen/Core>

Expand Down Expand Up @@ -117,7 +117,7 @@ namespace ticl {
calculateRawEmPt();
}
template <typename T>
inline void mergeTracksters(const MultiVectorManager<Trackster> &allTracksters, const std::vector<T> &others) {
inline void mergeTracksters(const edm::MultiSpan<Trackster> &allTracksters, const std::vector<T> &others) {
for (auto &other : others) {
*this += allTracksters[other];
}
Expand Down
6 changes: 3 additions & 3 deletions RecoHGCal/TICL/interface/TICLInterpretationAlgoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "DataFormats/HGCalReco/interface/Common.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"
#include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
#include "DataFormats/HGCalReco/interface/MultiVectorManager.h"
#include "DataFormats/Common/interface/MultiSpan.h"

namespace edm {
class Event;
Expand All @@ -40,7 +40,7 @@ namespace ticl {
const edm::EventSetup& es;
const std::vector<reco::CaloCluster>& layerClusters;
const edm::ValueMap<std::pair<float, float>>& layerClustersTime;
const MultiVectorManager<Trackster>& tracksters;
const edm::MultiSpan<Trackster>& tracksters;
const std::vector<std::vector<unsigned int>>& linkedResultTracksters;
const edm::Handle<std::vector<T>> tracksHandle;
const std::vector<bool>& maskedTracks;
Expand All @@ -49,7 +49,7 @@ namespace ticl {
const edm::EventSetup& eS,
const std::vector<reco::CaloCluster>& lC,
const edm::ValueMap<std::pair<float, float>>& lcT,
const MultiVectorManager<Trackster>& tS,
const edm::MultiSpan<Trackster>& tS,
const std::vector<std::vector<unsigned int>>& links,
const edm::Handle<std::vector<T>> trks,
const std::vector<bool>& mT)
Expand Down
Loading