HiPipe  0.7.0
C++17 data pipeline with Python bindings.
Classes | Functions
Multidimensional container utilities.

Classes

struct  hipipe::utility::ndims< Rng, PrevRng, IsRange >
 Gets the number of dimensions of a multidimensional range. More...
 
struct  hipipe::utility::is_specialization< typename, typename >
 Detect whether the given type is a specialization of the given container template. More...
 
struct  hipipe::utility::ndim_container< T, Dims, Container >
 Fast declaration of a multidimensional container. More...
 

Functions

template<long NDims, typename Rng >
std::vector< std::vector< long > > ndim_size (const Rng &rng)
 Calculates the size of a multidimensional range. More...
 
template<typename Rng >
std::vector< std::vector< long > > ndim_size (const Rng &rng)
 Calculates the size of a multidimensional range. More...
 
template<long NDims, typename Rng , typename ValT = ndim_type_t<Rng, NDims>>
Rng & ndim_resize (Rng &vec, const std::vector< std::vector< long >> &vec_size, ValT val=ValT{})
 Resizes a multidimensional range to the given size. More...
 
template<long NDims, typename Rng , typename ValT = ndim_type_t<Rng, NDims>>
Rng & ndim_pad (Rng &vec, ValT val=ValT{})
 Pads a mutlidimensional range to a rectangular size. More...
 
template<long NDims, typename Rng >
std::vector< long > shape (const Rng &rng)
 Calculates the shape of a multidimensional range. More...
 
template<typename Rng >
std::vector< long > shape (const Rng &rng)
 Calculates the shape of a multidimensional range. More...
 
template<long NDims, typename Rng >
auto flat_view (Rng &rng)
 Makes a flat view out of a multidimensional range. More...
 
template<long N, typename Rng >
auto reshaped_view (Rng &rng, std::vector< long > shape)
 Makes a view of a multidimensional range with a specific shape. More...
 
template<long NDims, typename Rng , typename Gen >
void generate (Rng &&rng, Gen &&gen, long gendims=std::numeric_limits< long >::max())
 Fill a multidimensional range with values generated by a nullary function. More...
 
template<long NDims, typename Rng , typename Prng = std::mt19937&, typename Dist = std::uniform_real_distribution<double>>
void random_fill (Rng &&rng, Dist &&dist=Dist{0, 1}, Prng &&prng=utility::random_generator, long gendims=std::numeric_limits< long >::max())
 Fill a multidimensional range with random values. More...
 
template<typename Tuple >
bool same_size (Tuple &&rngs)
 Utility function which checks that all the ranges in a tuple have the same size. More...
 

Detailed Description

Function Documentation

◆ flat_view()

template<long NDims, typename Rng >
auto flat_view ( Rng &  rng)

Makes a flat view out of a multidimensional range.

std::vector<std::list<int>> vec{{1, 2}, {3}, {}, {4, 5, 6}};
std::vector<int> rvec = ranges::to_vector(flat_view(vec));
// rvec == {1, 2, 3, 4, 5, 6};
// the same with manually defined number of dimensions
std::vector<int> rvec = ranges::to_vector(flat_view<2>(vec));
// rvec == {1, 2, 3, 4, 5, 6};
Parameters
rngThe range to be flattened.
Template Parameters
NDimsThe number of dimensions that should be flattened into one.
Returns
Flat view (input_range) of the given range.

Definition at line 450 of file ndim.hpp.

◆ generate()

template<long NDims, typename Rng , typename Gen >
void generate ( Rng &&  rng,
Gen &&  gen,
long  gendims = std::numeric_limits<long>::max() 
)

Fill a multidimensional range with values generated by a nullary function.

If the vector is multidimensional, the generator will be used only up to the given dimension and the rest of the dimensions will be constant.

Example:

struct gen {
int i = 0;
int operator()() { return i++; };
};
std::vector<std::vector<std::vector<int>>> data = {{{-1, -1, -1}, {-1}}, {{-1}, {-1, -1}}};
generate(data, gen{}, 0);
// data == {{{0, 0, 0}, {0}}, {{0}, {0, 0}}};
generate(data, gen{}, 1);
// data == {{{0, 0, 0}, {0}}, {{1}, {1, 1}}};
generate(data, gen{}, 2);
// data == {{{0, 0, 0}, {1}}, {{2}, {3, 3}}};
generate(data, gen{}, 3);
// data == {{{0, 1, 2}, {3}}, {{4}, {5, 6}}};
// Note that the generator is called with every element in the
// filled dimension, even if there are no actual elements in
// higher dimensions! Example:
data = {{{-1, -1, -1}, {-1}}, {{}, {}}, {{-1}, {-1, -1}}};
generate(data, gen{}, 1);
// data == {{{0, 0, 0}, {0}}, {{}, {}}, {{2}, {2, 2}}};
generate(data, gen{}, 2);
// data == {{{0, 0, 0}, {1}}, {{}, {}}, {{4}, {5, 5}}};
generate(data, gen{}, 3);
// data == {{{0, 1, 2}, {3}}, {{}, {}}, {{4}, {5, 6}}};
Parameters
rngThe range to be filled.
genThe generator to be used.
gendimsThe generator will be used only for this number of dimension. The rest of the dimensions will be filled by the last generated value. Use std::numeric_limits<long>::max() to fill all the dimensions using the generator.
Template Parameters
NDimsThe number of dimensions to which will the fill routine recurse. Elements after this dimension are considered to be units (even if they are ranges). If omitted, it defaults to ndims<Rng> - ndims<gen()>.

Definition at line 628 of file ndim.hpp.

◆ ndim_pad()

template<long NDims, typename Rng , typename ValT = ndim_type_t<Rng, NDims>>
Rng& ndim_pad ( Rng &  vec,
ValT  val = ValT{} 
)

Pads a mutlidimensional range to a rectangular size.

The range has to support .resize() method up to the given dimension.

Example:

std::vector<std::vector<int>> v1 = {{1, 2}, {3, 4, 5}, {}};
ndim_pad(v1, -1);
// v1 == {{1, 2, -1}, {3, 4, 5}, {-1, -1, -1}};
std::vector<std::list<std::vector<int>>> v2 = {{{1}, {2, 3}}, {{4, 5, 6}}};
ndim_pad<2>(vec, {-1, -2, -3, -4}); // pad only the first two dimensions
// v2 = {{{1}, {2, 3}}, {{4, 5, 6}, {-1, -2, -3, -4}}};
Parameters
vecThe range to be padded.
valThe value to pad with.
Template Parameters
NDimsThe number of dimensions to be considered. If omitted, it defaults to ndims<Rng> - ndims<ValT>.
Returns
The reference to the given vector after padding.

Definition at line 309 of file ndim.hpp.

◆ ndim_resize()

template<long NDims, typename Rng , typename ValT = ndim_type_t<Rng, NDims>>
Rng& ndim_resize ( Rng &  vec,
const std::vector< std::vector< long >> &  vec_size,
ValT  val = ValT{} 
)

Resizes a multidimensional range to the given size.

i-th element of the given size vector are the sizes of the ranges in the i-th dimension. See ndim_size. The range has to support .resize() method up to the given dimension.

Example:

std::vector<std::vector<int>> vec;
ndim_resize(vec, {{2}, {3, 1}}, 2);
// vec == {{2, 2, 2}, {2}};
Parameters
vecThe range to be resized.
vec_sizeThe requested size created by ndim_size.
valThe value to pad with.
Template Parameters
NDimsThe number of dimensions to be considered. If omitted, it defaults to ndims<Rng> - ndims<ValT>.
Returns
The reference to the given vector after resizing.

Definition at line 262 of file ndim.hpp.

◆ ndim_size() [1/2]

template<long NDims, typename Rng >
std::vector<std::vector<long> > ndim_size ( const Rng &  rng)

Calculates the size of a multidimensional range.

i-th element of the resulting vector are the sizes of the ranges in the i-th dimension.

Example:

std::vector<std::list<int>> rng{{1, 2, 3}, {1}, {5, 6}, {7}};
std::vector<std::vector<long>> rng_size = ndim_size<2>(rng);
// rng_size == {{4}, {3, 1, 2, 1}};
rng_size = ndim_size(rng); // default number of dimensions is the depth of nested ranges
// rng_size == {{4}, {3, 1, 2, 1}};
rng_size = ndim_size<1>(rng);
// rng_size == {{4}};
Parameters
rngThe multidimensional range whose size shall be calculated.
Template Parameters
NDimsThe number of dimensions that should be considered.
Returns
The sizes of the given range.

Definition at line 191 of file ndim.hpp.

◆ ndim_size() [2/2]

template<typename Rng >
std::vector<std::vector<long> > ndim_size ( const Rng &  rng)

Calculates the size of a multidimensional range.

This overload automatically deduces the number of dimension using ndims<Rng>.

Definition at line 204 of file ndim.hpp.

◆ random_fill()

template<long NDims, typename Rng , typename Prng = std::mt19937&, typename Dist = std::uniform_real_distribution<double>>
void random_fill ( Rng &&  rng,
Dist &&  dist = Dist{0, 1},
Prng &&  prng = utility::random_generator,
long  gendims = std::numeric_limits<long>::max() 
)

Fill a multidimensional range with random values.

If the range is multidimensional, the random generator will be used only up to the given dimension and the rest of the dimensions will be constant.

Note: This function internally uses utility::generate().

Example:

std::mt19937 gen = ...;
std::uniform_int_distribution dist = ...;
std::vector<std::vector<std::vector<int>>> data = {{{0, 0, 0}, {0}}, {{0}, {0, 0}}};
random_fill(data, dist, gen, 0);
// data == e.g., {{{4, 4, 4}, {4}}, {{4}, {4, 4}}};
random_fill(data, dist, gen, 1);
// data == e.g., {{{8, 8, 8}, {8}}, {{2}, {2, 2}}};
random_fill(data, dist, gen, 2);
// data == e.g., {{{8, 8, 8}, {6}}, {{7}, {3, 3}}};
random_fill(data, dist, gen, 3);
// data == e.g., {{{8, 2, 3}, {1}}, {{2}, {4, 7}}};
Parameters
rngThe range to be filled.
distThe distribution to be used.
prngThe random generator to be used.
gendimsThe random generator will be used only for this number of dimension. The rest of the dimensions will be filled by the last generated value. Use std::numeric_limits<long>::max() to randomly fill all the dimensions.
Template Parameters
NDimsThe number of dimensions to which will the fill routine recurse. Elements after this dimension are considered to be units (even if they are ranges). If omitted, it defaults to ndims<Rng> - ndims<dist(prng)>.

Definition at line 682 of file ndim.hpp.

◆ reshaped_view()

template<long N, typename Rng >
auto reshaped_view ( Rng &  rng,
std::vector< long >  shape 
)

Makes a view of a multidimensional range with a specific shape.

Usage:

std::list<int> lst{1, 2, 3, 4, 5, 6};
std::vector<std::vector<int>> rlst = ranges::to_vector(reshaped_view<2>(lst, {2, 3}));
// rlst == {{1, 2, 3}, {4, 5, 6}};
Parameters
rngThe base range for the view.
shapeThe list of shapes. Those can contain a single -1, which denotes that the dimension size shall be automatically deduced. All the other values have to be positive.
Template Parameters
NThe number of dimensions. Has to be equal to shape.size().
Returns
View (input_range) of the original range with the given shape.

Definition at line 540 of file ndim.hpp.

◆ same_size()

template<typename Tuple >
bool same_size ( Tuple &&  rngs)

Utility function which checks that all the ranges in a tuple have the same size.

Example:

std::vector<int> v1 = {1, 2, 3};
std::vector<double> v2 = {1., 2., 3.};
std::vector<bool> v3 = {true};
assert(same_size(std::tie(v1, v2)) == true);
assert(same_size(std::tie(v1, v3)) == false);
Parameters
rngsA tuple of ranges.

Definition at line 735 of file ndim.hpp.

◆ shape() [1/2]

template<long NDims, typename Rng >
std::vector<long> shape ( const Rng &  rng)

Calculates the shape of a multidimensional range.

std::list<std::vector<int>> rng{{1, 2}, {3, 4}, {5, 6}, {5, 6}};
std::vector<long> rng_shape = shape<2>(rng);
// rng_shape == {4, 2};
rng_shape = shape<1>(rng);
// rng_shape == {4};
rng_shape = shape(rng); // the number of dimensions defaults to ndims<Rng>
// rng_shape == {4, 2};
Parameters
rngThe range whose shape shall be calculated. All the subranges on the same dimension have to be of equal size.
Template Parameters
NDimsThe number of dimensions that should be considered.
Returns
The shape of the given range.

Definition at line 387 of file ndim.hpp.

◆ shape() [2/2]

template<typename Rng >
std::vector<long> shape ( const Rng &  rng)

Calculates the shape of a multidimensional range.

This overload automatically deduces the number of dimension using ndims<Rng>.

Definition at line 402 of file ndim.hpp.

flat_view
auto flat_view(Rng &rng)
Makes a flat view out of a multidimensional range.
Definition: ndim.hpp:450
same_size
bool same_size(Tuple &&rngs)
Utility function which checks that all the ranges in a tuple have the same size.
Definition: ndim.hpp:735
ndim_pad
Rng & ndim_pad(Rng &vec, ValT val=ValT{})
Pads a mutlidimensional range to a rectangular size.
Definition: ndim.hpp:309
ndim_size
std::vector< std::vector< long > > ndim_size(const Rng &rng)
Calculates the size of a multidimensional range.
Definition: ndim.hpp:191
ndim_resize
Rng & ndim_resize(Rng &vec, const std::vector< std::vector< long >> &vec_size, ValT val=ValT{})
Resizes a multidimensional range to the given size.
Definition: ndim.hpp:262
shape
std::vector< long > shape(const Rng &rng)
Calculates the shape of a multidimensional range.
Definition: ndim.hpp:387
random_fill
void random_fill(Rng &&rng, Dist &&dist=Dist{0, 1}, Prng &&prng=utility::random_generator, long gendims=std::numeric_limits< long >::max())
Fill a multidimensional range with random values.
Definition: ndim.hpp:682
generate
void generate(Rng &&rng, Gen &&gen, long gendims=std::numeric_limits< long >::max())
Fill a multidimensional range with values generated by a nullary function.
Definition: ndim.hpp:628