SheafSystem  0.0.0.0
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/index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/hub_index_space_handle.h"
25 #include "SheafSystem/index_space_family.h"
26 
27 // ===========================================================
28 // INDEX_SPACE_ITERATOR FACET
29 // ===========================================================
30 
31 // PUBLIC MEMBER FUNCTIONS
32 
35 {
36  // Preconditions:
37 
38  // Body:
39 
40  // Nothing to do;
41 
42  // Postconditions:
43 
44  // Exit:
45 
46  return;
47 }
48 
52 {
53  // Preconditions:
54 
55  // Body:
56 
57  if(xother.is_attached())
58  {
59  _is_done = xother._is_done;
60  _pod = xother._pod;
61  _hub_pod = xother._hub_pod;
62  }
63 
64  // Postconditions:
65 
66  ensure(invariant());
67 // ensure((*this) == xother);
68 
69  // Exit
70 
71  return *this;
72 }
73 
74 bool
76 operator==(const index_space_iterator& xother) const
77 {
78  // Preconditions:
79 
80  // Body:
81 
82  bool result = (is_attached() == xother.is_attached());
83  if(result && is_attached())
84  {
85  result = result && (_is_done == xother._is_done);
86  result = result && (_pod == xother._pod);
87  result = result && (_hub_pod == xother._hub_pod);
88  }
89 
90  // Postconditions:
91 
92  ensure(is_basic_query);
93 
94  // Exit:
95 
96  return result;
97 }
98 
101 clone() const
102 {
103  index_space_iterator* result;
104 
105  // Preconditions:
106 
107  // Body:
108 
109  result = 0; // Just to silence compiler warnings.
110 
111  is_abstract();
112 
113  // Postconditions:
114 
115  ensure(result != 0);
116  ensure(is_same_type(result));
117  ensure(*result == *this);
118 
119  // Exit:
120 
121  return result;
122 }
123 
124 // PROTECTED MEMBER FUNCTIONS
125 
128 {
129  // Preconditions:
130 
131  // Body:
132 
133  invalidate_ids();
134 
135  // Postconditions:
136 
137  // Can't call invariant because it calls pure virtual functions.
138  //ensure(invariant());
139  ensure(is_done());
140 
141  // Exit:
142 
143  return;
144 }
145 
148 {
149  // Preconditions:
150 
151  // Body:
152 
153  _is_done = xother._is_done;
154  _pod = xother._pod;
155  _hub_pod = xother._hub_pod;
156 
157  // Postconditions:
158 
159  // Exit:
160 
161  return;
162 }
163 
164 // PRIVATE MEMBER FUNCTIONS
165 
166 
167 // ===========================================================
168 // ITERATOR FACET
169 // ===========================================================
170 
171 // PUBLIC MEMBER FUNCTIONS
172 
173 void
176 {
177  // Preconditions:
178 
179  require(is_attached());
180  require(!is_done());
181 
182  // Body:
183 
184  define_old_variable(pod_type old_pod = pod());
185  define_old_variable(pod_type old_hub_pod = hub_pod());
186 
187  is_abstract();
188 
189  // Postconditions:
190 
191  ensure(invariant());
192  ensure(is_done() || pod() != old_pod);
193  ensure(is_done() || hub_pod() != old_hub_pod);
194 
195  // Exit:
196 
197  return;
198 }
199 
200 bool
202 is_done() const
203 {
204  // Preconditions:
205 
206  // Body:
207 
208  // Postconditions:
209 
210  ensure(is_basic_query);
211 
212  // Exit:
213 
214  return _is_done;
215 }
216 
217 void
220 {
221  // Preconditions:
222 
223  require(is_attached());
224 
225  // Body:
226 
227  is_abstract();
228 
229  // Postconditions:
230 
231  ensure(invariant());
232 
233  // Exit:
234 
235  return;
236 }
237 
238 void
241 {
242  // Preconditions:
243 
244  // Body:
245 
246  invalidate_ids();
247 
248  // Postconditions:
249 
250  ensure(is_done());
251 
252  // Exit:
253 
254  return;
255 }
256 
257 // PROTECTED MEMBER FUNCTIONS
258 
259 // PRIVATE MEMBER FUNCTIONS
260 
261 
262 // ===========================================================
263 // POD FACET
264 // ===========================================================
265 
266 // PUBLIC MEMBER FUNCTIONS
267 
270 pod() const
271 {
272  // Preconditions:
273 
274  require(is_attached());
275  require(!is_done());
276 
277  // Body:
278 
279  // Postconditions:
280 
281  ensure(is_basic_query);
282 
283  // Exit:
284 
285  return _pod;
286 }
287 
290 hub_pod() const
291 {
292  // Preconditions:
293 
294  require(is_attached());
295  require(!is_done());
296 
297  // Body:
298 
299  // Postconditions:
300 
301  ensure(is_basic_query);
302 
303  // Exit:
304 
305  return _hub_pod;
306 }
307 
311 {
312  // Preconditions:
313 
314  require(is_attached());
315  require(!is_done());
316 
317  // Body:
318 
319  // Postconditions:
320 
321  ensure(is_basic_query);
322 
323  // Exit:
324 
325  return _hub_pod;
326 }
327 
331 {
332  // Preconditions:
333 
334  require(is_attached());
335  require(!is_done());
336 
337  // Body:
338 
340 
341  // Postconditions:
342 
343  ensure(hub_id_space().contains_unglued_hub(result));
344 
345  // Exit:
346 
347  return result;
348 }
349 
350 // PROTECTED MEMBER FUNCTIONS
351 
352 void
355 {
356  // Preconditions:
357 
358  // Body:
359 
362  _is_done = true;
363 
364  // Posconditions:
365 
366  ensure(is_done());
367 
368  // Exite:
369 
370  return;
371 }
372 
373 // PRIVATE MEMBER FUNCTIONS
374 
375 
376 // ===========================================================
377 // INDEX SPACE FACET
378 // ===========================================================
379 
380 // PUBLIC MEMBER FUNCTIONS
381 
384 id_spaces() const
385 {
386  // Preconditions:
387 
388  require(is_attached());
389 
390  // Body:
391 
392  const index_space_family& result = host().id_spaces();
393 
394  // Postconditions:
395 
396  ensure(is_basic_query);
397 
398  // Exit:
399 
400  return result;
401 }
402 
406 {
407  // Preconditions:
408 
409  require(is_attached());
410 
411  // Body:
412 
413  const hub_index_space_handle& result = id_spaces().hub_id_space();
414 
415  // Postconditions:
416 
417  ensure(is_basic_query);
418 
419  // Exit:
420 
421  return result;
422 }
423 
424 // PROTECTED MEMBER FUNCTIONS
425 
426 // PRIVATE MEMBER FUNCTIONS
427 
428 
429 // ===========================================================
430 // HANDLE FACET
431 // ===========================================================
432 
433 // PUBLIC MEMBER FUNCTIONS
434 
437 host() const
438 {
439  // Preconditions:
440 
441  require(is_attached());
442 
443  // Body:
444 
445  index_space_collection* result = 0; // Just to silence compiler warnings.
446 
447  is_abstract();
448 
449  // Postconditions:
450 
451  ensure(is_basic_query);
452 
453  // Exit:
454 
455  return *result;
456 }
457 
460 index() const
461 {
462  // Preconditions:
463 
464  require(is_attached());
465 
466  // Body:
467 
468  pod_type result = 0; // Just to silence compiler warnings.
469 
470  is_abstract();
471 
472  // Postconditions:
473 
474  ensure(id_spaces().contains(result));
475 
476  // Exit:
477 
478  return result;
479 }
480 
481 std::string
483 name() const
484 {
485  // Preconditions:
486 
487  require(is_attached());
488 
489  // Body:
490 
491  std::string result = id_spaces().name(index());
492 
493  // Postconditions:
494 
495  ensure(result.empty() || id_spaces().contains(result));
496 
497  // Exit:
498 
499  return result;
500 }
501 
502 bool
504 is_attached() const
505 {
506  // Preconditions:
507 
508  require(is_attached());
509 
510  // Body:
511 
512  bool result = false; // Just to silence compiler warnings.
513 
514  is_abstract();
515 
516  // Postconditions:
517 
518  ensure(is_basic_query);
519 
520  // Exit:
521 
522  return result;
523 }
524 
525 void
527 attach_to(const index_space_family& xid_spaces, pod_type xindex)
528 {
529  // Preconditions:
530 
531  require(xid_spaces.contains(xindex));
532  require(conforms_to_state(xid_spaces, xindex));
533 
534  // Body:
535 
536  const index_space_collection* lhost = xid_spaces.collection(xindex);
537  attach_to(*lhost, lhost->local_scope(xindex));
538 
539  // Postconditions:
540 
541  ensure(invariant());
542  ensure(is_attached());
543  ensure(&id_spaces() == &xid_spaces);
544  ensure(index() == xindex);
545 
546  // Exit:
547 
548  return;
549 }
550 
551 void
553 attach_to(const index_space_family& xid_spaces, const std::string& xname)
554 {
555  // Preconditions:
556 
557  require(!xname.empty());
558  require(xid_spaces.contains(xname));
559  require(conforms_to_state(xid_spaces, xname));
560 
561  // Body:
562 
563  attach_to(xid_spaces, xid_spaces.index(xname));
564 
565  // Postconditions:
566 
567  ensure(invariant());
568  ensure(is_attached());
569  ensure(&id_spaces() == &xid_spaces);
570  ensure(name() == xname);
571 
572  // Exit:
573 
574  return;
575 }
576 
577 void
580 {
581  // Preconditions:
582 
583  require(is_attached());
584  require(id_spaces().contains(xindex));
585  require(conforms_to_state(xindex));
586 
587  // Body:
588 
589  define_old_variable(const index_space_family& old_id_spaces = id_spaces());
590 
591  is_abstract();
592 
593  // Postconditions:
594 
595  ensure(invariant());
596  ensure(is_attached());
597  ensure(&id_spaces() == &old_id_spaces);
598  ensure(index() == xindex);
599 
600  // Exit:
601 
602  return;
603 }
604 
605 void
607 attach_to(const std::string& xname)
608 {
609  // Preconditions:
610 
611  require(is_attached());
612  require(id_spaces().contains(xname));
613  require(conforms_to_state(xname));
614 
615  // Body:
616 
617  define_old_variable(const index_space_family& old_id_spaces = id_spaces());
618 
619  attach_to(id_spaces().index(xname));
620 
621  // Postconditions:
622 
623  ensure(invariant());
624  ensure(is_attached());
625  ensure(&id_spaces() == &old_id_spaces);
626  ensure(name() == xname);
627 
628  // Exit:
629 
630  return;
631 }
632 
633 void
635 attach_to(const index_space_handle& xid_space)
636 {
637  // Preconditions:
638 
639  require(xid_space.is_attached());
640  require(conforms_to_state(xid_space.id_spaces(), xid_space.index()));
641 
642  // Body:
643 
644  if(is_attached() && (host() == xid_space.host()))
645  {
646  attach_to(xid_space.index());
647  }
648  else
649  {
650  attach_to(xid_space.id_spaces(), xid_space.index());
651  }
652 
653  // Postconditions:
654 
655  ensure(invariant());
656  ensure(is_attached());
657  ensure(&host() == &xid_space.host());
658  ensure(index() == xid_space.index());
659 
660  // Exit:
661 
662  return;
663 }
664 
665 void
667 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
668 {
669  // Preconditions:
670 
671  require(xhost.contains(xlocal_id));
672  require(conforms_to_state(xhost, xlocal_id));
673 
674  // Body:
675 
676  is_abstract();
677 
678  // Postconditions:
679 
680  ensure(is_attached());
681  ensure(&host() == &xhost);
682  ensure(index() == xhost.family_scope(xlocal_id));
683 
684  // Exit:
685 
686  return;
687 }
688 
689 void
692 {
693  // Preconditions:
694 
695  // Body:
696 
697  is_abstract();
698 
699  // Postconditions:
700 
701  ensure(!is_attached());
702 
703  // Exit:
704 
705  return;
706 }
707 
708 bool
711  pod_type xindex) const
712 {
713  // Preconditions:
714 
715  require(xid_spaces.contains(xindex));
716 
717  // Body:
718 
719  const index_space_collection* lhost = xid_spaces.collection(xindex);
720  bool result = conforms_to_state(*lhost, lhost->local_scope(xindex));
721 
722  // Postconditions:
723 
724  ensure(is_basic_query);
725 
726  // Exit:
727 
728  return result;
729 }
730 
731 bool
734  const std::string& xname) const
735 {
736  // Preconditions:
737 
738  require(xid_spaces.contains(xname));
739 
740  // Body:
741 
742  bool result = conforms_to_state(xid_spaces, xid_spaces.index(xname));
743 
744  // Postconditions:
745 
746  ensure(is_basic_query);
747 
748  // Exit:
749 
750  return result;
751 }
752 
753 bool
756 {
757  // Preconditions:
758 
759  require(id_spaces().contains(xindex));
760 
761  // Body:
762 
763  bool result = conforms_to_state(id_spaces(), xindex);
764 
765  // Postconditions:
766 
767  ensure(is_basic_query);
768 
769  // Exit:
770 
771  return result;
772 }
773 
774 bool
776 conforms_to_state(const std::string& xname) const
777 {
778  // Preconditions:
779 
780  require(id_spaces().contains(xname));
781 
782  // Body:
783 
784  bool result = conforms_to_state(id_spaces(), xname);
785 
786  // Postconditions:
787 
788  ensure(is_basic_query);
789 
790  // Exit:
791 
792  return result;
793 }
794 
795 bool
798  pod_type xlocal_id) const
799 {
800  // Preconditions:
801 
802  require(xhost.contains(xlocal_id));
803 
804  // Body:
805 
806  bool result = false;
807 
808  is_abstract();
809 
810  // Postconditions:
811 
812  ensure(is_basic_query);
813 
814  // Exit:
815 
816  return result;
817 }
818 
819 // PROTECTED MEMBER FUNCTIONS
820 
821 // PRIVATE MEMBER FUNCTIONS
822 
823 
824 // ===========================================================
825 // ANY FACET
826 // ===========================================================
827 
828 // PUBLIC MEMBER FUNCTIONS
829 
830 bool
832 is_ancestor_of(const any *other) const
833 {
834  // Preconditions:
835 
836  require(other != 0);
837 
838  // Body:
839 
840  // True if other conforms to this
841 
842  bool result = dynamic_cast<const index_space_iterator*>(other) != 0;
843 
844  // Postconditions:
845 
846  // Exit:
847 
848  return result;
849 }
850 
851 bool
853 invariant() const
854 {
855  bool result = true;
856 
857  if(invariant_check())
858  {
859  // Must satisfy base class invariant
860 
861  invariance(any::invariant());
862 
863  // Prevent recursive calls to invariant
864 
866 
867  // Invariances for this class:
868 
869  invariance(!is_attached() || is_done() || is_valid(pod()));
870  invariance(!is_attached() || is_done() || is_valid(hub_pod()));
871 
874 // invariance(!is_attached() || is_done() || pod() == id_spaces().pod(index(), hub_pod()));
875 // invariance(!is_attached() || is_done() || hub_pod() == id_spaces().hub_pod(index(), pod()));
876 
877  // Finished, turn invariant checking back on.
878 
880  }
881 
882  // Exit
883 
884  return result;
885 }
886 
887 // PROTECTED MEMBER FUNCTIONS
888 
889 // PRIVATE MEMBER FUNCTIONS
890 
891 
892 // ===========================================================
893 // NON-MEMBER FUNCTIONS
894 // ===========================================================
895 
896 size_t
897 sheaf::
898 deep_size(const index_space_iterator& xn, bool xinclude_shallow)
899 {
900  // Preconditions:
901 
902  // Body:
903 
904  size_t result = xinclude_shallow ? sizeof(xn) : 0;
905 
906  // Postconditions:
907 
908  ensure(result >= 0);
909 
910  // Exit
911 
912  return result;
913 }
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_collection * collection(pod_type xid) const
Id space collection for id xid (const version).
const hub_index_space_handle & hub_id_space() const
The hub id space.
std::string name(pod_type xid) const
The name for id xid.
An abstract iterator over the ids of an id space.
bool _is_done
True if the iteration is finished.
virtual const index_space_collection & host() const =0
The host collection.
pod_type pod() const
The current id in the iteration.
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 invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
virtual void detach()=0
Detach this handle form its state, if any.
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
virtual const index_space_family & id_spaces() const =0
The id space family for this (const version).
virtual pod_type index() const =0
Index of this space.
virtual void next()=0
Makes id() the next id in the iteration.
pod_type glued_hub_pod() const
The current glued hub id in the iteration.
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 const index_space_family & id_spaces() const =0
The id space family for this (const version).
virtual pod_type local_scope(pod_type xspace_id) const =0
Id relative to this collection equivalent to the id space family id xspace_id.
pod_type index(const std::string &xname) const
The index for name xname.
virtual void reset()=0
Restarts the iteration.
Abstract base class with useful features for all objects.
Definition: any.h:39
pod_type unglued_hub_pod() const
The current unglued hub id in the iteration.
std::string name() const
Name of this space.
bool is_done() const
True if iteration is finished.
const index_space_family & id_spaces() const
The id space family for this (const version).
void attach_to(const index_space_family &xid_spaces, pod_type xindex)
Attach to the state with index xindex in the id space family xid_spaces.
SHEAF_DLL_SPEC size_t deep_size(const dof_descriptor_array &xp, bool xinclude_shallow=true)
The deep size of the referenced object of type dof_descriptor_array.
virtual const index_space_collection & host() const =0
The host collection.
pod_type _hub_pod
The current hub id in the iteration.
A collection of id space states. This is a virtual class with provides an interface for accessing the...
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual bool contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual pod_type index() const =0
Index of this space.
virtual 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_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
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
pod_type _pod
The current id in the iteration.
virtual index_space_iterator & operator=(const index_space_iterator &xother)
Assignment operator.
Factory and container for a family of id spaces.
virtual bool invariant() const
Class invariant.
index_space_iterator()
Creates an iterator for the id space.
pod_index_type pod_type
The "plain old data" index type for this.
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
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
virtual index_space_iterator * clone() const =0
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
void force_is_done()
Makes is_done() true.
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
bool conforms_to_state(const index_space_family &xid_spaces, pod_type xindex) const
True if this conforms to the handle type required by the state with index xindex in the id space fami...
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
virtual ~index_space_iterator()
Destructor.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.