TRIQS/itertools 1.3.0
C++ range library
Loading...
Searching...
No Matches
itertools::range

#include <itertools/range.hpp>

Detailed Description

A lazy range of integers that mimics a Python range.

It stores the first value, the last value (excluded) and the step size between two indices. By default, the step size is set to 1.

This function returns an iterable lazy object, which can be used in range-based for loops:

for (auto i : range(5)) {
std::cout << i << " ";
}
std::cout << "\n";
for (auto i : range(-2, 1)) {
std::cout << i << " ";
}
std::cout << "\n";
for (auto i : range(10, 3, -2)) {
std::cout << i << " ";
}
std::cout << "\n";
for (auto i : range(0, 10, -1)) {
std::cout << i << " "; // empty
}
range()=default
Default constructor.

Output:

0 1 2 3 4
-2 -1 0
10 8 6 4

See also std::ranges::views::iota.

Definition at line 80 of file range.hpp.

Classes

struct  all_t
 Denote a full range at compile-time. More...
 
struct  const_iterator
 Const iterator type for itertools::range. More...
 

Public Types

using index_t = long
 Integer type for backward compatibility.
 

Public Member Functions

 range ()=default
 Default constructor.
 
 range (long first, long last) noexcept
 Construct a range with a step size of 1 and a given first and last (excluded) value.
 
 range (long first, long last, long step)
 Construct a range with a given step size and a given first and last (excluded) value.
 
 range (long last)
 Construct a range with a step size of 1, a first value set to 0 and a given last value (excluded).
 
const_iterator begin () const noexcept
 The same as cbegin().
 
const_iterator cbegin () const noexcept
 Beginning of the integer range.
 
const_iterator cend () const noexcept
 End of the range.
 
const_iterator end () const noexcept
 The same as cend().
 
long first () const
 Get first value of the range.
 
long last () const
 Get last value of the range (excluded).
 
range operator+ (long shift) const
 Shift the whole range by a given amount.
 
bool operator== (range const &) const =default
 Default equal-to operator.
 
long size () const
 Get number of elements in the range.
 
long step () const
 Get step size between two elements of the range.
 

Static Public Attributes

static constexpr all_t all = {}
 See range::all_t.
 

Friends

std::ostream & operator<< (std::ostream &os, const range &rg)
 Write the range details to std::ostream.
 

Constructor & Destructor Documentation

◆ range() [1/4]

itertools::range::range ( )
default

◆ range() [2/4]

itertools::range::range ( long first,
long last )
inlinenoexcept

Construct a range with a step size of 1 and a given first and last (excluded) value.

Parameters
firstFirst value of the range.
lastLast value of the range (excluded).

Definition at line 116 of file range.hpp.

◆ range() [3/4]

itertools::range::range ( long first,
long last,
long step )
inline

Construct a range with a given step size and a given first and last (excluded) value.

Throws an exception if the step size is zero.

Parameters
firstFirst value of the range.
lastLast value of the range (excluded).
stepNumber of integers between two elements of the range.

Definition at line 127 of file range.hpp.

◆ range() [4/4]

itertools::range::range ( long last)
inlineexplicit

Construct a range with a step size of 1, a first value set to 0 and a given last value (excluded).

Parameters
lastLast value of the range (excluded).

Definition at line 135 of file range.hpp.

Member Function Documentation

◆ cbegin()

const_iterator itertools::range::cbegin ( ) const
inlinenodiscardnoexcept

Beginning of the integer range.

Returns
Iterator with its current value set to the first value of the range.

Definition at line 262 of file range.hpp.

◆ cend()

const_iterator itertools::range::cend ( ) const
inlinenodiscardnoexcept

End of the range.

Returns
Iterator with its current value set to the excluded last value of the range.

Definition at line 271 of file range.hpp.

◆ operator+()

range itertools::range::operator+ ( long shift) const
inlinenodiscard

Shift the whole range by a given amount.

Simply adds the given shift to the first and last value of the range, while keeping the same step size.

Parameters
shiftAmount to shift the range by.
Returns
Shifted range.

Definition at line 161 of file range.hpp.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const range & rg )
friend

Write the range details to std::ostream.

Parameters
osstd::ostream object.
rgrange object.
Returns
Reference to os.

Definition at line 170 of file range.hpp.


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