source: src/grid/grid_iterator.hpp@ a40eea

Last change on this file since a40eea was ac6d04, checked in by Julian Iseringhausen <isering@…>, 14 years ago

Merge recent changes of the vmg library into ScaFaCos.

Includes a fix for the communication problems on Jugene.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1666 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file grid_iterator.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Wed Apr 20 12:24:09 2011
5 *
6 * @brief Iterator class to iterate over subgrids.
7 *
8 */
9
10#ifndef GRID_ITERATOR_HPP_
11#define GRID_ITERATOR_HPP_
12
13#include <list>
14
15#include "base/index.hpp"
16#include "base/object.hpp"
17
18namespace VMG
19{
20
21class GridIterator : public Object
22{
23public:
24 GridIterator() :
25 index(0),
26 begin(0),
27 end(0)
28 {}
29
30 GridIterator(const Index& index, const Index& begin, const Index& end) :
31 index(index),
32 begin(begin),
33 end(end)
34 {}
35
36 GridIterator(const GridIterator& rhs) :
37 index(rhs.index),
38 begin(rhs.begin),
39 end(rhs.end)
40 {}
41
42 virtual ~GridIterator()
43 {}
44
45 GridIterator& operator=(const GridIterator& rhs)
46 {
47 this->index = rhs.index;
48 this->begin = rhs.begin;
49 this->end = rhs.end;
50 return *this;
51 }
52
53 Index& operator*();
54 Index* operator->();
55
56 bool operator==(const GridIterator& rhs) const;
57 bool operator==(const Index& rhs) const;
58 bool operator==(const int& rhs) const;
59
60 bool operator!=(const GridIterator& rhs) const;
61 bool operator!=(const Index& rhs) const;
62 bool operator!=(const int& rhs) const;
63
64 GridIterator& operator++();
65 GridIterator operator++(int);
66
67 GridIterator& operator--();
68 GridIterator operator--(int);
69
70 GridIterator& operator+=(const int& steps);
71
72 const Index& GetIndex() const {return index;}
73 const Index& GetBegin() const {return begin;}
74 const Index& GetEnd() const {return end;}
75
76private:
77 Index index;
78 Index begin, end;
79};
80
81inline Index& GridIterator::operator*()
82{
83 return this->index;
84}
85
86inline Index* GridIterator::operator->()
87{
88 return &*(static_cast<GridIterator>(*this));
89}
90
91inline bool GridIterator::operator==(const GridIterator& rhs) const
92{
93 return this->index == rhs.index;
94}
95
96inline bool GridIterator::operator==(const Index& rhs) const
97{
98 return this->index == rhs;
99}
100
101inline bool GridIterator::operator==(const int& rhs) const
102{
103 return this->index[0] == rhs &&
104 this->index[1] == rhs &&
105 this->index[2] == rhs;
106}
107
108inline bool GridIterator::operator!=(const GridIterator& rhs) const
109{
110 return this->index != rhs.index;
111}
112
113inline bool GridIterator::operator!=(const Index& rhs) const
114{
115 return this->index != rhs;
116}
117
118inline bool GridIterator::operator!=(const int& rhs) const
119{
120 return this->index[0] != rhs ||
121 this->index[1] != rhs ||
122 this->index[2] != rhs;
123}
124
125inline GridIterator& GridIterator::operator++()
126{
127 if (++index.Z() >= end.Z()) {
128 index.Z() = begin.Z();
129 if (++index.Y() >= end.Y()) {
130 index.Y() = begin.Y();
131 if (++index.X() >= end.X())
132 index = -1;
133 }
134 }
135
136 return *this;
137}
138
139inline GridIterator GridIterator::operator++(int)
140{
141 GridIterator result = *this;
142 ++(*this);
143 return result;
144}
145
146inline GridIterator& GridIterator::operator--()
147{
148 if (--index.Z() < begin.Z()) {
149 index.Z() = end.Z() - 1;
150 if (--index.Y() < begin.Y()) {
151 index.Y() = end.Y() - 1;
152 if (--index.X() < begin.X())
153 index = -1;
154 }
155 }
156
157 return *this;
158}
159
160inline GridIterator GridIterator::operator--(int)
161{
162 GridIterator result = *this;
163 --(*this);
164 return result;
165}
166
167inline GridIterator& GridIterator::operator+=(const int& steps)
168{
169 index.Z() += steps;
170 if (index.Z() >= end.Z()) {
171 index.Z() = begin.Z();
172 index.Y() += steps;
173 if (index.Y() >= end.Y()) {
174 index.Y() = begin.Y();
175 index.X() += steps;
176 if (index.X() >= end.X())
177 index = -1;
178 }
179 }
180
181 return *this;
182}
183
184}
185
186#endif /* GRID_ITERATOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.