# Bounds

## Description

`bounds_t<LowerT, UpperT>` holds the lower and upper bounds of a half-open interval. It is templated to allow the different integer representations for the lower and upper bounds. The intent is to model a valid iteration space over a single dimension.

Bounds can be used to iterate over an entire extent or to restrict iteration space within an extent

Creating Bounds

Bounds can be created using full bounds_t type, but this may be tedious.

    bounds_t<int, int>(start, finish)
    bounds_t<int, aligned<16>>(start, aligned<16>(finish))
    bounds_t<fixed<0>, fixed<1920>>()

It is simpler and clearer to use factory function bounds to build a bounds_t<>.

    bounds(start,finish);
    bounds(start, aligned<16>(finish));
    bounds(0_fixed, 1920_fixed)

Discovering Bounds

Accessors know their valid iteraton space. Initial bounds for an accessor are set to set the lower bound to be fixed<0> and the upper bound set to the value and type of the dimension's extent as specified during construction of the n_container(fixed<>,aligned<>, or int).

To query bounds for given dimension of the accessor use template function `bounds_d<int DimensionT>(object)`.

    auto b0 = bounds_d<0>(ca);
    auto b1 = bounds_d<1>(ca);
    for (int y = b0.lower(); y < b0.upper(); ++y)
        for (int x = b1.lower(); x < b1.upper(); ++x) {
              RGBAs pixel = ca[y][x];
    		 // …
        }

`bounds_t` can participate in C++11 range-based for loops.

    for (auto y: bounds_d<0>(ca))
        for (auto x: bounds_d<1>(ca)) {
              RGBAs pixel = ca[y][x];
    		 // …
        }

    for (auto y: ca.bounds_d0())
        for (auto x: ca.bounds_d1()) {
              RGBAs pixel = ca[y][x];
    		 // …
        }

N-Dimensional Indexes and Bounds

To model index and bounds values over multiple dimensions, respectively the following template classes are provided: `n_index_t<…>` and `n_bounds_t<…>` . These are both variadic templates, accepting any number of arguments.

`n_index` is a generator to simplify creating instances of `n_index_t`.

    n_index[540][960]

`n_bounds` is a generator to simplify creating instances of `n_bounds_t`.

    n_bounds[bounds(540,1080)][bounds(960,1920)]

Alternatively, `n_bounds_t` can be defined in terms of a `n_index_t` and `n_extent_t`.

    n_bounds(n_index[540][960], n_extent[540][960]);

Accessing Subsections

From a container's accessors, a new accessor can be created over a subsection defined by a `n_bounds_t`.

    auto ca = c.const_access();
    auto subsect = ca.section(n_bounds[bounds(540, 1080)][bounds(960,1920)]);

The effect is to restrict the results of `bounds_d<int Dimension>` on the subsection accessor.

You can create a new accessor translated to a different index space.

    auto offsetnewSpace = ca.translated_to(n_index[1000][2000]);
    auto zeroSpace = ca.translated_to_zero();

Accesses will have a translation applied that maps the `n_index` back to the lower bounds of the accessor that created it. This allows a smaller container to be reused in a larger index space that is being walked over by blocks, or to move a subsection index space back to the origin.

Parent topic: Usage Guidelines: Function Calls and Containers