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  const_storage_iterator(BaseIter baseIter_)
56  : baseIter(baseIter) {}
57 
58  friend class GridTransformStorage;
59 
60  public:
61  T operator*()
62  { return transformation(*baseIter);}
63 
64  const_storage_iterator &operator++()
65  {
66  ++baseIter;
67  return *this;
68  }
69 
70  bool operator==(const const_storage_iterator &SI)
71  { return baseIter==SI.baseIter; }
72 
73  bool operator!=(const const_storage_iterator &SI)
74  { return baseIter!=SI.baseIter; }
75  };
76 
78 
79  T& get(const IndexType &index)
80  {
81  static T result;
82  result = transformation(baseGrid->get(index));
83  return result;
84  }
85 
86  T get(const IndexType &index) const
87  {
88  return transformation(baseGrid->get(index));
89  }
91  const IndexType& getLo() const { return baseGrid->getLo(); }
93  const IndexType& getHi() const { return baseGrid->getHi(); }
95  const IndexType& getDims() const { return baseGrid->getDims(); }
96 
98  int getLo(int k) const { return baseGrid->getLo(k); }
100  int getHi(int k) const { return baseGrid->getHi(k); }
102  int getDims(int k) const { return baseGrid->getDims(k); }
103 
104  const_storage_iterator begin() { return const_storage_iterator(baseGrid->begin()); }
105  const_storage_iterator end() { return const_storage_iterator(baseGrid->end()); }
106 
107  const_storage_iterator cbegin() const { return const_storage_iterator(baseGrid->cbegin()); }
108  const_storage_iterator cend() const { return const_storage_iterator(baseGrid->cend()); }
109 
110  void setBaseGrid(BaseGridType &baseGrid_)
111  {
112  baseGrid = &baseGrid_;
113  }
114 
115  Transformation getTransformation() const
116  {
117  return transformation;
118  }
119 
120  void setTransformation(const Transformation &transformation)
121  {
122  this->transformation = transformation;
123  }
124 };
125 
126 template<
127  class BaseGrid,
128  typename Transformation,
129  template<int> class CheckingPolicy = GridNoArgCheck
130 >
132  : public GridBase
133  <
134  typename Transformation::value_type,
135  BaseGrid::Rank,
136  CheckingPolicy<BaseGrid::Rank>,
137  GridTransformStorage<
138  typename BaseGrid::value_type,
139  BaseGrid::Rank,
140  BaseGrid,
141  Transformation
142  >
143  >
144 {
145  private:
146  typedef GridBase
147  <
148  typename Transformation::value_type,
149  BaseGrid::Rank,
150  CheckingPolicy<BaseGrid::Rank>,
152  typename BaseGrid::value_type,
153  BaseGrid::Rank,
154  BaseGrid,
155  Transformation
156  >
157  > ParentType;
158 
159  public:
160  enum {Rank = BaseGrid::Rank};
161  typedef typename Transformation::value_type value_type;
162  typedef typename BaseGrid::IndexType IndexType;
163  typedef BaseGrid BaseGridType;
165  GridTransform();
166 
177  GridTransform(BaseGridType &baseGrid_);
178 };
179 
180 template<typename SrcType, typename DestType>
182 {
183  public:
184  typedef DestType value_type;
185  public:
186  DestType operator()(SrcType x) {return (DestType)x; }
187 };
188 
189 } // namespace schnek
190 
191 #include "gridtransform.t"
192 
193 #endif // SCHNEK_SUBGRID_HPP_
Definition: grid.hpp:54
Definition: gridtransform.hpp:41
Definition: algo.hpp:30
Definition: gridtransform.hpp:181
Definition: gridcheck.hpp:38
Definition: gridtransform.hpp:131