1#ifndef UTIL_GRID_ARRAY_H
2#define UTIL_GRID_ARRAY_H
11#include <util/space/IntVector.h>
12#include <util/misc/Memory.h>
28 template <
typename Data>
71 template <
class Archive>
72 void serialize(Archive& ar,
const unsigned int version);
123 bool isInGrid(
int coordinate,
int i)
const;
146 int shift(
int& coordinate,
int i)
const;
217 template <
typename Data>
230 template <
typename Data>
242 template <
typename Data>
251 UTIL_THROW(
"Other GridArray must be allocated");
254 UTIL_THROW(
"GridArray already allocated in copy constructor");
258 if (offsets_ != other.offsets_ ) {
261 if (size_ != other.size_ ) {
264 for (
int i = 0; i < size_; ++i) {
265 data_[i] = other.data_[i];
272 template <
typename Data>
277 if (
this == &other) {
283 UTIL_THROW(
"Other GridArray must be allocated before assignment");
291 if (dimensions_ != other.dimensions_ ) {
295 if (offsets_ != other.offsets_ ) {
298 if (size_ != other.size_ ) {
303 for (
int i = 0; i < size_; ++i) {
304 data_[i] = other.data_[i];
313 template <
typename Data>
317 UTIL_THROW(
"Attempt to re-allocate a GridArray");
326 for (
int i =
Dimension - 1; i > 0; --i) {
327 offsets_[i-1] = offsets_[i]*dimensions_[i];
329 size_ = offsets_[0]*dimensions_[0];
336 template <
typename Data>
337 template <
class Archive>
341 if (Archive::is_saving()) {
345 if (Archive::is_loading()) {
350 for (
int i = 0; i < size_; ++i) {
358 template <
typename Data>
360 {
return dimensions_; }
365 template <
typename Data>
367 {
return dimensions_[i]; }
372 template <
typename Data>
379 template <
typename Data>
386 assert(position[i] >= 0);
387 assert(position[i] < dimensions_[i]);
388 result += position[i]*offsets_[i];
390 assert(position[i] >= 0);
391 assert(position[i] < dimensions_[i]);
392 result += position[i];
405 template <
typename Data>
409 int remainder =
rank;
413 position[i] = remainder/offsets_[i];
414 remainder -=
position[i]*offsets_[i];
423 template <
typename Data>
429 if (coordinate >= dimensions_[i])
437 template <
typename Data>
453 template <
typename Data>
457 if (coordinate >= 0) {
458 shift = coordinate/dimensions_[i];
460 shift = -1 + ((coordinate+1)/dimensions_[i]);
462 coordinate -=
shift*dimensions_[i];
469 template <
typename Data>
482 template <
typename Data>
484 {
return *(data_ +
rank); }
489 template <
typename Data>
491 {
return *(data_ +
rank); }
496 template <
typename Data>
505 template <
typename Data>
512 template <
typename Data>
513 inline Data * GridArray<Data>::data()
519 template <
typename Data>
521 {
return (
bool)data_; }
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.
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
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.