27 #ifndef CDPL_MATH_GRID_HPP
28 #define CDPL_MATH_GRID_HPP
34 #include <type_traits>
68 typedef typename std::conditional<std::is_const<G>::value,
69 typename G::ConstReference,
118 return data(i, j, k);
130 return data(i, j, k);
139 return data.getSize();
148 return data.getSize1();
157 return data.getSize2();
166 return data.getSize3();
175 return data.getMaxSize();
184 return data.getMaxSize1();
193 return data.getMaxSize2();
202 return data.getMaxSize3();
211 return data.isEmpty();
239 data.operator=(
r.data);
249 template <
typename E>
262 template <
typename E>
275 template <
typename E>
288 template <
typename T>
301 template <
typename T>
315 template <
typename E>
328 template <
typename E>
341 template <
typename E>
376 template <
typename T,
typename A = std::vector<T> >
412 data(), size1(0), size2(0), size3(0) {}
421 data(storageSize(
m, n, o)), size1(
m), size2(n), size3(o) {}
431 data(storageSize(
m, n, o), v), size1(
m), size2(n), size3(o) {}
438 data(g.data), size1(g.size1), size2(g.size2), size3(g.size3) {}
445 data(), size1(0), size2(0), size3(0)
455 template <
typename E>
459 gridAssignGrid<ScalarAssignment>(*
this, e);
529 return (size1 * size2 * size3);
565 return data.max_size();
617 template <
typename C>
629 template <
typename E>
643 template <
typename C>
655 template <
typename E>
669 template <
typename C>
681 template <
typename E>
695 template <
typename T1>
696 typename std::enable_if<IsScalar<T1>::value,
Grid>::type&
operator*=(
const T1& t)
698 gridAssignScalar<ScalarMultiplicationAssignment>(*
this, t);
708 template <
typename T1>
709 typename std::enable_if<IsScalar<T1>::value,
Grid>::type&
operator/=(
const T1& t)
711 gridAssignScalar<ScalarDivisionAssignment>(*
this, t);
721 template <
typename E>
725 gridAssignGrid<ScalarAssignment>(*
this, e);
735 template <
typename E>
738 gridAssignGrid<ScalarAdditionAssignment>(*
this, e);
748 template <
typename E>
751 gridAssignGrid<ScalarSubtractionAssignment>(*
this, e);
762 std::swap(data, g.data);
763 std::swap(size1, g.size1);
764 std::swap(size2, g.size2);
765 std::swap(size3, g.size3);
785 std::fill(data.begin(), data.end(), v);
798 if (size1 ==
m && size2 == n && size3 == o)
802 Grid tmp(
m, n, o, v);
804 for (
SizeType i = 0, min_size1 = std::min(size1,
m); i < min_size1; i++)
805 for (
SizeType j = 0, min_size2 = std::min(size2, n); j < min_size2; j++)
806 for (
SizeType k = 0, min_size3 = std::min(size3, o); k < min_size3; k++)
807 tmp(i, j, k) = (*this)(i, j, k);
812 data.resize(storageSize(
m, n, o), v);
823 (n <= (std::numeric_limits<SizeType>::max() / o) &&
m <= (std::numeric_limits<SizeType>::max() / (n * o))),
838 template <
typename T>
866 size1(0), size2(0), size3(0) {}
875 size1(
m), size2(n), size3(o) {}
882 size1(
m.size1), size2(
m.size2), size3(
m.size3) {}
916 return (size1 == 0 || size2 == 0 || size3 == 0);
925 return (size1 * size2 * size3);
961 return std::numeric_limits<SizeType>::max();
970 return std::numeric_limits<SizeType>::max();
979 return std::numeric_limits<SizeType>::max();
1005 std::swap(size1, g.size1);
1006 std::swap(size2, g.size2);
1007 std::swap(size3, g.size3);
1041 template <
typename T>
1048 template <
typename T>
1076 size1(0), size2(0), size3(0), value() {}
1086 size1(
m), size2(n), size3(o), value(v) {}
1093 size1(
m.size1), size2(
m.size2), size3(
m.size3), value(
m.value) {}
1127 return (size1 == 0 || size2 == 0 || size3 == 0);
1136 return (size1 * size2 * size3);
1172 return std::numeric_limits<SizeType>::max();
1181 return std::numeric_limits<SizeType>::max();
1190 return std::numeric_limits<SizeType>::max();
1217 std::swap(size1, g.size1);
1218 std::swap(size2, g.size2);
1219 std::swap(size3, g.size3);
1220 std::swap(value, g.value);
1258 template <
typename G>
1266 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:320
CRTP base class for all grid expression types.
Definition: Expression.hpp:180
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:316
GridReference(GridType &g)
Constructs the reference wrapping the grid g.
Definition: Math/Grid.hpp:86
std::conditional< std::is_const< G >::value, typename G::ConstReference, typename G::Reference >::type Reference
Mutable reference type (degrades to ConstReference when the wrapped grid is const).
Definition: Math/Grid.hpp:70
friend void swap(GridReference &r1, GridReference &r2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:362
GridReference & minusAssign(const GridExpression< E > &e)
Subtracts the grid expression e from the wrapped grid without intermediate temporary.
Definition: Math/Grid.hpp:342
GridType & getData()
Returns a mutable reference to the wrapped grid.
Definition: Math/Grid.hpp:227
Reference operator()(SizeType i, SizeType j, SizeType k)
Returns a mutable reference to the element at the (i, j, k) cell of the wrapped grid.
Definition: Math/Grid.hpp:116
G::SizeType SizeType
The unsigned size type used by the wrapped grid.
Definition: Math/Grid.hpp:74
bool isEmpty() const
Tells whether the wrapped grid is empty (zero cells along any dimension).
Definition: Math/Grid.hpp:209
SizeType getSize2() const
Returns the size of the wrapped grid along the second dimension.
Definition: Math/Grid.hpp:155
GridReference & operator+=(const GridExpression< E > &e)
Adds the grid expression e cell-wise to the wrapped grid.
Definition: Math/Grid.hpp:263
std::enable_if< IsScalar< T >::value, GridReference >::type & operator/=(const T &t)
Divides every cell of the wrapped grid by the scalar t.
Definition: Math/Grid.hpp:302
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:104
SizeType getMaxSize() const
Returns the maximum total number of cells the wrapped grid can hold.
Definition: Math/Grid.hpp:173
GridReference & operator=(const GridExpression< E > &e)
Assigns the grid expression e to the wrapped grid.
Definition: Math/Grid.hpp:250
SizeType getMaxSize2() const
Returns the maximum size of the wrapped grid along the second dimension.
Definition: Math/Grid.hpp:191
std::enable_if< IsScalar< T >::value, GridReference >::type & operator*=(const T &t)
Multiplies every cell of the wrapped grid by the scalar t.
Definition: Math/Grid.hpp:289
SizeType getMaxSize3() const
Returns the maximum size of the wrapped grid along the third dimension.
Definition: Math/Grid.hpp:200
G::ValueType ValueType
The element value type of the wrapped grid.
Definition: Math/Grid.hpp:66
GridReference & plusAssign(const GridExpression< E > &e)
Adds the grid expression e to the wrapped grid without intermediate temporary.
Definition: Math/Grid.hpp:329
const GridType & getData() const
Returns a const reference to the wrapped grid.
Definition: Math/Grid.hpp:218
SizeType getSize3() const
Returns the size of the wrapped grid along the third dimension.
Definition: Math/Grid.hpp:164
GridReference & operator-=(const GridExpression< E > &e)
Subtracts the grid expression e cell-wise from the wrapped grid.
Definition: Math/Grid.hpp:276
G::DifferenceType DifferenceType
The signed difference type used by the wrapped grid.
Definition: Math/Grid.hpp:76
SizeType getMaxSize1() const
Returns the maximum size of the wrapped grid along the first dimension.
Definition: Math/Grid.hpp:182
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the element at the (i, j, k) cell of the wrapped grid.
Definition: Math/Grid.hpp:128
SizeType getSize1() const
Returns the size of the wrapped grid along the first dimension.
Definition: Math/Grid.hpp:146
G GridType
The wrapped grid type.
Definition: Math/Grid.hpp:64
SizeType getSize() const
Returns the total number of cells of the wrapped grid.
Definition: Math/Grid.hpp:137
GridReference & operator=(const GridReference &r)
Copies the cells of r into the wrapped grid.
Definition: Math/Grid.hpp:237
Reference operator()(SizeType i)
Returns a mutable reference to the element at the linear index i of the wrapped grid.
Definition: Math/Grid.hpp:94
G::ConstReference ConstReference
Constant reference type to an element.
Definition: Math/Grid.hpp:72
const SelfType ConstClosureType
Constant closure type used when this reference appears inside another expression.
Definition: Math/Grid.hpp:80
SelfType ClosureType
Closure type used when this reference appears inside another expression.
Definition: Math/Grid.hpp:78
void swap(GridReference &r)
Swaps the contents of the wrapped grid with those of the grid wrapped by r.
Definition: Math/Grid.hpp:352
Dynamically-sized dense 3D grid ( ) with configurable underlying storage.
Definition: Math/Grid.hpp:378
T * Pointer
Pointer type for raw element access.
Definition: Math/Grid.hpp:396
A::size_type SizeType
The unsigned size type used by the underlying storage container.
Definition: Math/Grid.hpp:390
Grid & operator+=(const GridExpression< E > &e)
Adds the grid expression e cell-wise to this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:656
Grid & operator=(const Grid &g)
Copy-assigns the contents of g to this grid.
Definition: Math/Grid.hpp:591
void resize(SizeType m, SizeType n, SizeType o, bool preserve=true, const ValueType &v=ValueType())
Resizes the grid to cells.
Definition: Math/Grid.hpp:796
Grid & operator-=(const GridContainer< C > &c)
Subtracts the contents of the grid container c cell-wise from this grid (no alias check needed).
Definition: Math/Grid.hpp:670
Reference operator()(SizeType i, SizeType j, SizeType k)
Returns a mutable reference to the element at (i, j, k).
Definition: Math/Grid.hpp:494
ConstReference operator()(SizeType i) const
Returns a const reference to the element at linear index i.
Definition: Math/Grid.hpp:480
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:430
Grid(Grid &&g)
Move-constructs a grid from g (g is left in a valid empty state).
Definition: Math/Grid.hpp:444
std::shared_ptr< SelfType > SharedPointer
A reference-counted smart pointer [SHPTR] for dynamically allocated Grid instances.
Definition: Math/Grid.hpp:406
Grid()
Constructs an empty grid (zero size on every axis).
Definition: Math/Grid.hpp:411
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:618
const T * ConstPointer
Constant pointer type for raw element access.
Definition: Math/Grid.hpp:398
std::enable_if< IsScalar< T1 >::value, Grid >::type & operator*=(const T1 &t)
Multiplies every cell by the scalar t.
Definition: Math/Grid.hpp:696
Grid & assign(const GridExpression< E > &e)
Resizes this grid to match e and assigns its cells without intermediate temporary.
Definition: Math/Grid.hpp:722
A::difference_type DifferenceType
The signed difference type used by the underlying storage container.
Definition: Math/Grid.hpp:392
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the element at (i, j, k).
Definition: Math/Grid.hpp:508
T & Reference
Mutable reference type to an element.
Definition: Math/Grid.hpp:386
Grid & operator=(const GridExpression< E > &e)
Assigns the grid expression e to this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:630
Grid & operator-=(const GridExpression< E > &e)
Subtracts the grid expression e cell-wise from this grid (via a temporary to handle aliasing).
Definition: Math/Grid.hpp:682
SizeType getMaxSize() const
Returns the maximum total cell count the underlying storage container can hold.
Definition: Math/Grid.hpp:563
const ArrayType & getData() const
Returns a const reference to the underlying storage container.
Definition: Math/Grid.hpp:581
void clear(const ValueType &v=ValueType())
Sets every cell of the grid to the value v.
Definition: Math/Grid.hpp:783
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:545
friend void swap(Grid &g1, Grid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:774
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:402
Grid & operator+=(const GridContainer< C > &c)
Adds the contents of the grid container c cell-wise to this grid (no alias check needed).
Definition: Math/Grid.hpp:644
void swap(Grid &g)
Swaps the contents of this grid with those of g.
Definition: Math/Grid.hpp:759
Grid(const Grid &g)
Constructs a copy of the grid g.
Definition: Math/Grid.hpp:437
Grid & operator=(Grid &&g)
Move-assigns the contents of g to this grid.
Definition: Math/Grid.hpp:605
SelfType GridTemporaryType
Concrete temporary grid type used by expression-template machinery.
Definition: Math/Grid.hpp:404
Reference operator()(SizeType i)
Returns a mutable reference to the element at linear index i.
Definition: Math/Grid.hpp:468
const T & ConstReference
Constant reference type to an element.
Definition: Math/Grid.hpp:388
ArrayType & getData()
Returns a mutable reference to the underlying storage container.
Definition: Math/Grid.hpp:572
bool isEmpty() const
Tells whether the grid is empty.
Definition: Math/Grid.hpp:518
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:536
Grid(const GridExpression< E > &e)
Constructs the grid from the grid expression e (materializing the expression result).
Definition: Math/Grid.hpp:456
std::enable_if< IsScalar< T1 >::value, Grid >::type & operator/=(const T1 &t)
Divides every cell by the scalar t.
Definition: Math/Grid.hpp:709
A ArrayType
The underlying storage container type.
Definition: Math/Grid.hpp:394
Grid & plusAssign(const GridExpression< E > &e)
Adds the cells of the grid expression e to this grid without intermediate temporary.
Definition: Math/Grid.hpp:736
SizeType getSize() const
Returns the total cell count .
Definition: Math/Grid.hpp:527
Grid & minusAssign(const GridExpression< E > &e)
Subtracts the cells of the grid expression e from this grid without intermediate temporary.
Definition: Math/Grid.hpp:749
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:400
T ValueType
The scalar value type stored in the grid.
Definition: Math/Grid.hpp:384
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:554
Grid(SizeType m, SizeType n, SizeType o)
Constructs an m × n × o grid with default-initialized elements.
Definition: Math/Grid.hpp:420
Constant grid expression in which every cell equals the same scalar value.
Definition: Math/Grid.hpp:1050
ScalarGrid()
Constructs an empty scalar grid.
Definition: Math/Grid.hpp:1075
SizeType getMaxSize3() const
Returns the maximum representable third-axis size.
Definition: Math/Grid.hpp:1188
SizeType getMaxSize1() const
Returns the maximum representable first-axis size.
Definition: Math/Grid.hpp:1170
ConstReference operator()(SizeType i) const
Returns a const reference to the common cell value (linear-index form).
Definition: Math/Grid.hpp:1101
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:1066
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:1143
friend void swap(ScalarGrid &g1, ScalarGrid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:1229
ScalarGrid(const ScalarGrid &m)
Constructs a copy of the scalar grid m.
Definition: Math/Grid.hpp:1092
ScalarGrid(SizeType m, SizeType n, SizeType o, const ValueType &v=ValueType())
Constructs a scalar grid of size in which every cell equals v.
Definition: Math/Grid.hpp:1085
bool isEmpty() const
Tells whether the grid is empty (any axis has zero size).
Definition: Math/Grid.hpp:1125
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the common cell value (3D-index form).
Definition: Math/Grid.hpp:1115
const T & Reference
Reference type (always a const reference — cells are immutable).
Definition: Math/Grid.hpp:1058
std::size_t SizeType
The unsigned size type.
Definition: Math/Grid.hpp:1062
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:1161
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:1068
ScalarGrid & operator=(const ScalarGrid &g)
Copy-assigns the axis sizes and common value from g.
Definition: Math/Grid.hpp:1198
const T & ConstReference
Constant reference type to a cell.
Definition: Math/Grid.hpp:1060
Grid< T > GridTemporaryType
Concrete temporary grid type used by expression-template machinery.
Definition: Math/Grid.hpp:1070
SizeType getMaxSize2() const
Returns the maximum representable second-axis size.
Definition: Math/Grid.hpp:1179
void swap(ScalarGrid &g)
Swaps the axis sizes and common value with g.
Definition: Math/Grid.hpp:1214
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:1152
SizeType getSize() const
Returns the total cell count .
Definition: Math/Grid.hpp:1134
void resize(SizeType m, SizeType n, SizeType o)
Resizes the grid axes to (m, n, o).
Definition: Math/Grid.hpp:1240
T ValueType
The scalar value type.
Definition: Math/Grid.hpp:1056
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Math/Grid.hpp:1064
Constant grid expression whose cells are all zero.
Definition: Math/Grid.hpp:840
SizeType getMaxSize3() const
Returns the maximum representable third-axis size.
Definition: Math/Grid.hpp:977
ZeroGrid & operator=(const ZeroGrid &g)
Copy-assigns the axis sizes from g.
Definition: Math/Grid.hpp:987
Grid< T > GridTemporaryType
Concrete temporary grid type used by expression-template machinery.
Definition: Math/Grid.hpp:860
SizeType getSize2() const
Returns the second-axis size.
Definition: Math/Grid.hpp:941
SizeType getSize3() const
Returns the third-axis size.
Definition: Math/Grid.hpp:950
SizeType getSize() const
Returns the total cell count .
Definition: Math/Grid.hpp:923
void swap(ZeroGrid &g)
Swaps the axis sizes with g.
Definition: Math/Grid.hpp:1002
SizeType getMaxSize2() const
Returns the maximum representable second-axis size.
Definition: Math/Grid.hpp:968
friend void swap(ZeroGrid &g1, ZeroGrid &g2)
ADL-enabled free-function form of swap().
Definition: Math/Grid.hpp:1016
ZeroGrid(SizeType m, SizeType n, SizeType o)
Constructs a zero grid of size .
Definition: Math/Grid.hpp:874
T ValueType
The scalar value type.
Definition: Math/Grid.hpp:846
ConstReference operator()(SizeType i, SizeType j, SizeType k) const
Returns a const reference to the zero element (3D-index form).
Definition: Math/Grid.hpp:904
ConstReference operator()(SizeType i) const
Returns a const reference to the zero element (linear-index form).
Definition: Math/Grid.hpp:890
const T & Reference
Reference type (always a const reference — all cells are zero).
Definition: Math/Grid.hpp:848
SizeType getMaxSize1() const
Returns the maximum representable first-axis size.
Definition: Math/Grid.hpp:959
ZeroGrid(const ZeroGrid &m)
Constructs a copy of the zero grid m.
Definition: Math/Grid.hpp:881
const GridReference< const SelfType > ConstClosureType
Constant closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:858
std::ptrdiff_t DifferenceType
The signed difference type.
Definition: Math/Grid.hpp:854
ZeroGrid()
Constructs an empty zero grid (zero size on every axis).
Definition: Math/Grid.hpp:865
GridReference< SelfType > ClosureType
Closure type used when this grid appears inside another expression.
Definition: Math/Grid.hpp:856
SizeType getSize1() const
Returns the first-axis size.
Definition: Math/Grid.hpp:932
const T & ConstReference
Constant reference type to the zero element.
Definition: Math/Grid.hpp:850
std::size_t SizeType
The unsigned size type.
Definition: Math/Grid.hpp:852
void resize(SizeType m, SizeType n, SizeType o)
Resizes the grid axes to (m, n, o).
Definition: Math/Grid.hpp:1027
bool isEmpty() const
Tells whether the grid is empty (any axis has zero size).
Definition: Math/Grid.hpp:914
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:1298
ScalarGrid< float > FScalarGrid
Immutable grid where all elements have the same value of type float.
Definition: Math/Grid.hpp:1283
ZeroGrid< double > DZeroGrid
Immutable grid where all elements have the value zero of type double.
Definition: Math/Grid.hpp:1278
Grid< float > FGrid
Unbounded dense grid storing floating point values of type float.
Definition: Math/Grid.hpp:1293
ZeroGrid< float > FZeroGrid
Immutable grid where all elements have the value zero of type float.
Definition: Math/Grid.hpp:1273
ScalarGrid< double > DScalarGrid
Immutable grid where all elements have the same value of type double.
Definition: Math/Grid.hpp:1288
The namespace of the Chemical Data Processing Library.
Selects a concrete temporary grid type compatible with the grid expression G.
Definition: TypeTraits.hpp:337