Schnek
gridtransform.hpp
1 /*
2  * gridtransform.hpp
3  *
4  * Created on: 24 Sep 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_GRIDTRANSFORM_HPP_
28 #define SCHNEK_GRIDTRANSFORM_HPP_
29 
30 #include "grid.hpp"
31 #include "range.hpp"
32 
33 namespace schnek {
34 
35 template<
36  typename T,
37  int rank,
38  class BaseGrid,
39  typename Transformation
40 >
42  public:
43  typedef Array<int,rank> IndexType;
44  typedef BaseGrid BaseGridType;
45  typedef Transformation TransformationType;
46  protected:
47  BaseGridType *baseGrid;
48  Transformation transformation;
49  public:
50 
52  protected:
53  typedef typename BaseGridType::const_storage_iterator BaseIter;
54  BaseIter baseIter;
55  GridTransformStorage &storage;
56 
57  const_storage_iterator(BaseIter baseIter, GridTransformStorage &storage)
58  : baseIter(baseIter), storage(storage) {}
59 
60  friend class GridTransformStorage;
61 
62  public:
63  T operator*()
64  { return storage.transformation(*baseIter);}
65 
66  const_storage_iterator &operator++()
67  {
68  ++baseIter;
69  return *this;
70  }
71 
72  bool operator==(const const_storage_iterator &SI)
73  { return baseIter==SI.baseIter; }
74 
75  bool operator!=(const const_storage_iterator &SI)
76  { return baseIter!=SI.baseIter; }
77  };
78 
80 
81  SCHNEK_INLINE T& get(const IndexType &index)
82  {
83  static T result;
84  result = transformation(baseGrid->get(index));
85  return result;
86  }
87 
88  SCHNEK_INLINE T get(const IndexType &index) const
89  {
90  return transformation(baseGrid->get(index));
91  }
93  SCHNEK_INLINE const IndexType& getLo() const { return baseGrid->getLo(); }
95  SCHNEK_INLINE const IndexType& getHi() const { return baseGrid->getHi(); }
97  SCHNEK_INLINE const IndexType& getDims() const { return baseGrid->getDims(); }
98 
100  SCHNEK_INLINE int getLo(int k) const { return baseGrid->getLo(k); }
102  SCHNEK_INLINE int getHi(int k) const { return baseGrid->getHi(k); }
104  SCHNEK_INLINE int getDims(int k) const { return baseGrid->getDims(k); }
105 
106  SCHNEK_INLINE const_storage_iterator begin() { return const_storage_iterator(baseGrid->begin(), *this); }
107  SCHNEK_INLINE const_storage_iterator end() { return const_storage_iterator(baseGrid->end(), *this); }
108 
109  const_storage_iterator cbegin() const { return const_storage_iterator(baseGrid->cbegin(), *this); }
110  const_storage_iterator cend() const { return const_storage_iterator(baseGrid->cend(), *this); }
111 
112  void setBaseGrid(BaseGridType &baseGrid_)
113  {
114  baseGrid = &baseGrid_;
115  }
116 
117  Transformation getTransformation() const
118  {
119  return transformation;
120  }
121 
122  void setTransformation(const Transformation &transformation)
123  {
124  this->transformation = transformation;
125  }
126 };
127 
128 template<
129  class BaseGrid,
130  typename Transformation,
131  template<int> class CheckingPolicy = GridNoArgCheck
132 >
134  : public internal::GridBase
135  <
136  typename Transformation::value_type,
137  BaseGrid::Rank,
138  CheckingPolicy<BaseGrid::Rank>,
139  GridTransformStorage<
140  typename BaseGrid::value_type,
141  BaseGrid::Rank,
142  BaseGrid,
143  Transformation
144  >
145  >
146 {
147  private:
148  typedef internal::GridBase
149  <
150  typename Transformation::value_type,
151  BaseGrid::Rank,
152  CheckingPolicy<BaseGrid::Rank>,
154  typename BaseGrid::value_type,
155  BaseGrid::Rank,
156  BaseGrid,
157  Transformation
158  >
159  > ParentType;
160 
161  public:
162  enum {Rank = BaseGrid::Rank};
163  typedef typename Transformation::value_type value_type;
164  typedef typename BaseGrid::IndexType IndexType;
165  typedef BaseGrid BaseGridType;
167  GridTransform();
168 
179  GridTransform(BaseGridType &baseGrid_);
180 };
181 
182 template<typename SrcType, typename DestType>
184 {
185  public:
186  typedef DestType value_type;
187  public:
188  DestType operator()(SrcType x) {return (DestType)x; }
189 };
190 
191 } // namespace schnek
192 
193 #include "gridtransform.t"
194 
195 #endif // SCHNEK_SUBGRID_HPP_
Definition: gridtransform.hpp:41
Definition: algo.hpp:30
The generic base class for the Grid class template.
Definition: grid.hpp:61
Definition: gridtransform.hpp:183
Definition: gridcheck.hpp:39
Definition: gridtransform.hpp:133