Public Types | Public Member Functions | List of all members
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...
 
- Public Types inherited from util::span_base
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...
 

Additional Inherited Members

- Static Public Member Functions inherited from util::span_base
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 125 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 127 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 128 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 134 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 140 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 131 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 137 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 143 of file span.h.

143 : pair_t(b, e) {}
const double e
static bool * b
Definition: config.cpp:1043
std::pair< begin_iterator, end_iterator > pair_t
Type of iterator pair.
Definition: span.h:134
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 147 of file span.h.

148  : span
149  (adaptor(std::forward<SrcIterB>(b)), adaptor(std::forward<SrcIterE>(e)))
150  {}
span(begin_iterator b, end_iterator e)
Constructor: specifies the begin and end iterator.
Definition: span.h:143
const double e
static bool * b
Definition: config.cpp:1043
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 154 of file span.h.

154 : span(from.begin(), from.end()) {}
span(begin_iterator b, end_iterator e)
Constructor: specifies the begin and end iterator.
Definition: span.h:143
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 164 of file span.h.

164 { 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 173 of file span.h.

173 { return begin() == end(); }
end_iterator end() const
Returns a copy of the end iterator.
Definition: span.h:167
begin_iterator begin() const
Returns a copy of the begin iterator.
Definition: span.h:164
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 167 of file span.h.

167 { return pair_t::second; }
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
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 ( void  ) const
inline

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

Definition at line 170 of file span.h.

170 { return std::distance(begin(), end()); }
end_iterator end() const
Returns a copy of the end iterator.
Definition: span.h:167
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
begin_iterator begin() const
Returns a copy of the begin iterator.
Definition: span.h:164

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