SheafSystem  0.0.0.0
binary_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 BINARY_INDEX_SPACE_H
19 #define BINARY_INDEX_SPACE_H
20 
21 #ifndef SHEAF_DLL_SPEC_H
22 #include "SheafSystem/sheaf_dll_spec.h"
23 #endif
24 
25 #ifndef BINARY_INDEX_H
26 #include "SheafSystem/binary_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 binary_index_space
42 {
43 public:
47  unary_index i_size;
48 
52  unary_index j_size;
53 
57  unary_index size;
58 
62  bool invariant() const
63  {
64  invariance(i_size >= 0);
65  invariance(j_size >= 0);
66  invariance(size == i_size*j_size);
67  return true;
68  }
69 
70 
75  {
76  // Preconditions:
77 
78  // Body:
79 
80  put_bounds(0, 0);
81 
82  // Postconditions:
83 
84  ensure(invariant());
85  ensure(i_size == 0);
86  ensure(j_size == 0);
87 
88  // Exit:
89 
90  return;
91  };
92 
96  binary_index_space(unary_index xi_size, unary_index xj_size)
97  {
98  // Preconditions:
99 
100  require(xi_size >= 0);
101  require(xj_size >= 0);
102 
103  // Body:
104 
105  put_bounds(xi_size, xj_size);
106 
107  // Postconditions:
108 
109  ensure(invariant());
110  ensure(i_size == xi_size);
111  ensure(j_size == xj_size);
112 
113  // Exit:
114 
115  return;
116  };
117 
121  void put_bounds(unary_index xi_size = 0, unary_index xj_size = 0)
122  {
123  // Preconditions:
124 
125  // Body:
126 
127  i_size = xi_size;
128  j_size = xj_size;
129  size = i_size*j_size;
130 
131  // Postconditions:
132 
133  ensure(invariant());
134  ensure(i_size == xi_size);
135  ensure(j_size == xj_size);
136 
137  // Exit:
138 
139  return;
140  };
141 
145  binary_index_space(unary_index xl)
146  {
147  // Preconditions:
148 
149  // Body:
150 
151  put_bounds(xl, xl);
152 
153  // Postconditions:
154 
155  ensure(invariant());
156  ensure(i_size == xl);
157  ensure(j_size == xl);
158 
159  // Exit:
160 
161  return;
162  };
163 
168  {
169  // Preconditions:
170 
171  // Body:
172 
173  binary_index_space result(i_size+xother.i_size, j_size+xother.j_size);
174 
175  // Postconditions:
176 
177  ensure(invariant());
178  ensure(result.i_size == i_size+xother.i_size);
179  ensure(result.j_size == j_size+xother.j_size);
180 
181  // Exit:
182 
183  return result;
184  };
185 
190  {
191  // Preconditions:
192 
193  // Body:
194 
195  binary_index_space result(i_size*xother.i_size, j_size*xother.j_size);
196 
197  // Postconditions:
198 
199  ensure(invariant());
200  ensure(result.i_size == i_size*xother.i_size);
201  ensure(result.j_size == j_size*xother.j_size);
202 
203  // Exit:
204 
205  return result;
206  };
207 
211  bool contains(const binary_index& xindex) const
212  {
213  // Preconditions:
214 
215  // Body:
216 
217  bool result =
218  ((0 <= xindex.i) && (xindex.i < i_size) &&
219  (0 <= xindex.j) && (xindex.j < j_size));
220 
221  // Postconditions:
222 
223  ensure(result ==
224  ((0 <= xindex.i) && (xindex.i < i_size) &&
225  (0 <= xindex.j) && (xindex.j < j_size)));
226 
227  // Exit:
228 
229  return result;
230  };
231 
235  bool contains(const unary_index& xindex) const
236  {
237  // Preconditions:
238 
239  // Body:
240 
241  bool result = ((0 <= xindex) && (xindex < size));
242 
243  // Postconditions:
244 
245  ensure( result == ((0 <= xindex) && (xindex < size)));
246 
247  // Exit:
248 
249  return result;
250  };
251 
255  unary_index to_row_major_offset(const binary_index& xindex) const
256  {
257  // Preconditions:
258 
259  // Body:
260 
261  unary_index result = j_size*xindex.i+ xindex.j;
262 
263  // Postconditions:
264 
265  ensure(result == (j_size*xindex.i+ xindex.j));
266 
267  // Exit:
268 
269  return result;
270  };
271 
275  unary_index to_row_major_offset(const unary_index xi, const unary_index xj) const
276  {
277  // Preconditions:
278 
279  // Body:
280 
281  unary_index result = xj + j_size*xi;
282 
283  // Postconditions:
284 
285  ensure(result == (xi*j_size + xj));
286 
287  // Exit:
288 
289  return result;
290  };
291 
295  binary_index from_row_major_offset(const unary_index& xindex) const
296  {
297  // Preconditions:
298 
299  // Body:
300 
301  binary_index result(xindex/j_size, xindex%j_size);
302 
303  // Postconditions:
304 
305  ensure(to_row_major_offset(result) == xindex );
306 
307  // Exit:
308 
309  return result;
310  };
311 
316  ragged_array<unary_index>* neighbor_list() const;
317 };
318 
319 
320 // ===========================================================
321 // NON-MEMBER FUNCTIONS
322 // ===========================================================
323 
324 #ifndef DOXYGEN_1_5_4_SKIP_UNKNOWN
325 
329 SHEAF_DLL_SPEC
330 std::ostream& operator<<(std::ostream& os, const binary_index_space& xbis);
331 
332 #endif // ifndef DOXYGEN_1_5_4_SKIP_UNKNOWN
333 
334 } // namespace fiber_bundle
335 
336 #endif // ifndef BINARY_INDEX_SPACE_H
A pair of indices (i,j).
Definition: binary_index.h:44
void put_bounds(unary_index xi_size=0, unary_index xj_size=0)
Sets i_size = xi_size and j_size = xj_size and recomputes size.
unary_index j_size
Upper bound for the second index.
bool contains(const unary_index &xindex) const
True if xindex is in this space.
binary_index_space()
Creates an instance with bound (0,0).
binary_index_space(unary_index xi_size, unary_index xj_size)
Creates an instance with bounds (xi_size, xj_size).
unary_index to_row_major_offset(const binary_index &xindex) const
Converts xindex to a unary_index using row-major ordering.
binary_index_space(unary_index xl)
Conversion from unary_index; interpreted as square.
unary_index size
Number in the space.
A bounded domain for binary_index objects.
unary_index i
The first index.
Definition: binary_index.h:51
unary_index j
The second index.
Definition: binary_index.h:56
Namespace for the sheaves component of the sheaf system.
binary_index from_row_major_offset(const unary_index &xindex) const
Converts xindex to a binary_index using row-major ordering.
A two index array with variable length rows.
binary_index_space operator+(const binary_index_space &xother) const
Sum.
bool contains(const binary_index &xindex) const
True if xindex is in this space.
unary_index to_row_major_offset(const unary_index xi, const unary_index xj) const
Converts (xi, xj) to a unary_index using row-major ordering.
unary_index i_size
Upper bound for the first index.
bool invariant() const
Class invariant.
Namespace for the fiber_bundles component of the sheaf system.
binary_index_space operator*(const binary_index_space &xother) const
Cartesian product.
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