Schnek
gridstorage.hpp
1 /*
2  * gridstorage.hpp
3  *
4  * Created on: 23 Jan 2007
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_GRIDSTORAGE_H_
28 #define SCHNEK_GRIDSTORAGE_H_
29 
30 #include "array.hpp"
31 
32 namespace schnek {
33 
34 template<typename T, int rank>
36 {
37  public:
38  typedef Array<int,rank> IndexType;
39 
40  protected:
41  T* data;
42  T* data_fast;
43  int size;
44  IndexType low;
45  IndexType high;
46  IndexType dims;
47 
48  public:
50  : data(NULL) , data_fast(NULL), size(0) {}
51 
55  void resize(const IndexType &low_, const IndexType &high_);
56  private:
58  void deleteData();
60  void newData(const IndexType &low_, const IndexType &high_);
61 };
62 
63 
64 template<typename T, int rank>
66 {
67  public:
68  typedef Array<int,rank> IndexType;
69 
70  protected:
71  T* data;
72  T* data_fast;
73  int size;
74  IndexType low;
75  IndexType high;
76  IndexType dims;
77 
78  public:
80  : data(NULL) , data_fast(NULL), size(0) {}
81 
85  void resize(const IndexType &low_, const IndexType &high_);
86  private:
88  void deleteData();
90  void newData(const IndexType &low_, const IndexType &high_);
91 };
92 
93 template<typename T, int rank>
95 {
96  public:
97  typedef Array<int,rank> IndexType;
98 
99  protected:
100  T* data;
101  T* data_fast;
102  int size;
103  IndexType low;
104  IndexType high;
105  IndexType dims;
106 
107  private:
108  int bufSize;
109  double avgSize;
110  double avgVar;
111  double r;
112 
113  public:
115 
119  void resize(const IndexType &low_, const IndexType &high_);
120  private:
123  void deleteData();
125  void newData(int size);
126 };
127 
132 template<typename T, int rank, template<typename, int> class AllocationPolicy>
133 class SingleArrayGridStorageBase : public AllocationPolicy<T, rank> {
134  public:
135  typedef Array<int,rank> IndexType;
136  public:
138  protected:
139  T* element;
140  storage_iterator(T* element_) : element(element_) {}
141 
142  friend class SingleArrayGridStorageBase;
143 
144  public:
145  storage_iterator(const storage_iterator &it) : element(it.element) {}
146  T& operator*() { return *element;}
147  storage_iterator &operator++() {++element; return *this;}
148  bool operator==(const storage_iterator &SI)
149  { return element == SI.element; }
150  bool operator!=(const storage_iterator &SI)
151  { return element != SI.element; }
152  };
153 
155  protected:
156  const T* element;
157  const_storage_iterator(const T* element_) : element(element_) {}
158 
159  friend class SingleArrayGridStorageBase;
160 
161  public:
162  const T& operator*() { return *element;}
163  const_storage_iterator &operator++() {++element; return *this;}
164  bool operator==(const const_storage_iterator &SI)
165  { return element == SI.element; }
166  bool operator!=(const const_storage_iterator &SI)
167  { return element != SI.element; }
168  };
169 
171 
172  SingleArrayGridStorageBase(const IndexType &low_, const IndexType &high_);
173 
174 
175  T* getRawData() const { return this->data; }
176 
178  const IndexType& getLo() const { return this->low; }
180  const IndexType& getHi() const { return this->high; }
182  const IndexType& getDims() const { return this->dims; }
183 
185  int getLo(int k) const { return this->low[k]; }
187  int getHi(int k) const { return this->high[k]; }
189  int getDims(int k) const { return this->dims[k]; }
190 
191  int getSize() const { return this->size; }
192 
193  storage_iterator begin() { return storage_iterator(this->data); }
194  storage_iterator end() { return storage_iterator(this->data + this->size); }
195 
196  const_storage_iterator cbegin() const { return const_storage_iterator(this->data); }
197  const_storage_iterator cend() const { return const_storage_iterator(this->data + this->size); }
198 
199 };
200 
201 template<typename T, int rank, template<typename, int> class AllocationPolicy>
202 class SingleArrayGridCOrderStorageBase : public SingleArrayGridStorageBase<T, rank, AllocationPolicy> {
203  public:
205  typedef typename BaseType::IndexType IndexType;
206 
207  SingleArrayGridCOrderStorageBase() : BaseType() {}
208 
209  SingleArrayGridCOrderStorageBase(const IndexType &low_, const IndexType &high_)
210  : BaseType(low_, high_) {}
211 
212  T &get(const IndexType &index);
213  const T &get(const IndexType &index) const;
214 };
215 
216 template<typename T, int rank, template<typename, int> class AllocationPolicy>
217 class SingleArrayGridFortranOrderStorageBase : public SingleArrayGridStorageBase<T, rank, AllocationPolicy> {
218  public:
220  typedef typename BaseType::IndexType IndexType;
221 
223 
224  SingleArrayGridFortranOrderStorageBase(const IndexType &low_, const IndexType &high_)
225  : BaseType(low_, high_) {}
226 
227  T &get(const IndexType &index);
228  const T &get(const IndexType &index) const;
229 };
230 
231 template<typename T, int rank>
233  : public SingleArrayGridCOrderStorageBase<T, rank, SingleArrayInstantAllocation>
234 {
235  public:
237  typedef typename BaseType::IndexType IndexType;
238 
239  SingleArrayGridStorage() : BaseType() {}
240 
241  SingleArrayGridStorage(const IndexType &low_, const IndexType &high_)
242  : BaseType(low_, high_) {}
243 };
244 
245 
246 template<typename T, int rank>
248  : public SingleArrayGridFortranOrderStorageBase<T, rank, SingleArrayInstantFortranAllocation>
249 {
250  public:
252  typedef typename BaseType::IndexType IndexType;
253 
254  SingleArrayGridStorageFortran() : BaseType() {}
255 
256  SingleArrayGridStorageFortran(const IndexType &low_, const IndexType &high_)
257  : BaseType(low_, high_) {}
258 };
259 
260 template<typename T, int rank>
262  : public SingleArrayGridCOrderStorageBase<T, rank, SingleArrayLazyAllocation>
263 {
264  public:
266  typedef typename BaseType::IndexType IndexType;
267 
268  LazyArrayGridStorage() : BaseType() {}
269 
270  LazyArrayGridStorage(const IndexType &low_, const IndexType &high_)
271  : BaseType(low_, high_) {}
272 };
273 
274 } // namespace schnek
275 
276 
277 #include "gridstorage.t"
278 
279 
280 #endif // SCHNEK_GSTORAGE_H_
281 
Definition: gridstorage.hpp:65
Definition: gridstorage.hpp:35
Definition: gridstorage.hpp:232
Definition: gridstorage.hpp:202
Definition: algo.hpp:30
Definition: gridstorage.hpp:247
Definition: gridstorage.hpp:133
void resize(const IndexType &low_, const IndexType &high_)
Definition: gridstorage.hpp:217
Definition: gridstorage.hpp:94
Definition: gridstorage.hpp:261