source: src/base/index.hpp@ 759a6a

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

VMG: Removed useless function from VMG::Index.

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

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/**
2 * @file index.hpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Mon Apr 18 12:19:49 2011
5 *
6 * @brief Header file for the class VMG::Index.
7 *
8 */
9
10#ifndef INDEX_HPP_
11#define INDEX_HPP_
12
13#include <algorithm>
14#include <cmath>
15#include <cstdlib>
16#include <cstring>
17#include <iostream>
18
19namespace VMG
20{
21
22class Vector;
23
24class Index
25{
26public:
27 Index(const Index& rhs);
28 Index(const Vector& vec);
29 Index(int x, int y, int z) {i[0]=x;i[1]=y;i[2]=z;}
30 Index(int val) {i[0]=val;i[1]=val;i[2]=val;}
31 Index(int* arr) {i[0]=arr[0];i[1]=arr[1];i[2]=arr[2];}
32 Index() {i[0]=0;i[1]=0;i[2]=0;}
33
34 int& operator[](const int& index) {return i[index];}
35 const int& operator[](const int& index) const {return i[index];}
36
37 int& X() {return i[0];}
38 int& Y() {return i[1];}
39 int& Z() {return i[2];}
40
41 const int& X() const {return i[0];}
42 const int& Y() const {return i[1];}
43 const int& Z() const {return i[2];}
44
45 Index Abs() {return Index(abs(i[0]), abs(i[1]), abs(i[2]));}
46 int Max() const {return std::max(std::max(i[0],i[1]),i[2]);}
47 int Min() const {return std::min(std::min(i[0],i[1]),i[2]);}
48 int Product() const {return i[0]*i[1]*i[2];}
49 int Sum() const {return i[0]+i[1]+i[2];}
50
51 bool IsInBounds(const Index& begin, const Index& end) const
52 {
53 return i[0] >= begin[0] && i[0] < end[0] &&
54 i[1] >= begin[1] && i[1] < end[1] &&
55 i[2] >= begin[2] && i[2] < end[2];
56 }
57
58 bool IsComponentwiseLess(const Index& other) const
59 {
60 return this->i[0] < other.i[0]
61 && this->i[1] < other.i[1]
62 && this->i[2] < other.i[2];
63 }
64
65 bool IsComponentwiseLessOrEqual(const Index& other) const
66 {
67 return this->i[0] <= other.i[0]
68 && this->i[1] <= other.i[1]
69 && this->i[2] <= other.i[2];
70 }
71
72 bool IsComponentwiseGreater(const Index& other) const
73 {
74 return this->i[0] > other.i[0]
75 && this->i[1] > other.i[1]
76 && this->i[2] > other.i[2];
77 }
78
79 bool IsComponentwiseGreaterOrEqual(const Index& other) const
80 {
81 return this->i[0] >= other.i[0]
82 && this->i[1] >= other.i[1]
83 && this->i[2] >= other.i[2];
84 }
85
86 Index MaxComponentwise(const Index& rhs) const
87 {
88 return Index(std::max(i[0], rhs.i[0]), std::max(i[1], rhs.i[1]), std::max(i[2], rhs.i[2]));
89 }
90
91 Index MinComponentwise(const Index& rhs) const
92 {
93 return Index(std::min(i[0], rhs.i[0]), std::min(i[1], rhs.i[1]), std::min(i[2], rhs.i[2]));
94 }
95
96 Index Clamp(const Index& lower_bound, const Index& upper_bound) const
97 {
98 Index index(*this);
99 for (int j=0; j<3; ++j) {
100 index.i[j] = std::max(index.i[j], lower_bound[j]);
101 index.i[j] = std::min(index.i[j], upper_bound[j]);
102 }
103 return index;
104 }
105
106 int* vec() {return i;}
107 const int* vec() const {return i;}
108
109 Index& operator=(const Index& rhs)
110 {
111 i[0] = rhs.X();
112 i[1] = rhs.Y();
113 i[2] = rhs.Z();
114 return *this;
115 }
116
117 Index& operator=(const int& rhs)
118 {
119 i[0] = rhs;
120 i[1] = rhs;
121 i[2] = rhs;
122 return *this;
123 }
124
125 bool operator==(const Index& other) const
126 {
127 return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z());
128 }
129
130 bool operator<(const Index& other) const
131 {
132 for (int j=0; j<3; ++j) {
133 if (this->i[j] < other.i[j]) return true;
134 if (this->i[j] != other.i[j]) return false;
135 }
136
137 return false;
138 }
139
140 bool operator!=(const Index& other) const
141 {
142 return !(*this == other);
143 }
144
145 Index& operator+=(const Index& rhs)
146 {
147 i[0] += rhs.X();
148 i[1] += rhs.Y();
149 i[2] += rhs.Z();
150 return *this;
151 }
152
153 Index& operator-=(const Index& rhs)
154 {
155 i[0] -= rhs.X();
156 i[1] -= rhs.Y();
157 i[2] -= rhs.Z();
158 return *this;
159 }
160
161 Index& operator*=(const Index& rhs)
162 {
163 i[0] *= rhs.X();
164 i[1] *= rhs.Y();
165 i[2] *= rhs.Z();
166 return *this;
167 }
168
169 Index& operator/=(const Index& rhs)
170 {
171 i[0] /= rhs.X();
172 i[1] /= rhs.Y();
173 i[2] /= rhs.Z();
174 return *this;
175 }
176
177 Index& operator%=(const Index& rhs)
178 {
179 i[0] %= rhs.X();
180 i[1] %= rhs.Y();
181 i[2] %= rhs.Z();
182 return *this;
183 }
184
185 Index& operator+=(const int& rhs)
186 {
187 i[0] += rhs;
188 i[1] += rhs;
189 i[2] += rhs;
190 return *this;
191 }
192
193 Index& operator-=(const int& rhs)
194 {
195 i[0] -= rhs;
196 i[1] -= rhs;
197 i[2] -= rhs;
198 return *this;
199 }
200
201 Index& operator*=(const int& rhs)
202 {
203 i[0] *= rhs;
204 i[1] *= rhs;
205 i[2] *= rhs;
206 return *this;
207 }
208
209 Index& operator/=(const int& rhs)
210 {
211 i[0] /= rhs;
212 i[1] /= rhs;
213 i[2] /= rhs;
214 return *this;
215 }
216
217 Index& operator%=(const int& rhs)
218 {
219 i[0] %= rhs;
220 i[1] %= rhs;
221 i[2] %= rhs;
222 return *this;
223 }
224
225 Index operator+(const Index& rhs) const
226 {
227 return Index(*this) += rhs;
228 }
229
230 Index operator-(const Index& rhs) const
231 {
232 return Index(*this) -= rhs;
233 }
234
235 Index operator*(const Index& rhs) const
236 {
237 return Index(*this) *= rhs;
238 }
239
240 Index operator/(const Index& rhs) const
241 {
242 return Index(*this) /= rhs;
243 }
244
245 Index operator%(const Index& rhs) const
246 {
247 return Index(*this) %= rhs;
248 }
249
250 Index operator+(const int& rhs) const
251 {
252 return Index(*this) += rhs;
253 }
254
255 Index operator-(const int& rhs) const
256 {
257 return Index(*this) -= rhs;
258 }
259
260 Index operator*(const int& rhs) const
261 {
262 return Index(*this) *= rhs;
263 }
264
265 Index operator/(const int& rhs) const
266 {
267 return Index(*this) /= rhs;
268 }
269
270 Index operator%(const int& rhs) const
271 {
272 return Index(*this) %= rhs;
273 }
274
275 Vector operator+(const Vector& rhs) const;
276 Vector operator-(const Vector& rhs) const;
277 Vector operator*(const Vector& rhs) const;
278 Vector operator/(const Vector& rhs) const;
279
280 Vector operator+(const vmg_float& rhs) const;
281 Vector operator-(const vmg_float& rhs) const;
282 Vector operator*(const vmg_float& rhs) const;
283 Vector operator/(const vmg_float& rhs) const;
284
285private:
286 int i[3];
287};
288
289inline Index operator+(const int& lhs, const Index& rhs)
290{
291 return Index(lhs) += rhs;
292}
293
294inline Index operator-(const int& lhs, const Index& rhs)
295{
296 return Index(lhs) -= rhs;
297}
298
299inline Index operator*(const int& lhs, const Index& rhs)
300{
301 return Index(lhs) *= rhs;
302}
303
304inline Index operator/(const int& lhs, const Index& rhs)
305{
306 return Index(lhs) /= rhs;
307}
308
309inline Index operator%(const int& lhs, const Index& rhs)
310{
311 return Index(lhs) %= rhs;
312}
313
314const Index Max(const Index& index1, const Index& index2);
315std::ostream& operator<<(std::ostream& out, const Index& index);
316
317}
318
319#endif /* INDEX_HPP_ */
Note: See TracBrowser for help on using the repository browser.