libpnicore
Public Member Functions | Private Member Functions | Private Attributes
pni::core::slice Class Reference

index slice More...

#include <slice.hpp>

Public Member Functions

 slice ()
 default constructor
 
 slice (size_t first, size_t last, size_t stride=1)
 standard constructor More...
 
 slice (const std::initializer_list< size_t > &l)
 construction from a initializer list More...
 
 slice (size_t index)
 construction from a single index More...
 
 ~slice ()
 destructor
 
sliceoperator= (const slice &s)
 copy assignment operator
 
size_t first () const
 return the first element More...
 
size_t last () const
 return last element More...
 
size_t stride () const
 return stride More...
 

Private Member Functions

void _check_start_stop (const exception_record &o) const
 check first and last index More...
 
void _check_stride (const exception_record &o) const
 check stride More...
 

Private Attributes

size_t _first
 first index in the slice
 
size_t _last
 last index in the slice
 
size_t _stride
 distance between indices
 

Detailed Description

This types represents an index slice for an array. A slice can be used to identify an index range along a single dimensions. slice objects are of particular importance for creating array_view from mdarray instancers.

A slice includes all indices between the the first and last index of the slice where the last index is not included. A stride can be passed during construction which determines the number of steps between each subsequent element. If the stride is not 1 the last index will be adopted so that the

Examples:
example_array3.cpp, and example_array4.cpp.

Constructor & Destructor Documentation

pni::core::slice::slice ( size_t  first,
size_t  last,
size_t  stride = 1 
)
explicit

This is the default constructor for a Slice object.

Exceptions
range_errorif the first index exceeds the last
Parameters
firstfirst index
lastindex
stridesteps between subsequent elements
pni::core::slice::slice ( const std::initializer_list< size_t > &  l)
explicit

This can be used for implicit conversion which is sometimes quite useful for the creation fo array views.

mdarray<...> a({10,100});
//default construction of a view
auto v1 = a(slice(1,4),slice(50,100));
//one can also use initializer lists
auto v2 = a({1,4},{50,100});

! If the initializer list has only two elements they are treated as first and last element of the slice and the stride is set to 1. In the case of a three element initializer list the last element is considered as the stride of the slice.

Exceptions
range_errorif the first element is larger than the last
Parameters
linitializer list
pni::core::slice::slice ( size_t  index)
explicit

Constructor uses a single index. This constructor is equivalent to use slice(index,index+1,1).

Parameters
indexsingle index from which to create the slice

Member Function Documentation

void pni::core::slice::_check_start_stop ( const exception_record o) const
private

This private method throws an exception if the first index of the range exceeds the last one.

Exceptions
range_errorif first > last
Parameters
osignature of the origin where the error occured
void pni::core::slice::_check_stride ( const exception_record o) const
private

Checks if the stride does not exceed the span of the slice. If it does an exception will be thrown.

Exceptions
range_errorif stride exceeds span
Parameters
osignature of the origin where the error occured
size_t pni::core::slice::first ( ) const
inline
Returns
first element of slice
size_t pni::core::slice::last ( ) const
inline
Returns
last element of slice
size_t pni::core::slice::stride ( ) const
inline
Returns
stride of the slice

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