LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::span< BIter, EIter > Struct Template Reference

Simple class with a begin and an end. More...

#include "span.h"

Inheritance diagram for util::span< BIter, EIter >:
util::span_base

Public Types

using begin_iterator = BIter
 Type of begin iterator. More...
 
using end_iterator = EIter
 Type of end iterator. More...
 
using span_t = span< begin_iterator, end_iterator >
 Type of this class. More...
 
using pair_t = std::pair< begin_iterator, end_iterator >
 Type of iterator pair. More...
 
using value_type = typename begin_iterator::value_type
 Type of values pointed by the iterators. More...
 
using reference = typename begin_iterator::reference
 Type of reference pointed by the iterators. More...
 
template<typename Cont >
using get_begin_iterator = std::decay_t< decltype(get_begin(std::declval< Cont >()))>
 Type of begin iterator of Cont type. More...
 
template<typename Cont >
using get_end_iterator = std::decay_t< decltype(get_end(std::declval< Cont >()))>
 Type of end iterator of Cont type. More...
 
template<typename Cont >
using get_cbegin_iterator = std::decay_t< decltype(get_cbegin(std::declval< Cont >()))>
 Type of constant begin iterator of Cont type. More...
 
template<typename Cont >
using get_cend_iterator = std::decay_t< decltype(get_cend(std::declval< Cont >()))>
 Type of constant end iterator of Cont type. More...
 

Public Member Functions

 span (begin_iterator b, end_iterator e)
 Constructor: specifies the begin and end iterator. More...
 
template<typename SrcIterB , typename SrcIterE , typename Adaptor >
 span (SrcIterB &&b, SrcIterE &&e, Adaptor &&adaptor)
 Constructor: specifies the begin and end iterator and an adapter. More...
 
template<typename OBIter , typename OEIter >
 span (span< OBIter, OEIter > const &from)
 Constructor: copies from another span (possibly with different types). More...
 
 span (span_t const &)=default
 
 span (span_t &&)=default
 
span_toperator= (span_t const &)=default
 
span_toperator= (span_t &&)=default
 
begin_iterator begin () const
 Returns a copy of the begin iterator. More...
 
end_iterator end () const
 Returns a copy of the end iterator. More...
 
std::size_t size () const
 Returns the size between begin and end, converted to std::size_t. More...
 
bool empty () const
 Returns whether the span is empty (that is, no steps between them). More...
 

Static Public Member Functions

template<typename Cont >
static decltype(auto) get_begin (Cont &cont)
 Returns the begin iterator of the specified container. More...
 
template<typename Cont >
static decltype(auto) get_end (Cont &cont)
 Returns the end iterator of the specified container. More...
 
template<typename Cont >
static decltype(auto) get_cbegin (Cont &cont)
 Returns the constant begin iterator of the specified container. More...
 
template<typename Cont >
static decltype(auto) get_cend (Cont &cont)
 Returns the constant end iterator of the specified container. More...
 

Detailed Description

template<typename BIter, typename EIter = BIter>
struct util::span< BIter, EIter >

Simple class with a begin and an end.

Template Parameters
BITERtype of begin iterator
EITERtype of end iterator (default: as BITER)

This class is a glorified pair of iterators which can be used in a range-for loop. All input iterators are accepted.

It is probably going to be redundant with the advent of C++20 and its span and/or range libraries (if the latter is ever going to happen).

Example:

using Data_t = std::vector<int>;
decltype(r)::value_type s = 0;
for (auto v: r) s += v;
return s;
} // sum
void analyse() {
Data_t v(10U);
std::iota(v.begin(), v.end(), 1); // { 1, 2, 3, 4 ,5 ,6, 7, 8, 9, 10 }
auto span5 = util::span(v.begin(), v.begin() + 5);
std::cout << "Sum of 5 numbers: " << sum(span5) << std::endl;
auto span8 = util::span(v.begin(), v.begin() + 8);
std::cout << "Sum of 8 numbers: " << sum(span8) << std::endl;
auto full_span = util::make_span(v);
std::cout << "Sum of all numbers: " << sum(full_span) << std::endl;
} // analyse()

The call to analyse() will produce output like:

Sum of 5 numbers: 15
Sum of 8 numbers: 36
Sum of all numbers: 55

The convenience of this class, not evident from the example, is the beginning and end of the span can be passed around as a single object, with the added bonus of a (very reduced) collection interface.

Note
The constantness of the access is completely determined by the type of the iterators. A const span object does not guarantee unmutable access to the iterated data.

Definition at line 129 of file span.h.

Member Typedef Documentation

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::begin_iterator = BIter

Type of begin iterator.

Definition at line 131 of file span.h.

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::end_iterator = EIter

Type of end iterator.

Definition at line 132 of file span.h.

template<typename Cont >
using util::span_base::get_begin_iterator = std::decay_t<decltype(get_begin(std::declval<Cont>()))>
inherited

Type of begin iterator of Cont type.

Definition at line 44 of file span.h.

template<typename Cont >
using util::span_base::get_cbegin_iterator = std::decay_t<decltype(get_cbegin(std::declval<Cont>()))>
inherited

Type of constant begin iterator of Cont type.

Definition at line 68 of file span.h.

template<typename Cont >
using util::span_base::get_cend_iterator = std::decay_t<decltype(get_cend(std::declval<Cont>()))>
inherited

Type of constant end iterator of Cont type.

Definition at line 72 of file span.h.

template<typename Cont >
using util::span_base::get_end_iterator = std::decay_t<decltype(get_end(std::declval<Cont>()))>
inherited

Type of end iterator of Cont type.

Definition at line 48 of file span.h.

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::pair_t = std::pair<begin_iterator, end_iterator>

Type of iterator pair.

Definition at line 138 of file span.h.

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::reference = typename begin_iterator::reference

Type of reference pointed by the iterators.

Definition at line 144 of file span.h.

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::span_t = span<begin_iterator, end_iterator>

Type of this class.

Definition at line 135 of file span.h.

template<typename BIter, typename EIter = BIter>
using util::span< BIter, EIter >::value_type = typename begin_iterator::value_type

Type of values pointed by the iterators.

Definition at line 141 of file span.h.

Constructor & Destructor Documentation

template<typename BIter, typename EIter = BIter>
util::span< BIter, EIter >::span ( begin_iterator  b,
end_iterator  e 
)
inline

Constructor: specifies the begin and end iterator.

Definition at line 147 of file span.h.

147 : pair_t(b, e) {}
std::pair< begin_iterator, end_iterator > pair_t
Type of iterator pair.
Definition: span.h:138
Float_t e
Definition: plot.C:35
template<typename BIter, typename EIter = BIter>
template<typename SrcIterB , typename SrcIterE , typename Adaptor >
util::span< BIter, EIter >::span ( SrcIterB &&  b,
SrcIterE &&  e,
Adaptor &&  adaptor 
)
inline

Constructor: specifies the begin and end iterator and an adapter.

Definition at line 151 of file span.h.

152  : span(adaptor(std::forward<SrcIterB>(b)), adaptor(std::forward<SrcIterE>(e)))
153  {}
span(begin_iterator b, end_iterator e)
Constructor: specifies the begin and end iterator.
Definition: span.h:147
Float_t e
Definition: plot.C:35
template<typename BIter, typename EIter = BIter>
template<typename OBIter , typename OEIter >
util::span< BIter, EIter >::span ( span< OBIter, OEIter > const &  from)
inline

Constructor: copies from another span (possibly with different types).

Definition at line 157 of file span.h.

References util::span().

157  : span(from.begin(), from.end())
158  {}
span(begin_iterator b, end_iterator e)
Constructor: specifies the begin and end iterator.
Definition: span.h:147
template<typename BIter, typename EIter = BIter>
util::span< BIter, EIter >::span ( span_t const &  )
default
template<typename BIter, typename EIter = BIter>
util::span< BIter, EIter >::span ( span_t &&  )
default

Member Function Documentation

template<typename BIter, typename EIter = BIter>
begin_iterator util::span< BIter, EIter >::begin ( ) const
inline

Returns a copy of the begin iterator.

Definition at line 167 of file span.h.

167 { return pair_t::first; }
template<typename BIter, typename EIter = BIter>
bool util::span< BIter, EIter >::empty ( ) const
inline

Returns whether the span is empty (that is, no steps between them).

Definition at line 176 of file span.h.

References util::begin(), e, util::end(), and util::span().

176 { return begin() == end(); }
end_iterator end() const
Returns a copy of the end iterator.
Definition: span.h:170
begin_iterator begin() const
Returns a copy of the begin iterator.
Definition: span.h:167
template<typename BIter, typename EIter = BIter>
end_iterator util::span< BIter, EIter >::end ( ) const
inline

Returns a copy of the end iterator.

Definition at line 170 of file span.h.

170 { return pair_t::second; }
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
template<typename Cont >
static decltype(auto) util::span_base::get_begin ( Cont &  cont)
inlinestaticinherited

Returns the begin iterator of the specified container.

Definition at line 28 of file span.h.

References util::begin().

Referenced by util::make_adapted_span(), util::make_span(), and util::make_transformed_span().

29  {
30  using std::begin;
31  return begin(cont);
32  }
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
template<typename Cont >
static decltype(auto) util::span_base::get_cbegin ( Cont &  cont)
inlinestaticinherited

Returns the constant begin iterator of the specified container.

Definition at line 52 of file span.h.

References util::cbegin().

Referenced by util::make_adapted_const_span(), and util::make_const_span().

53  {
54  using std::cbegin;
55  return cbegin(cont);
56  }
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:85
template<typename Cont >
static decltype(auto) util::span_base::get_cend ( Cont &  cont)
inlinestaticinherited

Returns the constant end iterator of the specified container.

Definition at line 60 of file span.h.

References util::cend().

Referenced by util::make_adapted_const_span(), and util::make_const_span().

61  {
62  using std::cend;
63  return cend(cont);
64  }
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
template<typename Cont >
static decltype(auto) util::span_base::get_end ( Cont &  cont)
inlinestaticinherited

Returns the end iterator of the specified container.

Definition at line 36 of file span.h.

References util::end().

Referenced by util::make_adapted_span(), util::make_span(), and util::make_transformed_span().

37  {
38  using std::end;
39  return end(cont);
40  }
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
template<typename BIter, typename EIter = BIter>
span_t& util::span< BIter, EIter >::operator= ( span_t const &  )
default
template<typename BIter, typename EIter = BIter>
span_t& util::span< BIter, EIter >::operator= ( span_t &&  )
default
template<typename BIter, typename EIter = BIter>
std::size_t util::span< BIter, EIter >::size ( ) const
inline

Returns the size between begin and end, converted to std::size_t.

Definition at line 173 of file span.h.

References util::begin(), and util::end().

173 { return std::distance(begin(), end()); }
end_iterator end() const
Returns a copy of the end iterator.
Definition: span.h:170
begin_iterator begin() const
Returns a copy of the begin iterator.
Definition: span.h:167

The documentation for this struct was generated from the following file: