SheafSystem  0.0.0.0
index_space_interval_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_interval_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_family.h"
26 #include "SheafSystem/index_space_iterator.h"
27 
28 // ===========================================================
29 // INDEX_SPACE_INTERVAL_ITERATOR FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  // nothing to do.
42 
43  // Postconditions:
44 
45  // Exit:
46 
47  return;
48 }
49 
53 {
54  // Preconditions:
55 
56  require(is_ancestor_of(&xother));
57 
58  // Body:
59 
60  _host = xother._host;
62  _ub = xother._ub;
63  _local_id = xother._local_id;
64 
65  // Postconditions:
66 
67  ensure(invariant());
68  ensure((*this) == xother);
69 
70  // Exit
71 
72  return *this;
73 }
74 
75 bool
78 {
79  // Preconditions:
80 
81  require(is_ancestor_of(&xother));
82 
83  // Body:
84 
85  bool result = (_host == xother._host);
86  result = result && (_only_persistent == xother._only_persistent);
87  result = result && (_ub == xother._ub);
88  result = result && (_local_id == xother._local_id);
89 
90  // Postconditions:
91 
92  ensure(is_basic_query);
93 
94  // Exit
95 
96  return result;
97 }
98 
101 clone() const
102 {
103  // Preconditions:
104 
105  // Body:
106 
109 
110  // Postconditions:
111 
112  ensure(result != 0);
113  ensure(is_same_type(result));
114  ensure(*result == *this);
115 
116  // Exit:
117 
118  return result;
119 }
120 
123 host() const
124 {
125  // Preconditions:
126 
127  require(is_attached());
128 
129  // Body:
130 
131  const index_space_interval& result = *_host;
132 
133  // Postconditions:
134 
135  ensure(is_basic_query);
136 
137  // Exit:
138 
139  return result;
140 }
141 
142 bool
145 {
146  // Preconditions:
147 
148  // Body:
149 
150  bool result = _only_persistent;
151 
152  // Postconditions:
153 
154  ensure(is_basic_query);
155 
156  // Exit:
157 
158  return result;
159 }
160 
163 ub() const
164 {
165  // Preconditions:
166 
167  require(is_attached());
168 
169  // Body:
170 
171  size_type result = _ub;
172 
173  // Postconditions:
174 
175  ensure(is_basic_query);
176 
177  // Exit:
178 
179  return result;
180 }
181 
182 void
185 {
186  // Preconditions:
187 
188  require(is_attached());
189  require(!is_done());
190 
191  // Body:
192 
193  define_old_variable(pod_type old_index = index());
194 
195  do
196  {
197  ++_local_id;
198  if(_local_id == _ub)
199  {
200  // Done with the iteration, invalid index and exit.
201 
203  break;
204  }
205  }
206  while(!_host->contains(_local_id) ||
208 
209  // Postconditions:
210 
211  ensure(invariant());
212  ensure(is_done() || index() != old_index);
213 
214  // Exit:
215 
216  return;
217 }
218 
219 bool
221 is_done() const
222 {
223  // Preconditions:
224 
225  require(is_attached());
226 
227  // Body:
228 
229  bool result = !is_valid(_local_id);
230 
231  // Postconditions:
232 
233  ensure(is_basic_query);
234 
235  // Exit:
236 
237  return result;
238 }
239 
240 void
243 {
244  // Preconditions:
245 
246  require(is_attached());
247 
248  // Body:
249 
250  _local_id = 0;
251 
252  while(!_host->contains(_local_id))
253  {
254  ++_local_id;
255  if(_local_id == _ub)
256  {
257  // Done with the iteration, invalid index and exit.
258 
260  break;
261  }
262  }
263 
264  // Postconditions:
265 
266  ensure(invariant());
267 
268  // Exit:
269 
270  return;
271 }
272 
275 index() const
276 {
277  // Preconditions:
278 
279  require(is_attached());
280  require(!is_done());
281 
282  // Body:
283 
285 
286  // Postconditions:
287 
288  ensure(is_valid(result));
289 
290  // Exit:
291 
292  return result;
293 }
294 
295 // PROTECTED MEMBER FUNCTIONS
296 
299 {
300  // Preconditions:
301 
302  // Body:
303 
304  (*this) = xother;
305 
306  // Postconditions:
307 
308  ensure(invariant());
309  ensure((*this) == xother);
310 
311  // Exit:
312 
313  return;
314 }
315 
317 index_space_interval_iterator(bool xonly_persistent)
318  : _host(0),
320  _only_persistent(xonly_persistent)
321 {
322  // Preconditions:
323 
324  // Body:
325 
326  // Postconditions:
327 
328  ensure(invariant());
329  ensure(only_persistent() == xonly_persistent);
330  ensure(!is_attached());
331 
332  // Exit:
333 
334  return;
335 }
336 
337 void
340 {
341  // Preconditions:
342 
343  // Body:
344 
345  _host = &xhost;
346  _ub = _host->end() - _host->begin();
347 
348  reset();
349 
350  // Postconditions:
351 
352  ensure(invariant());
353  ensure(is_attached());
354  ensure(&host() == &xhost);
355  ensure(ub() == (xhost.end() - xhost.begin()));
356 
357  // Exit:
358 
359  return;
360 }
361 
362 void
365 {
366  // Preconditions:
367 
368  // Body:
369 
370  _host = 0;
371 
372  // Postconditions:
373 
374  ensure(invariant());
375  ensure(!is_attached());
376 
377  // Exit:
378 
379  return;
380 }
381 
382 bool
384 is_attached() const
385 {
386  // Preconditions:
387 
388  // Body:
389 
390  bool result = (_host != 0);
391 
392  // Postconditions:
393 
394  ensure(is_basic_query);
395 
396  // Exit:
397 
398  return result;
399 }
400 
401 // PRIVATE MEMBER FUNCTIONS
402 
403 
404 // ===========================================================
405 // INDEX SPACE FACET
406 // ===========================================================
407 
408 // PUBLIC MEMBER FUNCTIONS
409 
412 id_spaces() const
413 {
414  // Preconditions:
415 
416  require(is_attached());
417 
418  // Body:
419 
420  const index_space_family& result = _host->id_spaces();
421 
422  // Postconditions:
423 
424  ensure(is_basic_query);
425 
426  // Exit:
427 
428  return result;
429 }
430 
434 {
435  // Preconditions:
436 
437  require(is_attached());
438 
439  // Body:
440 
441  index_space_family& result = _host->id_spaces();
442 
443  // Postconditions:
444 
445  ensure(is_basic_query);
446 
447  // Exit:
448 
449  return result;
450 }
451 
455 {
456  // Preconditions:
457 
458  require(is_attached());
459 
460  // Body:
461 
462  const hub_index_space_handle& result = id_spaces().hub_id_space();
463 
464  // Postconditions:
465 
466  ensure(is_basic_query);
467 
468  // Exit:
469 
470  return result;
471 }
472 
475 ct() const
476 {
477  // Preconditions:
478 
479  require(is_attached());
480  require(!is_done());
481 
482  // Body:
483 
484  size_type result = _host->ct(_local_id);
485 
486  // Postconditions:
487 
488  ensure(is_basic_query);
489 
490  // Exit:
491 
492  return result;
493 }
494 
497 begin() const
498 {
499  // Preconditions:
500 
501  require(is_attached());
502  require(!is_done());
503 
504  // Body:
505 
506  pod_type result = _host->begin(_local_id);
507 
508  // Postconditions:
509 
510  ensure(is_basic_query);
511 
512  // Exit:
513 
514  return result;
515 }
516 
519 end() const
520 {
521  // Preconditions:
522 
523  require(is_attached());
524  require(!is_done());
525 
526  // Body:
527 
528  pod_type result = _host->end(_local_id);
529 
530  // Postconditions:
531 
532  ensure(is_basic_query);
533 
534  // Exit:
535 
536  return result;
537 }
538 
539 bool
541 contains(pod_type xid) const
542 {
543  // Preconditions:
544 
545  require(is_attached());
546  require(!is_done());
547 
548  // Body:
549 
550  bool result = _host->contains(_local_id, xid);
551 
552  // Postconditions:
553 
554  ensure(is_basic_query);
555 
556  // Exit:
557 
558  return result;
559 }
560 
561 bool
564 {
565  // Preconditions:
566 
567  require(is_attached());
568  require(!is_done());
569 
570  // Body:
571 
572  bool result = _host->contains_hub(_local_id, xid);
573 
574  // Postconditions:
575 
576  ensure(is_basic_query);
577 
578  // Exit:
579 
580  return result;
581 }
582 
583 bool
586 {
587  // Preconditions:
588 
589  require(is_attached());
590  require(!is_done());
591 
592  // Body:
593 
594  bool result = _host->contains_unglued_hub(_local_id, xid);
595 
596  // Postconditions:
597 
598  ensure(is_basic_query);
599 
600  // Exit:
601 
602  return result;
603 }
604 
605 bool
608 {
609  // Preconditions:
610 
611  require(is_attached());
612  require(!is_done());
613 
614  // Body:
615 
616  bool result = _host->contains_glued_hub(_local_id, xid);
617 
618  // Postconditions:
619 
620  ensure(is_basic_query);
621 
622  // Exit:
623 
624  return result;
625 }
626 
627 bool
629 contains(pod_type xid, pod_type xhub_id) const
630 {
631  // Preconditions:
632 
633  require(is_attached());
634  require(!is_done());
635 
636  // Body:
637 
638  bool result = _host->contains(_local_id, xid, xhub_id);
639 
640  // Postconditions:
641 
642  ensure(is_basic_query);
643 
644  // Exit:
645 
646  return result;
647 }
648 
651 pod(pod_type xid) const
652 {
653  // Preconditions:
654 
655  require(is_attached());
656  require(!is_done());
657 
658  // Body:
659 
660  pod_type result = _host->pod(_local_id, xid);
661 
662  // Postconditions:
663 
664  ensure(!is_valid(result) || contains(result));
665 
666  // Exit:
667 
668  return result;
669 }
670 
673 hub_pod(pod_type xid) const
674 {
675  // Preconditions:
676 
677  require(is_attached());
678  require(!is_done());
679 
680  // Body:
681 
682  pod_type result = _host->hub_pod(_local_id, xid);
683 
684  // Postconditions:
685 
686  ensure(!is_valid(result) || contains_hub(result));
687 
688  // Exit:
689 
690  return result;
691 }
692 
696 {
697  // Preconditions:
698 
699  require(is_attached());
700  require(!is_done());
701 
702  // Body:
703 
704  pod_type result = _host->unglued_hub_pod(_local_id, xid);
705 
706  // Postconditions:
707 
708  ensure(!is_valid(result) || contains_unglued_hub(result));
709 
710  // Exit:
711 
712  return result;
713 }
714 
718 {
719  // Preconditions:
720 
721  require(is_attached());
722  require(!is_done());
723 
724  // Body:
725 
726  pod_type result = _host->glued_hub_pod(_local_id, xid);
727 
728  // Postconditions:
729 
730  ensure(!is_valid(result) || contains_glued_hub(result));
731 
732  // Exit:
733 
734  return result;
735 }
736 
737 bool
740 {
741  // Preconditions:
742 
743  require(is_attached());
744  require(!is_done());
745 
746  // Body:
747 
748  bool result = _host->is_persistent(_local_id);
749 
750  // Postconditions:
751 
752  ensure(is_basic_query);
753 
754  // Exit:
755 
756  return result;
757 }
758 
759 // PROTECTED MEMBER FUNCTIONS
760 
761 // PRIVATE MEMBER FUNCTIONS
762 
763 
764 // ===========================================================
765 // HANDLE POOL FACET
766 // ===========================================================
767 
768 // PUBLIC MEMBER FUNCTIONS
769 
773 {
774  // Preconditions:
775 
776  require(is_attached());
777  require(!is_done());
778 
779  // Body:
780 
782 
783  // Postconditions:
784 
785  ensure(result.is_attached());
786 
787  // Exit:
788 
789  return result;
790 }
791 
792 void
795 {
796  // Preconditions:
797 
798  require(is_attached());
799  require(!is_done());
800  require(allocated_id_space(xid_space));
801 
802  // Body:
803 
804  _host->release_id_space(xid_space);
805 
806  // Postconditions:
807 
808  ensure(is_basic_query);
809 
810  // Exit:
811 
812  return;
813 }
814 
815 bool
818 {
819  // Preconditions:
820 
821  require(is_attached());
822  require(!is_done());
823 
824  // Body:
825 
826  bool result = _host->allocated_id_space(xid_space);
827 
828  // Postconditions:
829 
830  ensure(is_basic_query);
831 
832  // Exit:
833 
834  return result;
835 }
836 
837 // PROTECTED MEMBER FUNCTIONS
838 
839 // PRIVATE MEMBER FUNCTIONS
840 
841 
842 // ===========================================================
843 // ITERATOR POOL FACET
844 // ===========================================================
845 
846 // PUBLIC MEMBER FUNCTIONS
847 
851 {
852  // Preconditions:
853 
854  require(is_attached());
855  require(!is_done());
856 
857  // Body:
858 
860 
861  // Postconditions:
862 
863  ensure(result.is_attached());
864 
865  // Exit:
866 
867  return result;
868 }
869 
870 void
873 {
874  // Preconditions:
875 
876  require(is_attached());
877  require(!is_done());
878  require(allocated_id_space_iterator(xitr));
879 
880  // Body:
881 
883 
884  // Postconditions:
885 
886  ensure(is_basic_query);
887 
888  // Exit:
889 
890  return;
891 }
892 
893 bool
896 {
897  // Preconditions:
898 
899  require(is_attached());
900  require(!is_done());
901 
902  // Body:
903 
904  bool result = _host->allocated_id_space_iterator(xitr);
905 
906  // Postconditions:
907 
908  ensure(is_basic_query);
909 
910  // Exit:
911 
912  return result;
913 }
914 
915 // PROTECTED MEMBER FUNCTIONS
916 
917 // PRIVATE MEMBER FUNCTIONS
918 
919 
920 // ===========================================================
921 // ANY FACET
922 // ===========================================================
923 
924 // PUBLIC MEMBER FUNCTIONS
925 
926 bool
928 is_ancestor_of(const any *other) const
929 {
930  // Preconditions:
931 
932  require(other != 0);
933 
934  // Body:
935 
936  // True if other conforms to this
937 
938  bool result = dynamic_cast<const index_space_interval_iterator*>(other) != 0;
939 
940  // Postconditions:
941 
942  // Exit:
943 
944  return result;
945 }
946 
947 bool
949 invariant() const
950 {
951  bool result = true;
952 
953  if(invariant_check())
954  {
955  // Prevent recursive calls to invariant
956 
958 
959  // Must satisfy base class invariant
960 
961  invariance(any::invariant());
962 
963  // Invariances for this class:
964 
965  invariance(!is_attached() || is_done() || is_valid(index()));
966 
967  // Finished, turn invariant checking back on.
968 
970  }
971 
972  // Exit
973 
974  return result;
975 }
976 
977 // PROTECTED MEMBER FUNCTIONS
978 
979 // PRIVATE MEMBER FUNCTIONS
980 
981 
982 // ===========================================================
983 // NON-MEMBER FUNCTIONS
984 // ===========================================================
985 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
size_type _ub
The upper bound of the iteration.
virtual bool allocated_id_space_iterator(const index_space_iterator &xitr) const =0
True if and only if id space iterator xitr was allocated by the iterator pool.
virtual const index_space_family & id_spaces() const
The id space family for this (const version).
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 operator==(const index_space_interval_iterator &xother) const
True if this is equivalent to xother.
bool is_attached() const
True, if this iterator is attached.
const index_space_family & id_spaces() const
The id space family for this (const version).
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.
bool contains_glued_hub(pod_type xlocal_id, pod_type xid) const
True if the space with id xlocal_id contains an id equivalent to xid in the glued hub id space...
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 pod_type family_scope(pod_type xlocal_id) const
Id relative to the id space family equivalent to the local id xlocal_id.
index_space_iterator & get_id_space_iterator() const
Allocates an id space iterator from the iterator pool attached to the current id space state...
virtual bool allocated_id_space(const index_space_handle &xid_space) const =0
True if and only if id space handle xid_space was allocated by the handle pool.
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...
virtual bool contains_unglued_hub(pod_type xlocal_id, pod_type xid) const =0
True if the space with id xlocal_id contains an id equivalent to xid in the unglued hub id space...
bool _only_persistent
True if only the persistent id spaces are iterated over.
bool is_done() const
True if iteration is finished.
Abstract base class with useful features for all objects.
Definition: any.h:39
index_space_interval_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
An implemenation of index_space_collection that adds an interface for the interval [begin()...
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.
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.
pod_index_type pod_type
The "plain old data" index type for this.
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.
virtual size_type ct(pod_type xlocal_id) const =0
The number of members for the id space with id xlocal_id.
virtual pod_type pod(pod_type xlocal_id, pod_type xid) const =0
The pod index in the space with id xlocal_id equivalent to xid in the hub id space.
pod_type glued_hub_pod(pod_type xlocal_id, pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in the id space with id xlocal_id...
virtual bool is_persistent(pod_type xlocal_id) const =0
True if the id space with id xlocal_id should be written to disk.
virtual pod_type unglued_hub_pod(pod_type xlocal_id, pod_type xid) const =0
The pod index in the glued hub id space equivalent to xid in the id space with id xlocal_id...
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.
An abstract iterator over the id spaces of a interval.
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.
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
virtual index_space_handle & get_id_space(pod_type xlocal_id) const =0
Allocates an id space handle from the handle pool attached to state with id xlocal_id.
pod_type index() const
The index of the current id space in the iteration.
virtual index_space_iterator & get_id_space_iterator(pod_type xlocal_id) const =0
Allocates an id space iterator from the iterator pool attached to state with id xlocal_id.
pod_type end() const
Ending space id of this interval in the id space family scope.
virtual bool contains(pod_type xlocal_id) const
True if this collection contains the id space with local id xlocal_id.
virtual void release_id_space_iterator(index_space_iterator &xitr) const =0
Returns the id space iterator xitr to the iterator pool.
index_space_interval_iterator()
Default constructor; disabled.
Factory and container for a family of id spaces.
pod_type hub_pod(pod_type xlocal_id, pod_type xid) const
The pod index in the glued hub id space equivalent to xid in the id space with id xlocal_id...
index_space_interval_iterator & operator=(const index_space_interval_iterator &xother)
Assignment operator.
bool contains_hub(pod_type xlocal_id, pod_type xid) const
True if the space with id xlocal_id contains an id equivalent to xid in the hub id space...
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
pod_type begin() const
Beginning space id of this interval in the id space family scope.
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual void release_id_space(index_space_handle &xid_space) const =0
Returns the id space handle xid_space to the handle pool.
index_space_interval * _host
The host id space interval being iterated over.
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.
virtual bool invariant() const
Class invariant.
const hub_index_space_handle & hub_id_space() const
The hub id space.
pod_type _local_id
The local id of the current id space in the iteration.
size_type ub() const
The upper bound of the id spaces.