27 #ifndef CDPL_MATH_GRID_HPP
28 #define CDPL_MATH_GRID_HPP
34 #include <type_traits>
76 typedef typename std::conditional<std::is_const<G>::value,
77 typename G::ConstReference,
141 return data(i, j, k);
153 return data(i, j, k);
162 return data.getSize();
171 return data.getSize1();
180 return data.getSize2();
189 return data.getSize3();
198 return data.getMaxSize();
207 return data.getMaxSize1();
216 return data.getMaxSize2();
225 return data.getMaxSize3();
234 return data.isEmpty();
262 data.operator=(
r.data);
272 template <
typename E>
285 template <
typename E>
298 template <
typename E>
311 template <
typename T>
324 template <
typename T>
338 template <
typename E>
351 template <
typename E>
364 template <
typename E>
399 template <
typename T,
typename A = std::vector<T> >
470 data(), size1(0), size2(0), size3(0) {}
479 data(storageSize(
m, n, o)), size1(
m), size2(n), size3(o) {}
489 data(storageSize(
m, n, o), v), size1(
m), size2(n), size3(o) {}
496 data(g.data), size1(g.size1), size2(g.size2), size3(g.size3) {}
503 data(), size1(0), size2(0), size3(0)
513 template <
typename E>
517 gridAssignGrid<ScalarAssignment>(*
this, e);
587 return (size1 * size2 * size3);
623 return data.max_size();
675 template <
typename C>
687 template <
typename E>
701 template <
typename C>
713 template <
typename E>
727 template <
typename C>
739 template <
typename E>
753 template <
typename T1>
754 typename std::enable_if<IsScalar<T1>::value,
Grid>::type&
operator*=(
const T1& t)
756 gridAssignScalar<ScalarMultiplicationAssignment>(*
this, t);
766 template <
typename T1>
767 typename std::enable_if<IsScalar<T1>::value,
Grid>::type&
operator/=(
const T1& t)
769 gridAssignScalar<ScalarDivisionAssignment>(*
this, t);
779 template <
typename E>
783 gridAssignGrid<ScalarAssignment>(*
this, e);
793 template <
typename E>
796 gridAssignGrid<ScalarAdditionAssignment>(*
this, e);
806 template <
typename E>
809 gridAssignGrid<ScalarSubtractionAssignment>(*
this, e);
820 std::swap(data, g.data);
821 std::swap(size1, g.size1);
822 std::swap(size2, g.size2);
823 std::swap(size3, g.size3);
843 std::fill(data.begin(), data.end(), v);
856 if (size1 ==
m && size2 == n && size3 == o)
860 Grid tmp(
m, n, o, v);
862 for (
SizeType i = 0, min_size1 = std::min(size1,
m); i < min_size1; i++)
863 for (
SizeType j = 0, min_size2 = std::min(size2, n); j < min_size2; j++)
864 for (
SizeType k = 0, min_size3 = std::min(size3, o); k < min_size3; k++)
865 tmp(i, j, k) = (*this)(i, j, k);
870 data.resize(storageSize(
m, n, o), v);
881 (n <= (std::numeric_limits<SizeType>::max() / o) &&
m <= (std::numeric_limits<SizeType>::max() / (n * o))),
896 template <
typename T>
947 size1(0), size2(0), size3(0) {}
956 size1(
m), size2(n), size3(o) {}
963 size1(
m.size1), size2(
m.size2), size3(
m.size3) {}
997 return (size1 == 0 || size2 == 0 || size3 == 0);
1006 return (size1 * size2 * size3);
1042 return std::numeric_limits<SizeType>::max();
1051 return std::numeric_limits<SizeType>::max();
1060 return std::numeric_limits<SizeType>::max();
1086 std::swap(size1, g.size1);
1087 std::swap(size2, g.size2);
1088 std::swap(size3, g.size3);
1122 template <
typename T>
1129 template <
typename T>
1180 size1(0), size2(0), size3(0), value() {}
1190 size1(
m), size2(n), size3(o), value(v) {}
1197 size1(
m.size1), size2(
m.size2), size3(
m.size3), value(
m.value) {}
1231 return (size1 == 0 || size2 == 0 || size3 == 0);
1240 return (size1 * size2 * size3);
1276 return std::numeric_limits<SizeType>::max();
1285 return std::numeric_limits<SizeType>::max();
1294 return std::numeric_limits<SizeType>::max();
1321 std::swap(size1, g.size1);
1322 std::swap(size2, g.size2);
1323 std::swap(size3, g.size3);
1324 std::swap(value, g.value);
1362 template <
typename G>
1370 template <
typename G>
Definition of exception classes.
Definition of various preprocessor macros for error checking.
#define CDPL_MATH_CHECK(expr, msg, e)
Throws the exception e with message msg when the boolean expression expr evaluates to false.
Definition: Check.hpp:47
Definition of various functors.
Implementation of grid assignment routines.
Definition of various grid expression types and operations.
Definition of type traits.
Thrown to indicate that an index is out of range.
Definition: Base/Exceptions.hpp:152
Thrown to indicate that the size of a (multidimensional) array is not correct.
Definition: Base/Exceptions.hpp:133
Refinement of Math::GridExpression marking the derived type as a concrete (writable) grid container.
Definition: Expression.hpp:336
CRTP base class of all grid expression types.
Definition: Expression.hpp:188
Lightweight grid expression that proxies a reference to an underlying grid container.
Definition: Math/Grid.hpp:58
GridReference & assign(const GridExpression< E > &e)
Assigns the grid expression e to the wrapped grid without intermediate temporary (use only when e doe...
Definition: Math/Grid.hpp:339
GridReference(GridType &g)
Constructs the reference wrapping the grid g.
Definition: Math/Grid.hpp:109
std::conditional< std::is_const< G >::value, typename G::ConstReference, typename G::Reference >::type Reference
Mutable element reference type (degrades to ConstReference when the wrapped grid is const).
Definition: Math/Grid.hpp:78
friend void swap(GridReference &r1, GridReference &r2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:385
GridReference & minusAssign(const GridExpression< E > &e)
Subtracts the grid expression e from the wrapped grid without intermediate temporary.
Definition: Math/Grid.hpp:365
GridType & getData()
Returns a mutable reference to the wrapped grid.
Definition: Math/Grid.hpp:250
Reference operator()(SizeType i, SizeType j, SizeType k)
Returns a mutable reference to the element (i, j, k) of the wrapped grid.
Definition: Math/Grid.hpp:139
G::SizeType SizeType
The unsigned size type used by the wrapped grid.
Definition: Math/Grid.hpp:88
bool isEmpty() const
Tells whether the wrapped grid is empty (zero elements along any dimension).
Definition: Math/Grid.hpp:232
SizeType getSize2() const
Returns the size of the wrapped grid along the second dimension.
Definition: Math/Grid.hpp:178
GridReference & operator+=(const GridExpression< E > &e)
Adds the grid expression e element-wise to the wrapped grid.
Definition: Math/Grid.hpp:286
std::enable_if< IsScalar< T >::value, GridReference >::type & operator/=(const T &t)
Divides every element of the wrapped grid by the scalar t.
Definition: Math/Grid.hpp:325
ConstReference operator()(SizeType i) const
Returns a const reference to the element at the linear index i of the wrapped grid.
Definition: Math/Grid.hpp:127
SizeType getMaxSize() const
Returns the maximum total number of elements the wrapped grid can hold.
Definition: Math/Grid.hpp:196
GridReference & operator=(const GridExpression< E > &e)
Assigns the grid expression e to the wrapped grid.
Definition: Math/Grid.hpp:273
SizeType getMaxSize2() const
Returns the maximum size of the wrapped grid along the second dimension.
Definition: Math/Grid.hpp:214
std::enable_if< IsScalar< T >::value, GridReference >::type & operator*=(const T &t)
Multiplies every element of the wrapped grid by the scalar t.
Definition: Math/Grid.hpp:312
SizeType getMaxSize3() const
Returns the maximum size of the wrapped grid along the third dimension.
Definition: Math/Grid.hpp:223
G::ValueType ValueType
The element value type of the wrapped grid.
Definition: Math/Grid.hpp:71
GridReference & plusAssign(const GridExpression< E > &e)
Adds the grid expression e to the wrapped grid without intermediate temporary.
Definition: Math/Grid.hpp:352
const GridType & getData() const
Returns a const reference to the wrapped grid.
Definition: Math/Grid.hpp:241
SizeType getSize3() const
Returns the size of the wrapped grid along the third dimension.
Definition: Math/Grid.hpp:187
GridReference & operator-=(const GridExpression< E > &e)
Subtracts the grid expression e element-wise from the wrapped grid.
Definition: Math/Grid.hpp:299
G::DifferenceType DifferenceType
The signed difference type used by the wrapped grid.
Definition: Math/Grid.hpp:93
SizeType getMaxSize1() const
Returns the maximum size of the wrapped grid along the first dimension.
Definition: Math/Grid.hpp:205
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the element (i, j, k) of the wrapped grid.
Definition: Math/Grid.hpp:151
SizeType getSize1() const
Returns the size of the wrapped grid along the first dimension.
Definition: Math/Grid.hpp:169
G GridType
The wrapped grid type.
Definition: Math/Grid.hpp:66
SizeType getSize() const
Returns the total number of elements of the wrapped grid.
Definition: Math/Grid.hpp:160
GridReference & operator=(const GridReference &r)
Copies the elements of r into the wrapped grid.
Definition: Math/Grid.hpp:260
Reference operator()(SizeType i)
Returns a mutable reference to the element at the linear index i of the wrapped grid.
Definition: Math/Grid.hpp:117
G::ConstReference ConstReference
Constant element reference type.
Definition: Math/Grid.hpp:83
const SelfType ConstClosureType
Constant closure type used when this reference appears inside another expression.
Definition: Math/Grid.hpp:103
SelfType ClosureType
Closure type used when this reference appears inside another expression.
Definition: Math/Grid.hpp:98
void swap(GridReference &r)
Swaps the contents of the wrapped grid with those of the grid wrapped by r.
Definition: Math/Grid.hpp:375
Dynamically-sized dense 3D grid with configurable underlying storage.
Definition: Math/Grid.hpp:401
T * Pointer
Pointer type for raw element access.
Definition: Math/Grid.hpp:439
A::size_type SizeType
The unsigned size type used by the underlying storage container.
Definition: Math/Grid.hpp:424
Grid & operator+=(const GridExpression< E > &e)
Adds the grid expression e element-wise to this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:714
Grid & operator=(const Grid &g)
Copy-assigns the contents of g to this grid.
Definition: Math/Grid.hpp:649
void resize(SizeType m, SizeType n, SizeType o, bool preserve=true, const ValueType &v=ValueType())
Resizes the grid to m×n×o elements.
Definition: Math/Grid.hpp:854
Grid & operator-=(const GridContainer< C > &c)
Subtracts the contents of the grid container c element-wise from this grid (no alias check needed).
Definition: Math/Grid.hpp:728
Reference operator()(SizeType i, SizeType j, SizeType k)
Returns a mutable reference to the element at (i, j, k).
Definition: Math/Grid.hpp:552
ConstReference operator()(SizeType i) const
Returns a const reference to the element at linear index i.
Definition: Math/Grid.hpp:538
Grid(SizeType m, SizeType n, SizeType o, const ValueType &v)
Constructs an m×n×o grid with every element initialized to v.
Definition: Math/Grid.hpp:488
Grid(Grid &&g)
Move-constructs a grid from g (g is left in a valid empty state).
Definition: Math/Grid.hpp:502
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Grid instances.
Definition: Math/Grid.hpp:464
Grid()
Constructs an empty grid (zero size on every axis).
Definition: Math/Grid.hpp:469
Grid & operator=(const GridContainer< C > &c)
Assigns the contents of the grid container c to this grid (no alias check needed).
Definition: Math/Grid.hpp:676
const T * ConstPointer
Constant pointer type for raw element access.
Definition: Math/Grid.hpp:444
std::enable_if< IsScalar< T1 >::value, Grid >::type & operator*=(const T1 &t)
Multiplies every element by the scalar t.
Definition: Math/Grid.hpp:754
Grid & assign(const GridExpression< E > &e)
Resizes this grid to match e and assigns its elements without intermediate temporary.
Definition: Math/Grid.hpp:780
A::difference_type DifferenceType
The signed difference type used by the underlying storage container.
Definition: Math/Grid.hpp:429
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the element at (i, j, k).
Definition: Math/Grid.hpp:566
T & Reference
Mutable element reference type.
Definition: Math/Grid.hpp:414
Grid & operator=(const GridExpression< E > &e)
Assigns the grid expression e to this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:688
Grid & operator-=(const GridExpression< E > &e)
Subtracts the grid expression e element-wise from this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:740
SizeType getMaxSize() const
Returns the maximum total element count the underlying storage container can hold.
Definition: Math/Grid.hpp:621
const ArrayType & getData() const
Returns a const reference to the underlying storage container.
Definition: Math/Grid.hpp:639
void clear(const ValueType &v=ValueType())
Sets every element of the grid to the value v.
Definition: Math/Grid.hpp:841
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:603
friend void swap(Grid &g1, Grid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:832
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:454
Grid & operator+=(const GridContainer< C > &c)
Adds the contents of the grid container c element-wise to this grid (no alias check needed).
Definition: Math/Grid.hpp:702
void swap(Grid &g)
Swaps the contents of this grid with those of g.
Definition: Math/Grid.hpp:817
Grid(const Grid &g)
Constructs a copy of the grid g.
Definition: Math/Grid.hpp:495
Grid & operator=(Grid &&g)
Move-assigns the contents of g to this grid.
Definition: Math/Grid.hpp:663
SelfType GridTemporaryType
Concrete temporary grid type used by expression template machinery.
Definition: Math/Grid.hpp:459
Reference operator()(SizeType i)
Returns a mutable reference to the element at linear index i.
Definition: Math/Grid.hpp:526
const T & ConstReference
Constant element reference type.
Definition: Math/Grid.hpp:419
ArrayType & getData()
Returns a mutable reference to the underlying storage container.
Definition: Math/Grid.hpp:630
bool isEmpty() const
Tells whether the grid is empty.
Definition: Math/Grid.hpp:576
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:594
Grid(const GridExpression< E > &e)
Constructs the grid from the grid expression e (materializing the expression result).
Definition: Math/Grid.hpp:514
std::enable_if< IsScalar< T1 >::value, Grid >::type & operator/=(const T1 &t)
Divides every element by the scalar t.
Definition: Math/Grid.hpp:767
A ArrayType
The underlying storage container type.
Definition: Math/Grid.hpp:434
Grid & plusAssign(const GridExpression< E > &e)
Adds the elements of the grid expression e to this grid without intermediate temporary.
Definition: Math/Grid.hpp:794
SizeType getSize() const
Returns the total element count .
Definition: Math/Grid.hpp:585
Grid & minusAssign(const GridExpression< E > &e)
Subtracts the elements of the grid expression e from this grid without intermediate temporary.
Definition: Math/Grid.hpp:807
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:449
T ValueType
The scalar value type stored in the grid.
Definition: Math/Grid.hpp:409
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:612
Grid(SizeType m, SizeType n, SizeType o)
Constructs an m×n×o grid with default-initialized elements.
Definition: Math/Grid.hpp:478
Constant grid expression in which every element equals the same scalar value.
Definition: Math/Grid.hpp:1131
ScalarGrid()
Constructs an empty scalar grid.
Definition: Math/Grid.hpp:1179
SizeType getMaxSize3() const
Returns the maximum representable third-axis size.
Definition: Math/Grid.hpp:1292
SizeType getMaxSize1() const
Returns the maximum representable first-axis size.
Definition: Math/Grid.hpp:1274
ConstReference operator()(SizeType i) const
Returns a const reference to the common element value (linear-index form).
Definition: Math/Grid.hpp:1205
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:1164
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:1247
friend void swap(ScalarGrid &g1, ScalarGrid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:1333
ScalarGrid(const ScalarGrid &m)
Constructs a copy of the scalar grid m.
Definition: Math/Grid.hpp:1196
ScalarGrid(SizeType m, SizeType n, SizeType o, const ValueType &v=ValueType())
Constructs a scalar grid of size m×n×o in which every element equals v.
Definition: Math/Grid.hpp:1189
bool isEmpty() const
Tells whether the grid is empty (any axis has zero size).
Definition: Math/Grid.hpp:1229
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the common element value (3D-index form).
Definition: Math/Grid.hpp:1219
const T & Reference
Mutable element reference type (always a const reference — elements are immutable).
Definition: Math/Grid.hpp:1144
std::size_t SizeType
The unsigned size type.
Definition: Math/Grid.hpp:1154
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:1265
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:1169
ScalarGrid & operator=(const ScalarGrid &g)
Copy-assigns the axis sizes and common value from g.
Definition: Math/Grid.hpp:1302
const T & ConstReference
Constant element reference type.
Definition: Math/Grid.hpp:1149
Grid< T > GridTemporaryType
Concrete temporary grid type used by expression template machinery.
Definition: Math/Grid.hpp:1174
SizeType getMaxSize2() const
Returns the maximum representable second-axis size.
Definition: Math/Grid.hpp:1283
void swap(ScalarGrid &g)
Swaps the axis sizes and common value with g.
Definition: Math/Grid.hpp:1318
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:1256
SizeType getSize() const
Returns the total element count .
Definition: Math/Grid.hpp:1238
void resize(SizeType m, SizeType n, SizeType o)
Resizes the grid axes to (m, n, o).
Definition: Math/Grid.hpp:1344
T ValueType
The scalar value type.
Definition: Math/Grid.hpp:1139
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Math/Grid.hpp:1159
Constant grid expression whose elements are all zero.
Definition: Math/Grid.hpp:898
SizeType getMaxSize3() const
Returns the maximum representable third-axis size.
Definition: Math/Grid.hpp:1058
ZeroGrid & operator=(const ZeroGrid &g)
Copy-assigns the axis sizes from g.
Definition: Math/Grid.hpp:1068
Grid< T > GridTemporaryType
Concrete temporary grid type used by expression template machinery.
Definition: Math/Grid.hpp:941
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:1022
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:1031
SizeType getSize() const
Returns the total element count .
Definition: Math/Grid.hpp:1004
void swap(ZeroGrid &g)
Swaps the axis sizes with g.
Definition: Math/Grid.hpp:1083
SizeType getMaxSize2() const
Returns the maximum representable second-axis size.
Definition: Math/Grid.hpp:1049
friend void swap(ZeroGrid &g1, ZeroGrid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:1097
ZeroGrid(SizeType m, SizeType n, SizeType o)
Constructs a zero grid of size m×n×o.
Definition: Math/Grid.hpp:955
T ValueType
The scalar value type.
Definition: Math/Grid.hpp:906
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the zero element (3D-index form).
Definition: Math/Grid.hpp:985
ConstReference operator()(SizeType i) const
Returns a const reference to the zero element (linear-index form).
Definition: Math/Grid.hpp:971
const T & Reference
Reference type (always a const reference — all elements are zero).
Definition: Math/Grid.hpp:911
SizeType getMaxSize1() const
Returns the maximum representable first-axis size.
Definition: Math/Grid.hpp:1040
ZeroGrid(const ZeroGrid &m)
Constructs a copy of the zero grid m.
Definition: Math/Grid.hpp:962
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:936
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Math/Grid.hpp:926
ZeroGrid()
Constructs an empty zero grid (zero size on every axis).
Definition: Math/Grid.hpp:946
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:931
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:1013
const T & ConstReference
Constant reference type to the zero element.
Definition: Math/Grid.hpp:916
std::size_t SizeType
The unsigned size type.
Definition: Math/Grid.hpp:921
void resize(SizeType m, SizeType n, SizeType o)
Resizes the grid axes to (m, n, o).
Definition: Math/Grid.hpp:1108
bool isEmpty() const
Tells whether the grid is empty (any axis has zero size).
Definition: Math/Grid.hpp:995
constexpr unsigned int A
Generic type that covers any element except hydrogen.
Definition: AtomType.hpp:637
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
constexpr unsigned int m
Specifies that the stereocenter has m configuration.
Definition: CIPDescriptor.hpp:116
Grid< double > DGrid
Unbounded dense grid storing floating-point values of type double.
Definition: Math/Grid.hpp:1402
ScalarGrid< float > FScalarGrid
Immutable grid where all elements have the same value of type float.
Definition: Math/Grid.hpp:1387
ZeroGrid< double > DZeroGrid
Immutable grid where all elements have the value zero of type double.
Definition: Math/Grid.hpp:1382
Grid< float > FGrid
Unbounded dense grid storing floating-point values of type float.
Definition: Math/Grid.hpp:1397
ZeroGrid< float > FZeroGrid
Immutable grid where all elements have the value zero of type float.
Definition: Math/Grid.hpp:1377
ScalarGrid< double > DScalarGrid
Immutable grid where all elements have the same value of type double.
Definition: Math/Grid.hpp:1392
The namespace of the Chemical Data Processing Library.
Selects a concrete temporary grid type compatible with the grid expression G.
Definition: TypeTraits.hpp:365