swift
Loading...
Searching...
No Matches
swift::misc::CRange< I > Class Template Reference

A range is a conceptual container which does not contain any elements of its own, but is constructed from a begin iterator and an end iterator of another container. More...

#include <range.h>

Inheritance diagram for swift::misc::CRange< I >:
[legend]

Public Types

using value_type = typename std::iterator_traits<I>::value_type
 STL compatibility.
using reference = typename std::iterator_traits<I>::reference
 STL compatibility.
using difference_type = typename std::iterator_traits<I>::difference_type
 STL compatibility.
using const_reference = const value_type &
 STL compatibility.
using key_type = value_type
 STL compatibility.
using size_type = difference_type
 STL compatibility.
using iterator = I
 STL compatibility.
using const_iterator = I
 STL compatibility.
using reverse_iterator = std::reverse_iterator<I>
 STL compatibility.
using const_reverse_iterator = std::reverse_iterator<I>
 STL compatibility.

Public Member Functions

 CRange (I begin, I end)
 Constructor.
CRange< const_reverse_iterator > reverse () const
 Create a range from reverse iterators.
template<class T>
requires (std::is_convertible_v<value_type, typename T::value_type>)
 operator T () const
 Implicit conversion to any container of value_type which supports push_back. This will copy elements.
size_type size () const
 Returns the number of elements in the range.
const_reference front () const
 Returns the element at the beginning of the range. Undefined if the range is empty.
const_reference frontOrDefault () const
 Returns the element at the beginning of the range, or a default value if the range is empty.
value_type frontOrDefault (value_type def) const
 Returns the element at the beginning of the range, or a default value if the range is empty.
const_iterator begin () const
 Begin and end iterators.
const_iterator cbegin () const
 Begin and end iterators.
const_iterator end () const
 Begin and end iterators.
const_iterator cend () const
 Begin and end iterators.
const_reverse_iterator rbegin () const
 Reverse begin and end iterators.
const_reverse_iterator crbegin () const
 Reverse begin and end iterators.
const_reverse_iterator rend () const
 Reverse begin and end iterators.
const_reverse_iterator crend () const
 Reverse begin and end iterators.
template<class T>
T to () const
 Explicit conversion to any container of value_type which supports push_back. This will copy elements.
template<template< class... > class T>
auto to () const
 Explicit conversion to any container of value_type which supports push_back. This will copy elements.
bool empty () const
 Returns true if the range is empty.
bool isEmpty () const
 Returns true if the range is empty.
Public Member Functions inherited from swift::misc::CRangeBase< CRange< I > >
auto transform (F function) const
 Return a new container generated by applying some transformation function to all elements of this one.
auto findBy (Predicate p) const
 Return a copy containing only those elements for which a given predicate returns true.
const auto & findFirstBy (Predicate p) const
 Return a reference to the first element for which a given predicate returns true. Undefined if there is none.
auto findFirstByOrDefault (Predicate p, const Value &def) const
 Return a copy of the first element for which a given predicate returns true, or a default value if there is none.
bool containsBy (Predicate p) const
 Return true if there is an element for which a given predicate returns true.
bool contains (const T &object) const
 Return true if there is an element equal to given object. Uses the most efficient implementation available in the derived container.
bool equalsBy (const T &other, Predicate c) const
 Return true if this container equals another container according to the given element equality predicate.
bool equalsByKeys (const T &other, Key0 k0, Keys... keys) const
 Return true if this container equals another container, considering only the given element members.
T randomElement () const
 Pick one random element.
CRange< I > randomElements (int n) const
 Copy n elements from the container at random.
CRange< I > sampleElements (int n) const
 Copy n elements from the container, randomly selected but evenly distributed.

Additional Inherited Members

Static Protected Member Functions inherited from swift::misc::CRangeBase< CRange< I > >
static bool equalPointers (const T *a, const U *b)
 Efficiently compare addresses of two objects. Return false if types are not compatible.

Detailed Description

template<class I>
class swift::misc::CRange< I >

A range is a conceptual container which does not contain any elements of its own, but is constructed from a begin iterator and an end iterator of another container.

By using iterator wrappers, it is possible to use CRange to iterate over the results of predicate methods without copying elements.

Warning
Remember that the iterators in the range refer to elements in the original container, so take care that the original container remains valid and does not invalidate its iterators during the lifetime of the range.

Definition at line 198 of file range.h.

Member Typedef Documentation

◆ const_iterator

template<class I>
using swift::misc::CRange< I >::const_iterator = I

STL compatibility.

Definition at line 210 of file range.h.

◆ const_reference

template<class I>
using swift::misc::CRange< I >::const_reference = const value_type &

STL compatibility.

Definition at line 206 of file range.h.

◆ const_reverse_iterator

template<class I>
using swift::misc::CRange< I >::const_reverse_iterator = std::reverse_iterator<I>

STL compatibility.

Definition at line 212 of file range.h.

◆ difference_type

template<class I>
using swift::misc::CRange< I >::difference_type = typename std::iterator_traits<I>::difference_type

STL compatibility.

Definition at line 205 of file range.h.

◆ iterator

template<class I>
using swift::misc::CRange< I >::iterator = I

STL compatibility.

Definition at line 209 of file range.h.

◆ key_type

template<class I>
using swift::misc::CRange< I >::key_type = value_type

STL compatibility.

Definition at line 207 of file range.h.

◆ reference

template<class I>
using swift::misc::CRange< I >::reference = typename std::iterator_traits<I>::reference

STL compatibility.

Definition at line 204 of file range.h.

◆ reverse_iterator

template<class I>
using swift::misc::CRange< I >::reverse_iterator = std::reverse_iterator<I>

STL compatibility.

Definition at line 211 of file range.h.

◆ size_type

template<class I>
using swift::misc::CRange< I >::size_type = difference_type

STL compatibility.

Definition at line 208 of file range.h.

◆ value_type

template<class I>
using swift::misc::CRange< I >::value_type = typename std::iterator_traits<I>::value_type

STL compatibility.

Definition at line 203 of file range.h.

Constructor & Destructor Documentation

◆ CRange()

template<class I>
swift::misc::CRange< I >::CRange ( I begin,
I end )
inline

Constructor.

Definition at line 216 of file range.h.

Member Function Documentation

◆ begin()

template<class I>
const_iterator swift::misc::CRange< I >::begin ( ) const
inline

Begin and end iterators.

Definition at line 220 of file range.h.

◆ cbegin()

template<class I>
const_iterator swift::misc::CRange< I >::cbegin ( ) const
inline

Begin and end iterators.

Definition at line 221 of file range.h.

◆ cend()

template<class I>
const_iterator swift::misc::CRange< I >::cend ( ) const
inline

Begin and end iterators.

Definition at line 223 of file range.h.

◆ crbegin()

template<class I>
const_reverse_iterator swift::misc::CRange< I >::crbegin ( ) const
inline

Reverse begin and end iterators.

Definition at line 229 of file range.h.

◆ crend()

template<class I>
const_reverse_iterator swift::misc::CRange< I >::crend ( ) const
inline

Reverse begin and end iterators.

Definition at line 231 of file range.h.

◆ empty()

template<class I>
bool swift::misc::CRange< I >::empty ( ) const
inline

Returns true if the range is empty.

Definition at line 266 of file range.h.

◆ end()

template<class I>
const_iterator swift::misc::CRange< I >::end ( ) const
inline

Begin and end iterators.

Definition at line 222 of file range.h.

◆ front()

template<class I>
const_reference swift::misc::CRange< I >::front ( ) const
inline

Returns the element at the beginning of the range. Undefined if the range is empty.

Definition at line 274 of file range.h.

◆ frontOrDefault() [1/2]

template<class I>
const_reference swift::misc::CRange< I >::frontOrDefault ( ) const
inline

Returns the element at the beginning of the range, or a default value if the range is empty.

Definition at line 281 of file range.h.

◆ frontOrDefault() [2/2]

template<class I>
value_type swift::misc::CRange< I >::frontOrDefault ( value_type def) const
inline

Returns the element at the beginning of the range, or a default value if the range is empty.

Definition at line 288 of file range.h.

◆ isEmpty()

template<class I>
bool swift::misc::CRange< I >::isEmpty ( ) const
inline

Returns true if the range is empty.

Definition at line 267 of file range.h.

◆ operator T()

template<class I>
template<class T>
requires (std::is_convertible_v<value_type, typename T::value_type>)
swift::misc::CRange< I >::operator T ( ) const
inline

Implicit conversion to any container of value_type which supports push_back. This will copy elements.

Definition at line 243 of file range.h.

◆ rbegin()

template<class I>
const_reverse_iterator swift::misc::CRange< I >::rbegin ( ) const
inline

Reverse begin and end iterators.

Definition at line 228 of file range.h.

◆ rend()

template<class I>
const_reverse_iterator swift::misc::CRange< I >::rend ( ) const
inline

Reverse begin and end iterators.

Definition at line 230 of file range.h.

◆ reverse()

template<class I>
CRange< const_reverse_iterator > swift::misc::CRange< I >::reverse ( ) const
inline

Create a range from reverse iterators.

Definition at line 235 of file range.h.

◆ size()

template<class I>
size_type swift::misc::CRange< I >::size ( ) const
inline

Returns the number of elements in the range.

Definition at line 271 of file range.h.

◆ to() [1/2]

template<class I>
template<template< class... > class T>
auto swift::misc::CRange< I >::to ( ) const
inline

Explicit conversion to any container of value_type which supports push_back. This will copy elements.

Definition at line 258 of file range.h.

◆ to() [2/2]

template<class I>
template<class T>
T swift::misc::CRange< I >::to ( ) const
inline

Explicit conversion to any container of value_type which supports push_back. This will copy elements.

Definition at line 251 of file range.h.


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