SheafSystem  0.0.0.0
gathered_insertion_index_space_handle.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/gathered_insertion_index_space_handle.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/index_space_family.h"
24 #include "SheafSystem/index_space_iterator.h"
25 #include "SheafSystem/gathered_insertion_index_space_state.h"
26 #include "SheafSystem/scoped_index.h"
27 
28 // ===========================================================
29 // GATHERED_INSERTION_INDEX_SPACE_HANDLE FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
37 {
38  // Preconditions:
39 
40  // Body:
41 
42  // Postconditions:
43 
44  ensure(invariant());
45 
46  // Exit:
47 
48  return;
49 }
50 
53 {
54  // Preconditions:
55 
56  // Body:
57 
58  (*this) = xother;
59 
60  // Postconditions:
61 
62  ensure(invariant());
63  ensure((*this) == xother);
64 
65  // Exit:
66 
67  return;
68 }
69 
72  pod_type xindex)
73 {
74  // Preconditions:
75 
76  require(xid_spaces.contains(xindex));
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(xid_spaces.contains(xname));
102  require(conforms_to_state(xid_spaces, xname));
103 
104  // Body:
105 
106  attach_to(xid_spaces, xname);
107 
108  // Postconditions:
109 
110  ensure(invariant());
111  ensure(is_attached());
112  ensure(&id_spaces() == &xid_spaces);
113  ensure(name() == xname);
114 
115  // Exit:
116 
117  return;
118 }
119 
123 {
124  // Preconditions:
125 
126  require(xother.is_attached() ? conforms_to_state(xother) : true);
127 
128  // Body:
129 
130  attach_to(xother);
131 
132  // Postconditions:
133 
134  ensure(invariant());
135  ensure((*this) == xother);
136 
137  // Exit:
138 
139  return *this;
140 }
141 
144 {
145  // Preconditions:
146 
147  // Body:
148 
149  // nothing to do.
150 
151  // Postconditions:
152 
153  // Exit:
154 
155  return;
156 }
157 
158 // PROTECTED MEMBER FUNCTIONS
159 
163 {
164  // Preconditions:
165 
166  require(is_attached());
167 
168  // Body:
169 
171  reinterpret_cast<gathered_insertion_index_space_state&>(*_state);
172 
173  // Postconditions:
174 
175  ensure(is_basic_query);
176 
177  // Exit:
178 
179  return result;
180 }
181 
184 state() const
185 {
186  // Preconditions:
187 
188  require(is_attached());
189 
190  // Body:
191 
193  reinterpret_cast<const gathered_insertion_index_space_state&>(*_state);
194 
195  // Postconditions:
196 
197  ensure(is_basic_query);
198 
199  // Exit:
200 
201  return result;
202 }
203 
204 // PRIVATE MEMBER FUNCTIONS
205 
206 
207 // ===========================================================
208 // GATHERED_INSERTION INDEX SPACE FACET
209 // ===========================================================
210 
211 // PUBLIC MEMBER FUNCTIONS
212 
213 void
215 push_back(const scoped_index& xhub_id)
216 {
217  // Preconditions:
218 
219  require(is_attached());
220  require(xhub_id.in_scope());
221  require(precondition_of(push_back(xhub_id.hub_pod())));
222 
223  // Body:
224 
225  push_back(xhub_id.hub_pod());
226 
227  // Postconditions:
228 
229  ensure(postcondition_of(push_back(xhub_id.hub_pod())));
230 
231  // Exit:
232 
233  return;
234 }
235 
236 void
239 {
240  // Preconditions:
241 
242  require(is_attached());
243 
244  // Body:
245 
246  define_old_variable(size_type old_ct = ct());
247  define_old_variable(pod_type old_begin = begin());
248  define_old_variable(pod_type old_end = end());
249 
250  pod_type old_next_id = next_id();
251 
252  state().push_back(xhub_id);
253 
254  // Postconditions:
255 
256  ensure(invariant());
257  ensure(contains(old_next_id, xhub_id));
258  ensure(ct() == old_ct + 1);
259  ensure(ct() > 1 ? begin() <= old_begin : true);
260  ensure(end() == old_next_id+1);
261 
262  // Exit:
263 
264  return;
265 }
266 
267 void
269 push(index_space_iterator& xitr, const scoped_index& xhub_id)
270 {
271  // Preconditions:
272 
273  require(is_attached());
274  require(xhub_id.is_scoped());
275  require(precondition_of(push(xitr, xhub_id.pod())));
276 
277  // Body:
278 
279  push(xitr, xhub_id.pod());
280 
281  // Postconditions:
282 
283  ensure(postcondition_of(push(xitr, xhub_id.pod())));
284 
285  // Exit:
286 
287  return;
288 }
289 
290 void
293 {
294  // Preconditions:
295 
296  require(is_attached());
297  require(!xitr.is_done());
298  require(allocated_iterator(xitr));
299  require(is_valid(xhub_id));
300  require(!contains_hub(xhub_id));
301  require(hub_id_space().contains(xhub_id));
302 
303  // Body:
304 
305  define_old_variable(size_type old_ct = ct());
306  define_old_variable(pod_type old_begin = begin());
307  define_old_variable(pod_type old_end = end());
308  define_old_variable(pod_type old_itr_hub_id = xitr.hub_pod());
309  define_old_variable(pod_type old_itr_id = xitr.pod());
310 
311  state().push(xitr, xhub_id);
312 
313  // Postconditions:
314 
315  ensure(invariant());
316  ensure(contains(old_itr_id, xhub_id));
317  ensure(ct() == old_ct + 1);
318  ensure(ct() > 1 ? begin() == old_begin : true);
319  ensure(ct() > 1 ? end() == old_end + 1 : true);
320  ensure(xitr.pod() == old_itr_id+1);
321  ensure(xitr.hub_pod() == old_itr_hub_id);
322 
323  // Exit:
324 
325  return;
326 }
327 
330 next_id() const
331 {
332  // Preconditions:
333 
334  require(is_attached());
335 
336  // Body:
337 
338  pod_type result = state().next_id();
339 
340  // Postconditions:
341 
342  ensure(is_basic_query);
343 
344  // Exit:
345 
346  return result;
347 }
348 
351 remove(const scoped_index& xid, bool xupdate_extrema)
352 {
353  // Preconditions:
354 
355  require(is_attached());
356  require(xid.in_scope());
357  require(precondition_of(remove_hub(xid.hub_pod(), xupdate_extrema)));
358 
359  // Body:
360 
361  size_type result = remove_hub(xid.hub_pod(), xupdate_extrema);
362 
363  // Postconditions:
364 
365  ensure(postcondition_of(remove_hub(xid.hub_pod(), xupdate_extrema)));
366 
367  // Exit
368 
369  return result;
370 }
371 
374 remove(pod_type xid, bool xupdate_extrema)
375 {
376  // Preconditions:
377 
378  require(is_attached());
379 
380  // Body:
381 
382  define_old_variable(size_type old_ct = ct());
383  define_old_variable(pod_type old_begin = begin());
384  define_old_variable(pod_type old_end = end());
385  define_old_variable(pod_type old_hub_pod = hub_pod(xid));
386 
387  size_type result = state().remove(xid, xupdate_extrema);
388 
389  // Postconditions:
390 
391  // ensure(!contains(xid));
392  ensure(!contains(xid, old_hub_pod));
393  ensure((result != 0) ? ct() == old_ct - 1 : ct() == old_ct);
394  ensure((result == 0) || xupdate_extrema || begin() == old_begin);
395  ensure((result == 0) || xupdate_extrema || end() == old_end);
396  ensure((result != 0) && xupdate_extrema && is_empty() ? !is_valid(begin()) : true);
397  ensure((result != 0) && xupdate_extrema && is_empty() ? !is_valid(end()) : true);
398  ensure((result != 0) && xupdate_extrema && !is_empty() ? begin() >= old_begin : true);
399  ensure((result != 0) && xupdate_extrema && !is_empty() ? end() <= old_end : true);
400 
401  // Exit:
402 
403  return result;
404 }
405 
408 remove_hub(pod_type xhub_id, bool xupdate_extrema)
409 {
410  // Preconditions:
411 
412  require(is_attached());
413 
414  // Body:
415 
416  define_old_variable(pod_type old_id = pod(xhub_id));
417  define_old_variable(size_type old_ct = ct());
418  define_old_variable(pod_type old_begin = begin());
419  define_old_variable(pod_type old_end = end());
420 
421  pod_type result = state().remove_hub(xhub_id, xupdate_extrema);
422 
423  // Postconditions:
424 
425  ensure(!contains_hub(xhub_id));
426  ensure((result != 0) ? ct() == old_ct - 1 : ct() == old_ct);
427  ensure((result == 0) || xupdate_extrema || begin() == old_begin);
428  ensure((result == 0) || xupdate_extrema || end() == old_end);
429  ensure((result != 0) && xupdate_extrema && is_empty() ? !is_valid(begin()) : true);
430  ensure((result != 0) && xupdate_extrema && is_empty() ? !is_valid(end()) : true);
431  ensure((result != 0) && xupdate_extrema && !is_empty() ? begin() >= old_begin : true);
432  ensure((result != 0) && xupdate_extrema && !is_empty() ? end() <= old_end : true);
433 
434  // Exit:
435 
436  return result;
437 }
438 
439 void
441 remove(index_space_iterator& xitr, bool xupdate_extrema)
442 {
443  // Preconditions:
444 
445  require(is_attached());
446  require(!xitr.is_done());
447  require(contains(xitr.pod()));
448 
449  // Body:
450 
451  define_old_variable(size_type old_ct = ct());
452  define_old_variable(pod_type old_begin = begin());
453  define_old_variable(pod_type old_end = end());
454  define_old_variable(pod_type old_itr_id = xitr.pod());
455  define_old_variable(pod_type old_itr_hub_id = xitr.hub_pod());
456 
457  state().remove(xitr, xupdate_extrema);
458 
459  // Postconditions:
460 
461  // ensure(!contains(old_itr_id)); not true for list_cover_id_space
462  ensure(!contains_hub(old_itr_hub_id));
463  ensure(ct() == old_ct - 1);
464  ensure(xupdate_extrema || begin() == old_begin);
465  ensure(xupdate_extrema || end() == old_end);
466  ensure(xupdate_extrema && is_empty() ? !is_valid(begin()) : true);
467  ensure(xupdate_extrema && is_empty() ? !is_valid(end()) : true);
468  ensure(xupdate_extrema && !is_empty() ? begin() >= old_begin : true);
469  ensure(xupdate_extrema && !is_empty() ? end() <= old_end : true);
470  // ensure(xitr.is_done() || xitr.pod() > old_itr_pod); Not true for list_index_space.
471  ensure(unexecutable("xitr.is_done() || xitr.hub_pod() == old_next_itr_hub_pod"));
472 
473  // Exit:
474 
475  return;
476 }
477 
478 void
481 {
482  // Preconditions:
483 
484  require(is_attached());
485 
486  // Body:
487 
488  state().gather();
489 
490  // Postconditions:
491 
492  ensure(invariant());
493  ensure(is_gathered());
494 
495  // Exit:
496 
497  return;
498 }
499 
500 void
503 {
504  // Preconditions:
505 
506  require(is_attached());
507 
508  // Body:
509 
510  state().update_extrema();
511 
512  // Postconditions:
513 
514  ensure(is_basic_query);
515 
516  // Exit
517 
518  return;
519 }
520 
521 void
523 reserve(size_type xcapacity)
524 {
525  // Preconditions:
526 
527  require(is_attached());
528 
529  // Body:
530 
531  state().reserve(xcapacity);
532 
533  // Postconditions:
534 
535  ensure(invariant());
536  ensure(capacity() >= xcapacity);
537 
538  // Exit:
539 
540  return;
541 }
542 
545 capacity() const
546 {
547  // Preconditions:
548 
549  require(is_attached());
550 
551  // Body:
552 
553  size_type result = state().capacity();
554 
555  // Postconditions:
556 
557  ensure(is_basic_query);
558 
559  // Exit:
560 
561  return result;
562 }
563 
564 void
567 {
568  // Preconditions:
569 
570  require(is_attached());
571 
572  // Body:
573 
574  state().clear();
575 
576  // Postconditions:
577 
578  ensure(invariant());
579  ensure(is_empty());
580 
581  // Exit:
582 
583  return;
584 }
585 
586 // PROTECTED MEMBER FUNCTIONS
587 
588 // PRIVATE MEMBER FUNCTIONS
589 
590 
591 // ===========================================================
592 // EXPLICIT_INDEX_SPACE_HANDLE FACET
593 // ===========================================================
594 
595 // PUBLIC MEMBER FUNCTIONS
596 
597 // PROTECTED MEMBER FUNCTIONS
598 
599 // PRIVATE MEMBER FUNCTIONS
600 
601 
602 // ===========================================================
603 // INDEX_SPACE_HANDLE FACET
604 // ===========================================================
605 
606 // PUBLIC MEMBER FUNCTIONS
607 
611 {
612  // Preconditions:
613 
614  require(xother.is_attached() ? conforms_to_state(xother) : true);
615 
616  // Body:
617 
618  attach_to(xother);
619 
620  // Postconditions:
621 
622  ensure(invariant());
623  ensure((*this) == xother);
624 
625  // Exit:
626 
627  return *this;
628 }
629 
632 clone() const
633 {
634  // Preconditions:
635 
636  // Body:
637 
639 
640  // Postconditions:
641 
642  ensure(result != 0);
643  ensure(is_same_type(result));
644  ensure(*result == *this);
645 
646  // Exit:
647 
648  return result;
649 }
650 
651 // PROTECTED MEMBER FUNCTIONS
652 
653 // PRIVATE MEMBER FUNCTIONS
654 
655 
656 // ===========================================================
657 // INDEX SPACE FACET
658 // ===========================================================
659 
660 // PUBLIC MEMBER FUNCTIONS
661 
662 void
664 put_is_persistent(bool xis_persistent)
665 {
666  // Preconditions:
667 
668  // Body:
669 
670  state().put_is_persistent(xis_persistent);
671 
672  // Postconditions:
673 
674  ensure(is_persistent() == xis_persistent);
675 
676  // Exit:
677 
678  return;
679 }
680 
681 // PROTECTED MEMBER FUNCTIONS
682 
683 // PRIVATE MEMBER FUNCTIONS
684 
685 
686 // ===========================================================
687 // HANDLE FACET
688 // ===========================================================
689 
690 // PUBLIC MEMBER FUNCTIONS
691 
692 bool
695  pod_type xlocal_id) const
696 {
697  // Preconditions:
698 
699  require(xhost.contains(xlocal_id));
700 
701  // Body:
702 
703  bool result =
704  (dynamic_cast<gathered_insertion_index_space_state*>(state(xhost, xlocal_id)) != 0);
705 
706  // Postconditions:
707 
708  ensure(is_basic_query);
709 
710  // Exit:
711 
712  return result;
713 }
714 
715 // PROTECTED MEMBER FUNCTIONS
716 
717 // PRIVATE MEMBER FUNCTIONS
718 
719 
720 // ===========================================================
721 // ANY FACET
722 // ===========================================================
723 
724 // PUBLIC MEMBER FUNCTIONS
725 
726 bool
728 is_ancestor_of(const any *other) const
729 {
730  // Preconditions:
731 
732  require(other != 0);
733 
734  // Body:
735 
736  // True if other conforms to this
737 
738  bool result = dynamic_cast<const gathered_insertion_index_space_handle*>(other) != 0;
739 
740  // Postconditions:
741 
742  // Exit:
743 
744  return result;
745 }
746 
747 bool
749 invariant() const
750 {
751  bool result = true;
752 
753  if(invariant_check())
754  {
755  // Prevent recursive calls to invariant
756 
758 
759  // Must satisfy base class invariant
760 
762 
763  // Invariances for this class:
764 
765  // Finished, turn invariant checking back on.
766 
768  }
769 
770  // Exit
771 
772  return result;
773 }
774 
775 // PROTECTED MEMBER FUNCTIONS
776 
777 // PRIVATE MEMBER FUNCTIONS
778 
779 
780 // ===========================================================
781 // NON-MEMBER FUNCTIONS
782 // ===========================================================
783 
784 size_t
785 sheaf::
786 deep_size(const gathered_insertion_index_space_handle& xn, bool xinclude_shallow)
787 {
788  // Preconditions:
789 
790  // Body:
791 
792  size_t result = xinclude_shallow ? sizeof(xn) : 0;
793 
794  // Postconditions:
795 
796  ensure(result >= 0);
797 
798  // Exit
799 
800  return result;
801 }
802 
gathered_insertion_index_space_handle & operator=(const gathered_insertion_index_space_handle &xother)
Assignment operator; attach this handle to the state of xother. synonym for attach_to(xother).
virtual void attach_to(pod_type xindex)
Attach to the state with index xindex in the id space family id_spaces().
virtual pod_type index() const
Index of this space.
bool in_scope() const
True if and only if scope() contains an entry for pod().
Definition: scoped_index.h:584
const pod_type & pod() const
The "plain old data" storage of this; the value in the external id space.
Definition: scoped_index.h:672
bool is_empty() const
True if there are no ids in the space.
An abstract iterator over the ids of an id space.
pod_type next_id() const
The id inserted by the next call to push_back.
void push_back(const scoped_index &xhub_id)
Make the next id in this space equivalent to xhub_id in the hub id space. synonym for push_back(xhub_...
size_type remove(const scoped_index &xid, bool update_extrema)
Removes the equivalence associated with xid.hub_pod(). synonym for remove_hub(xid.hub_pod(), xupdate_extrema). Returns the number of entries actually removed, either 0 or 1.
virtual void reserve(size_type xcapacity)=0
Reserve enough memory for xcapacity number of ids.
pod_type pod() const
The current id in the iteration.
virtual size_type capacity() const =0
The number of ids reserved in memory.
virtual bool is_persistent() const
True if this id space should be written to disk.
bool contains_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space. synonym for contains...
void gather()
Gathers this id space into an interval.
size_type remove(const scoped_index &xid, bool update_extrema)
Removes the equivalence associated with xid.hub_pod(). synonym for remove_hub(xid.hub_pod(), xupdate_extrema). Returns the number of entries actually removed, either 0 or 1.
void put_is_persistent(bool xis_persistent)
Set if this id space should be written to disk.
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
The id space family for this (const version).
void put_is_persistent(bool xis_persistent)
Set if this id space should be written to disk.
virtual bool is_attached() const =0
True if this handle is attached to a state.
const hub_index_space_handle & hub_id_space() const
The hub id space.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
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...
gathered_insertion_index_space_state & state()
The gathered_insertion id space state (gathered_insertion version).
Abstract base class with useful features for all objects.
Definition: any.h:39
bool is_done() const
True if iteration is finished.
An implementation of class index_space_handle for an explicit_index_space_state.
std::string name() const
Name of this space.
pod_index_type pod_type
The "plain old data" index type for this.
bool is_scoped() const
True if and only if scope() is attached.
Definition: scoped_index.h:592
void push(index_space_iterator &xitr, const scoped_index &xhub_id)
Makes id xitr.id() in this id space equivalent to xhub_id in the hub id space. Increments all ids gre...
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
virtual bool is_attached() const
True if this handle is attached to a state.
A handle for a gathered_insertion_index_space_state.
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.
bool is_gathered() const
True if begin() == 0 and end() == ct().
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
virtual bool invariant() const
Class invariant.
void reserve(size_type xcapacity)
Reserve enough memory for xcapacity number of ids.
size_type remove_hub(pod_type xhub_id, bool update_extrema)
Removes the equivalence associated with hub id xhub_id. Returns the number of entries actually remove...
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
void gather()
Gathers this id space into a 0-based, contiguous interval.
virtual bool contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual gathered_insertion_index_space_handle * clone() const
Virtual constructor, makes a new instance of the same type as this. If the handle is attached...
pod_type hub_pod(pod_type xid) const
The pod index in the unglued hub id space equivalent to xid in this id space; synonym for unglued_hub...
size_type capacity() const
The number of ids reserved in memory.
virtual bool allocated_iterator(const index_space_iterator &xitr) const
True if and only if id space iterator xitr was allocated by the iterator pool.
virtual pod_type begin() const
Beginning id of this space.
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.
virtual pod_type end() const
Ending id of this space.
void push_back(const scoped_index &xhub_id)
Make the next id in this space equivalent to xhub_id in the hub id space. synonym for push_back(xhub_...
virtual size_type ct() const
The number of members.
Factory and container for a family of id spaces.
An implementation of class explicit_index_space_state that supports gathered insertion of new members...
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 next_id() const
The id inserted by the next call to push_back.
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 bool contains(pod_type xid) const
True if this space contains id xid.
size_type remove_hub(pod_type xhub_id, bool update_extrema)
Removes the equivalence associated with hub id xhub_id. Returns the number of entries actually remove...
void push(index_space_iterator &xitr, const scoped_index &xhub_id)
Makes id xitr.id() in this id space equivalent to xhub_id in the hub id space. Increments all ids gre...
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710