1#ifndef UTIL_GRID_ARRAY_H
2#define UTIL_GRID_ARRAY_H
11#include <util/misc/Memory.h>
27 template <
typename Data>
70 template <
class Archive>
71 void serialize(Archive& ar,
const unsigned int version);
122 bool isInGrid(
int coordinate,
int i)
const;
145 int shift(
int& coordinate,
int i)
const;
216 template <
typename Data>
229 template <
typename Data>
233 Memory::deallocate<Data>(data_, size_);
241 template <
typename Data>
249 if (other.data_ == 0) {
250 UTIL_THROW(
"Other GridArray must be allocated");
253 UTIL_THROW(
"GridArray already allocated in copy constructor");
257 if (offsets_ != other.offsets_ ) {
260 if (size_ != other.size_ ) {
263 for (
int i = 0; i < size_; ++i) {
264 data_[i] = other.data_[i];
271 template <
typename Data>
276 if (
this == &other) {
281 if (other.data_ == 0) {
282 UTIL_THROW(
"Other GridArray must be allocated before assignment");
287 if (!isAllocated()) {
288 allocate(other.dimensions_);
290 if (dimensions_ != other.dimensions_ ) {
294 if (offsets_ != other.offsets_ ) {
297 if (size_ != other.size_ ) {
302 for (
int i = 0; i < size_; ++i) {
303 data_[i] = other.data_[i];
312 template <
typename Data>
316 UTIL_THROW(
"Attempt to re-allocate a GridArray");
319 if (dimensions[i] <= 0) {
323 dimensions_ = dimensions;
325 for (
int i =
Dimension - 1; i > 0; --i) {
326 offsets_[i-1] = offsets_[i]*dimensions_[i];
328 size_ = offsets_[0]*dimensions_[0];
329 Memory::allocate<Data>(data_, size_);
335 template <
class Data>
336 template <
class Archive>
340 if (Archive::is_saving()) {
341 dimensions = dimensions_;
344 if (Archive::is_loading()) {
345 if (!isAllocated()) {
346 allocate(dimensions);
349 for (
int i = 0; i < size_; ++i) {
357 template <
typename Data>
359 {
return dimensions_; }
364 template <
class Data>
366 {
return dimensions_[i]; }
371 template <
class Data>
378 template <
typename Data>
385 assert(position[i] >= 0);
386 assert(position[i] < dimensions_[i]);
387 result += position[i]*offsets_[i];
389 assert(position[i] >= 0);
390 assert(position[i] < dimensions_[i]);
391 result += position[i];
397 return (position[0]*offsets_[0] + position[1]*offsets_[1] + position[2]);
404 template <
typename Data>
408 int remainder = rank;
412 position[i] = remainder/offsets_[i];
413 remainder -= position[i]*offsets_[i];
415 position[i] = remainder;
422 template <
typename Data>
428 if (coordinate >= dimensions_[i])
436 template <
typename Data>
443 if (position[i] >= dimensions_[i])
452 template <
typename Data>
456 if (coordinate >= 0) {
457 shift = coordinate/dimensions_[i];
459 shift = -1 + ((coordinate+1)/dimensions_[i]);
461 coordinate -= shift*dimensions_[i];
468 template <
typename Data>
473 shifts[i] = shift(position[i], i);
481 template <
typename Data>
483 {
return *(data_ + rank); }
488 template <
typename Data>
490 {
return *(data_ + rank); }
495 template <
typename Data>
499 {
return *(data_ + rank(position)); }
504 template <
typename Data>
506 {
return *(data_ + rank(position)); }
511 template <
typename Data>
518 template <
class Data>
520 {
return (
bool)(data_ != 0); }
Multi-dimensional array with the dimensionality of space.
int rank(IntVector const &position) const
Get the rank of a grid point with specified position.
Data const & operator[](int rank) const
Return element by const reference, indexed by 1D rank.
bool isAllocated() const
Return true if the GridArray has been allocated, false otherwise.
Data const & operator()(IntVector const &position) const
Return element by const reference, indexed by IntVector position.
IntVector position(int rank) const
Get the position IntVector of a grid point with a specified rank.
bool isInGrid(int coordinate, int i) const
Is this 1D coordinate in range?
GridArray< Data > & operator=(GridArray< Data > const &other)
Assignment.
void serialize(Archive &ar, const unsigned int version)
Serialize a GridArray to/from an Archive.
void allocate(IntVector const &dimensions)
Allocate memory for a matrix.
int dimension(int i) const
Get number of grid points along direction i.
int shift(int &coordinate, int i) const
Shift a periodic 1D coordinate into primary range.
IntVector const & dimensions()
Get all dimensions of array as an IntVector.
int size() const
Get total number of grid points.
An IntVector is an integer Cartesian vector.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
const int Dimension
Dimensionality of space.
Utility classes for scientific computation.