Public Types | Protected Member Functions | Private Types | Private Member Functions | Friends | List of all members
lar::CollectionView< Range > Class Template Reference

Provides features of a collections, from begin and end iterators. More...

#include <CollectionView.h>

Inheritance diagram for lar::CollectionView< Range >:
Range

Public Types

using collection_type = range_t
 Type of collection being wrapped. More...
 
using value_type = typename iter_traits_t::value_type
 
using const_reference = typename iter_traits_t::reference
 
using const_pointer = typename iter_traits_t::pointer
 
using const_iterator = begin_iter_t
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 
using difference_type = typename iter_traits_t::difference_type
 
using size_type = std::size_t
 

Public Member Functions

Forward access.
bool empty () const noexcept
 Returns whether the collection is empty. More...
 
size_type size () const noexcept
 Returns the size of the collection. More...
 
const_iterator cbegin () const noexcept
 Returns an iterator to the begin of the collection. More...
 
end_iter_t cend () const noexcept
 Returns an iterator past the end of the collection. More...
 
const_iterator begin () const noexcept
 Returns an iterator to the begin of the collection. More...
 
end_iter_t end () const noexcept
 Returns an iterator past the end of the collection. More...
 
auto front () const -> decltype(auto)
 Returns the first element in the collection. More...
 
Backward access.
const_reverse_iterator rbegin () const noexcept
 Returns a reverse iterator to the begin of the collection. More...
 
const_reverse_iterator rend () const noexcept
 Returns a reverse iterator past the end of the collection. More...
 
const_reverse_iterator crbegin () const noexcept
 Returns a reverse iterator to the begin of the collection. More...
 
const_reverse_iterator crend () const noexcept
 Returns a reverse iterator past the end of the collection. More...
 
auto back () const -> decltype(auto)
 Returns the last element in the collection. More...
 
Random access.
auto operator[] (size_type i) const -> decltype(auto)
 Returns the content of the i-th element. More...
 
auto at (size_type i) const -> decltype(auto)
 Returns the content of the i-th element. More...
 
Contiguous access.
const_pointer data () const
 

Protected Member Functions

 CollectionView (range_t &&range)
 Constructor: steals the data from the specified range. More...
 

Private Types

using this_t = CollectionView< Range >
 This type. More...
 
using range_t = Range
 Type of the range being wrapped. More...
 
using traits_t = details::RangeTraits< range_t >
 Range traits. More...
 
using begin_iter_t = typename traits_t::begin_iterator_t
 Type of the begin iterator. More...
 
using end_iter_t = typename traits_t::end_iterator_t
 Type of the end iterator. More...
 
using iter_traits_t = std::iterator_traits< begin_iter_t >
 Type of traits of iterator. More...
 

Private Member Functions

range_t const & asRange () const
 Returns this very object, cast back to range_t. More...
 
- Private Member Functions inherited from Range
 Range (int s, int e)
 

Friends

this_t details::makeCollectionView (range_t &&)
 

Additional Inherited Members

- Private Attributes inherited from Range
int start
 
int end
 

Detailed Description

template<typename Range>
class lar::CollectionView< Range >

Provides features of a collections, from begin and end iterators.

Template Parameters
Rangethe type of collection to be wrapped

A collection view is a class that offers a collection-like interface, mostly like the C+ standard library containers, based on two iterators.

The base, wrapped collection is required to respond to begin() and a end() global functions. If the desired view is not described by such an object, a temporary one must be created (see makeCollectionView()).

There are two ways to use this class:

  1. to wrap an existing Range-like container
  2. to turn two iterators into a container

The two use cases are both addressed by this class, but helper functions are provided to make it easier to create them as needed.

Note
While the object is currently copiable and moveable, this is not guaranteed for the future.

Wrap an existing Range-like container

Here we assume there is somewhere an instance of the object range which fulfills the requirement of the Range type above, that is it responds to the requests of a begin() and a end() iterator.

To create a collection view of range, the easiest way is to use wrapCollectionIntoView(). In the following example the range object is a STL vector (which does not really need any added interface...):

std::vector<int> range(5);
std::iota(range.begin(), range.end(), 1); // { 1, 2, 3, 4, 5 }
for (int d: lar::wrapCollectionIntoView(range)) {
std::cout << d << " ";
}
std::cout << std::endl;

which will print "1 2 3 4 5 ". Here the wrapped collection object, returned by wrapCollectionIntoView(), is insubstantial. It can be saved with

decltype(auto) view = lar::wrapCollectionIntoView(range);

or even:

auto const& view = lar::wrapCollectionIntoView(range);

but it will be just a (constant) reference to something else.

Turn two iterators into a container

In this approach, we have two iterators to an existing collection, and we want to use them as extremes of a "virtual" collection. Again we use a STL vector as a base container for the example. Here we want to see a subrange of it as a new collection. We use makeCollectionView().

std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0); // { 0, 1, ..., 9 }
for (int d: lar::makeCollectionView(v.begin() + 4, v.begin() + 7)) {
std::cout << d << " ";
}
std::cout << std::endl;

will print "0 1 2 3 4 5 6 7 8 9 ".

Declaring a wrapper class

The function lar::makeCollectionView() creates a view owning the information the view requires. Similarly, a new class can be defined which does the same, by simply deriving it from lar::CollectionView:

class IntVector {
using vector_t = std::vector<int>;
vector_t data;
public:
IntVector(vector_t&& data): data(std::move(data)) {}
auto begin() const -> decltype(auto) { return data.cbegin(); }
auto end() const -> decltype(auto) { return data.cend(); }
}; // struct IntVector
using IntViewBase_t = lar::CollectionView<IntVector>;
struct MyCollection: public IntViewBase_t {
MyCollection(std::vector<int>&& data) : IntViewBase_t(std::move(data)) {}
}; // class MyCollection

aftter which MyCollection interface can be enriched as needed. The IntViewBase_t alias is a way to overcome the fact that the name IntVector can't be used inside MyCollection because it's actually a private base class (the base class, even if not direct, will hide IntVector, even if, being private, it can't even be used). Another way is to fully qualify its name (e.g. IntVector).

Note that to avoid accidental copies, lar::CollectionView objects can't be directly instantiated: using directly IntViewBase_t will not be allowed.

Definition at line 98 of file CollectionView.h.

Member Typedef Documentation

template<typename Range>
using lar::CollectionView< Range >::begin_iter_t = typename traits_t::begin_iterator_t
private

Type of the begin iterator.

Definition at line 292 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::collection_type = range_t

Type of collection being wrapped.

Definition at line 300 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::const_iterator = begin_iter_t

Definition at line 308 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::const_pointer = typename iter_traits_t::pointer

Definition at line 306 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::const_reference = typename iter_traits_t::reference

Definition at line 304 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Definition at line 310 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::difference_type = typename iter_traits_t::difference_type

Definition at line 311 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::end_iter_t = typename traits_t::end_iterator_t
private

Type of the end iterator.

Definition at line 294 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::iter_traits_t = std::iterator_traits<begin_iter_t>
private

Type of traits of iterator.

Definition at line 297 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::range_t = Range
private

Type of the range being wrapped.

Definition at line 288 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::size_type = std::size_t

Definition at line 312 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::this_t = CollectionView<Range>
private

This type.

Definition at line 287 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::traits_t = details::RangeTraits<range_t>
private

Range traits.

Definition at line 289 of file CollectionView.h.

template<typename Range>
using lar::CollectionView< Range >::value_type = typename iter_traits_t::value_type

Definition at line 302 of file CollectionView.h.

Constructor & Destructor Documentation

template<typename Range>
lar::CollectionView< Range >::CollectionView ( range_t &&  range)
inlineexplicitprotected

Constructor: steals the data from the specified range.

Definition at line 408 of file CollectionView.h.

408 : range_t(std::move(range)) {}
Range range_t
Type of the range being wrapped.
def move(depos, offset)
Definition: depos.py:107

Member Function Documentation

template<typename Range>
range_t const& lar::CollectionView< Range >::asRange ( ) const
inlineprivate

Returns this very object, cast back to range_t.

Definition at line 416 of file CollectionView.h.

417  { return static_cast<range_t const&>(*this); }
Range range_t
Type of the range being wrapped.
template<typename Range>
auto lar::CollectionView< Range >::at ( size_type  i) const -> decltype(auto)
inline

Returns the content of the i-th element.

Definition at line 372 of file CollectionView.h.

373  {
374  if (i >= size()) {
375  throw std::out_of_range(
376  "CollectionView index out of range: "
377  + std::to_string(i) + " >= " + std::to_string(size())
378  );
379  }
380  return operator[](i);
381  }
size_type size() const noexcept
Returns the size of the collection.
auto operator[](size_type i) const -> decltype(auto)
Returns the content of the i-th element.
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
template<typename Range>
auto lar::CollectionView< Range >::back ( ) const -> decltype(auto)
inline

Returns the last element in the collection.

Definition at line 360 of file CollectionView.h.

360 { return *crbegin(); }
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the begin of the collection.
template<typename Range>
const_iterator lar::CollectionView< Range >::begin ( ) const
inlinenoexcept

Returns an iterator to the begin of the collection.

Definition at line 332 of file CollectionView.h.

332 { return cbegin(); }
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
template<typename Range>
const_iterator lar::CollectionView< Range >::cbegin ( ) const
inlinenoexcept

Returns an iterator to the begin of the collection.

Definition at line 324 of file CollectionView.h.

325  { using std::cbegin; return cbegin(asRange()); }
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
range_t const & asRange() const
Returns this very object, cast back to range_t.
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:82
template<typename Range>
end_iter_t lar::CollectionView< Range >::cend ( ) const
inlinenoexcept

Returns an iterator past the end of the collection.

Definition at line 328 of file CollectionView.h.

329  { using std::cend; return cend(asRange()); }
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
end_iter_t cend() const noexcept
Returns an iterator past the end of the collection.
range_t const & asRange() const
Returns this very object, cast back to range_t.
template<typename Range>
const_reverse_iterator lar::CollectionView< Range >::crbegin ( ) const
inlinenoexcept

Returns a reverse iterator to the begin of the collection.

Definition at line 352 of file CollectionView.h.

353  { return const_reverse_iterator(cend()); }
end_iter_t cend() const noexcept
Returns an iterator past the end of the collection.
std::reverse_iterator< const_iterator > const_reverse_iterator
template<typename Range>
const_reverse_iterator lar::CollectionView< Range >::crend ( ) const
inlinenoexcept

Returns a reverse iterator past the end of the collection.

Definition at line 356 of file CollectionView.h.

357  { return const_reverse_iterator(cbegin()); }
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
std::reverse_iterator< const_iterator > const_reverse_iterator
template<typename Range>
const_pointer lar::CollectionView< Range >::data ( ) const
inline

Definition at line 388 of file CollectionView.h.

388 { return &front(); }
auto front() const -> decltype(auto)
Returns the first element in the collection.
template<typename Range>
bool lar::CollectionView< Range >::empty ( ) const
inlinenoexcept

Returns whether the collection is empty.

Definition at line 318 of file CollectionView.h.

318 { return cbegin() == cend(); }
end_iter_t cend() const noexcept
Returns an iterator past the end of the collection.
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
template<typename Range>
end_iter_t lar::CollectionView< Range >::end ( ) const
inlinenoexcept

Returns an iterator past the end of the collection.

Definition at line 335 of file CollectionView.h.

335 { return cend(); }
end_iter_t cend() const noexcept
Returns an iterator past the end of the collection.
template<typename Range>
auto lar::CollectionView< Range >::front ( ) const -> decltype(auto)
inline

Returns the first element in the collection.

Definition at line 338 of file CollectionView.h.

338 { return *cbegin(); }
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
template<typename Range>
auto lar::CollectionView< Range >::operator[] ( size_type  i) const -> decltype(auto)
inline

Returns the content of the i-th element.

Definition at line 368 of file CollectionView.h.

369  { return cbegin()[i]; }
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
template<typename Range>
const_reverse_iterator lar::CollectionView< Range >::rbegin ( ) const
inlinenoexcept

Returns a reverse iterator to the begin of the collection.

Definition at line 346 of file CollectionView.h.

346 { return crbegin(); }
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the begin of the collection.
template<typename Range>
const_reverse_iterator lar::CollectionView< Range >::rend ( ) const
inlinenoexcept

Returns a reverse iterator past the end of the collection.

Definition at line 349 of file CollectionView.h.

349 { return crend(); }
const_reverse_iterator crend() const noexcept
Returns a reverse iterator past the end of the collection.
template<typename Range>
size_type lar::CollectionView< Range >::size ( void  ) const
inlinenoexcept

Returns the size of the collection.

Definition at line 321 of file CollectionView.h.

321 { return std::distance(cbegin(), cend()); }
end_iter_t cend() const noexcept
Returns an iterator past the end of the collection.
const_iterator cbegin() const noexcept
Returns an iterator to the begin of the collection.
double distance(double x1, double y1, double z1, double x2, double y2, double z2)

Friends And Related Function Documentation

template<typename Range>
this_t details::makeCollectionView ( range_t &&  )
friend

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