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

Protected Attributes

size_type _shape
 the size along each index
 
unsigned int _dim
 the number of dimensions TODO: get rid of this -> _shape.size()
 
unsigned int _nentries
 the number of entries TODO: get rid of this -> _data.size()
 
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]
 
std::vector< T > _data
 the data unrolled into a vector
 

Private Member Functions

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

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 30 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:311

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}

◆ 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); }

◆ 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.

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

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

◆ 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}
size_type _shape
the size along each index
Definition MultiIndex.h:101
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

◆ dataLoad()

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

Definition at line 413 of file MultiIndex.h.

414{
415 ::dataLoad(stream, _shape, context);
416 ::dataLoad(stream, _data, context);
417 _dim = _shape.size();
418 _nentries = _data.size();
420}
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

Referenced by dataLoad().

◆ 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.

406{
407 ::dataStore(stream, _shape, context);
408 ::dataStore(stream, _data, context);
409}
void dataStore(std::ostream &stream, void *context)
Implement loadHelper and storeHelper for easier data (de)serialization.
Definition MultiIndex.h:405

Referenced by dataStore().

◆ dim()

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

dimension of the container

Definition at line 62 of file MultiIndex.h.

62{ return _shape.size(); }

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

◆ 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); }

◆ 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); }

◆ 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}

◆ 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}

◆ 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; }

◆ 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; }

◆ 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}
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}

◆ 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]; }

◆ 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]; }

◆ 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}

◆ 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.

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 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
std::vector< unsigned int > size_type
Definition MultiIndex.h:31

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

◆ 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.

59{ return _shape; }

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

◆ slice() [1/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}
MultiIndex container iterator.
Definition MultiIndex.h:129
Implements a container class for multi-indexed objects with an arbitrary number of indices.
Definition MultiIndex.h:23
iterator begin()
iterators for begin and end of this container
Definition MultiIndex.h:87

◆ slice() [2/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.

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

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

◆ stride() [1/2]

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

Definition at line 78 of file MultiIndex.h.

78{ return _stride; }

◆ stride() [2/2]

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

access the stride

Definition at line 389 of file MultiIndex.h.

390{
391 mooseAssert(j < _dim, "Dimension is" << _dim << ", stride(j) called with j = " << j);
392 return _stride[j];
393}

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< T >::at(), MultiIndex< T >::getRawData(), MultiIndex< T >::operator[](), and MultiIndex< T >::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< T >::end(), MultiIndex< T >::end(), and MultiIndex< T >::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< T >::stride().


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