SheafSystem  0.0.0.0
ternary_index_space.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 #ifndef TERNARY_INDEX_SPACE_H
19 #define TERNARY_INDEX_SPACE_H
20 
21 #ifndef SHEAF_DLL_SPEC_H
22 #include "SheafSystem/sheaf_dll_spec.h"
23 #endif
24 
25 #ifndef TERNARY_INDEX_H
26 #include "SheafSystem/ternary_index.h"
27 #endif
28 
29 #ifndef RAGGED_ARRAY_H
30 #include "SheafSystem/ragged_array.h"
31 #endif
32 
33 namespace fiber_bundle
34 {
35 
36  using namespace sheaf;
37 
41 class SHEAF_DLL_SPEC ternary_index_space
42 {
43 public:
47  unary_index i_size;
48 
52  unary_index j_size;
53 
57  unary_index k_size;
58 
62  unary_index size;
63 
67  bool invariant() const
68  {
69  invariance(i_size >= 0);
70  invariance(j_size >= 0);
71  invariance(k_size >= 0);
72  invariance(size == i_size*j_size*k_size);
73  return true;
74  }
75 
76 
81  {
82  // Preconditions:
83 
84  // Body:
85 
86  put_bounds(0, 0, 0);
87 
88  // Postconditions:
89 
90  ensure(invariant());
91  ensure(i_size == 0);
92  ensure(j_size == 0);
93  ensure(k_size == 0);
94 
95  // Exit:
96 
97  return;
98  };
99 
103  ternary_index_space(unary_index xi_size, unary_index xj_size, unary_index xk_size)
104  {
105  // Preconditions:
106 
107  require(xi_size >= 0);
108  require(xj_size >= 0);
109  require(xk_size >= 0);
110 
111  // Body:
112 
113  put_bounds(xi_size, xj_size, xk_size);
114 
115  // Postconditions:
116 
117  ensure(invariant());
118  ensure(i_size == xi_size);
119  ensure(j_size == xj_size);
120  ensure(k_size == xk_size);
121 
122  // Exit:
123 
124  return;
125  };
126 
130  void put_bounds(unary_index xi_size, unary_index xj_size, unary_index xk_size)
131  {
132  // Preconditions:
133 
134  // Body:
135 
136  i_size = xi_size;
137  j_size = xj_size;
138  k_size = xk_size;
139  size = i_size*j_size*k_size;
140 
141  // Postconditions:
142 
143  ensure(invariant());
144  ensure(i_size == xi_size);
145  ensure(j_size == xj_size);
146  ensure(k_size == xk_size);
147 
148  // Exit:
149 
150  return;
151  };
152 
156  ternary_index_space(unary_index xl)
157  {
158  // Preconditions:
159 
160  // Body:
161 
162  put_bounds(xl, xl, xl);
163 
164  // Postconditions:
165 
166  ensure(invariant());
167  ensure(i_size == xl);
168  ensure(j_size == xl);
169  ensure(k_size == xl);
170 
171  // Exit:
172 
173  return;
174  };
175 
180  {
181  // Preconditions:
182 
183  // Body:
184 
185  ternary_index_space result(i_size+xother.i_size,
186  j_size+xother.j_size,
187  k_size+xother.k_size);
188 
189  // Postconditions:
190 
191  ensure(invariant());
192  ensure(result.i_size == i_size+xother.i_size);
193  ensure(result.j_size == j_size+xother.j_size);
194  ensure(result.k_size == k_size+xother.k_size);
195 
196  // Exit:
197 
198  return result;
199  };
200 
205  {
206  // Preconditions:
207 
208  // Body:
209 
210  ternary_index_space result(i_size*xother.i_size,
211  j_size*xother.j_size,
212  k_size*xother.k_size);
213 
214  // Postconditions:
215 
216  ensure(invariant());
217  ensure(result.i_size == i_size*xother.i_size);
218  ensure(result.j_size == j_size*xother.j_size);
219  ensure(result.k_size == k_size*xother.k_size);
220 
221  // Exit:
222 
223  return result;
224  };
225 
229  bool contains(const ternary_index& xindex) const
230  {
231  // Preconditions:
232 
233  // Body:
234 
235  bool result =
236  ((0 <= xindex.i) && (xindex.i < i_size) &&
237  (0 <= xindex.j) && (xindex.j < j_size) &&
238  (0 <= xindex.k) && (xindex.k < k_size));
239 
240  // Postconditions:
241 
242  ensure(result ==
243  ((0 <= xindex.i) && (xindex.i < i_size) &&
244  (0 <= xindex.j) && (xindex.j < j_size) &&
245  (0 <= xindex.k) && (xindex.k < k_size)));
246 
247  // Exit:
248 
249  return result;
250  };
251 
255  bool contains(const unary_index& xindex) const
256  {
257  // Preconditions:
258 
259  // Body:
260 
261  bool result = ((0 <= xindex) && (xindex < size));
262 
263  // Postconditions:
264 
265  ensure( result == ((0 <= xindex) && (xindex < size)));
266 
267  // Exit:
268 
269  return result;
270  };
271 
275  unary_index to_row_major_offset(const ternary_index& xindex) const
276  {
277  // Preconditions:
278 
279  // Body:
280 
281  unary_index result = xindex.k + k_size*(xindex.j + j_size*xindex.i);
282 
283  // Postconditions:
284 
285  ensure(result == (xindex.i*j_size*k_size + xindex.j*k_size + xindex.k));
286 
287  // Exit:
288 
289  return result;
290  };
291 
295  unary_index to_row_major_offset(const unary_index xi, const unary_index xj, const unary_index xk) const
296  {
297  // Preconditions:
298 
299  // Body:
300 
301  unary_index result = xk + k_size*(xj + j_size*xi);
302 
303  // Postconditions:
304 
305  ensure(result == (xi*j_size*k_size + xj*k_size + xk));
306 
307  // Exit:
308 
309  return result;
310  };
311 
315  ternary_index from_row_major_offset(const unary_index& xindex) const
316  {
317  // Preconditions:
318 
319  require((0 <= xindex) && (xindex < size));
320 
321  // Body:
322 
323  // offset = k_size*j_size*i + k_size*j + k;
324 
325  // Precondition ensures 0 <= i < i_size.
326 
327  ternary_index result(xindex/(k_size*j_size), (xindex/k_size)%j_size, xindex%k_size);
328 
329  // Postconditions:
330 
331  ensure(to_row_major_offset(result) == xindex );
332 
333  // Exit:
334 
335  return result;
336  };
337 
341  unary_index to_col_major_offset(const ternary_index& xindex) const
342  {
343  // Preconditions:
344 
345  // Body:
346 
347  unary_index result = xindex.i + i_size*(xindex.j + j_size*xindex.k);
348 
349  // Postconditions:
350 
351  ensure(result == (xindex.i + i_size*xindex.j + i_size*j_size*xindex.k));
352 
353  // Exit:
354 
355  return result;
356  };
357 
361  unary_index to_col_major_offset(const unary_index xi, const unary_index xj, const unary_index xk) const
362  {
363  // Preconditions:
364 
365  // Body:
366 
367  unary_index result = xi + i_size*(xj + j_size*xk);
368 
369  // Postconditions:
370 
371  ensure(result == (xi + i_size*xj + i_size*j_size*xk));
372 
373  // Exit:
374 
375  return result;
376  };
377 
381  ternary_index from_col_major_offset(const unary_index& xindex) const
382  {
383  // Preconditions:
384 
385  require((0 <= xindex) && (xindex < size));
386 
387  // Body:
388 
389  // offset = i + i_size*j + i_size*j_size*k;
390 
391  // Precondition ensures 0 <= i < i_size.
392 
393  ternary_index result(xindex%i_size, (xindex/i_size)%j_size, xindex/(i_size*j_size));
394 
395  // Postconditions:
396 
397  ensure(to_row_major_offset(result) == xindex);
398 
399  // Exit:
400 
401  return result;
402  };
403 
408  ragged_array<unary_index>* neighbor_list() const;
409 };
410 
411 
412 // ===========================================================
413 // NON-MEMBER FUNCTIONS
414 // ===========================================================
415 
416 #ifndef DOXYGEN_1_5_4_SKIP_UNKNOWN
417 
421 SHEAF_DLL_SPEC std::ostream& operator<<(std::ostream& os, const ternary_index_space& xbis);
422 
423 #endif // ifndef DOXYGEN_1_5_4_SKIP_UNKNOWN
424 
425 } // namespace fiber_bundle
426 
427 #endif // ifndef TERNARY_INDEX_SPACE_H
A triple of indices (i,j, k).
Definition: ternary_index.h:45
unary_index to_row_major_offset(const unary_index xi, const unary_index xj, const unary_index xk) const
Converts (xi, xj, xk) to a unary_index using row-major ordering.
unary_index to_row_major_offset(const ternary_index &xindex) const
Converts xindex to a unary_index using row-major ordering.
ternary_index_space(unary_index xi_size, unary_index xj_size, unary_index xk_size)
Creates an instance with bound (xi_size, xj_size, xk_size).
ternary_index_space operator+(const ternary_index_space &xother) const
Sum.
unary_index j
The second index.
Definition: ternary_index.h:57
void put_bounds(unary_index xi_size, unary_index xj_size, unary_index xk_size)
Sets i_size = xi_size, j_size = xj_size, k_size = xk_size and recomputes size.
unary_index i
The first index.
Definition: ternary_index.h:52
ternary_index_space()
Creates an instance with bound (0,0,0).
ternary_index_space(unary_index xl)
Conversion from unary_index; interpreted as cube.
bool invariant() const
Class invariant.
ternary_index_space operator*(const ternary_index_space &xother) const
Cartesian product.
A bounded domain for ternary_index objects.
bool contains(const ternary_index &xindex) const
True if xindex is in this space.
unary_index size
Number in the space.
Namespace for the sheaves component of the sheaf system.
unary_index k
The third index.
Definition: ternary_index.h:62
unary_index j_size
Upper bound for the second index.
unary_index k_size
Upper bound for the third index.
unary_index to_col_major_offset(const ternary_index &xindex) const
Converts xindex to a unary_index using col-major ordering.
A two index array with variable length rows.
ternary_index from_row_major_offset(const unary_index &xindex) const
Converts xindex to a ternary_index using row-major ordering.
Namespace for the fiber_bundles component of the sheaf system.
unary_index to_col_major_offset(const unary_index xi, const unary_index xj, const unary_index xk) const
Converts xindex to a unary_index using col-major ordering.
unary_index i_size
Upper bound for the first index.
ternary_index from_col_major_offset(const unary_index &xindex) const
Converts xindex to a ternary_index using col-major ordering.
bool contains(const unary_index &xindex) const
True if xindex is in this space.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const binary_index &xbi)
Insert binary_index& xbi into ostream& os.
Definition: binary_index.cc:35