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>
275 if (
this == &other) {
280 if (other.data_ == 0) {
281 UTIL_THROW(
"Other GridArray must be allocated before assignment");
289 if (dimensions_ != other.dimensions_ ) {
293 if (offsets_ != other.offsets_ ) {
296 if (size_ != other.size_ ) {
301 for (
int i = 0; i < size_; ++i) {
302 data_[i] = other.data_[i];
311 template <
typename Data>
315 UTIL_THROW(
"Attempt to re-allocate a GridArray");
318 if (dimensions[i] <= 0) {
323 offsets_[Dimension -1] = 1;
324 for (
int i = Dimension - 1; i > 0; --i) {
325 offsets_[i-1] = offsets_[i]*dimensions_[i];
327 size_ = offsets_[0]*dimensions_[0];
328 Memory::allocate<Data>(data_, size_);
334 template <
class Data>
335 template <
class Archive>
339 if (Archive::is_saving()) {
340 dimensions = dimensions_;
343 if (Archive::is_loading()) {
348 for (
int i = 0; i < size_; ++i) {
356 template <
typename Data>
358 {
return dimensions_; }
363 template <
class Data>
365 {
return dimensions_[i]; }
370 template <
class Data>
377 template <
typename Data>
384 assert(position[i] >= 0);
385 assert(position[i] < dimensions_[i]);
386 result += position[i]*offsets_[i];
388 assert(position[i] >= 0);
389 assert(position[i] < dimensions_[i]);
390 result += position[i];
396 return (position[0]*offsets_[0] + position[1]*offsets_[1] + position[2]);
403 template <
typename Data>
407 int remainder =
rank;
411 position[i] = remainder/offsets_[i];
412 remainder -= position[i]*offsets_[i];
414 position[i] = remainder;
421 template <
typename Data>
427 if (coordinate >= dimensions_[i])
435 template <
typename Data>
442 if (position[i] >= dimensions_[i])
451 template <
typename Data>
455 if (coordinate >= 0) {
456 shift = coordinate/dimensions_[i];
458 shift = -1 + ((coordinate+1)/dimensions_[i]);
460 coordinate -= shift*dimensions_[i];
467 template <
typename Data>
472 shifts[i] =
shift(position[i], i);
480 template <
typename Data>
482 {
return *(data_ +
rank); }
487 template <
typename Data>
489 {
return *(data_ +
rank); }
494 template <
typename Data>
497 {
return *(data_ +
rank(position)); }
502 template <
typename Data>
504 {
return *(data_ +
rank(position)); }
509 template <
typename Data>
516 template <
class Data>
518 {
return (
bool)(data_ != 0); }
int size() const
Get total number of grid points.
const int Dimension
Dimensionality of space.
int shift(int &coordinate, int i) const
Shift a periodic 1D coordinate into primary range.
int dimension(int i) const
Get number of grid points along direction i.
IntVector position(int rank) const
Get the position IntVector of a grid point with a specified rank.
int rank(const IntVector &position) const
Get the rank of a grid point with specified position.
const Data & 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.
GridArray< Data > & operator=(const GridArray< Data > &other)
Assignment.
File containing preprocessor macros for error handling.
const Data & operator()(const IntVector &position) const
Return element by const reference, indexed by IntVector position.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
void serialize(Archive &ar, const unsigned int version)
Serialize a GridArray to/from an Archive.
Utility classes for scientific computation.
void allocate(const IntVector &dimensions)
Allocate memory for a matrix.
bool isInGrid(int coordinate, int i) const
Is this 1D coordinate in range?
Multi-dimensional array with the dimensionality of space.
const IntVector & dimensions()
Get all dimensions of array as an IntVector.
An IntVector is an integer Cartesian vector.