SheafSystem  0.0.0.0
i_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/i_adjacency_implicit_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/i_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 // I_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  _i_ct = xother._i_ct;
131  _i = xother._i;
132 
133  (void) implicit_index_space_iterator::operator=(xother);
134  }
135  else
136  {
137  detach();
138  }
139 
140  // Postconditions:
141 
142  ensure(invariant());
143  ensure((*this) == xother);
144 
145  // Exit
146 
147  return *this;
148 }
149 
152 {
153  // Preconditions:
154 
155  // Body:
156 
157  // Nothing to do;
158 
159  // Postconditions:
160 
161  // Exit:
162 
163  return;
164 }
165 
168 hub_begin() const
169 {
170  // Preconditions:
171 
172  // Body:
173 
174  // Postconditions:
175 
176  ensure(is_basic_query);
177 
178  // Exit:
179 
180  return _hub_begin;
181 }
182 
185 i_ct() const
186 {
187  // Preconditions:
188 
189  // Body:
190 
191  // Postconditions:
192 
193  ensure(is_basic_query);
194 
195  // Exit:
196 
197  return _i_ct;
198 }
199 
200 // PROTECTED MEMBER FUNCTIONS
201 
202 // PRIVATE MEMBER FUNCTIONS
203 
204 
205 // ===========================================================
206 // IMPLICIT_INDEX_SPACE_ITERATOR FACET
207 // ===========================================================
208 
209 // PUBLIC MEMBER FUNCTIONS
210 
211 // PROTECTED MEMBER FUNCTIONS
212 
213 // PRIVATE MAMBER FUNCTIONS
214 
215 
216 // ===========================================================
217 // INDEX_SPACE_ITERATOR FACET
218 // ===========================================================
219 
220 // PUBLIC MEMBER FUNCTIONS
221 
225 {
226  // Preconditions:
227 
228  require(is_ancestor_of(&xother));
229 
230  // Body:
231 
233  dynamic_cast<const i_adjacency_implicit_index_space_iterator&>(xother);
234 
235  (void) this->operator=(lother);
236 
237  // Postconditions:
238 
239  ensure(invariant());
240  ensure((*this) == xother);
241 
242  // Exit
243 
244  return *this;
245 }
246 
247 bool
249 operator==(const index_space_iterator& xother) const
250 {
251  // Preconditions:
252 
253  require(is_ancestor_of(&xother));
254 
255  // Body:
256 
257  bool result = implicit_index_space_iterator::operator==(xother);
258  if(result && is_attached())
259  {
261  dynamic_cast<const i_adjacency_implicit_index_space_iterator&>(xother);
262 
263  result = result && (_hub_begin == lother._hub_begin);
264  result = result && (_i_ct == lother._i_ct);
265  result = result && (_i == lother._i);
266  }
267 
268  // Postconditions:
269 
270  ensure(invariant());
271 
272  // Exit
273 
274  return result;
275 }
276 
279 clone() const
280 {
282 
283  // Preconditions:
284 
285  // Body:
286 
287  result = new i_adjacency_implicit_index_space_iterator(*this);
288 
289  // Postconditions:
290 
291  ensure(result != 0);
292  ensure(is_same_type(result));
293  ensure(*result == *this);
294 
295  // Exit:
296 
297  return result;
298 }
299 
300 // PROTECTED MEMBER FUNCTIONS
301 
302 // PRIVATE MAMBER FUNCTIONS
303 
304 
305 // ===========================================================
306 // ITERATOR FACET
307 // ===========================================================
308 
309 // PUBLIC MEMBER FUNCTIONS
310 
311 void
314 {
315  // Preconditions:
316 
317  require(is_attached());
318  require(!is_done());
319 
320  // Body:
321 
322  define_old_variable(pod_type old_pod = pod());
323  define_old_variable(pod_type old_hub_pod = hub_pod());
324 
325  // Increment to the next id.
326 
327  _i++;
328 
329  if(_i == _i_ct)
330  {
331  // The iteration is done.
332 
333  invalidate_ids();
334  }
335  else
336  {
337  // The iteration is not done, increment the ids.
338 
339  ++_pod;
340  ++_hub_pod;
341  }
342 
343  // Postconditions:
344 
345  ensure(invariant());
346  ensure(is_done() || pod() != old_pod);
347  ensure(is_done() || hub_pod() != old_hub_pod);
348 
349  // Exit:
350 
351  return;
352 }
353 
354 void
357 {
358  // Preconditions:
359 
360  require(is_attached());
361 
362  // Body:
363 
364  _is_done = false;
365 
366  // Reset to first adjacent zone.
367 
368  _pod = 0;
369  _hub_pod = _hub_begin;
370 
371  _i = 0;
372 
373  // Postconditions:
374 
375  ensure(invariant());
376 
377  // Exit:
378 
379  return;
380 }
381 
382 // PROTECTED MEMBER FUNCTIONS
383 
384 // PRIVATE MEMBER FUNCTIONS
385 
386 
387 // ===========================================================
388 // HANDLE FACET
389 // ===========================================================
390 
391 // PUBLIC MEMBER FUNCTIONS
392 
393 void
395 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
396 {
397  // Preconditions:
398 
399  require(xhost.contains(xlocal_id));
400  require(conforms_to_state(xhost, xlocal_id));
401 
402  // Body:
403 
404  // Assign the state information.
405 
406  _host = &xhost;
407  _local_id = xlocal_id;
408 
409  // Find the section of the i_adjacency that represents this id space.
410 
411  const i_adjacency_index_space_interval* lhost =
412  reinterpret_cast<const i_adjacency_index_space_interval*>(_host);
413 
414  lhost->adjacent_zones(xlocal_id, _hub_begin, _i_ct);
415 
416  // Reset the iterator.
417 
418  reset();
419 
420  // Postconditions:
421 
422  ensure(invariant());
423  ensure(is_attached());
424  ensure(&host() == &xhost);
425  ensure(index() == xhost.family_scope(xlocal_id));
426 
427  // Exit:
428 
429  return;
430 }
431 
432 bool
435  pod_type xlocal_id) const
436 {
437  // Preconditions:
438 
439  // Body:
440 
441  bool result = (dynamic_cast<const i_adjacency_index_space_interval*>(&xhost) != 0);
442 
443  // Postconditions:
444 
445  ensure(is_basic_query);
446 
447  // Exit:
448 
449  return result;
450 }
451 
452 // PROTECTED MEMBER FUNCTIONS
453 
454 // PRIVATE MAMBER FUNCTIONS
455 
456 
457 // ===========================================================
458 // ANY FACET
459 // ===========================================================
460 
461 // PUBLIC MEMBER FUNCTIONS
462 
463 bool
465 is_ancestor_of(const any *other) const
466 {
467  // Preconditions:
468 
469  require(other != 0);
470 
471  // Body:
472 
473  // True if other conforms to this
474 
475  bool result = dynamic_cast<const i_adjacency_implicit_index_space_iterator*>(other) != 0;
476 
477  // Postconditions:
478 
479  // Exit:
480 
481  return result;
482 }
483 
484 bool
486 invariant() const
487 {
488  bool result = true;
489 
490  if(invariant_check())
491  {
492  // Must satisfy base class invariant
493 
495 
496  // Prevent recursive calls to invariant
497 
498  disable_invariant_check();
499 
500  // Invariances for this class:
501 
502  // Finished, turn invariant checking back on.
503 
504  enable_invariant_check();
505  }
506 
507  // Exit
508 
509  return result;
510 }
511 
512 // PROTECTED MEMBER FUNCTIONS
513 
514 // PRIVATE MEMBER FUNCTIONS
515 
516 
517 // ===========================================================
518 // NON-MEMBER FUNCTIONS
519 // ===========================================================
520 
521 
virtual i_adjacency_implicit_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
An abstract iterator over the ids of an id space.
An implementation of implicit_index_space_iterator for an implicit id space in an i_adjacency_index_s...
pod_type _hub_begin
The hub id of the first zone in the adjacency list.
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.
An implementation of index_space_interval for an interval of implicit id spaces for the adjacency of ...
Abstract base class with useful features for all objects.
Definition: any.h:39
i_adjacency_implicit_index_space_iterator & operator=(const i_adjacency_implicit_index_space_iterator &xother)
Assignment operator.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
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 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 operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
size_type _i_ct
The number of adjacent zones in the i 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 bool is_attached() const
True if this iterator is attached to a state.
pod_type hub_begin() const
The hub id of the first zone in the adjacency list.
virtual void next()
Makes item() the next id in the iteration.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
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.
void adjacent_zones(pod_type xlocal_id, pod_type &xzone_hub_begin, size_type &xi_ct) const
Compute the hub id of the fist adjacent zone and the number of adjacent zones in each direction for i...
size_type i_ct() const
The number of adjacent zones in the i direction.