SheafSystem  0.0.0.0
singleton_index_space_state.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/singleton_index_space_state.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/deep_size.h"
24 #include "SheafSystem/index_space_family.h"
25 #include "SheafSystem/hub_index_space_handle.h"
26 #include "SheafSystem/singleton_index_space_handle.h"
27 #include "SheafSystem/singleton_index_space_iterator.h"
28 
29 // ===========================================================
30 // SPACE FACTORY FACET
31 // ===========================================================
32 
33 // PUBLIC MEMBER FUNCTIONS
34 
38  const std::string& xname,
39  pod_type xhub_id)
40 {
41  // Preconditions:
42 
43  require(!xname.empty());
44  require(!xid_spaces.contains(xname));
45  require(xid_spaces.hub_id_space().contains(xhub_id));
46 
47  // Body:
48 
50  lstate->new_state(xid_spaces, xname, false);
51 
52  lstate->_hub_id = xhub_id;
53  lstate->_ct = 1;
54  lstate->_begin = 0;
55  lstate->_end = 1;
56 
57  singleton_index_space_handle result(*lstate);
58 
59  // Postconditions:
60 
61  ensure(&result.id_spaces() == &xid_spaces);
62  ensure(xid_spaces.contains(xname));
63  ensure(result.conforms_to_state(xname));
64 
65  ensure(result.name() == xname);
66  ensure(!result.is_persistent());
67 
68  ensure(result.hub_id() == xhub_id);
69  ensure(result.ct() == 1);
70  ensure(result.is_gathered());
71 
72  // Exit:
73 
74  return result;
75 }
76 
79 new_space(index_space_family& xid_spaces,
80  pod_index_type xid,
81  const std::string& xname,
82  pod_type xhub_id)
83 {
84  // Preconditions:
85 
86  require(!xid_spaces.contains(xid));
87  require(xid_spaces.is_explicit_interval(xid));
88  require(!xname.empty());
89  require(!xid_spaces.contains(xname));
90  require(xid_spaces.hub_id_space().contains(xhub_id));
91 
92  // Body:
93 
95  lstate->new_state(xid_spaces, xid, xname, false);
96 
97  lstate->_hub_id = xhub_id;
98  lstate->_ct = 1;
99  lstate->_begin = 0;
100  lstate->_end = 1;
101 
102  singleton_index_space_handle result(*lstate);
103 
104  // Postconditions:
105 
106  ensure(&result.id_spaces() == &xid_spaces);
107  ensure(xid_spaces.contains(xname));
108  ensure(result.conforms_to_state(xname));
109 
110  ensure(result.index() == xid);
111  ensure(result.name() == xname);
112  ensure(!result.is_persistent());
113 
114  ensure(result.hub_id() == xhub_id);
115  ensure(result.ct() == 1);
116  ensure(result.is_gathered());
117 
118  // Exit:
119 
120  return result;
121 }
122 
123 // PROTECTED MEMBER FUNCTIONS
124 
125 // PRIVATE MEMBER FUNCTIONS
126 
127 
128 // ===========================================================
129 // SINGLETON_INDEX_SPACE_STATE FACET
130 // ===========================================================
131 
132 // PUBLIC MEMBER FUNCTIONS
133 
134 // PROTECTED MEMBER FUNCTIONS
135 
139  _hub_id(invalid_pod_index())
140 {
141  // Preconditions:
142 
143  // Body:
144 
145  _ct = 0;
146  _begin = 0;
147  _end = 0;
148 
149  // Postconditions:
150 
151  ensure(invariant());
152  ensure(is_empty());
153  ensure(!is_valid(hub_id()));
154  ensure(ct() == 0);
155  ensure(begin() == 0);
156  ensure(end() == 0);
157 
158  // Exit:
159 
160  return;
161 }
162 
165 {
166  // Preconditions:
167 
168  // Body:
169 
170  // nothing to do.
171 
172  // Postconditions:
173 
174  // Exit:
175 
176  return;
177 }
178 
179 // PRIVATE MEMBER FUNCTIONS
180 
181 
182 // ===========================================================
183 // SINGLETON INDEX SPACE FACET
184 // ===========================================================
185 
186 // PUBLIC MEMBER FUNCTIONS
187 
190 hub_id() const
191 {
192  // Preconditions:
193 
194  // Body:
195 
196  pod_type result = _hub_id;
197 
198  // Postconditions:
199 
200  ensure(is_basic_query);
201 
202  // Exit:
203 
204  return result;
205 }
206 
207 // PROTECTED MEMBER FUNCTIONS
208 
209 // PRIVATE MEMBER FUNCTIONS
210 
211 
212 // ===========================================================
213 // EXPLICIT_INDEX_SPACE_STATE FACET
214 // ===========================================================
215 
216 // PUBLIC MEMBER FUNCTIONS
217 
218 bool
221 {
222  // Preconditions:
223 
224  require(is_ancestor_of(&xother));
225 
226  // Body:
227 
228  const singleton_index_space_state& lother =
229  dynamic_cast<const singleton_index_space_state&>(xother);
230 
231  bool result = explicit_index_space_state::operator==(xother);
232  result = result && (_hub_id == lother._hub_id);
233 
234  // Postconditions:
235 
236  // Exit
237 
238  return result;
239 }
240 
243 deep_size(bool xinclude_shallow) const
244 {
245  // Preconditions:
246 
247  // Body:
248 
249  size_type result = sheaf::deep_size(*this, xinclude_shallow);
250 
251  // Postconditions:
252 
253  ensure(result >= 0);
254 
255  // Exit:
256 
257  return result;
258 }
259 
260 // PROTECTED MEMBER FUNCTIONS
261 
265 {
266  // Preconditions:
267 
268  require(is_ancestor_of(&xother));
269 
270  // Body:
271 
272  const singleton_index_space_state& lother =
273  dynamic_cast<const singleton_index_space_state&>(xother);
274 
275  _hub_id = lother._hub_id;
276 
277  (void) explicit_index_space_state::operator=(xother);
278 
279  // Postconditions:
280 
281  ensure(invariant());
282  ensure((*this) == xother);
283 
284  // Exit
285 
286  return *this;
287 }
288 
289 // PRIVATE MEMBER FUNCTIONS
290 
291 
292 // ===========================================================
293 // INDEX SPACE FACET
294 // ===========================================================
295 
296 // PUBLIC MEMBER FUNCTIONS
297 
298 bool
300 contains(pod_type xid) const
301 {
302  // Preconditions:
303 
304  // Body:
305 
306  bool result = (xid == 0);
307 
308  // Postconditions:
309 
310  ensure(is_basic_query);
311 
312  // Exit
313 
314  return result;
315 }
316 
317 bool
320 {
321  // Preconditions:
322 
323  // Body:
324 
325  bool result = (xhub_id == _hub_id);
326 
327  // Postconditions:
328 
329  ensure(is_basic_query);
330 
331  // Exit
332 
333  return result;
334 }
335 
338 pod(pod_type xhub_id) const
339 {
340  // Preconditions:
341 
342  // Body:
343 
344  pod_type result =
345  contains_hub(xhub_id) ? 0 : invalid_pod_index();
346 
347  // Postconditions:
348 
349  ensure(!is_valid(result) || contains(result));
350 
351  // Exit
352 
353  return result;
354 }
355 
359 {
360  // Preconditions:
361 
362  // Body:
363 
364  pod_type result = contains(xid) ? _hub_id : invalid_pod_index();
365 
366  // Postconditions:
367 
368  ensure(!is_valid(result) || contains_unglued_hub(result));
369 
370  // Exit:
371 
372  return result;
373 }
374 
375 // PROTECTED MEMBER FUNCTIONS
376 
377 // PRIVATE MEMBER FUNCTIONS
378 
379 
380 // ===========================================================
381 // HANDLE POOL FACET
382 // ===========================================================
383 
384 // PUBLIC MEMBER FUNCTIONS
385 
389 {
390  // Preconditions:
391 
392  // Body:
393 
394  size_type result = handles().ct();
395 
396  // Postconditions:
397 
398  ensure(result >= 0);
399 
400  // Exit:
401 
402  return result;
403 }
404 
408 {
409  // Preconditions:
410 
411  // Body:
412 
413  size_type result = sheaf::deep_size(handles(), true);
414 
415  // Postconditions:
416 
417  ensure(result >= 0);
418 
419  // Exit:
420 
421  return result;
422 }
423 
427 {
428  // Preconditions:
429 
430  // Body:
431 
432  singleton_index_space_handle& result = handles().get();
433  attach(result);
434 
435  // Postconditions:
436 
437  ensure(result.is_attached());
438 
439  // Exit:
440 
441  return result;
442 }
443 
444 void
447 {
448  // Preconditions:
449 
450  require(allocated_id_space(xid_space));
451 
452  // Body:
453 
454  // Detach the handle.
455 
456  xid_space.detach();
457 
458  // Release the handle to the pool.
459 
460  handles().release(reinterpret_cast<singleton_index_space_handle&>(xid_space));
461 
462  // Postconditions:
463 
464  ensure(is_basic_query);
465 
466  // Exit:
467 
468  return;
469 }
470 
471 bool
473 allocated_id_space(const index_space_handle& xid_space) const
474 {
475  // Preconditions:
476 
477  // Body:
478 
479  const singleton_index_space_handle* lid_space =
480  dynamic_cast<const singleton_index_space_handle*>(&xid_space);
481 
482  bool result = (lid_space != 0) && handles().allocated(*lid_space);
483 
484  // Postconditions:
485 
486  ensure(is_basic_query);
487 
488  // Exit:
489 
490  return result;
491 }
492 
493 // PROTECTED MEMBER FUNCTIONS
494 
495 // PRIVATE MEMBER FUNCTIONS
496 
498 sheaf::singleton_index_space_state::
499 handles()
500 {
501  // Preconditions:
502 
503  // Body:
504 
506 
507  // Postconditions:
508 
509  ensure(is_basic_query);
510 
511  // Exit:
512 
513  return result;
514 }
515 
516 
517 // ===========================================================
518 // ITERATOR POOL FACET
519 // ===========================================================
520 
521 // PUBLIC MEMBER FUNCTIONS
522 
526 {
527  // Preconditions:
528 
529  // Body:
530 
531  size_type result = iterators().ct();
532 
533  // Postconditions:
534 
535  ensure(result >= 0);
536 
537  // Exit:
538 
539  return result;
540 }
541 
545 {
546  // Preconditions:
547 
548  // Body:
549 
550  size_type result = sheaf::deep_size(iterators(), true);
551 
552  // Postconditions:
553 
554  ensure(result >= 0);
555 
556  // Exit:
557 
558  return result;
559 }
560 
564 {
565  // Preconditions:
566 
567  // Body:
568 
569  singleton_index_space_iterator& result = iterators().get();
570  attach(result);
571 
572  // Postconditions:
573 
574  ensure(result.is_attached());
575 
576  // Exit:
577 
578  return result;
579 }
580 
581 void
584 {
585  // Preconditions:
586 
587  require(allocated_iterator(xitr));
588 
589  // Body:
590 
591  // Detach the iterator.
592 
593  xitr.detach();
594 
595  // Release the iterator to the pool.
596 
597  iterators().release(reinterpret_cast<singleton_index_space_iterator&>(xitr));
598 
599  // Postconditions:
600 
601  ensure(is_basic_query);
602 
603  // Exit:
604 
605  return;
606 }
607 
608 bool
611 {
612  // Preconditions:
613 
614  // Body:
615 
616  const singleton_index_space_iterator* litr =
617  dynamic_cast<const singleton_index_space_iterator*>(&xitr);
618 
619  bool result = (litr != 0) && iterators().allocated(*litr);
620 
621  // Postconditions:
622 
623  ensure(is_basic_query);
624 
625  // Exit:
626 
627  return result;
628 }
629 
630 // PROTECTED MEMBER FUNCTIONS
631 
632 // PRIVATE MEMBER FUNCTIONS
633 
635 sheaf::singleton_index_space_state::
636 iterators()
637 {
638  // Preconditions:
639 
640  // Body:
641 
643 
644  // Postconditions:
645 
646  ensure(is_basic_query);
647 
648  // Exit:
649 
650  return result;
651 }
652 
653 
654 // ===========================================================
655 // FACTORY FACET
656 // ===========================================================
657 
658 // PUBLIC MEMBER FUNCTIONS
659 
660 const std::string&
662 class_name() const
663 {
664  static const std::string result("singleton_index_space_state");
665  return result;
666 }
667 
670 clone() const
671 {
672  // Preconditions:
673 
674  // Body:
675 
677 
678  // Postconditions:
679 
680  ensure(result != 0);
681  ensure(is_same_type(result));
682 
683  // Exit:
684 
685  return result;
686 }
687 
688 // PROTECTED MEMBER FUNCTIONS
689 
690 // PRIVATE MEMBER FUNCTIONS
691 
692 bool
693 sheaf::singleton_index_space_state::
694 make_prototype()
695 {
696  // Preconditions:
697 
698  // Body:
699 
701 
702  id_space_factory().insert_prototype(lproto);
703 
704  // Postconditions:
705 
706  // Exit:
707 
708  return true;
709 }
710 
711 
712 // ===========================================================
713 // ANY FACET
714 // ===========================================================
715 
716 // PUBLIC MEMBER FUNCTIONS
717 
718 bool
720 is_ancestor_of(const any *other) const
721 {
722  // Preconditions:
723 
724  require(other != 0);
725 
726  // Body:
727 
728  // True if other conforms to this
729 
730  bool result = dynamic_cast<const singleton_index_space_state*>(other) != 0;
731 
732  // Postconditions:
733 
734  // Exit:
735 
736  return result;
737 }
738 
739 bool
741 invariant() const
742 {
743  bool result = true;
744 
745  if(invariant_check())
746  {
747  // Prevent recursive calls to invariant
748 
750 
751  // Must satisfy base class invariant
752 
754 
755  // Invariances for this class:
756 
757  invariance(is_gathered());
758 
759  // Finished, turn invariant checking back on.
760 
762  }
763 
764  // Exit
765 
766  return result;
767 }
768 
769 // PROTECTED MEMBER FUNCTIONS
770 
771 // PRIVATE MEMBER FUNCTIONS
772 
773 
774 // ===========================================================
775 // NON-MEMBER FUNCTIONS
776 // ===========================================================
777 
778 size_t
779 sheaf::
780 deep_size(const singleton_index_space_state& xn, bool xinclude_shallow)
781 {
782  // Preconditions:
783 
784  // Body:
785 
786  size_t result = xinclude_shallow ? sizeof(xn) : 0;
787 
788  // Add any contributions from the parent class.
789 
790  const explicit_index_space_state& ixn = static_cast<const explicit_index_space_state&>(xn);
791  result += deep_size(ixn, false);
792 
793  // Postconditions:
794 
795  ensure(result >= 0);
796 
797  // Exit
798 
799  return result;
800 }
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 explicit_index_space_state &xother) const
True if this is equivalent to xother.
An iterator over an id space in which contains only one id.
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...
size_type ct() const
The number of members.
virtual void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
virtual singleton_index_space_state * clone() const
Virtual constructor; create a new instance of the same type at this.
virtual pod_type index() const
Index of this space.
virtual size_type deep_size(bool xinclude_shallow) const
The deep size of this.
An abstract iterator over the ids of an id space.
static singleton_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, pod_type xhub_id)
Create a new singleton id space in the id space family xid_space at the next available id space index...
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
pod_type end() const
Ending id of this space.
virtual bool is_persistent() const
True if this id space should be written to disk.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
virtual bool invariant() const
Class invariant.
virtual void detach()=0
Detach this handle form its state, if any.
bool is_gathered() const
True if begin() == 0 and end() == ct().
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).
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
virtual void detach()=0
Detach this handle form its state, if any.
virtual singleton_index_space_state & operator=(const explicit_index_space_state &xother)
Assignment operator.
void attach(explicit_index_space_handle &xid_space) const
Attach the id space handle xid_space to this state.
pod_type _end
Ending id of this space.
Abstract base class with useful features for all objects.
Definition: any.h:39
static size_type handle_pool_ct()
The number of handles in the pool.
virtual bool contains_unglued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual bool contains(pod_type xid) const
True if this space contains id xid.
std::string name() const
Name of this space.
virtual index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool.
pod_type begin() const
Beginning id of this space.
virtual bool is_attached() const
True if this handle is attached to a 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
static size_type iterator_pool_ct()
The number of iterators in the pool.
bool is_explicit_interval(pod_type xid)
True, if and only if the id space interval that contains index xid is an explicit interval...
static size_type iterator_pool_deep_size()
The deep size of the iterator pool.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
pod_type hub_id() const
The hub id of the singleton id.
An implementation of class explicit_index_space_handle that has a singleton id space state...
size_type _ct
The number of members.
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.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
pod_type hub_id() const
The hub id of the singleton id.
pod_index_type pod_type
The "plain old data" index type for this.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
static size_type handle_pool_deep_size()
The deep size of the handle pool.
virtual bool invariant() const
Class invariant.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
virtual bool allocated_id_space(const index_space_handle &xid_space) const
True if and only if id space handle xid_space was allocated by the handle pool.
An implementation of explicit_index_space_state for an id space containing a single id...
virtual size_type ct() const
The number of members.
bool is_empty() const
True if there are no ids in the space.
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.
Factory and container for a family of id spaces.
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
static factory< explicit_index_space_state > & id_space_factory()
A factory for making descendants of this class.
pod_type _begin
Beginning id of this space.
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 bool is_attached() const
True if this iterator is attached to a state.
A reallocated pool of objects of type T. Objects in the pool are either allocated or stored in a free...
Definition: list_pool.h:42
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
virtual bool contains(pod_type xid) const
True if this space contains id xid.
virtual const std::string & class_name() const
The name of this class.