SheafSystem  0.0.0.0
ijk_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/ijk_adjacency_implicit_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/ijk_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 // IJK_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  _k_size = xother._k_size;
132  _i_ct = xother._i_ct;
133  _j_ct = xother._j_ct;
134  _k_ct = xother._k_ct;
135  _i = xother._i;
136  _j = xother._j;
137  _k = xother._k;
138 
139  (void) implicit_index_space_iterator::operator=(xother);
140  }
141  else
142  {
143  detach();
144  }
145 
146  // Postconditions:
147 
148  ensure(invariant());
149  ensure((*this) == xother);
150 
151  // Exit
152 
153  return *this;
154 }
155 
158 {
159  // Preconditions:
160 
161  // Body:
162 
163  // Nothing to do;
164 
165  // Postconditions:
166 
167  // Exit:
168 
169  return;
170 }
171 
174 hub_begin() const
175 {
176  // Preconditions:
177 
178  // Body:
179 
180  // Postconditions:
181 
182  ensure(is_basic_query);
183 
184  // Exit:
185 
186  return _hub_begin;
187 }
188 
191 j_size() const
192 {
193  // Preconditions:
194 
195  // Body:
196 
197  // Postconditions:
198 
199  ensure(is_basic_query);
200 
201  // Exit:
202 
203  return _j_size;
204 }
205 
208 k_size() const
209 {
210  // Preconditions:
211 
212  // Body:
213 
214  // Postconditions:
215 
216  ensure(is_basic_query);
217 
218  // Exit:
219 
220  return _k_size;
221 }
222 
225 i_ct() const
226 {
227  // Preconditions:
228 
229  // Body:
230 
231  // Postconditions:
232 
233  ensure(is_basic_query);
234 
235  // Exit:
236 
237  return _i_ct;
238 }
239 
242 j_ct() const
243 {
244  // Preconditions:
245 
246  // Body:
247 
248  // Postconditions:
249 
250  ensure(is_basic_query);
251 
252  // Exit:
253 
254  return _j_ct;
255 }
256 
259 k_ct() const
260 {
261  // Preconditions:
262 
263  // Body:
264 
265  // Postconditions:
266 
267  ensure(is_basic_query);
268 
269  // Exit:
270 
271  return _k_ct;
272 }
273 
274 // PROTECTED MEMBER FUNCTIONS
275 
276 // PRIVATE MEMBER FUNCTIONS
277 
278 
279 // ===========================================================
280 // IMPLICIT_INDEX_SPACE_ITERATOR FACET
281 // ===========================================================
282 
283 // PUBLIC MEMBER FUNCTIONS
284 
285 // PROTECTED MEMBER FUNCTIONS
286 
287 // PRIVATE MAMBER FUNCTIONS
288 
289 
290 // ===========================================================
291 // INDEX_SPACE_ITERATOR FACET
292 // ===========================================================
293 
294 // PUBLIC MEMBER FUNCTIONS
295 
299 {
300  // Preconditions:
301 
302  require(is_ancestor_of(&xother));
303 
304  // Body:
305 
307  dynamic_cast<const ijk_adjacency_implicit_index_space_iterator&>(xother);
308 
309  (void) this->operator=(lother);
310 
311  // Postconditions:
312 
313  ensure(invariant());
314  ensure((*this) == xother);
315 
316  // Exit
317 
318  return *this;
319 }
320 
321 bool
323 operator==(const index_space_iterator& xother) const
324 {
325  // Preconditions:
326 
327  require(is_ancestor_of(&xother));
328 
329  // Body:
330 
331  bool result = implicit_index_space_iterator::operator==(xother);
332  if(result && is_attached())
333  {
335  dynamic_cast<const ijk_adjacency_implicit_index_space_iterator&>(xother);
336 
337  result = result && (_hub_begin == lother._hub_begin);
338  result = result && (_j_size == lother._j_size);
339  result = result && (_k_size == lother._k_size);
340  result = result && (_i_ct == lother._i_ct);
341  result = result && (_j_ct == lother._j_ct);
342  result = result && (_k_ct == lother._k_ct);
343  result = result && (_i == lother._i);
344  result = result && (_j == lother._j);
345  result = result && (_k == lother._k);
346  }
347 
348  // Postconditions:
349 
350  ensure(invariant());
351 
352  // Exit
353 
354  return result;
355 }
356 
359 clone() const
360 {
362 
363  // Preconditions:
364 
365  // Body:
366 
368 
369  // Postconditions:
370 
371  ensure(result != 0);
372  ensure(is_same_type(result));
373  ensure(*result == *this);
374 
375  // Exit:
376 
377  return result;
378 }
379 
380 // PROTECTED MEMBER FUNCTIONS
381 
382 // PRIVATE MAMBER FUNCTIONS
383 
384 
385 // ===========================================================
386 // ITERATOR FACET
387 // ===========================================================
388 
389 // PUBLIC MEMBER FUNCTIONS
390 
391 void
394 {
395  // Preconditions:
396 
397  require(is_attached());
398  require(!is_done());
399 
400  // Body:
401 
402  define_old_variable(pod_type old_pod = pod());
403  define_old_variable(pod_type old_hub_pod = hub_pod());
404 
405  // Increment to the next id.
406 
407  _k++;
408  if(_k == _k_ct)
409  {
410  // Done with k-iteration, try incrementing in the j-direction.
411 
412  _k = 0;
413  _j++;
414  if(_j == _j_ct)
415  {
416  // Done with the j-iteration, try incrementing in the i-direction.
417 
418  _j = 0;
419  _i++;
420 
421  if(_i == _i_ct)
422  {
423  // The iteration is done.
424 
425  invalidate_ids();
426  }
427  else
428  {
429  // The iteration is not done, increment the ids in the i-direction.
430 
431  ++_pod;
432  _hub_pod = ((_i * _j_size) + _j)*_k_size + _hub_begin;
433  }
434  }
435  else
436  {
437  // The iteration is not done, increment the ids in the j-direction.
438 
439  ++_pod;
440  _hub_pod = ((_i * _j_size) + _j)*_k_size + _hub_begin;
441  }
442  }
443  else
444  {
445  // The iteration is not done, increment the ids in the k-direction.
446 
447  ++_pod;
448  ++_hub_pod;
449  }
450 
451  // Postconditions:
452 
453  ensure(invariant());
454  ensure(is_done() || pod() != old_pod);
455  ensure(is_done() || hub_pod() != old_hub_pod);
456 
457  // Exit:
458 
459  return;
460 }
461 
462 void
465 {
466  // Preconditions:
467 
468  require(is_attached());
469 
470  // Body:
471 
472  _is_done = false;
473 
474  // Reset to first adjacent zone.
475 
476  _pod = 0;
477  _hub_pod = _hub_begin;
478 
479  _i = 0;
480  _j = 0;
481  _k = 0;
482 
483  // Postconditions:
484 
485  ensure(invariant());
486 
487  // Exit:
488 
489  return;
490 }
491 
492 // PROTECTED MEMBER FUNCTIONS
493 
494 // PRIVATE MEMBER FUNCTIONS
495 
496 
497 // ===========================================================
498 // HANDLE FACET
499 // ===========================================================
500 
501 // PUBLIC MEMBER FUNCTIONS
502 
503 void
505 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
506 {
507  // Preconditions:
508 
509  require(xhost.contains(xlocal_id));
510  require(conforms_to_state(xhost, xlocal_id));
511 
512  // Body:
513 
514  // Assign the state information.
515 
516  _host = &xhost;
517  _local_id = xlocal_id;
518 
519  // Find the section of the ijk_adjacency that represents this id space.
520 
522  reinterpret_cast<const ijk_adjacency_index_space_interval*>(_host);
523 
524  lhost->adjacent_zones(xlocal_id, _hub_begin, _i_ct, _j_ct, _k_ct);
525  _j_size = lhost->j_size();
526  _k_size = lhost->k_size();
527 
528  // Reset the iterator.
529 
530  reset();
531 
532  // Postconditions:
533 
534  ensure(invariant());
535  ensure(is_attached());
536  ensure(&host() == &xhost);
537  ensure(index() == xhost.family_scope(xlocal_id));
538 
539  // Exit:
540 
541  return;
542 }
543 
544 bool
547  pod_type xlocal_id) const
548 {
549  // Preconditions:
550 
551  // Body:
552 
553  bool result = (dynamic_cast<const ijk_adjacency_index_space_interval*>(&xhost) != 0);
554 
555  // Postconditions:
556 
557  ensure(is_basic_query);
558 
559  // Exit:
560 
561  return result;
562 }
563 
564 // PROTECTED MEMBER FUNCTIONS
565 
566 // PRIVATE MAMBER FUNCTIONS
567 
568 
569 // ===========================================================
570 // ANY FACET
571 // ===========================================================
572 
573 // PUBLIC MEMBER FUNCTIONS
574 
575 bool
577 is_ancestor_of(const any *other) const
578 {
579  // Preconditions:
580 
581  require(other != 0);
582 
583  // Body:
584 
585  // True if other conforms to this
586 
587  bool result = dynamic_cast<const ijk_adjacency_implicit_index_space_iterator*>(other) != 0;
588 
589  // Postconditions:
590 
591  // Exit:
592 
593  return result;
594 }
595 
596 bool
598 invariant() const
599 {
600  bool result = true;
601 
602  if(invariant_check())
603  {
604  // Must satisfy base class invariant
605 
607 
608  // Prevent recursive calls to invariant
609 
610  disable_invariant_check();
611 
612  // Invariances for this class:
613 
614  // Finished, turn invariant checking back on.
615 
616  enable_invariant_check();
617  }
618 
619  // Exit
620 
621  return result;
622 }
623 
624 // PROTECTED MEMBER FUNCTIONS
625 
626 // PRIVATE MEMBER FUNCTIONS
627 
628 
629 // ===========================================================
630 // NON-MEMBER FUNCTIONS
631 // ===========================================================
size_type j_size() const
The number of zones in the j direction.
pod_type hub_begin() const
The hub id of the first zone in the adjacency list.
An abstract iterator over the ids of an id space.
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...
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
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.
void adjacent_zones(pod_type xlocal_id, pod_type &xzone_hub_begin, size_type &xi_ct, size_type &xj_ct, size_type &xk_ct) const
Compute the hub id of the fist adjacent zone and the number of adjacent zones in each direction for i...
size_type j_ct() const
The number of adjacent zones in the j direction.
ijk_adjacency_implicit_index_space_iterator & operator=(const ijk_adjacency_implicit_index_space_iterator &xother)
Assignment operator.
An implementation of implicit_index_space_iterator for an implicit id space in an ijk_adjacency_index...
size_type k_size() const
The number of zones in the k direction.
Abstract base class with useful features for all objects.
Definition: any.h:39
size_type _i_ct
The number of adjacent zones in the i direction.
virtual ijk_adjacency_implicit_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
size_type _k_ct
The number of adjacent zones in the k direction.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
size_type i_ct() const
The number of adjacent zones in the i direction.
size_type _j_ct
The number of adjacent zones in the j direction.
An implementation of index_space_interval for an interval of implicit id spaces for the adjacency of ...
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 operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
pod_type _hub_begin
The hub id of the first zone in the adjacency list.
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...
virtual bool invariant() const
Class invariant.
virtual bool is_attached() const
True if this iterator is attached to a state.
Factory and container for a family of id spaces.
pod_index_type pod_type
The "plain old data" index type for this.
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 k_ct() const
The number of adjacent zones in the k direction.