Skip to content

Commit

Permalink
introduce edm::Span
Browse files Browse the repository at this point in the history
  • Loading branch information
guitargeek committed Jul 8, 2020
1 parent f8d93aa commit ea41702
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
14 changes: 7 additions & 7 deletions DataFormats/NanoAOD/interface/FlatTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "DataFormats/Math/interface/libminifloat.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/Range.h"
#include "FWCore/Utilities/interface/Span.h"

#include <cstdint>
#include <vector>
Expand All @@ -17,8 +17,8 @@ namespace nanoaod {
struct MaybeMantissaReduce {
MaybeMantissaReduce(int mantissaBits) {}
inline T one(const T &val) const { return val; }
template <typename Range>
inline void bulk(Range &&data) const {}
template <typename Span>
inline void bulk(Span &&data) const {}
};
template <>
struct MaybeMantissaReduce<float> {
Expand All @@ -27,8 +27,8 @@ namespace nanoaod {
inline float one(const float &val) const {
return (bits_ > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, bits_) : val);
}
template <typename Range>
inline void bulk(Range &&data) const {
template <typename Span>
inline void bulk(Span &&data) const {
if (bits_ > 0)
MiniFloatConverter::reduceMantissaToNbitsRounding(bits_, data.begin(), data.end(), data.begin());
}
Expand Down Expand Up @@ -69,14 +69,14 @@ namespace nanoaod {
template <typename T>
auto columnData(unsigned int column) const {
auto begin = beginData<T>(column);
return edm::Range(begin, begin + size_);
return edm::Span(begin, begin + size_);
}

/// get a column by index (non-const)
template <typename T>
auto columnData(unsigned int column) {
auto begin = beginData<T>(column);
return edm::Range(begin, begin + size_);
return edm::Span(begin, begin + size_);
}

/// get a column value for singleton (const)
Expand Down
8 changes: 0 additions & 8 deletions FWCore/Utilities/interface/Range.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef FWCore_Utilities_Range_h
#define FWCore_Utilities_Range_h

#include <cstddef>

namespace edm {
/*
*class which implements begin() and end() to use range-based loop with
Expand All @@ -18,12 +16,6 @@ namespace edm {
T end() const { return end_; }

bool empty() const { return begin_ == end_; }
auto size() const { return end_ - begin_; }

auto const& operator[](std::size_t idx) const { return *(begin_ + idx); }

auto const& front() const { return *begin_; }
auto const& back() const { return *(end_ - 1); }

private:
const T begin_;
Expand Down
37 changes: 37 additions & 0 deletions FWCore/Utilities/interface/Span.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef FWCore_Utilities_Span_h
#define FWCore_Utilities_Span_h

#include <cstddef>

namespace edm {
/*
*An edm::Span wraps begin() and end() iterators to a contiguous sequence
of objects with the first element of the sequence at position zero,
In other words the iterators should refer to random-access containers.
To be replaced with std::Span in C++20.
*/

template <class T>
class Span {
public:
Span(T begin, T end) : begin_(begin), end_(end) {}

T begin() const { return begin_; }
T end() const { return end_; }

bool empty() const { return begin_ == end_; }
auto size() const { return end_ - begin_; }

auto const& operator[](std::size_t idx) const { return *(begin_ + idx); }

auto const& front() const { return *begin_; }
auto const& back() const { return *(end_ - 1); }

private:
const T begin_;
const T end_;
};
}; // namespace edm

#endif

0 comments on commit ea41702

Please sign in to comment.