Ranges

Collections of elements are designed in accordance with the Range TS. The library extends those concepts with two concepts and helpers functions. Range-related functions and views are in the namespace mln::ranges whereas concepts are in the namespace mln::concepts.

Concepts

Segmented (Multidimensional) Range

Let SegRng be a type that models SegmentedRange (details).

The SegRng also models ForwardRange (stl).

Types

Let SegRng inherit all types defined for ForwardRange (stl).

Expressions

Let SegRng inherit all valid expressions defined for ForwardRange (stl). Let:

  • rng be an instance of SegRng.

Then we have the following valid expressions :

Expression

Return type

Pre-condition

Post-condition

Description

rng.rows()

  • models ForwardRange (stl)

  • The subrange’s value_type must be the same as SegRng::value_type

none

none

Return a range of sub-range where each sub-range is a “line” from the original non-segmented range.

Description

A segmented (multidimentional) range is hierarchical and provides a way to iterate over the last level of the hierarchy.

Normal range vs hierarchical range traversal. Left: a single range (hence iterator) traverses a non-contiguous range. Right: a hierarchical range, here a range of contiguous range.
../_images/linear_rng.svg ../_images/segmented_rng.svg

Example:

for (auto&& row : rng.rows())
  for (auto v : row)
    // Usage of v

Reversible Range

Let RevRng be a type that models ReversibleRange (details).

The RevRng also models ForwardRange (stl).

Types

Let RevRng inherit all types defined for ForwardRange (stl).

Expressions

Let RevRng inherit all valid expressions defined for ForwardRange (stl). Let:

  • rng be an instance of RevRng.

Then we have the following valid expressions :

Expression

Return type

Pre-condition

Post-condition

Description

rng.reversed()

  • models ForwardRange (stl)

  • The returned range’s value_type must be the same as SegRng::value_type

none

none

Return a range whose forward browsing is done in the reverse order comparing to the orignal range.

Description

A reversible range that can be traversed forward and backward. This is not as strict as BidirectionalRange (stl) that requires bidirectional iterators.

Utilities for generic programming

auto rows(Range rng)
Parameters:

rng – Input range

Generic util for iterating row-wise over a multidimentional or a non-multidimentional range for performance. If rng is multidimensional, it returns rng.rows(), otherwise it returns the singleton ::ranges::single(rng).

Example:

for (auto r : ranges::rows(rng))
  for (auto v : r)
    // Usage of v
mln_foreach

This macro eases the usage of multidimensional ranges for generic programming and efficient iteration. The syntax is mln_foreach(decl_expr, rng_expr). If decl_expr uses commas, it has to be parenthesized.

Examples

Simple usage:

mln_foreach(auto v, rng)
  // Usage of v

Usage with bind expressions:

mln_foreach((auto [v1, v2]), mln::ranges::zip(rng1, rng2))
  // Usage of v1 and v2

Predifined Views

Concepts detail