www.mooseframework.org
Classes | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | List of all members
MultiIndex< T > Class Template Reference

Implements a container class for multi-indexed objects with an arbitrary number of indices. More...

#include <MultiIndex.h>

Classes

class  const_noconst_iterator
 MultiIndex container iterator. More...
 

Public Types

typedef T value_type
 container related types and categories More...
 
typedef std::vector< unsigned intsize_type
 
using iterator = const_noconst_iterator< false >
 
using const_iterator = const_noconst_iterator< true >
 

Public Member Functions

 MultiIndex (const size_type &shape)
 construct zero initialized container of a given shape More...
 
 MultiIndex (const size_type &shape, const std::vector< T > &data)
 construct container of a given shape initialized from a flat data blob More...
 
const size_typesize () const
 container size as dim dimensional vector More...
 
unsigned int dim () const
 dimension of the container More...
 
unsigned int nEntries () const
 total number of values stored in the container More...
 
std::vector< T > getRawData () const
 get the raw data vector More...
 
void resize (const size_type &shape)
 Resize container. Must keep dimensionality constant. More...
 
void assign (const size_type &shape, T value)
 Reshape container arbitrarily and initialize with value. More...
 
unsigned int flatIndex (const size_type &indices) const
 compute the flat index for a size_type index More...
 
T & operator() (const size_type &indices)
 element access operators More...
 
const T & operator() (const size_type &indices) const
 
T & operator[] (unsigned int j)
 direct data access via bracket operator More...
 
const T & operator[] (unsigned int j) const
 
T & at (unsigned int j)
 
MultiIndex< T > slice (unsigned int dimension, unsigned int index) const
 accesses a slice of the multi index object More...
 
MultiIndex< T > slice (size_type dimension, size_type index) const
 
unsigned int stride (unsigned int j) const
 access the stride More...
 
const size_typestride () const
 
void dataStore (std::ostream &stream, void *context)
 Implement loadHelper and storeHelper for easier data (de)serialization. More...
 
void dataLoad (std::istream &stream, void *context)
 
iterator begin ()
 iterators for begin and end of this container More...
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 

Protected Member Functions

void findIndexVector (unsigned int flat_index, size_type &indices) const
 given a flat index computes the vector of indices i0, i1, i2, ... More...
 

Protected Attributes

size_type _shape
 the size along each index More...
 
unsigned int _dim
 the number of dimensions TODO: get rid of this -> _shape.size() More...
 
unsigned int _nentries
 the number of entries TODO: get rid of this -> _data.size() More...
 
size_type _stride
 stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[1] More...
 
std::vector< T > _data
 the data unrolled into a vector More...
 

Private Member Functions

void buildAccumulatedShape ()
 build accumulated shape vector for flat index calculation More...
 
void reshape (const size_type &shape)
 change the container shape and reset meta data More...
 

Detailed Description

template<class T>
class MultiIndex< T >

Implements a container class for multi-indexed objects with an arbitrary number of indices.

Definition at line 22 of file MultiIndex.h.

Member Typedef Documentation

◆ const_iterator

template<class T>
using MultiIndex< T >::const_iterator = const_noconst_iterator<true>

Definition at line 33 of file MultiIndex.h.

◆ iterator

template<class T>
using MultiIndex< T >::iterator = const_noconst_iterator<false>

Definition at line 32 of file MultiIndex.h.

◆ size_type

template<class T>
typedef std::vector<unsigned int> MultiIndex< T >::size_type

Definition at line 31 of file MultiIndex.h.

◆ value_type

template<class T>
typedef T MultiIndex< T >::value_type

container related types and categories

Definition at line 27 of file MultiIndex.h.

Constructor & Destructor Documentation

◆ MultiIndex() [1/2]

template<class T >
MultiIndex< T >::MultiIndex ( const size_type shape)

construct zero initialized container of a given shape

Definition at line 249 of file MultiIndex.h.

250 {
251  reshape(shape);
252  _data.resize(_nentries);
253 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void reshape(const size_type &shape)
change the container shape and reset meta data
Definition: MultiIndex.h:454

◆ MultiIndex() [2/2]

template<class T>
MultiIndex< T >::MultiIndex ( const size_type shape,
const std::vector< T > &  data 
)

construct container of a given shape initialized from a flat data blob

Definition at line 256 of file MultiIndex.h.

257 {
258  reshape(shape);
259 
260  if (data.size() != _nentries)
261  mooseError("shape and data arguments' sizes are inconsistent.");
262  _data = data;
263 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void reshape(const size_type &shape)
change the container shape and reset meta data
Definition: MultiIndex.h:454

Member Function Documentation

◆ assign()

template<class T>
void MultiIndex< T >::assign ( const size_type shape,
value 
)

Reshape container arbitrarily and initialize with value.

Definition at line 397 of file MultiIndex.h.

398 {
399  reshape(shape);
400  _data.assign(_nentries, value);
401 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void reshape(const size_type &shape)
change the container shape and reset meta data
Definition: MultiIndex.h:454

◆ at()

template<class T>
T& MultiIndex< T >::at ( unsigned int  j)
inline

Definition at line 50 of file MultiIndex.h.

50 { return _data.at(j); }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113

◆ begin() [1/2]

template<class T>
iterator MultiIndex< T >::begin ( )
inline

iterators for begin and end of this container

Definition at line 87 of file MultiIndex.h.

Referenced by MultiIndex< Real >::slice().

87 { return const_noconst_iterator<false>(*this, 0); }

◆ begin() [2/2]

template<class T>
const_iterator MultiIndex< T >::begin ( ) const
inline

Definition at line 89 of file MultiIndex.h.

89 { return const_noconst_iterator<true>(*this, 0); }

◆ buildAccumulatedShape()

template<class T >
void MultiIndex< T >::buildAccumulatedShape ( )
private

build accumulated shape vector for flat index calculation

Definition at line 439 of file MultiIndex.h.

440 {
441  // TODO: simplify - this is needlessly complicated - can be done in a single loop
442  _stride.resize(_dim);
443  for (unsigned int d = 0; d < _dim; ++d)
444  {
445  unsigned int k = 1;
446  for (unsigned int j = d + 1; j < _dim; ++j)
447  k *= _shape[j];
448  _stride[d] = k;
449  }
450 }
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
size_type _shape
the size along each index
Definition: MultiIndex.h:101
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ dataLoad()

template<class T >
void MultiIndex< T >::dataLoad ( std::istream &  stream,
void context 
)

Definition at line 413 of file MultiIndex.h.

Referenced by dataLoad().

414 {
415  ::dataLoad(stream, _shape, context);
416  ::dataLoad(stream, _data, context);
417  _dim = _shape.size();
418  _nentries = _data.size();
420 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void buildAccumulatedShape()
build accumulated shape vector for flat index calculation
Definition: MultiIndex.h:439
void dataLoad(std::istream &stream, void *context)
Definition: MultiIndex.h:413
size_type _shape
the size along each index
Definition: MultiIndex.h:101

◆ dataStore()

template<class T >
void MultiIndex< T >::dataStore ( std::ostream &  stream,
void context 
)

Implement loadHelper and storeHelper for easier data (de)serialization.

Definition at line 405 of file MultiIndex.h.

Referenced by dataStore().

406 {
407  ::dataStore(stream, _shape, context);
408  ::dataStore(stream, _data, context);
409 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
size_type _shape
the size along each index
Definition: MultiIndex.h:101
void dataStore(std::ostream &stream, void *context)
Implement loadHelper and storeHelper for easier data (de)serialization.
Definition: MultiIndex.h:405

◆ dim()

template<class T>
unsigned int MultiIndex< T >::dim ( ) const
inline

dimension of the container

Definition at line 62 of file MultiIndex.h.

Referenced by MultiDimensionalInterpolationTempl< T >::setData().

62 { return _shape.size(); }
size_type _shape
the size along each index
Definition: MultiIndex.h:101

◆ end() [1/2]

template<class T>
iterator MultiIndex< T >::end ( )
inline

Definition at line 88 of file MultiIndex.h.

88 { return const_noconst_iterator<false>(*this, _nentries); }
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107

◆ end() [2/2]

template<class T>
const_iterator MultiIndex< T >::end ( ) const
inline

Definition at line 90 of file MultiIndex.h.

90 { return const_noconst_iterator<true>(*this, _nentries); }
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107

◆ findIndexVector()

template<class T>
void MultiIndex< T >::findIndexVector ( unsigned int  flat_index,
size_type indices 
) const
protected

given a flat index computes the vector of indices i0, i1, i2, ...

Definition at line 424 of file MultiIndex.h.

425 {
426  indices.resize(_dim);
427  for (unsigned int d = 0; d < _dim; ++d)
428  {
429  unsigned int i = flat_index / _stride[d];
430  indices[d] = i;
431  flat_index -= i * _stride[d];
432  }
433 }
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ flatIndex()

template<class T >
unsigned int MultiIndex< T >::flatIndex ( const size_type indices) const

compute the flat index for a size_type index

Definition at line 477 of file MultiIndex.h.

478 {
479  mooseAssert(indices.size() == _dim,
480  "Indices vector has wrong size. size=" << indices.size() << " vs. dim=" << _dim);
481 #if DEBUG
482  for (unsigned int j = 0; j < indices.size(); ++j)
483  if (indices[j] >= _shape[j])
484  mooseError("Indices vector at entry ", j, " is ", indices[j], " vs. shape ", _shape[j]);
485 #endif // DEBUG
486 
487  // implement the index
488  // index = i_M + i_{M-1} * I_M + i_{M-1} * I_M * I_{M-1} ...
489  unsigned int index = 0;
490  for (unsigned int d = 0; d < _dim; ++d)
491  index += indices[d] * _stride[d];
492 
493  return index;
494 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
size_type _shape
the size along each index
Definition: MultiIndex.h:101
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ getRawData()

template<class T>
std::vector<T> MultiIndex< T >::getRawData ( ) const
inline

get the raw data vector

Definition at line 68 of file MultiIndex.h.

68 { return _data; }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113

◆ nEntries()

template<class T>
unsigned int MultiIndex< T >::nEntries ( ) const
inline

total number of values stored in the container

Definition at line 65 of file MultiIndex.h.

65 { return _nentries; }
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107

◆ operator()() [1/2]

template<class T >
T & MultiIndex< T >::operator() ( const size_type indices)

element access operators

Definition at line 267 of file MultiIndex.h.

268 {
269  return _data[flatIndex(indices)];
270 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int flatIndex(const size_type &indices) const
compute the flat index for a size_type index
Definition: MultiIndex.h:477

◆ operator()() [2/2]

template<class T >
const T & MultiIndex< T >::operator() ( const size_type indices) const

Definition at line 274 of file MultiIndex.h.

275 {
276  return _data[flatIndex(indices)];
277 }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int flatIndex(const size_type &indices) const
compute the flat index for a size_type index
Definition: MultiIndex.h:477

◆ operator[]() [1/2]

template<class T>
T& MultiIndex< T >::operator[] ( unsigned int  j)
inline

direct data access via bracket operator

Definition at line 48 of file MultiIndex.h.

48 { return _data[j]; }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113

◆ operator[]() [2/2]

template<class T>
const T& MultiIndex< T >::operator[] ( unsigned int  j) const
inline

Definition at line 49 of file MultiIndex.h.

49 { return _data[j]; }
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113

◆ reshape()

template<class T >
void MultiIndex< T >::reshape ( const size_type shape)
private

change the container shape and reset meta data

Definition at line 454 of file MultiIndex.h.

455 {
456  if (shape.size() == 0)
457  {
458  _shape = {};
459  _dim = 0;
460  _stride = {};
461  _nentries = 0;
462  return;
463  }
464 
465  _shape = shape;
466  _dim = shape.size();
467 
468  _nentries = 1;
469  for (unsigned int d = 0; d < _dim; ++d)
470  _nentries *= _shape[d];
471 
473 }
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void buildAccumulatedShape()
build accumulated shape vector for flat index calculation
Definition: MultiIndex.h:439
size_type _shape
the size along each index
Definition: MultiIndex.h:101
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ resize()

template<class T >
void MultiIndex< T >::resize ( const size_type shape)

Resize container. Must keep dimensionality constant.

Definition at line 345 of file MultiIndex.h.

Referenced by MultiIndex< Real >::findIndexVector(), and MultiDimensionalInterpolationTempl< T >::linearSearch().

346 {
347  if (shape.size() != _shape.size())
348  mooseError("resize cannot change the dimensionality of MultiIndex.");
349 
350  // first copy the old shape and data
351  size_type old_shape = _shape;
352  size_type old_stride = _stride;
353  std::vector<T> old_data = _data;
354 
355  // reset _shape & recompute meta data
356  reshape(shape);
357 
358  // fill in _data to all possible indices
359  _data.assign(_nentries, T(0));
360  for (unsigned int j = 0; j < _nentries; ++j)
361  {
362  size_type indices;
363  findIndexVector(j, indices);
364 
365  // check if indices existed in the old version of _data
366  bool existed_in_old = true;
367  for (unsigned int d = 0; d < _dim; ++d)
368  if (indices[d] >= old_shape[d])
369  {
370  existed_in_old = false;
371  break;
372  }
373 
374  // find the corresponding old_j
375  if (existed_in_old)
376  {
377  unsigned int old_j = 0;
378  for (unsigned int d = 0; d < _dim; ++d)
379  old_j += indices[d] * old_stride[d];
380 
381  // finally set the data entry
382  _data[j] = old_data[old_j];
383  }
384  }
385 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
void reshape(const size_type &shape)
change the container shape and reset meta data
Definition: MultiIndex.h:454
std::vector< unsigned int > size_type
Definition: MultiIndex.h:31
void findIndexVector(unsigned int flat_index, size_type &indices) const
given a flat index computes the vector of indices i0, i1, i2, ...
Definition: MultiIndex.h:424
size_type _shape
the size along each index
Definition: MultiIndex.h:101
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ size()

template<class T>
const size_type& MultiIndex< T >::size ( ) const
inline

container size as dim dimensional vector

Definition at line 59 of file MultiIndex.h.

Referenced by MultiDimensionalInterpolationTempl< T >::errorCheck(), and MultiDimensionalInterpolationTempl< T >::setData().

59 { return _shape; }
size_type _shape
the size along each index
Definition: MultiIndex.h:101

◆ slice() [1/2]

template<class T >
MultiIndex< T > MultiIndex< T >::slice ( unsigned int  dimension,
unsigned int  index 
) const

accesses a slice of the multi index object

Definition at line 281 of file MultiIndex.h.

Referenced by MultiDimensionalInterpolationTempl< T >::setData().

282 {
283  size_type dim(1, dimension);
284  size_type ind(1, index);
285  return slice(dim, ind);
286 }
unsigned int dim() const
dimension of the container
Definition: MultiIndex.h:62
std::vector< unsigned int > size_type
Definition: MultiIndex.h:31
MultiIndex< T > slice(unsigned int dimension, unsigned int index) const
accesses a slice of the multi index object
Definition: MultiIndex.h:281

◆ slice() [2/2]

template<class T >
MultiIndex< T > MultiIndex< T >::slice ( size_type  dimension,
size_type  index 
) const

Definition at line 290 of file MultiIndex.h.

291 {
292  // input checks
293  if (dimension.size() != index.size())
294  mooseError("dimension and index must have the same size.");
295  if (dimension.size() > _dim - 1)
296  mooseError("The result of slice must be at least of dimension 1.");
297 
298 #if DEBUG
299  for (unsigned int d = 0; d < dimension.size(); ++d)
300  {
301  if (dimension[d] >= _dim)
302  mooseError("dimension is set to ", dimension[d], " which is larger than _dim ", _dim);
303  if (index[d] >= _shape[dimension[d]])
304  mooseError("index= ",
305  index[d],
306  " at dimension=",
307  dimension[d],
308  " is larger than ",
309  _shape[dimension[d]]);
310  }
311 #endif // DEBUG
312 
313  // create a MultiIndex object with new dim = dim - dimension.size()
314  size_type new_shape;
315  for (unsigned int j = 0; j < _dim; ++j)
316  if (std::find(dimension.begin(), dimension.end(), j) == dimension.end())
317  new_shape.push_back(_shape[j]);
318  MultiIndex<T> multi_index = MultiIndex(new_shape);
319 
320  // copy the data
321  MultiIndex<T>::iterator it = multi_index.begin();
322  for (unsigned int n = 0; n < _nentries; ++n)
323  {
324  size_type indices;
325  findIndexVector(n, indices);
326  bool addTo = true;
327  for (unsigned int d = 0; d < dimension.size(); ++d)
328  if (indices[dimension[d]] != index[d])
329  {
330  addTo = false;
331  break;
332  }
333 
334  if (addTo)
335  {
336  (*it).second = _data[n];
337  ++it;
338  }
339  }
340  return multi_index;
341 }
iterator begin()
iterators for begin and end of this container
Definition: MultiIndex.h:87
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:299
std::vector< T > _data
the data unrolled into a vector
Definition: MultiIndex.h:113
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
unsigned int _nentries
the number of entries TODO: get rid of this -> _data.size()
Definition: MultiIndex.h:107
MultiIndex container iterator.
Definition: MultiIndex.h:27
MultiIndex(const size_type &shape)
construct zero initialized container of a given shape
Definition: MultiIndex.h:249
std::vector< unsigned int > size_type
Definition: MultiIndex.h:31
void findIndexVector(unsigned int flat_index, size_type &indices) const
given a flat index computes the vector of indices i0, i1, i2, ...
Definition: MultiIndex.h:424
size_type _shape
the size along each index
Definition: MultiIndex.h:101
Implements a container class for multi-indexed objects with an arbitrary number of indices...
Definition: MultiIndex.h:22

◆ stride() [1/2]

template<class T >
unsigned int MultiIndex< T >::stride ( unsigned int  j) const

access the stride

Definition at line 389 of file MultiIndex.h.

Referenced by MultiDimensionalInterpolationTempl< T >::multiLinearInterpolation().

390 {
391  mooseAssert(j < _dim, "Dimension is" << _dim << ", stride(j) called with j = " << j);
392  return _stride[j];
393 }
unsigned int _dim
the number of dimensions TODO: get rid of this -> _shape.size()
Definition: MultiIndex.h:104
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

◆ stride() [2/2]

template<class T>
const size_type& MultiIndex< T >::stride ( ) const
inline

Definition at line 78 of file MultiIndex.h.

78 { return _stride; }
size_type _stride
stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[...
Definition: MultiIndex.h:110

Member Data Documentation

◆ _data

template<class T>
std::vector<T> MultiIndex< T >::_data
protected

the data unrolled into a vector

Definition at line 113 of file MultiIndex.h.

Referenced by MultiIndex< Real >::at(), MultiIndex< Real >::getRawData(), and MultiIndex< Real >::operator[]().

◆ _dim

template<class T>
unsigned int MultiIndex< T >::_dim
protected

the number of dimensions TODO: get rid of this -> _shape.size()

Definition at line 104 of file MultiIndex.h.

◆ _nentries

template<class T>
unsigned int MultiIndex< T >::_nentries
protected

the number of entries TODO: get rid of this -> _data.size()

Definition at line 107 of file MultiIndex.h.

Referenced by MultiIndex< Real >::end(), and MultiIndex< Real >::nEntries().

◆ _shape

template<class T>
size_type MultiIndex< T >::_shape
protected

◆ _stride

template<class T>
size_type MultiIndex< T >::_stride
protected

stride for each index, e.g. if you know {i, j, k} -> flat_index, {i, j + 1, k} = flat_index + stride[1]

Definition at line 110 of file MultiIndex.h.

Referenced by MultiIndex< Real >::stride().


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