SheafSystem  0.0.0.0
index_space_family_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/index_space_family_iterator.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/hub_index_space_handle.h"
24 #include "SheafSystem/index_space_interval.h"
25 #include "SheafSystem/index_space_iterator.h"
26 
27 // ===========================================================
28 // INDEX_SPACE_FAMILY_ITERATOR FACET
29 // ===========================================================
30 
31 // PUBLIC MEMBER FUNCTIONS
32 
35  bool xonly_persistent)
36  : _id_spaces(&xid_spaces),
37  _id_space_itr(xonly_persistent)
38 {
39  // Preconditions:
40 
41  // Body:
42 
43  reset();
44 
45  // Postconditions:
46 
47  ensure(invariant());
48  ensure(&id_spaces() == &xid_spaces);
49  ensure(only_persistent() == xonly_persistent);
50 
51  // Exit:
52 
53  return;
54 }
55 
58 {
59  // Preconditions:
60 
61  // Body:
62 
63  // nothing to do.
64 
65  // Postconditions:
66 
67  // Exit:
68 
69  return;
70 }
71 
75 {
76  // Preconditions:
77 
78  require(is_ancestor_of(&xother));
79 
80  // Body:
81 
82  _id_spaces = xother._id_spaces;
85 
86  // Postconditions:
87 
88  ensure(invariant());
89  ensure((*this) == xother);
90 
91  // Exit
92 
93  return *this;
94 }
95 
96 bool
99 {
100  // Preconditions:
101 
102  require(is_ancestor_of(&xother));
103 
104  // Body:
105 
106  bool result = (_id_spaces == xother._id_spaces);
107  result = result && (_interval_itr == xother._interval_itr);
108  result = result && (_id_space_itr == xother._id_space_itr);
109 
110  // Postconditions:
111 
112  ensure(is_basic_query);
113 
114  // Exit
115 
116  return result;
117 }
118 
121 clone() const
122 {
123  // Preconditions:
124 
125  // Body:
126 
128  new index_space_family_iterator(*this);
129 
130  // Postconditions:
131 
132  ensure(result != 0);
133  ensure(is_same_type(result));
134  ensure(*result == *this);
135 
136  // Exit:
137 
138  return result;
139 }
140 
141 bool
144 {
145  // Preconditions:
146 
147  // Body:
148 
149  bool result = _id_space_itr.only_persistent();
150 
151  // Postconditions:
152 
153  ensure(is_basic_query);
154 
155  // Exit:
156 
157  return result;
158 }
159 
160 void
163 {
164  // Preconditions:
165 
166  require(!is_done());
167 
168  // Body:
169 
170  define_old_variable(pod_type old_index = index());
171 
172  // Increment to the next id space.
173 
175  next_id_space();
176 
177  while(_id_space_itr.is_done())
178  {
179  // Done with the current id space interval, move to the next one.
180 
181  ++_interval_itr;
182  if(_interval_itr == _id_spaces->_intervals.end())
183  {
184  // The iteration is done.
185 
187  break;
188  }
189  else if((_interval_itr->second != 0) &&
190  (!only_persistent() ||
191  _interval_itr->second->supports_persistent_id_spaces()))
192  {
193  // Attach the interval iterator to the current interval.
194 
196 
197  // Increment to the next id space.
198 
199  next_id_space();
200  }
201  }
202 
203  // Postconditions:
204 
205  ensure(invariant());
206  ensure(is_done() || index() != old_index);
207 
208  // Exit:
209 
210  return;
211 }
212 
213 bool
215 is_done() const
216 {
217  // Preconditions:
218 
219  // Body:
220 
221  bool result = !_id_space_itr.is_attached() || _id_space_itr.is_done();
222 
223  // Postconditions:
224 
225  ensure(is_basic_query);
226 
227  // Exit:
228 
229  return result;
230 }
231 
232 void
235 {
236  // Preconditions:
237 
238  // Body:
239 
242 
243  // The first entry should always be null.
244 
245  assertion(_interval_itr != _id_spaces->_intervals.end());
246  assertion(_interval_itr->second == 0);
247 
248  // Increment to the first interval.
249 
250  do
251  {
252  ++_interval_itr;
253 
254  if(_interval_itr == _id_spaces->_intervals.end())
255  {
256  // The iteration is done.
257 
258  break;
259  }
260  else if((_interval_itr->second != 0) &&
261  (!only_persistent() ||
262  _interval_itr->second->supports_persistent_id_spaces()))
263  {
264  // Attach the interval iterator to the current interval.
265 
267 
268  // Increment to the next id space.
269 
270  next_id_space();
271 
272  if(!_id_space_itr.is_done())
273  {
274  // An id space was found, done searching.
275 
276  break;
277  }
278  }
279  }
280  while(_interval_itr != _id_spaces->_intervals.end());
281 
282  // Postconditions:
283 
284  ensure(invariant());
285 
286  // Exit:
287 
288  return;
289 }
290 
293 host() const
294 {
295  // Preconditions:
296 
297  require(!is_done());
298 
299  // Body:
300 
301  const index_space_interval& result = _id_space_itr.host();
302 
303  // Postconditions:
304 
305  ensure(is_basic_query);
306 
307  // Exit:
308 
309  return result;
310 }
311 
314 index() const
315 {
316  // Preconditions:
317 
318  require(!is_done());
319 
320  // Body:
321 
322  pod_type result = _id_space_itr.index();
323 
324  // Postconditions:
325 
326  ensure(is_valid(result));
327 
328  // Exit:
329 
330  return result;
331 }
332 
333 std::string
335 name() const
336 {
337  // Preconditions:
338 
339  require(!is_done());
340 
341  // Body:
342 
343  std::string result = _id_spaces->name(index());
344 
345  // Postconditions:
346 
347  ensure(is_basic_query);
348 
349  // Exit:
350 
351  return result;
352 }
353 
354 // PROTECTED MEMBER FUNCTIONS
355 
358 {
359  // Preconditions:
360 
361  // Body:
362 
363  (*this) = xother;
364 
365  // Postconditions:
366 
367  ensure(invariant());
368  ensure((*this) == xother);
369 
370  // Exit:
371 
372  return;
373 }
374 
375 void
378 {
379  // Preconditions:
380 
381  require(_id_space_itr.is_attached());
382 
383  // Body:
384 
385  if(only_persistent())
386  {
387  // Find the first persistent id space.
388 
389  while(!_id_space_itr.is_done())
390  {
391  if(is_persistent())
392  {
393  // Found persistent id space, search is done.
394 
395  break;
396  }
397 
399  }
400  }
401 
402  // Postconditions:
403 
404  ensure(_id_space_itr.is_done() || !only_persistent() || is_persistent());
405 
406  // Exit:
407 
408  return;
409 }
410 
411 // PRIVATE MEMBER FUNCTIONS
412 
413 
414 // ===========================================================
415 // INDEX SPACE FACET
416 // ===========================================================
417 
418 // PUBLIC MEMBER FUNCTIONS
419 
422 id_spaces() const
423 {
424  // Preconditions:
425 
426  // Body:
427 
428  const index_space_family& result = *_id_spaces;
429 
430  // Postconditions:
431 
432  ensure(is_basic_query);
433 
434  // Exit:
435 
436  return result;
437 }
438 
442 {
443  // Preconditions:
444 
445  // Body:
446 
448 
449  // Postconditions:
450 
451  ensure(is_basic_query);
452 
453  // Exit:
454 
455  return result;
456 }
457 
460 ct() const
461 {
462  // Preconditions:
463 
464  require(!is_done());
465 
466  // Body:
467 
468  size_type result = _id_space_itr.ct();
469 
470  // Postconditions:
471 
472  ensure(is_basic_query);
473 
474  // Exit:
475 
476  return result;
477 }
478 
481 begin() const
482 {
483  // Preconditions:
484 
485  require(!is_done());
486 
487  // Body:
488 
489  pod_type result = _id_space_itr.begin();
490 
491  // Postconditions:
492 
493  ensure(is_basic_query);
494 
495  // Exit:
496 
497  return result;
498 }
499 
502 end() const
503 {
504  // Preconditions:
505 
506  require(!is_done());
507 
508  // Body:
509 
510  pod_type result = _id_space_itr.end();
511 
512  // Postconditions:
513 
514  ensure(is_basic_query);
515 
516  // Exit:
517 
518  return result;
519 }
520 
521 bool
523 contains(pod_type xid) const
524 {
525  // Preconditions:
526 
527  require(!is_done());
528 
529  // Body:
530 
531  bool result = _id_space_itr.contains(xid);
532 
533  // Postconditions:
534 
535  ensure(is_basic_query);
536 
537  // Exit:
538 
539  return result;
540 }
541 
542 bool
545 {
546  // Preconditions:
547 
548  require(!is_done());
549 
550  // Body:
551 
552  bool result = _id_space_itr.contains_hub(xid);
553 
554  // Postconditions:
555 
556  ensure(is_basic_query);
557 
558  // Exit:
559 
560  return result;
561 }
562 
563 bool
566 {
567  // Preconditions:
568 
569  require(!is_done());
570 
571  // Body:
572 
573  bool result = _id_space_itr.contains_unglued_hub(xid);
574 
575  // Postconditions:
576 
577  ensure(is_basic_query);
578 
579  // Exit:
580 
581  return result;
582 }
583 
584 bool
587 {
588  // Preconditions:
589 
590  require(!is_done());
591 
592  // Body:
593 
594  bool result = _id_space_itr.contains_glued_hub(xid);
595 
596  // Postconditions:
597 
598  ensure(is_basic_query);
599 
600  // Exit:
601 
602  return result;
603 }
604 
605 bool
607 contains(pod_type xid, pod_type xhub_id) const
608 {
609  // Preconditions:
610 
611  require(!is_done());
612 
613  // Body:
614 
615  bool result = _id_space_itr.contains(xid, xhub_id);
616 
617  // Postconditions:
618 
619  ensure(is_basic_query);
620 
621  // Exit:
622 
623  return result;
624 }
625 
628 pod(pod_type xid) const
629 {
630  // Preconditions:
631 
632  require(!is_done());
633 
634  // Body:
635 
636  pod_type result = _id_space_itr.pod(xid);
637 
638  // Postconditions:
639 
640  ensure(!is_valid(result) || contains(result));
641 
642  // Exit:
643 
644  return result;
645 }
646 
649 hub_pod(pod_type xid) const
650 {
651  // Preconditions:
652 
653  require(!is_done());
654 
655  // Body:
656 
657  pod_type result = _id_space_itr.hub_pod(xid);
658 
659  // Postconditions:
660 
661  ensure(!is_valid(result) || contains_hub(result));
662 
663  // Exit:
664 
665  return result;
666 }
667 
671 {
672  // Preconditions:
673 
674  require(!is_done());
675 
676  // Body:
677 
678  pod_type result = _id_space_itr.unglued_hub_pod(xid);
679 
680  // Postconditions:
681 
682  ensure(!is_valid(result) || contains_unglued_hub(result));
683 
684  // Exit:
685 
686  return result;
687 }
688 
692 {
693  // Preconditions:
694 
695  require(!is_done());
696 
697  // Body:
698 
699  pod_type result = _id_space_itr.glued_hub_pod(xid);
700 
701  // Postconditions:
702 
703  ensure(!is_valid(result) || contains_glued_hub(result));
704 
705  // Exit:
706 
707  return result;
708 }
709 
710 bool
713 {
714  // Preconditions:
715 
716  require(!is_done());
717 
718  // Body:
719 
720  bool result = _id_space_itr.is_persistent();
721 
722  // Postconditions:
723 
724  ensure(is_basic_query);
725 
726  // Exit:
727 
728  return result;
729 }
730 
731 // PROTECTED MEMBER FUNCTIONS
732 
733 // PRIVATE MEMBER FUNCTIONS
734 
735 
736 // ===========================================================
737 // HANDLE POOL FACET
738 // ===========================================================
739 
740 // PUBLIC MEMBER FUNCTIONS
741 
745 {
746  // Preconditions:
747 
748  require(!is_done());
749 
750  // Body:
751 
753 
754  // Postconditions:
755 
756  ensure(result.is_attached());
757 
758  // Exit:
759 
760  return result;
761 }
762 
763 void
766 {
767  // Preconditions:
768 
769  require(!is_done());
770  require(allocated_id_space(xid_space));
771 
772  // Body:
773 
774  _id_space_itr.release_id_space(xid_space);
775 
776  // Postconditions:
777 
778  ensure(is_basic_query);
779 
780  // Exit:
781 
782  return;
783 }
784 
785 bool
788 {
789  // Preconditions:
790 
791  require(!is_done());
792 
793  // Body:
794 
795  bool result =_id_space_itr.allocated_id_space(xid_space);
796 
797  // Postconditions:
798 
799  ensure(is_basic_query);
800 
801  // Exit:
802 
803  return result;
804 }
805 
806 // PROTECTED MEMBER FUNCTIONS
807 
808 // PRIVATE MEMBER FUNCTIONS
809 
810 
811 // ===========================================================
812 // ITERATOR POOL FACET
813 // ===========================================================
814 
815 // PUBLIC MEMBER FUNCTIONS
816 
820 {
821  // Preconditions:
822 
823  require(!is_done());
824 
825  // Body:
826 
828 
829  // Postconditions:
830 
831  ensure(result.is_attached());
832 
833  // Exit:
834 
835  return result;
836 }
837 
838 void
841 {
842  // Preconditions:
843 
844  require(!is_done());
845  require(allocated_id_space_iterator(xitr));
846 
847  // Body:
848 
850 
851  // Postconditions:
852 
853  ensure(is_basic_query);
854 
855  // Exit:
856 
857  return;
858 }
859 
860 bool
863 {
864  // Preconditions:
865 
866  require(!is_done());
867 
868  // Body:
869 
870  bool result = _id_space_itr.allocated_id_space_iterator(xitr);
871 
872  // Postconditions:
873 
874  ensure(is_basic_query);
875 
876  // Exit:
877 
878  return result;
879 }
880 
881 // PROTECTED MEMBER FUNCTIONS
882 
883 // PRIVATE MEMBER FUNCTIONS
884 
885 
886 // ===========================================================
887 // ANY FACET
888 // ===========================================================
889 
890 // PUBLIC MEMBER FUNCTIONS
891 
892 bool
894 is_ancestor_of(const any *other) const
895 {
896  // Preconditions:
897 
898  require(other != 0);
899 
900  // Body:
901 
902  // True if other conforms to this
903 
904  bool result = dynamic_cast<const index_space_family_iterator*>(other) != 0;
905 
906  // Postconditions:
907 
908  // Exit:
909 
910  return result;
911 }
912 
913 bool
915 invariant() const
916 {
917  bool result = true;
918 
919  if(invariant_check())
920  {
921  // Prevent recursive calls to invariant
922 
924 
925  // Must satisfy base class invariant
926 
927  invariance(any::invariant());
928 
929  // Invariances for this class:
930 
931  invariance(is_done() || is_valid(index()));
932 
933  // Finished, turn invariant checking back on.
934 
936  }
937 
938  // Exit
939 
940  return result;
941 }
942 
943 // PROTECTED MEMBER FUNCTIONS
944 
945 // PRIVATE MEMBER FUNCTIONS
946 
947 
948 // ===========================================================
949 // NON-MEMBER FUNCTIONS
950 // ===========================================================
951 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
const index_space_family * _id_spaces
The index space family.
bool contains_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
void release_id_space_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
pod_type hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space.
std::string name(pod_type xid) const
The name for id xid.
bool contains_unglued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
pod_type hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space.
An abstract iterator over the ids of an id space.
bool is_attached() const
True, if this iterator is attached.
void next()
Makes id_space() the next id space in the iteration.
pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
index_space_family_iterator()
Default constructor; disabled.
bool contains_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
pod_type end() const
Ending id of this space.
bool allocated_id_space(index_space_handle &xid_space) const
True if and only if id space handle xid_space was allocated by the handle pool.
virtual bool invariant() const
Class invariant.
index_space_iterator & get_id_space_iterator() const
Allocates an id space iterator from the iterator pool attached to the current id space state...
pod_type unglued_hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space.
pod_type begin() const
Beginning id of this space.
virtual bool is_attached() const =0
True if this handle is attached to a state.
An implementation of class sum_index_space_handle that has a primary sum id space state...
pod_type glued_hub_pod(pod_type xid) const
The pod index in the glued hub id space equivalent to xid in this id space.
bool is_persistent() const
True if this id space should be written to disk.
index_space_family_iterator & operator=(const index_space_family_iterator &xother)
Assignment operator.
pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
pod_type end() const
Ending id of this space.
bool is_done() const
True if iteration is finished.
An abstract iterator over the id spaces of a interval.
Abstract base class with useful features for all objects.
Definition: any.h:39
bool contains(pod_type xid) const
True if this space contains id xid.
An implemenation of index_space_collection that adds an interface for the interval [begin()...
index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool attached to the current id space state...
bool contains_unglued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
void release_id_space_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
pod_type unglued_hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
bool contains_glued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the glued hub id space.
pod_type glued_hub_pod(pod_type xid) const
The pod index in the glued hub id space equivalent to xid in this id space.
pod_index_type pod_type
The "plain old data" index type for this.
bool only_persistent() const
True if only the persistent id spaces are iterated over.
std::string name() const
The name of the current id space in the iteration.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
size_type ct() const
The number of members.
const index_space_family & id_spaces() const
The id space family for this (const version).
interval_map_type _intervals
Map from id to id space interval.
bool only_persistent() const
True if only the persistent id spaces are iterated over.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
bool is_persistent() const
True if this id space should be written to disk.
bool operator==(const index_space_family_iterator &xother) const
True if this is equivalent to xother.
index_space_interval_iterator _id_space_itr
The iterator over id spaces for the current id space interval.
pod_type index() const
The index of the current id space in the iteration.
void attach_to(index_space_interval &xhost)
Attach this iterator to the host xhost.
const index_space_interval & host() const
The host id space interval being iterated over.
pod_type begin() const
Beginning id of this space.
void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
virtual bool is_attached() const =0
True if this iterator is attached to a state.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
void next()
Makes id_space() the next id space in the iteration.
size_type ct() const
The number of members.
index_space_iterator & get_id_space_iterator() const
Allocates an id space iterator from the iterator pool attached to the current id space state...
const_interval_iterator_type _interval_itr
The stl iterator over the intervals.
pod_type index() const
The index of the current id space in the iteration.
bool allocated_id_space(index_space_handle &xid_space) const
True if and only if id space handle xid_space was allocated by the handle pool.
bool contains_glued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the glued hub id space.
index_space_family_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
Factory and container for a family of id spaces.
bool is_done() const
True if iteration is finished.
const index_space_interval & host() const
The current host id space interval being iterated over.
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
const hub_index_space_handle & hub_id_space() const
The hub id space.
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
void next_id_space()
Increment to the next id space in the interval.
bool allocated_id_space_iterator(index_space_iterator &xitr) const
True if and only if id space iterator xitr was allocated by the iterator pool.
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool attached to the current id space state...
bool contains(pod_type xid) const
True if this space contains id xid.
bool allocated_id_space_iterator(index_space_iterator &xitr) const
True if and only if id space iterator xitr was allocated by the iterator pool.
pod_index_type pod_type
The "plain old data" index type for this.