Range-v3
Range algorithms, views, and actions for the Standard Library
User Manual

Preface


Range library for C++11/14/17. This code is the basis of a formal proposal to add range support to the C++ standard library.

Development Status:

This code is fairly stable, well-tested, and suitable for casual use, although currently lacking documentation. No promise is made about support or long-term stability. This code will evolve without regard to backwards compatibility.

Installation


This library is header-only. You can get the source code from the range-v3 repository on github. To compile with Range-v3, you can either #include the entire library:

#include <range/v3/all.hpp>

Or you can #include only the core, and then the individual headers you want:

#include <range/v3/....

License


Most of the source code in this project are mine, and those are under the Boost Software License. Parts are taken from Alex Stepanov's Elements of Programming, Howard Hinnant's libc++, and from the SGI STL. Please see the attached LICENSE file and the CREDITS file for the licensing and acknowledgements.

Supported Compilers


The code is known to work on the following compilers:

  • clang 3.4.0
  • GCC 4.9.0

Quick Start


Range v3 is a generic library that augments the existing standard library with facilities for working with ranges. A range can be loosely thought of a pair of iterators, although they need not be implemented that way. Bundling begin/end iterators into a single object brings several benefits.

Why Use Ranges?

Convenience

It's more convenient to pass a single range object to an algorithm than separate begin/end iterators. Compare:

std::vector<int> v{/*...*/};
std::sort( v.begin(), v.end() );

with

std::vector<int> v{/*...*/};

Range v3 contains a full implementation of all the standard algorithms with range-based overloads for convenience.

Composability

Having a single range object permits pipelines of operations. In a pipeline, a range is lazily adapted or eagerly mutated in some way, with the result immediately available for further adaptation or mutation. Lazy adaption is handled by views, and eager mutation is handled by actions.

Views

A view is a lightweight wrapper that presents a view of an underlying sequence of elements in some custom way without mutating or copying it. Views are cheap to create and copy, and have non-owning reference semantics. Below are some examples:

Filter a container using a predicate and transform it.

std::vector<int> vi{1,2,3,4,5,6,7,8,9,10};
using namespace ranges;
auto rng = vi | view::remove_if([](int i){return i % 2 == 1;})
| view::transform([](int i){return std::to_string(i);});
// rng == {"2","4","6","8","10"};

Generate an infinite list of integers starting at 1, square them, take the first 10, and sum them:

using namespace ranges;
int sum = accumulate(view::ints(1)
| view::transform([](int i){return i*i;})
| view::take(10), 0);

Generate a sequence on the fly with a range comprehension and initialize a vector with it:

using namespace ranges;
std::vector<int> vi =
view::for_each(view::ints(1,10), [](int i){
return yield_from(view::repeat_n(i,i));
});
// vi == {1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,...}

Actions

When you want to mutate a container in-place, or forward it through a chain of mutating operations, you can use actions. The following examples should make it clear.

Read data into a vector, sort it, and make it unique.

extern std::vector<int> read_data();
using namespace ranges;
std::vector<int> vi = read_data() | action::sort | action::unique;

Do the same to a vector that already contains some data:

Mutate the container in-place:

Same as above, but with function-call syntax instead of pipe syntax:

Create Custom Ranges

Range v3 provides a utility for easily creating your own range types, called view_facade. The code below uses view_facade to create a range that traverses a null-terminated string:

#include <range/v3/all.hpp>
using namespace ranges;
// A range that iterates over all the characters in a
// null-terminated string.
class c_string_range
: public view_facade<c_string_range>
{
friend range_access;
char const * sz_;
char const & get() const { return *sz_; }
bool done() const { return *sz_ == '\0'; }
void next() { ++sz_; }
public:
c_string_range() = default;
explicit c_string_range(char const *sz) : sz_(sz)
{
assert(sz != nullptr);
}
};

The view_facade class generates an iterator and begin/end member functions from the minimal interface provided by c_string_range. This is an example of a very simple range for which it is not necessary to separate the range itself from the thing that iterates the range. Future examples will show examples of more sophisticated ranges.

With c_string_range, you can now use algorithms to operate on null-terminated strings, as below:

#include <iostream>
int main()
{
c_string_range r("hello world");
// Iterate over all the characters and print them out
ranges::for_each(r, [](char ch){
std::cout << ch << ' ';
});
// prints: h e l l o w o r l d
}

Adapting Ranges

Often, a new range type is most easily expressed by adapting an existing range type. That's the case for many of the range views provided by the Range v3 library; for example, the view::remove_if and view::transform views. These are rich types with many moving parts, but thanks to a helper class called view_adaptor, they aren't hard to write.

Below in roughly 2 dozen lines of code is the transform view, which takes one range and transforms all the elements with a unary function.

// A class that adapts an existing range with a function
template<class Rng, class Fun>
class transform_view : public view_adaptor<transform_view<Rng, Fun>, Rng>
{
friend range_access;
semiregular_t<Fun> fun_; // Make Fun model SemiRegular if it doesn't
class adaptor : public adaptor_base
{
semiregular_t<Fun> fun_;
public:
adaptor() = default;
adaptor(semiregular_t<Fun> const &fun) : fun_(fun) {}
// Here is where we apply Fun to the elements:
auto get(range_iterator_t<Rng> it) const -> decltype(fun_(*it))
{
return fun_(*it);
}
};
adaptor begin_adaptor() const { return {fun_}; }
adaptor end_adaptor() const { return {fun_}; }
public:
transform_view() = default;
transform_view(Rng && rng, Fun fun)
: transform_view::view_adaptor{std::forward<Rng>(rng)}
, fun_(std::move(fun))
{}
};
template<class Rng, class Fun>
transform_view<Rng, Fun> transform(Rng && rng, Fun fun)
{
return {std::forward<Rng>(rng), std::move(fun)};
}

Range transformation is achieved by defining a nested adaptor class that handles the transformation, and then defining begin_adaptor and end_adaptor members that return adaptors for the begin iterator and the end sentinel, respectively. The adaptor class has a get member that performs the transformation. It is passed an iterator to the current element. Other members are available for customization: equal, next, prev, advance, and distance_to; but the transform adaptor accepts the defaults defined in adaptor_base.

With transform_view, we can print out the first 20 squares:

int main()
{
auto squares = ::transform(view::ints(1), [](int i){return i*i;});
for(int i : squares | view::take(20))
std::cout << i << ' ';
std::cout << '\n';
// prints 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
}

The transform_view defined above is an InputRange when its wrapping an InputRange, a ForwardRange when its wrapping a ForwardRange, etc. That happens because of smart defaults defined in the adaptor_base class. That frees you from having to deal with a host of niggly detail when implementing iterators.

*(Note: the above transform_view always stores a copy of the function in the sentinel. That is only necessary if the underlying range's sentinel type models BidirectionalIterator. That's a finer point that you shouldn't worry about right now.)*

Constrain Functions with Concepts

The Range v3 library makes heavy use of concepts to constrain functions, control overloading, and check type constraints at compile-time. It achieves this with the help of a Concepts Lite emulation layer that works on any standard-conforming C++11 compiler. The library provides many useful concepts, both for the core language and for iterators and ranges. You can use the concepts framework to constrain your own code.

For instance, if you would like to write a function that takes an iterator/sentinel pair, you can write it like this:

template<class Iter, class Sent, class Comp = /*...some_default..*/,
CONCEPT_REQUIRES_(Sentinel<Sent, Iter>())>
void my_algorithm(Iter first, Sent last, Comp comp = Comp{})
{
// ...
}

You can then add an overload that take a Range:

template<class Rng, class Comp = /*...some_default..*/,
CONCEPT_REQUIRES_(Range<Rng>())>
void my_algorithm(Rng && rng, Comp comp = Comp{})
{
return my_algorithm(ranges::begin(rng), ranges::end(rng));
}

With the type constraints expressed with the CONCEPTS_REQUIRES_ macro, these two overloads are guaranteed to not be ambiguous.

Range v3 and the Future

Range v3 forms the basis for a proposal to add ranges to the standard library (N4128), and will also be the basis for a Technical Specification on Ranges. Its design direction has already passed an initial review by the standardization committee. What that means is that you may see your compiler vendor shipping a library like Range v3 at some point in the future. That's the hope, anyway.

Enjoy!

Range Views


The big advantage of ranges over iterators is their composability. They permit a functional style of programming where data is manipulated by passing it through a series of combinators. In addition, the combinators can be lazy, only doing work when the answer is requested, and purely functional, without mutating the original data. This makes it easier to reason about your code, especially when writing concurrent programs.

Below is a list of the lazy range combinators, or views, that Range v3 provides, and a blurb about how each is intended to be used.

view::adjacent_filter
For each pair of adjacent elements in a source range, evaluate the specified binary predicate. If the predicate evaluates to false, the second element of the pair is removed from the result range; otherwise, it is included. The first element in the source range is always included. (For instance, adjacent_filter with std::not_equal_to filters out all the non-unique elements.)
view::adjacent_remove_if
For each pair of adjacent elements in a source range, evaluate the specified binary predicate. If the predicate evaluates to true, the first element of the pair is removed from the result range; otherwise, it is included. The last element in the source range is always included.
view::all
Return a range containing all the elements in the source. Useful for converting containers to ranges.
view::bounded
Convert the source range to a bounded range, where the type of the end is the same as the begin. Useful for iterating over a range with C++'s range-based for loop.
view::chunk
Given a source range and an integer N, produce a range of contiguous ranges where each inner range has N contiguous elements. The final range may have fewer than N elements.
view::concat
Given N source ranges, produce a result range that is the concatenation of all of them.
view::const_
Present a const view of a source range.
view::counted
Given an iterator it and a count n, create a range that starts at it and includes the next n elements.
view::delimit
Given a source range and a value, return a new range that ends either at the end of the source or at the first occurrence of the value, whichever comes first. Alternatively, view::delimit can be called with an iterator and a value, in which case it returns a range that starts at the specified position and ends at the first occurrence of the value.
view::drop
Given a source range and an integral count, return a range consisting of all but the first count elements from the source range, or an empty range if it has fewer elements.
view::drop_exactly
Given a source range and an integral count, return a range consisting of all but the first count elements from the source range. The source range must have at least that many elements.
view::drop_while
Remove elements from the front of a range that satisfy a unary predicate.
view::empty
Create an empty range with a given value type.
view::generate
Given a nullary function, return an infinite range whose elements are generated with the function.
view::generate_n
Given a nullary function and a count, return a range that generates the requested number of elements by calling the function.
view::group_by
Given a source range and a binary predicate, return a range of ranges where each range contains contiguous elements from the source range such that the following condition holds: for each element in the range apart from the first, when that element and the first element are passed to the binary predicate, the result is true. In essence, view::group_by groups contiguous elements together with a binary predicate.
view::indirect
Given a source range of readable values (e.g. pointers or iterators), return a new view that is the result of dereferencing each.
view::intersperse
Given a source range and a value, return a new range where the value is inserted between contiguous elements from the source.
view::ints
Generate a range of monotonically increasing ints. When used without arguments, it generates the quasi-infinite range [0,1,2,3...]. It can also be called with a lower bound, or with a lower and upper bound (exclusive).
view::iota
A generalization of view::ints that generates a sequence of monotonically increasing values of any incrementable type. When specified with a single argument, the result is an infinite range beginning at the specified value. With two arguments, the values are assumed to denote a half-open range.
view::join
Given a range of ranges, join them into a flattened sequence of elements. Optionally, you can specify a value or a range to be inserted between each source range.
view::keys
Given a range of pairs (like a std::map), return a new range consisting of just the first element of the pair.
view::move
Given a source range, return a new range where each element has been has been cast to an rvalue reference.
view::partial_sum
Given a range and a binary function, return a new range where the Nth element is the result of applying the function to the Nth element from the source range and the (N-1)th element from the result range.
view::remove_if
Given a source range and a unary predicate, filter out those elements that do not satisfy the predicate. (For users of Boost.Range, this is like the filter adaptor with the predicate negated.)
view::repeat
Given a value, create a range that is that value repeated infinitely.
view::repeat_n
Given a value and a count, create a range that is that value repeated count number of times.
view::replace
Given a source range, a source value and a target value, create a new range where all elements equal to the source value are replaced with the target value.
view::replace_if
Given a source range, a unary predicate and a target value, create a new range where all elements that satisfy the predicate are replaced with the target value.
view::reverse
Create a new range that traverses the source range in reverse order.
view::single
Given a value, create a range with exactly one element.
view::slice
Give a source range a lower bound (inclusive) and an upper bound (exclusive), create a new range that begins and ends at the specified offsets. Both the begin and the end can be integers relative to the front, or relative to the end with "`end-2`" syntax.
view::split
Given a source range and a delimiter specifier, split the source range into a range of ranges using the delimiter specifier to find the boundaries. The delimiter specifier can be a value, a subrange, a predicate, or a function. The predicate should take an single argument of the range's reference type and return true if and only if the element is part of a delimiter. The function should accept current/end iterators into the source range and return make_pair(true, iterator_past_the_delimiter) if the current position is a boundary; otherwise, make_pair(false, cur). The delimiter character(s) are excluded from the resulting range of ranges.
view::stride
Given a source range and an integral stride value, return a range consisting of every Nth element, starting with the first.
view::tail
Given a source range, return a new range without the first element. The range must have at least one element.
view::take
Given a source range and an integral count, return a range consisting of the first count elements from the source range, or the complete range if it has fewer elements. (The result of view::take is not a SizedRange.)
view::take_exactly
Given a source range and an integral count, return a range consisting of the first count elements from the source range. The source range must have at least that many elements. (The result of view::take_exactly is a SizedRange.)
view::take_while
Given a source range and a unary predicate, return a new range consisting of the elements from the front that satisfy the predicate.
view::tokenize
Given a source range and optionally a submatch specifier and a std::regex_constants::match_flag_type, return a std::regex_token_iterator to step through the regex submatches of the source range. The submatch specifier may be either a plain int, a std::vector<int>, or a std::initializer_list<int>.
view::transform
Given a source range and a unary function, return a new range where each result element is the result of applying the unary function to a source element.
view::unbounded
Given an iterator, return an infinite range that begins at that position.
view::unique
Given a range, return a new range where all consecutive elements that compare equal save the first have been filtered out.
view::values
Given a range of pairs (like a std::map), return a new range consisting of just the second element of the pair.
view::zip
Given N ranges, return a new range where Mth element is the result of calling make_tuple on the Mth elements of all N ranges.
view::zip_with
Given N ranges and a N-ary function, return a new range where Mth element is the result of calling the function on the Mth elements of all N ranges.