SheafSystem  0.0.0.0
ij_adjacency_implicit_index_space_iterator.cc
Go to the documentation of this file.
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 
20 
21 #include "SheafSystem/ij_adjacency_implicit_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/ij_adjacency_index_space_interval.h"
25 #include "SheafSystem/index_space_family.h"
26 
27 using namespace fiber_bundle; // Workaround for MS C++ bug.
28 
29 // ===========================================================
30 // IJ_ADJACENCY_IMPLICIT_INDEX_SPACE_ITERATOR FACET
31 // ===========================================================
32 
33 // PUBLIC MEMBER FUNCTIONS
34 
37 {
38  // Preconditions:
39 
40  // Body:
41 
42  // Postconditions:
43 
44  ensure(invariant());
45  ensure(!is_attached());
46 
47  // Exit:
48 
49  return;
50 }
51 
54 {
55  // Preconditions:
56 
57  // Body:
58 
59  *this = xother;
60 
61  // Postconditions:
62 
63  ensure(invariant());
64  ensure((*this) == xother);
65 
66  // Exit:
67 
68  return;
69 }
70 
73  pod_type xindex)
74 {
75  // Preconditions:
76 
77  require(conforms_to_state(xid_spaces, xindex));
78 
79  // Body:
80 
81  attach_to(xid_spaces, xindex);
82 
83  // Postconditions:
84 
85  ensure(invariant());
86  ensure(is_attached());
87  ensure(&id_spaces() == &xid_spaces);
88  ensure(index() == xindex);
89 
90  // Exit:
91 
92  return;
93 }
94 
97  const std::string& xname)
98 {
99  // Preconditions:
100 
101  require(conforms_to_state(xid_spaces, xname));
102 
103  // Body:
104 
105  attach_to(xid_spaces, xname);
106 
107  // Postconditions:
108 
109  ensure(invariant());
110  ensure(is_attached());
111  ensure(&id_spaces() == &xid_spaces);
112  ensure(name() == xname);
113 
114  // Exit:
115 
116  return;
117 }
118 
122 {
123  // Preconditions:
124 
125  // Body:
126 
127  if(xother.is_attached())
128  {
129  _hub_begin = xother._hub_begin;
130  _j_size = xother._j_size;
131  _i_ct = xother._i_ct;
132  _j_ct = xother._j_ct;
133  _i = xother._i;
134  _j = xother._j;
135 
136  (void) implicit_index_space_iterator::operator=(xother);
137  }
138  else
139  {
140  detach();
141  }
142 
143  // Postconditions:
144 
145  ensure(invariant());
146  ensure((*this) == xother);
147 
148  // Exit
149 
150  return *this;
151 }
152 
155 {
156  // Preconditions:
157 
158  // Body:
159 
160  // Nothing to do;
161 
162  // Postconditions:
163 
164  // Exit:
165 
166  return;
167 }
168 
171 hub_begin() const
172 {
173  // Preconditions:
174 
175  // Body:
176 
177  // Postconditions:
178 
179  ensure(is_basic_query);
180 
181  // Exit:
182 
183  return _hub_begin;
184 }
185 
188 j_size() const
189 {
190  // Preconditions:
191 
192  // Body:
193 
194  // Postconditions:
195 
196  ensure(is_basic_query);
197 
198  // Exit:
199 
200  return _j_size;
201 }
202 
205 i_ct() const
206 {
207  // Preconditions:
208 
209  // Body:
210 
211  // Postconditions:
212 
213  ensure(is_basic_query);
214 
215  // Exit:
216 
217  return _i_ct;
218 }
219 
222 j_ct() const
223 {
224  // Preconditions:
225 
226  // Body:
227 
228  // Postconditions:
229 
230  ensure(is_basic_query);
231 
232  // Exit:
233 
234  return _j_ct;
235 }
236 
237 // PROTECTED MEMBER FUNCTIONS
238 
239 // PRIVATE MEMBER FUNCTIONS
240 
241 
242 // ===========================================================
243 // IMPLICIT_INDEX_SPACE_ITERATOR FACET
244 // ===========================================================
245 
246 // PUBLIC MEMBER FUNCTIONS
247 
248 // PROTECTED MEMBER FUNCTIONS
249 
250 // PRIVATE MAMBER FUNCTIONS
251 
252 
253 // ===========================================================
254 // INDEX_SPACE_ITERATOR FACET
255 // ===========================================================
256 
257 // PUBLIC MEMBER FUNCTIONS
258 
262 {
263  // Preconditions:
264 
265  require(is_ancestor_of(&xother));
266 
267  // Body:
268 
270  dynamic_cast<const ij_adjacency_implicit_index_space_iterator&>(xother);
271 
272  (void) this->operator=(lother);
273 
274  // Postconditions:
275 
276  ensure(invariant());
277  ensure((*this) == xother);
278 
279  // Exit
280 
281  return *this;
282 }
283 
284 bool
286 operator==(const index_space_iterator& xother) const
287 {
288  // Preconditions:
289 
290  require(is_ancestor_of(&xother));
291 
292  // Body:
293 
294  bool result = implicit_index_space_iterator::operator==(xother);
295  if(result && is_attached())
296  {
298  dynamic_cast<const ij_adjacency_implicit_index_space_iterator&>(xother);
299 
300  result = result && (_hub_begin == lother._hub_begin);
301  result = result && (_j_size == lother._j_size);
302  result = result && (_i_ct == lother._i_ct);
303  result = result && (_j_ct == lother._j_ct);
304  result = result && (_i == lother._i);
305  result = result && (_j == lother._j);
306  }
307 
308  // Postconditions:
309 
310  ensure(invariant());
311 
312  // Exit
313 
314  return result;
315 }
316 
319 clone() const
320 {
322 
323  // Preconditions:
324 
325  // Body:
326 
327  result = new ij_adjacency_implicit_index_space_iterator(*this);
328 
329  // Postconditions:
330 
331  ensure(result != 0);
332  ensure(is_same_type(result));
333  ensure(*result == *this);
334 
335  // Exit:
336 
337  return result;
338 }
339 
340 // PROTECTED MEMBER FUNCTIONS
341 
342 // PRIVATE MAMBER FUNCTIONS
343 
344 
345 // ===========================================================
346 // ITERATOR FACET
347 // ===========================================================
348 
349 // PUBLIC MEMBER FUNCTIONS
350 
351 void
354 {
355  // Preconditions:
356 
357  require(is_attached());
358  require(!is_done());
359 
360  // Body:
361 
362  define_old_variable(pod_type old_pod = pod());
363  define_old_variable(pod_type old_hub_pod = hub_pod());
364 
365  // Increment to the next id.
366 
367  _j++;
368  if(_j == _j_ct)
369  {
370  // Done with the j-iteration, try incrementing in the i-direction.
371 
372  _j = 0;
373  _i++;
374 
375  if(_i == _i_ct)
376  {
377  // The iteration is done.
378 
379  invalidate_ids();
380  }
381  else
382  {
383  // The iteration is not done, increment the ids in the i-direction.
384 
385  ++_pod;
386  _hub_pod = (_i * _j_size) + _hub_begin;
387  }
388  }
389  else
390  {
391  // The iteration is not done, increment the ids in the j-direction.
392 
393  ++_pod;
394  ++_hub_pod;
395  }
396 
397  // Postconditions:
398 
399  ensure(invariant());
400  ensure(is_done() || pod() != old_pod);
401  ensure(is_done() || hub_pod() != old_hub_pod);
402 
403  // Exit:
404 
405  return;
406 }
407 
408 void
411 {
412  // Preconditions:
413 
414  require(is_attached());
415 
416  // Body:
417 
418  _is_done = false;
419 
420  // Reset to first adjacent zone.
421 
422  _pod = 0;
423  _hub_pod = _hub_begin;
424 
425  _i = 0;
426  _j = 0;
427 
428  // Postconditions:
429 
430  ensure(invariant());
431 
432  // Exit:
433 
434  return;
435 }
436 
437 // PROTECTED MEMBER FUNCTIONS
438 
439 // PRIVATE MEMBER FUNCTIONS
440 
441 
442 // ===========================================================
443 // HANDLE FACET
444 // ===========================================================
445 
446 // PUBLIC MEMBER FUNCTIONS
447 
448 void
450 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
451 {
452  // Preconditions:
453 
454  require(xhost.contains(xlocal_id));
455  require(conforms_to_state(xhost, xlocal_id));
456 
457  // Body:
458 
459  // Assign the state information.
460 
461  _host = &xhost;
462  _local_id = xlocal_id;
463 
464  // Find the section of the ij_adjacency that represents this id space.
465 
466  const ij_adjacency_index_space_interval* lhost =
467  reinterpret_cast<const ij_adjacency_index_space_interval*>(_host);
468 
469  lhost->adjacent_zones(xlocal_id, _hub_begin, _i_ct, _j_ct);
470  _j_size = lhost->j_size();
471 
472  // Reset the iterator.
473 
474  reset();
475 
476  // Postconditions:
477 
478  ensure(invariant());
479  ensure(is_attached());
480  ensure(&host() == &xhost);
481  ensure(index() == xhost.family_scope(xlocal_id));
482 
483  // Exit:
484 
485  return;
486 }
487 
488 bool
491  pod_type xlocal_id) const
492 {
493  // Preconditions:
494 
495  // Body:
496 
497  bool result = (dynamic_cast<const ij_adjacency_index_space_interval*>(&xhost) != 0);
498 
499  // Postconditions:
500 
501  ensure(is_basic_query);
502 
503  // Exit:
504 
505  return result;
506 }
507 
508 // PROTECTED MEMBER FUNCTIONS
509 
510 // PRIVATE MAMBER FUNCTIONS
511 
512 
513 // ===========================================================
514 // ANY FACET
515 // ===========================================================
516 
517 // PUBLIC MEMBER FUNCTIONS
518 
519 bool
521 is_ancestor_of(const any *other) const
522 {
523  // Preconditions:
524 
525  require(other != 0);
526 
527  // Body:
528 
529  // True if other conforms to this
530 
531  bool result = dynamic_cast<const ij_adjacency_implicit_index_space_iterator*>(other) != 0;
532 
533  // Postconditions:
534 
535  // Exit:
536 
537  return result;
538 }
539 
540 bool
542 invariant() const
543 {
544  bool result = true;
545 
546  if(invariant_check())
547  {
548  // Must satisfy base class invariant
549 
551 
552  // Prevent recursive calls to invariant
553 
554  disable_invariant_check();
555 
556  // Invariances for this class:
557 
558  // Finished, turn invariant checking back on.
559 
560  enable_invariant_check();
561  }
562 
563  // Exit
564 
565  return result;
566 }
567 
568 // PROTECTED MEMBER FUNCTIONS
569 
570 // PRIVATE MEMBER FUNCTIONS
571 
572 
573 // ===========================================================
574 // NON-MEMBER FUNCTIONS
575 // ===========================================================
576 
577 
size_type j_ct() const
The number of adjacent zones in the j direction.
pod_type _hub_begin
The hub id of the first zone in the adjacency list.
An abstract iterator over the ids of an id space.
An implementation of index_space_interval for an interval of implicit id spaces for the adjacency of ...
void adjacent_zones(pod_type xlocal_id, pod_type &xzone_hub_begin, size_type &xi_ct, size_type &xj_ct) const
Compute the hub id of the fist adjacent zone and the number of adjacent zones in each direction for i...
ij_adjacency_implicit_index_space_iterator & operator=(const ij_adjacency_implicit_index_space_iterator &xother)
Assignment operator.
virtual pod_type family_scope(pod_type xlocal_id) const =0
Id relative to the id space family equivalent to the local id xlocal_id.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
Abstract base class with useful features for all objects.
Definition: any.h:39
An implementation of implicit_index_space_iterator for an implicit id space in an ij_adjacency_index_...
size_type j_size() const
The number of zones in the j direction.
virtual bool conforms_to_state(const index_space_collection &xhost, pod_type xlocal_id) const
True if this conforms to the handle type required by the state with local scope id xlocal_id in the h...
pod_type hub_begin() const
The hub id of the first zone in the adjacency list.
size_type _i_ct
The number of adjacent zones in the i direction.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
virtual void next()
Makes item() the next id in the iteration.
size_type j_size() const
The number of zones in the j direction.
A collection of id space states. This is a virtual class with provides an interface for accessing the...
virtual bool contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual bool invariant() const
Class invariant.
virtual ij_adjacency_implicit_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual bool is_attached() const
True if this iterator is attached to a state.
virtual void attach_to(const index_space_collection &xhost, pod_type xlocal_id)
Attach to the state with local scope id, xlocal_id in the host id space collection xhost...
Factory and container for a family of id spaces.
pod_index_type pod_type
The "plain old data" index type for this.
size_type _j_ct
The number of adjacent zones in the j direction.
Namespace for the fiber_bundles component of the sheaf system.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
size_type i_ct() const
The number of adjacent zones in the i direction.