SheafSystem  0.0.0.0
primary_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/primary_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/primary_index_space_handle.h"
27 #include "SheafSystem/primary_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 xoffset,
40  size_type xct)
41 {
42  // Preconditions:
43 
44  require(!xname.empty());
45  require(!xid_spaces.contains(xname));
46  require(xoffset >= 0);
47  require(xct > 0);
48 
49  // Body:
50 
52  lstate->new_state(xid_spaces, xname, false);
53 
54  lstate->_offset = xoffset;
55  lstate->_ct = xct;
56  lstate->_begin = 0;
57  lstate->_end = xct;
58 
59  primary_index_space_handle result(*lstate);
60 
61  // Postconditions:
62 
63  ensure(&result.id_spaces() == &xid_spaces);
64  ensure(xid_spaces.contains(xname));
65  ensure(result.conforms_to_state(xname));
66 
67  ensure(result.name() == xname);
68  ensure(!result.is_persistent());
69 
70  ensure(result.offset() == xoffset);
71  ensure(result.ct() == xct);
72  ensure(result.is_gathered());
73 
74  // Exit:
75 
76  return result;
77 }
78 
81 new_space(index_space_family& xid_spaces,
82  pod_index_type xid,
83  const std::string& xname,
84  pod_type xoffset,
85  size_type xct)
86 {
87  // Preconditions:
88 
89  require(!xid_spaces.contains(xid));
90  require(xid_spaces.is_explicit_interval(xid));
91  require(!xname.empty());
92  require(!xid_spaces.contains(xname));
93  require(xoffset >= 0);
94  require(xct > 0);
95 
96  // Body:
97 
99  lstate->new_state(xid_spaces, xid, xname, false);
100 
101  lstate->_offset = xoffset;
102  lstate->_ct = xct;
103  lstate->_begin = 0;
104  lstate->_end = xct;
105 
106  primary_index_space_handle result(*lstate);
107 
108  // Postconditions:
109 
110  ensure(&result.id_spaces() == &xid_spaces);
111  ensure(xid_spaces.contains(xname));
112  ensure(result.conforms_to_state(xname));
113 
114  ensure(result.index() == xid);
115  ensure(result.name() == xname);
116  ensure(!result.is_persistent());
117 
118  ensure(result.offset() == xoffset);
119  ensure(result.ct() == xct);
120  ensure(result.is_gathered());
121 
122  // Exit:
123 
124  return result;
125 }
126 
127 // PROTECTED MEMBER FUNCTIONS
128 
129 // PRIVATE MEMBER FUNCTIONS
130 
131 
132 // ===========================================================
133 // PRIMARY_INDEX_SPACE_STATE FACET
134 // ===========================================================
135 
136 // PUBLIC MEMBER FUNCTIONS
137 
138 // PROTECTED MEMBER FUNCTIONS
139 
144  _offset(0)
145 {
146  // Preconditions:
147 
148  // Body:
149 
150  _ct = 0;
151  _begin = 0;
152  _end = 0;
153 
154  // Postconditions:
155 
156  ensure(invariant());
157  ensure(is_empty());
158  ensure(offset() == 0);
159  ensure(begin() == 0);
160  ensure(end() == 0);
161 
162  // Exit:
163 
164  return;
165 }
166 
169 {
170  // Preconditions:
171 
172  // Body:
173 
174  // nothing to do.
175 
176  // Postconditions:
177 
178  // Exit:
179 
180  return;
181 }
182 
183 // PRIVATE MEMBER FUNCTIONS
184 
185 
186 // ===========================================================
187 // PRIMARY INDEX SPACE FACET
188 // ===========================================================
189 
190 // PUBLIC MEMBER FUNCTIONS
191 
194 offset() const
195 {
196  // Preconditions:
197 
198  // Body:
199 
200  pod_type result = _offset;
201 
202  // Postconditions:
203 
204  ensure(is_basic_query);
205 
206  // Exit:
207 
208  return result;
209 }
210 
211 void
214 {
215  // Preconditions:
216 
217  require(xct > ct());
218  require(hub_id_space().interval_is_available(end(), end() - ct() + xct));
219 
220  // Body:
221 
222  _ct = xct;
223  _end = _ct;
224 
225  // Postconditions:
226 
227  ensure(invariant());
228  ensure(ct() == xct);
229 
230  // Exit:
231 
232  return;
233 }
234 
237 hub_term_id() const
238 {
239  // Preconditions:
240 
241  // Body:
242 
243  pod_type result = _hub_term_id;
244 
245  // Postconditions:
246 
247  ensure(is_basic_query);
248 
249  // Exit:
250 
251  return result;
252 }
253 
254 // PROTECTED MEMBER FUNCTIONS
255 
256 void
259 {
260  // Preconditions:
261 
262  // Body:
263 
264  _hub_term_id = xhub_term_id;
265 
266  // Postconditions:
267 
268  ensure(hub_term_id() == xhub_term_id);
269 
270  // Exit:
271 
272  return;
273 }
274 
275 // PRIVATE MEMBER FUNCTIONS
276 
277 
278 // ===========================================================
279 // EXPLICIT_INDEX_SPACE_STATE FACET
280 // ===========================================================
281 
282 // PUBLIC MEMBER FUNCTIONS
283 
284 bool
287 {
288  // Preconditions:
289 
290  require(is_ancestor_of(&xother));
291 
292  // Body:
293 
294  const primary_index_space_state& lother =
295  dynamic_cast<const primary_index_space_state&>(xother);
296 
297  bool result = explicit_index_space_state::operator==(xother);
298 
299  result = result && (_offset == lother._offset);
300  result = result && (_hub_term_id == lother._hub_term_id);
301 
302  // Postconditions:
303 
304  // Exit
305 
306  return result;
307 }
308 
311 deep_size(bool xinclude_shallow) const
312 {
313  // Preconditions:
314 
315  // Body:
316 
317  size_type result = sheaf::deep_size(*this, xinclude_shallow);
318 
319  // Postconditions:
320 
321  ensure(result >= 0);
322 
323  // Exit:
324 
325  return result;
326 }
327 
328 // PROTECTED MEMBER FUNCTIONS
329 
333 {
334  // Preconditions:
335 
336  require(is_ancestor_of(&xother));
337 
338  // Body:
339 
340  const primary_index_space_state& lother =
341  dynamic_cast<const primary_index_space_state&>(xother);
342 
343  _offset = lother._offset;
344  _hub_term_id = lother._hub_term_id;
345 
346  (void) explicit_index_space_state::operator=(xother);
347 
348  // Postconditions:
349 
350  ensure(invariant());
351  ensure((*this) == xother);
352 
353  // Exit
354 
355  return *this;
356 }
357 
358 // PRIVATE MEMBER FUNCTIONS
359 
360 
361 // ===========================================================
362 // INDEX SPACE FACET
363 // ===========================================================
364 
365 // PUBLIC MEMBER FUNCTIONS
366 
367 bool
369 contains(pod_type xid) const
370 {
371  // Preconditions:
372 
373  // Body:
374 
375  bool result = (0 <= xid) && (xid < _end);
376 
377  // Postconditions:
378 
379  ensure(is_basic_query);
380 
381  // Exit
382 
383  return result;
384 }
385 
386 bool
389 {
390  // Preconditions:
391 
392  // Body:
393 
394  bool result = (_offset <= xhub_id) && (xhub_id < _offset + _end);
395 
396  // Postconditions:
397 
398  ensure(is_basic_query);
399 
400  // Exit
401 
402  return result;
403 }
404 
407 pod(pod_type xhub_id) const
408 {
409  // Preconditions:
410 
411  // Body:
412 
413  pod_type result =
414  contains_hub(xhub_id) ? xhub_id - _offset : invalid_pod_index();
415 
416  // Postconditions:
417 
418  ensure(!is_valid(result) || contains(result));
419 
420  // Exit
421 
422  return result;
423 }
424 
428 {
429  // Preconditions:
430 
431  // Body:
432 
433  pod_type result = contains(xid) ? xid + _offset : invalid_pod_index();
434 
435  // Postconditions:
436 
437  ensure(!is_valid(result) || contains_unglued_hub(result));
438 
439  // Exit:
440 
441  return result;
442 }
443 
444 // PROTECTED MEMBER FUNCTIONS
445 
446 // PRIVATE MEMBER FUNCTIONS
447 
448 
449 // ===========================================================
450 // HANDLE POOL FACET
451 // ===========================================================
452 
453 // PUBLIC MEMBER FUNCTIONS
454 
458 {
459  // Preconditions:
460 
461  // Body:
462 
463  size_type result = handles().ct();
464 
465  // Postconditions:
466 
467  ensure(result >= 0);
468 
469  // Exit:
470 
471  return result;
472 }
473 
477 {
478  // Preconditions:
479 
480  // Body:
481 
482  size_type result = sheaf::deep_size(handles(), true);
483 
484  // Postconditions:
485 
486  ensure(result >= 0);
487 
488  // Exit:
489 
490  return result;
491 }
492 
496 {
497  // Preconditions:
498 
499  // Body:
500 
501  primary_index_space_handle& result = handles().get();
502  attach(result);
503 
504  // Postconditions:
505 
506  ensure(result.is_attached());
507 
508  // Exit:
509 
510  return result;
511 }
512 
513 void
516 {
517  // Preconditions:
518 
519  require(allocated_id_space(xid_space));
520 
521  // Body:
522 
523  // Detach the handle.
524 
525  xid_space.detach();
526 
527  // Release the handle to the pool.
528 
529  handles().release(reinterpret_cast<primary_index_space_handle&>(xid_space));
530 
531  // Postconditions:
532 
533  ensure(is_basic_query);
534 
535  // Exit:
536 
537  return;
538 }
539 
540 bool
542 allocated_id_space(const index_space_handle& xid_space) const
543 {
544  // Preconditions:
545 
546  // Body:
547 
548  const primary_index_space_handle* lid_space =
549  dynamic_cast<const primary_index_space_handle*>(&xid_space);
550 
551  bool result = (lid_space != 0) && handles().allocated(*lid_space);
552 
553  // Postconditions:
554 
555  ensure(is_basic_query);
556 
557  // Exit:
558 
559  return result;
560 }
561 
562 // PROTECTED MEMBER FUNCTIONS
563 
564 // PRIVATE MEMBER FUNCTIONS
565 
567 sheaf::primary_index_space_state::
568 handles()
569 {
570  // Preconditions:
571 
572  // Body:
573 
575 
576  // Postconditions:
577 
578  ensure(is_basic_query);
579 
580  // Exit:
581 
582  return result;
583 }
584 
585 
586 // ===========================================================
587 // ITERATOR POOL FACET
588 // ===========================================================
589 
590 // PUBLIC MEMBER FUNCTIONS
591 
595 {
596  // Preconditions:
597 
598  // Body:
599 
600  size_type result = iterators().ct();
601 
602  // Postconditions:
603 
604  ensure(result >= 0);
605 
606  // Exit:
607 
608  return result;
609 }
610 
614 {
615  // Preconditions:
616 
617  // Body:
618 
619  size_type result = sheaf::deep_size(iterators(), true);
620 
621  // Postconditions:
622 
623  ensure(result >= 0);
624 
625  // Exit:
626 
627  return result;
628 }
629 
633 {
634  // Preconditions:
635 
636  // Body:
637 
638  primary_index_space_iterator& result = iterators().get();
639  attach(result);
640 
641  // Postconditions:
642 
643  ensure(result.is_attached());
644 
645  // Exit:
646 
647  return result;
648 }
649 
650 void
653 {
654  // Preconditions:
655 
656  require(allocated_iterator(xitr));
657 
658  // Body:
659 
660  // Detach the iterator.
661 
662  xitr.detach();
663 
664  // Release the iterator to the pool.
665 
666  iterators().release(reinterpret_cast<primary_index_space_iterator&>(xitr));
667 
668  // Postconditions:
669 
670  ensure(is_basic_query);
671 
672  // Exit:
673 
674  return;
675 }
676 
677 bool
680 {
681  // Preconditions:
682 
683  // Body:
684 
685  const primary_index_space_iterator* litr =
686  dynamic_cast<const primary_index_space_iterator*>(&xitr);
687 
688  bool result = (litr != 0) && iterators().allocated(*litr);
689 
690  // Postconditions:
691 
692  ensure(is_basic_query);
693 
694  // Exit:
695 
696  return result;
697 }
698 
699 // PROTECTED MEMBER FUNCTIONS
700 
701 // PRIVATE MEMBER FUNCTIONS
702 
704 sheaf::primary_index_space_state::
705 iterators()
706 {
707  // Preconditions:
708 
709  // Body:
710 
712 
713  // Postconditions:
714 
715  ensure(is_basic_query);
716 
717  // Exit:
718 
719  return result;
720 }
721 
722 
723 // ===========================================================
724 // FACTORY FACET
725 // ===========================================================
726 
727 // PUBLIC MEMBER FUNCTIONS
728 
729 const std::string&
731 class_name() const
732 {
733  static const std::string result("primary_index_space_state");
734  return result;
735 }
736 
739 clone() const
740 {
741  // Preconditions:
742 
743  // Body:
744 
746 
747  // Postconditions:
748 
749  ensure(result != 0);
750  ensure(is_same_type(result));
751 
752  // Exit:
753 
754  return result;
755 }
756 
757 // PROTECTED MEMBER FUNCTIONS
758 
759 // PRIVATE MEMBER FUNCTIONS
760 
761 bool
762 sheaf::primary_index_space_state::
763 make_prototype()
764 {
765  // Preconditions:
766 
767  // Body:
768 
770 
771  id_space_factory().insert_prototype(lproto);
772 
773  // Postconditions:
774 
775  // Exit:
776 
777  return true;
778 }
779 
780 
781 // ===========================================================
782 // ANY FACET
783 // ===========================================================
784 
785 // PUBLIC MEMBER FUNCTIONS
786 
787 bool
789 is_ancestor_of(const any *other) const
790 {
791  // Preconditions:
792 
793  require(other != 0);
794 
795  // Body:
796 
797  // True if other conforms to this
798 
799  bool result = dynamic_cast<const primary_index_space_state*>(other) != 0;
800 
801  // Postconditions:
802 
803  // Exit:
804 
805  return result;
806 }
807 
808 bool
810 invariant() const
811 {
812  bool result = true;
813 
814  if(invariant_check())
815  {
816  // Prevent recursive calls to invariant
817 
819 
820  // Must satisfy base class invariant
821 
823 
824  // Invariances for this class:
825 
826  invariance(is_gathered());
827 
828  // Finished, turn invariant checking back on.
829 
831  }
832 
833  // Exit:
834 
835  return result;
836 }
837 
838 // PROTECTED MEMBER FUNCTIONS
839 
840 // PRIVATE MEMBER FUNCTIONS
841 
842 
843 
844 // ===========================================================
845 // NON-MEMBER FUNCTIONS
846 // ===========================================================
847 
848 size_t
849 sheaf::
850 deep_size(const primary_index_space_state& xn, bool xinclude_shallow)
851 {
852  // Preconditions:
853 
854  // Body:
855 
856  size_t result = xinclude_shallow ? sizeof(xn) : 0;
857 
858  // Add any contributions from the parent class.
859 
860  const explicit_index_space_state& ixn = static_cast<const explicit_index_space_state&>(xn);
861  result += deep_size(ixn, false);
862 
863  // Postconditions:
864 
865  ensure(result >= 0);
866 
867  // Exit
868 
869  return result;
870 }
virtual void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
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...
static size_type iterator_pool_deep_size()
The deep size of the iterator pool.
size_type ct() const
The number of members.
virtual pod_type index() const
Index of this space.
An abstract iterator over the ids of an id space.
virtual primary_index_space_state * clone() const
Virtual constructor; create a new instance of the same type at this.
pod_type end() const
Ending id of this space.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
static size_type iterator_pool_ct()
The number of iterators in the pool.
virtual void extend(size_type xct)
Extend this id space to have ct() == xct.
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 contains(pod_type xid) const
True if this space contains id xid.
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_handle & get_id_space() const
Allocates an id space handle from the handle pool.
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.
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_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual void detach()=0
Detach this handle form its state, if any.
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.
An implementation of class explicit_index_space_handle that has a primary id space state...
virtual const std::string & class_name() const
The name of this class.
static size_type handle_pool_deep_size()
The deep size of the handle pool.
Abstract base class with useful features for all objects.
Definition: any.h:39
pod_type hub_term_id() const
The term id of this in the hub id space.
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
pod_type offset() const
The offset into the hub id space.
virtual size_type deep_size(bool xinclude_shallow) const
The deep size of this.
std::string name() const
Name of this space.
static primary_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, pod_type xoffset, size_type xct)
Create a new primary id space in the id space family xid_space at the next available id space index w...
pod_type begin() const
Beginning id of this space.
void put_hub_term_id(pod_type xhub_term_id)
Sets hub_term_id() to xhub_term_id.
virtual bool is_attached() const
True if this handle is attached to a state.
pod_type _hub_term_id
The term id of this in the hub id space.
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
const hub_index_space_handle & hub_id_space() const
The hub id space.
bool is_explicit_interval(pod_type xid)
True, if and only if the id space interval that contains index xid is an explicit interval...
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
A computed implementation class explicit_index_space_state that is used for the terms of the primary ...
void new_state(index_space_family &xid_spaces, const std::string &xname, bool xis_persistent)
Create a new id space state in the id space family xid_spaces at the next available id space index wi...
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...
size_type _ct
The number of members.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
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
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.
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
virtual primary_index_space_state & operator=(const explicit_index_space_state &xother)
Assignment operator.
virtual bool invariant() const
Class invariant.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
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.
pod_type offset() const
The offset into 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
static factory< explicit_index_space_state > & id_space_factory()
A factory for making descendants of this class.
An iterator over a gathered id space in which the equivlence between the ids in the space and the hub...
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 invariant() const
Class invariant.
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
pod_type _offset
The offset into the hub id space.
static size_type handle_pool_ct()
The number of handles in the pool.