Schnek
subgrid.hpp
1 /*
2  * subgrid.hpp
3  *
4  * Created on: 18 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_SUBGRID_HPP_
28 #define SCHNEK_SUBGRID_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 >
41  public:
42  typedef Array<int,rank> IndexType;
43  typedef BaseGrid BaseGridType;
45  protected:
46  BaseGridType *baseGrid;
47  DomainType domain;
48  IndexType dims;
49 
50  public:
51 
53  protected:
54  typename DomainType::iterator it;
55  BaseGridType *baseGrid;
56  T* element;
57  storage_iterator(typename DomainType::iterator it_, BaseGridType *baseGrid_)
58  : it(it_), baseGrid(baseGrid_), element(&baseGrid->get(*it)) {}
59 
60  friend class SubGridStorage;
61 
62  public:
63  T& operator*() { return *element;}
64  storage_iterator &operator++()
65  {
66  ++it;
67  element = &baseGrid->get(*it);
68  return *this;
69  }
70  bool operator==(const storage_iterator &SI)
71  { return (it==SI.it) && (baseGrid == SI.baseGrid); }
72 
73  bool operator!=(const storage_iterator &SI)
74  { return (it!=SI.it) || (baseGrid != SI.baseGrid); }
75  };
76 
78  protected:
79  typename DomainType::iterator it;
80  const BaseGridType *baseGrid;
81  const T* element;
82  const_storage_iterator(typename DomainType::iterator it_, BaseGridType *baseGrid_)
83  : it(it_), baseGrid(baseGrid_), element(&baseGrid->get(*it)) {}
84 
85  friend class SubGridStorage;
86 
87  public:
88  const T& operator*() { return *element;}
89  const_storage_iterator &operator++()
90  {
91  ++it;
92  element = &baseGrid->get(*it);
93  return *this;
94  }
95  bool operator==(const const_storage_iterator &SI)
96  { return (it==SI.it) && (baseGrid == SI.baseGrid); }
97  bool operator!=(const const_storage_iterator &SI)
98  { return (it!=SI.it) || (baseGrid != SI.baseGrid); }
99  };
100 
101  SubGridStorage();
102 
103  SubGridStorage(const IndexType &low_, const IndexType &high_);
104 
105  void resize(const IndexType &low_, const IndexType &high_);
106 
107  T &get(const IndexType &index)
108  {
109  //typename BaseGrid::CheckingPolicy<rank>::check(index, domain.getLo(), domain.getHi());
110  return baseGrid->get(baseGrid->check(index, domain.getLo(), domain.getHi()));
111  }
112 
113  const T &get(const IndexType &index) const
114  {
115  //typename BaseGrid::CheckingPolicy<rank>::check(index, domain.getLo(), domain.getHi());
116  return baseGrid->get(baseGrid->check(index, domain.getLo(), domain.getHi()));
117  }
118 
120  const IndexType& getLo() const { return domain.getLo(); }
122  const IndexType& getHi() const { return domain.getHi(); }
124  const IndexType& getDims() const { return dims; }
125 
127  int getLo(int k) const { return domain.getLo()[k]; }
129  int getHi(int k) const { return domain.getHi()[k]; }
131  int getDims(int k) const { return dims[k]; }
132 
133  storage_iterator begin() { return storage_iterator(domain.begin(), baseGrid); }
134  storage_iterator end() { return storage_iterator(domain.end(), baseGrid); }
135 
136  const_storage_iterator cbegin() const { return const_storage_iterator(domain.cbegin(), baseGrid); }
137  const_storage_iterator cend() const { return const_storage_iterator(domain.cend(), baseGrid); }
138 
139  void setBaseGrid(BaseGridType &baseGrid_) { baseGrid = &baseGrid_; }
140 
141 };
142 
143 template<
144  class BaseGrid,
145  template<int> class CheckingPolicy = GridNoArgCheck
146 >
147 class SubGrid
148  : public GridBase
149  <
150  typename BaseGrid::value_type,
151  BaseGrid::Rank,
152  CheckingPolicy<BaseGrid::Rank>,
153  SubGridStorage<
154  typename BaseGrid::value_type,
155  BaseGrid::Rank,
156  BaseGrid
157  >
158  >
159 {
160  private:
161  typedef GridBase
162  <
163  typename BaseGrid::value_type,
164  BaseGrid::Rank,
165  CheckingPolicy<BaseGrid::Rank>,
167  typename BaseGrid::value_type,
168  BaseGrid::Rank,
169  BaseGrid
170  >
171  > ParentType;
172 
173  public:
174  enum {Rank = BaseGrid::Rank};
175  typedef typename BaseGrid::value_type value_type;
176  typedef Array<int,Rank> IndexType;
177  typedef Range<int,Rank> RangeType;
178  typedef BaseGrid BaseGridType;
180  SubGrid();
181 
192  SubGrid(const IndexType &size, BaseGridType &baseGrid_);
193 
206  SubGrid(const IndexType &low, const IndexType &high, BaseGridType &baseGrid_);
207 
220  SubGrid(const RangeType &range, BaseGridType &baseGrid_);
221 
222 };
223 
224 
225 } // namespace schnek
226 
227 #include "subgrid.t"
228 
229 #endif // SCHNEK_SUBGRID_HPP_
Definition: grid.hpp:54
Definition: algo.hpp:30
Definition: gridcheck.hpp:38
const LimitType & getHi() const
Return rectangle maximum.
Definition: range.hpp:77
Definition: subgrid.hpp:40
Definition: subgrid.hpp:147
iterator end()
Creates an iterator pointing to a position after the end of the rectangle.
Definition: range.hpp:348
iterator begin()
Creates an iterator pointing to the beginning of the rectangle.
Definition: range.hpp:343
const LimitType & getLo() const
Return rectangle minimum.
Definition: range.hpp:73