CustomIndexArray< Typ, Tags > Class Template Reference

CPP API: mio::CustomIndexArray< Typ, Tags > Class Template Reference

A class template for an array with custom indices. More...

#include <custom_index_array.h>

Collaboration diagram for mio::CustomIndexArray< Typ, Tags >:

Classes

class  Slice
 A Slice represents a slice of data along one dimension, given a start and end index into that dimension. More...
 

Public Types

using const_iterator = typename InternalArrayType::const_iterator
 
using Index = ::mio::Index< Tags... >
 
using InternalArrayType = Eigen::Array< Type, Eigen::Dynamic, 1 >
 
using iterator = typename InternalArrayType::iterator
 
using reference = Type &
 
using Type = Typ
 
using value_type = Type
 

Public Member Functions

auto & array ()
 
auto const & array () const
 array returns a reference to the internally stored flat array. More...
 
iterator begin ()
 Get a start iterator for the elements. More...
 
const_iterator begin () const
 Get a start iterator for the elements. More...
 
template<class OtherType >
requires std::convertible_to< Type, OtherType > CustomIndexArray< OtherType, Tags... > convert () const
 Convert internally stored data to OtherType and save into new CustomIndexArray. More...
 
 CustomIndexArray ()
 Create an empty CustomIndexArray with size 0. Use the resize member function to add entries. More...
 
template<std::input_or_output_iterator Iter>
 CustomIndexArray (Index const &dims, Iter b, Iter e)
 Initializes array with values from a range. More...
 
iterator end ()
 Get an end iterator for the elements. More...
 
const_iterator end () const
 Get an end iterator for the elements. More...
 
size_t get_flat_index (Index const &index) const
 get_flat_index returns the flat index into the stored array, given the indices of each category More...
 
Ts m_numel (product(dims))
 
Ts m_y (InternalArrayType::Constant(m_numel, 1, Type{std::forward< Ts >(args)...}))
 
constexpr size_t numel () const
 numel returns the number of elements More...
 
CustomIndexArrayoperator= (const Type &scalar)
 Assign the same value to each element of the array. More...
 
Typeoperator[] (Index const &index)
 returns the entry of the array given a MultiIndex More...
 
Type const & operator[] (Index const &index) const
 returns the entry of the array given a flat index index More...
 
template<class... Ts>
 requires (std::is_constructible_v< Type, Ts... >) CustomIndexArray(Index const &dims
 CustomIndexArray constructor, that initializes the array to constant instances of CustsomIndexArray::Type. More...
 
void resize (Index new_dims)
 Resize all dimensions, invalidating entries. More...
 
template<class Tag >
void resize (mio::Index< Tag > new_dim)
 Resize a single dimension, invalidating entries. More...
 
template<class IOContext >
void serialize (IOContext &io) const
 serialize this. More...
 
void set_multiple (const std::vector< typename CustomIndexArray< Typ, Tags... >::Index > &indices, const Typ &value)
 Set multiple entries to the same value. More...
 
template<typename Tag >
mio::Index< Tagsize () const
 returns the size along the dimension provided as template parameter More...
 
Index size () const
 returns the size of the array along all dimensions. More...
 
bool operator== (const CustomIndexArray &other) const
 Equality comparison. More...
 
bool operator!= (const CustomIndexArray &other) const
 Equality comparison. More...
 
template<typename Tag >
Slice< Tag, iteratorslice (Seq< size_t > idx_seq)
 Creates a slice into the multidimensional array. More...
 
template<typename Tag >
Slice< Tag, const_iteratorslice (Seq< size_t > idx_seq) const
 Creates a slice into the multidimensional array. More...
 
template<typename Tag >
Slice< Tag, iteratorslice (mio::Index< Tag > idx)
 Creates a slice into the multidimensional array. More...
 
template<typename Tag >
Slice< Tag, const_iteratorslice (mio::Index< Tag > idx) const
 Creates a slice into the multidimensional array. More...
 
template<typename Tag >
requires std::is_enum_v< Tag > Slice< Tag, iteratorslice (Tag idx)
 Creates a slice into the multidimensional array. More...
 
template<typename Tag >
requires std::is_enum_v< Tag > Slice< Tag, const_iteratorslice (Tag idx) const
 Creates a slice into the multidimensional array. More...
 

Static Public Member Functions

template<class IOContext >
static IOResult< CustomIndexArraydeserialize (IOContext &io)
 deserialize an object of this class. More...
 

Public Attributes

Ts && args: m_dimensions{dims}
 

Static Protected Member Functions

template<class IOContext , class Derived >
static IOResult< Derived > deserialize (IOContext &io, Tag< Derived >)
 deserialize an object of a class derived from this class. More...
 

Private Attributes

Index m_dimensions
 
size_t m_numel
 
InternalArrayType m_y {}
 

Detailed Description

template<class Typ, class... Tags>
class mio::CustomIndexArray< Typ, Tags >

A class template for an array with custom indices.

This class stores an array of elements that can be queried using a MultiIndex. Each index in the MultiIndex is associated with a category, or dimension into a multidimensional array.

Example:

struct AgeGroup{};

enum class Gender { Female, Male, Diverse, Count, //number of values in the enum to allow iterating over the enum }; struct Gender{};

CustomIndexArray<size_t, AgeGroup, Gender> populations({Index<AgeGroup>(2), Index<Gender>(Gender::Count)});

Here, populations represents a 2x3 size_t array (though the data is stored contigously). An element can be accessed using a MultiIndex:

auto x = populations[{Index<AgeGroup>(0), Index<Gender>(Gender::Female)}];

If the category is an enum class, the Index class can be omitted for convenience in almost all cases since Index<E> is implicitly constructible from enum type E, e.g.:

CustomIndexArray<size_t, AgeGroup, Gender> populations({Index<AgeGroup>(2), Gender::Count}); auto x = populations[{Index<AgeGroup>(0), Gender::Female}];

Template Parameters
Typthe type stored in the array
TagsTypes that tag the custom Index types, must be unique.

Member Typedef Documentation

◆ const_iterator

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::const_iterator = typename InternalArrayType::const_iterator

◆ Index

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::Index = ::mio::Index<Tags...>

◆ InternalArrayType

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::InternalArrayType = Eigen::Array<Type, Eigen::Dynamic, 1>

◆ iterator

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::iterator = typename InternalArrayType::iterator

◆ reference

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::reference = Type&

◆ Type

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::Type = Typ

◆ value_type

template<class Typ , class... Tags>
using mio::CustomIndexArray< Typ, Tags >::value_type = Type

Constructor & Destructor Documentation

◆ CustomIndexArray() [1/2]

template<class Typ , class... Tags>
mio::CustomIndexArray< Typ, Tags >::CustomIndexArray ( )
inlineexplicit

Create an empty CustomIndexArray with size 0. Use the resize member function to add entries.

◆ CustomIndexArray() [2/2]

template<class Typ , class... Tags>
template<std::input_or_output_iterator Iter>
mio::CustomIndexArray< Typ, Tags >::CustomIndexArray ( Index const &  dims,
Iter  b,
Iter  e 
)
inline

Initializes array with values from a range.

Each element of the array will be assigned the corresponding value from the range.

Template Parameters
IterIterator class.
Parameters
dimsdimensions of the array.
bbegin of the range of values.
eend of the range of values.

Member Function Documentation

◆ array() [1/2]

template<class Typ , class... Tags>
auto& mio::CustomIndexArray< Typ, Tags >::array ( )
inline

◆ array() [2/2]

template<class Typ , class... Tags>
auto const& mio::CustomIndexArray< Typ, Tags >::array ( ) const
inline

array returns a reference to the internally stored flat array.

Returns
const reference to the CustomIndexArray::InternalArrayType instance

◆ begin() [1/2]

template<class Typ , class... Tags>
iterator mio::CustomIndexArray< Typ, Tags >::begin ( )
inline

Get a start iterator for the elements.

Returns
random access iterator

◆ begin() [2/2]

template<class Typ , class... Tags>
const_iterator mio::CustomIndexArray< Typ, Tags >::begin ( ) const
inline

Get a start iterator for the elements.

Returns
random access iterator

◆ convert()

template<class Typ , class... Tags>
template<class OtherType >
requires std::convertible_to<Type, OtherType> CustomIndexArray<OtherType, Tags...> mio::CustomIndexArray< Typ, Tags >::convert ( ) const
inline

Convert internally stored data to OtherType and save into new CustomIndexArray.

Template Parameters
OtherTypeThe type to convert into.
Returns
New CustomIndexArray of OtherType with copy of internal data.

◆ deserialize() [1/2]

template<class Typ , class... Tags>
template<class IOContext >
static IOResult<CustomIndexArray> mio::CustomIndexArray< Typ, Tags >::deserialize ( IOContext &  io)
inlinestatic

deserialize an object of this class.

See also
mio::deserialize

◆ deserialize() [2/2]

template<class Typ , class... Tags>
template<class IOContext , class Derived >
static IOResult<Derived> mio::CustomIndexArray< Typ, Tags >::deserialize ( IOContext &  io,
Tag< Derived >   
)
inlinestaticprotected

deserialize an object of a class derived from this class.

See also
mio::deserialize

◆ end() [1/2]

template<class Typ , class... Tags>
iterator mio::CustomIndexArray< Typ, Tags >::end ( )
inline

Get an end iterator for the elements.

Returns
random access iterator

◆ end() [2/2]

template<class Typ , class... Tags>
const_iterator mio::CustomIndexArray< Typ, Tags >::end ( ) const
inline

Get an end iterator for the elements.

Returns
random access iterator

◆ get_flat_index()

template<class Typ , class... Tags>
size_t mio::CustomIndexArray< Typ, Tags >::get_flat_index ( Index const &  index) const
inline

get_flat_index returns the flat index into the stored array, given the indices of each category

Parameters
indicesthe custom indices for each category
Returns
a flat index into the data structure storing the compartment populations

◆ m_numel()

template<class Typ , class... Tags>
Ts mio::CustomIndexArray< Typ, Tags >::m_numel ( product(dims)  )

◆ m_y()

template<class Typ , class... Tags>
Ts mio::CustomIndexArray< Typ, Tags >::m_y ( InternalArrayType::Constant(m_numel, 1, Type{std::forward< Ts >(args)...})  )
inline

◆ numel()

template<class Typ , class... Tags>
constexpr size_t mio::CustomIndexArray< Typ, Tags >::numel ( ) const
inlineconstexpr

numel returns the number of elements

This corresponds to the product of the dimension sizes

Returns
number of elements

◆ operator!=()

template<class Typ , class... Tags>
bool mio::CustomIndexArray< Typ, Tags >::operator!= ( const CustomIndexArray< Typ, Tags > &  other) const
inline

Equality comparison.

◆ operator=()

template<class Typ , class... Tags>
CustomIndexArray& mio::CustomIndexArray< Typ, Tags >::operator= ( const Type scalar)
inline

Assign the same value to each element of the array.

Parameters
scalarscalar value.

◆ operator==()

template<class Typ , class... Tags>
bool mio::CustomIndexArray< Typ, Tags >::operator== ( const CustomIndexArray< Typ, Tags > &  other) const
inline

Equality comparison.

◆ operator[]() [1/2]

template<class Typ , class... Tags>
Type& mio::CustomIndexArray< Typ, Tags >::operator[] ( Index const &  index)
inline

returns the entry of the array given a MultiIndex

Parameters
MultiIndex
Returns
the value at the index

◆ operator[]() [2/2]

template<class Typ , class... Tags>
Type const& mio::CustomIndexArray< Typ, Tags >::operator[] ( Index const &  index) const
inline

returns the entry of the array given a flat index index

Parameters
indexa flat index
Returns
the value at the index

◆ requires()

template<class Typ , class... Tags>
template<class... Ts>
mio::CustomIndexArray< Typ, Tags >::requires ( std::is_constructible_v< Type, Ts... >  ) const &

CustomIndexArray constructor, that initializes the array to constant instances of CustsomIndexArray::Type.

It forwards the arguments for initializer_list construction of the contained objects.

Template Parameters
TsThe argument types of the constructor arguments of Type
Parameters
argsThe constructor arguments of Type

◆ resize() [1/2]

template<class Typ , class... Tags>
void mio::CustomIndexArray< Typ, Tags >::resize ( Index  new_dims)
inline

Resize all dimensions, invalidating entries.

All entries of the CustomIndexArray should be reassigned a new value after a resize, as it may delete or reorder entries in an unexpected way. Newly added entries are default constructed.

Parameters
new_dimsNew dimensions.

◆ resize() [2/2]

template<class Typ , class... Tags>
template<class Tag >
void mio::CustomIndexArray< Typ, Tags >::resize ( mio::Index< Tag new_dim)
inline

Resize a single dimension, invalidating entries.

All entries of the CustomIndexArray should be reassigned a new value after a resize, as it may delete or reorder entries in an unexpected way. Newly added entries are default constructed.

Parameters
new_dimNew dimension size.
Template Parameters
TagThe dimension to resize.

◆ serialize()

template<class Typ , class... Tags>
template<class IOContext >
void mio::CustomIndexArray< Typ, Tags >::serialize ( IOContext &  io) const
inline

serialize this.

See also
mio::serialize

◆ set_multiple()

template<class Typ , class... Tags>
void mio::CustomIndexArray< Typ, Tags >::set_multiple ( const std::vector< typename CustomIndexArray< Typ, Tags... >::Index > &  indices,
const Typ &  value 
)
inline

Set multiple entries to the same value.

Parameters
indicesA list of indices to be set to the same value.
valueThe value to set.

◆ size() [1/2]

template<class Typ , class... Tags>
template<typename Tag >
mio::Index<Tag> mio::CustomIndexArray< Typ, Tags >::size ( ) const
inline

returns the size along the dimension provided as template parameter

Template Parameters
TagTag of the queried dimension/category
Returns
size along a specified dimension

◆ size() [2/2]

template<class Typ , class... Tags>
Index mio::CustomIndexArray< Typ, Tags >::size ( ) const
inline

returns the size of the array along all dimensions.

Returns
multi-index with size of the array along all dimensions.

◆ slice() [1/6]

template<class Typ , class... Tags>
template<typename Tag >
Slice<Tag, iterator> mio::CustomIndexArray< Typ, Tags >::slice ( mio::Index< Tag idx)
inline

Creates a slice into the multidimensional array.

Selects a single index of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idxindex to be selected for the slice.

◆ slice() [2/6]

template<class Typ , class... Tags>
template<typename Tag >
Slice<Tag, const_iterator> mio::CustomIndexArray< Typ, Tags >::slice ( mio::Index< Tag idx) const
inline

Creates a slice into the multidimensional array.

Selects a single index of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idxindex to be selected for the slice.

◆ slice() [3/6]

template<class Typ , class... Tags>
template<typename Tag >
Slice<Tag, iterator> mio::CustomIndexArray< Typ, Tags >::slice ( Seq< size_t >  idx_seq)
inline

Creates a slice into the multidimensional array.

Selects a sequence of indices of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idx_seqAn index sequence, consisting of the first index, the number of indices and a stride
Returns
The slice

◆ slice() [4/6]

template<class Typ , class... Tags>
template<typename Tag >
Slice<Tag, const_iterator> mio::CustomIndexArray< Typ, Tags >::slice ( Seq< size_t >  idx_seq) const
inline

Creates a slice into the multidimensional array.

Selects a sequence of indices of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idx_seqAn index sequence, consisting of the first index, the number of indices and a stride
Returns
The slice

◆ slice() [5/6]

template<class Typ , class... Tags>
template<typename Tag >
requires std::is_enum_v<Tag> Slice<Tag, iterator> mio::CustomIndexArray< Typ, Tags >::slice ( Tag  idx)
inline

Creates a slice into the multidimensional array.

Selects a single index of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idxindex to be selected for the slice.

◆ slice() [6/6]

template<class Typ , class... Tags>
template<typename Tag >
requires std::is_enum_v<Tag> Slice<Tag, const_iterator> mio::CustomIndexArray< Typ, Tags >::slice ( Tag  idx) const
inline

Creates a slice into the multidimensional array.

Selects a single index of one dimension.

Template Parameters
TheTag corresponding to the dimension of the slice
Parameters
idxindex to be selected for the slice.

Member Data Documentation

◆ args

template<class Typ , class... Tags>
Ts&& mio::CustomIndexArray< Typ, Tags >::args

◆ m_dimensions

template<class Typ , class... Tags>
Index mio::CustomIndexArray< Typ, Tags >::m_dimensions
private

◆ m_numel

template<class Typ , class... Tags>
size_t mio::CustomIndexArray< Typ, Tags >::m_numel
private

◆ m_y

template<class Typ , class... Tags>
InternalArrayType mio::CustomIndexArray< Typ, Tags >::m_y {}
private