SheafSystem  0.0.0.0
reserved_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/reserved_primary_index_space_state.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/deep_size.h"
24 #include "SheafSystem/hub_index_space_handle.h"
25 #include "SheafSystem/index_space_family.h"
26 #include "SheafSystem/reserved_primary_index_space_handle.h"
27 #include "SheafSystem/reserved_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  lstate->_next_hub_id = xoffset;
60 
62 
63  // Postconditions:
64 
65  ensure(&result.id_spaces() == &xid_spaces);
66  ensure(xid_spaces.contains(xname));
67  ensure(result.conforms_to_state(xname));
68 
69  ensure(result.name() == xname);
70  ensure(!result.is_persistent());
71 
72  ensure(result.offset() == xoffset);
73  ensure(result.ct() == xct);
74  ensure(result.next_hub_id() == xoffset);
75 
76  // Exit:
77 
78  return result;
79 }
80 
83 new_space(index_space_family& xid_spaces,
84  pod_index_type xid,
85  const std::string& xname,
86  pod_type xoffset,
87  size_type xct)
88 {
89  // Preconditions:
90 
91  require(!xid_spaces.contains(xid));
92  require(xid_spaces.is_explicit_interval(xid));
93  require(!xname.empty());
94  require(!xid_spaces.contains(xname));
95  require(xoffset >= 0);
96  require(xct > 0);
97 
98  // Body:
99 
101  lstate->new_state(xid_spaces, xid, xname, false);
102 
103  lstate->_offset = xoffset;
104  lstate->_ct = xct;
105  lstate->_begin = 0;
106  lstate->_end = xct;
107 
108  lstate->_next_hub_id = xoffset;
109 
110  reserved_primary_index_space_handle result(*lstate);
111 
112  // Postconditions:
113 
114  ensure(&result.id_spaces() == &xid_spaces);
115  ensure(xid_spaces.contains(xname));
116  ensure(result.conforms_to_state(xname));
117 
118  ensure(result.index() == xid);
119  ensure(result.name() == xname);
120  ensure(!result.is_persistent());
121 
122  ensure(result.offset() == xoffset);
123  ensure(result.ct() == xct);
124  ensure(result.next_hub_id() == xoffset);
125 
126  // Exit:
127 
128  return result;
129 }
130 
131 // PROTECTED MEMBER FUNCTIONS
132 
133 // PRIVATE MEMBER FUNCTIONS
134 
135 
136 // ===========================================================
137 // RESERVED_PRIMARY_INDEX_SPACE_STATE FACET
138 // ===========================================================
139 
140 // PUBLIC MEMBER FUNCTIONS
141 
142 // PROTECTED MEMBER FUNCTIONS
143 
147 {
148  // Preconditions:
149 
150  // Body:
151 
153 
154  // Postconditions:
155 
156  ensure(invariant());
157  ensure(is_empty());
158  ensure(!is_valid(next_hub_id()));
159 
160  // Exit:
161 
162  return;
163 }
164 
167 {
168  // Preconditions:
169 
170  // Body:
171 
172  // nothing to do.
173 
174  // Postconditions:
175 
176  // Exit:
177 
178  return;
179 }
180 
181 // PRIVATE MEMBER FUNCTIONS
182 
183 
184 // ===========================================================
185 // OFFSET INDEX SPACE FACET
186 // ===========================================================
187 
188 // PUBLIC MEMBER FUNCTIONS
189 
193 {
194  // Preconditions:
195 
196  require(is_next_id_available());
197 
198  // Body:
199 
200  define_old_variable(size_type old_ct = ct());
201  define_old_variable(pod_type old_next_hub_id = next_hub_id());
202 
203  pod_type result = _next_hub_id;
204 
206 
207  // Postconditions:
208 
209  ensure(invariant());
210  ensure(next_hub_id() == old_next_hub_id + 1);
211 
212  // Exit:
213 
214  return result;
215 }
216 
220 {
221  // Preconditions:
222 
223  require(is_hub_id_available(xhub_id));
224 
225  // Body:
226 
227  define_old_variable(size_type old_ct = ct());
228  define_old_variable(pod_type old_next_hub_id = next_hub_id());
229 
230  if(_next_hub_id <= xhub_id)
231  {
232  _next_hub_id = xhub_id+1;
233  }
234 
235  // Postconditions:
236 
237  ensure(invariant());
238  ensure(next_hub_id() >= old_next_hub_id);
239 
240  // Exit:
241 
242  return xhub_id;
243 }
244 
247 next_hub_id() const
248 {
249  // Preconditions:
250 
251  // Body:
252 
253  pod_type result = _next_hub_id;
254 
255  // Postconditions:
256 
257  ensure(is_basic_query);
258 
259  // Exit:
260 
261  return result;
262 }
263 
264 bool
267 {
268  // Preconditions:
269 
270  // Body:
271 
272  bool result = (_next_hub_id < (_offset + _end));
273 
274  // Postconditions:
275 
276  ensure(is_basic_query);
277 
278  // Exit:
279 
280  return result;
281 }
282 
283 bool
286 {
287  // Preconditions:
288 
289  // Body:
290 
291  bool result =
292  (xhub_id >= _offset) && (xhub_id < (_offset + _end)) && !hub_id_space().contains(xhub_id);
293 
294  // Postconditions:
295 
296  ensure(is_basic_query);
297 
298  // Exit:
299 
300  return result;
301 }
302 
303 // PROTECTED MEMBER FUNCTIONS
304 
305 // PRIVATE MEMBER FUNCTIONS
306 
307 
308 // ===========================================================
309 // PRIMARY INDEX SPACE FACET
310 // ===========================================================
311 
312 // PUBLIC MEMBER FUNCTIONS
313 
314 // PROTECTED MEMBER FUNCTIONS
315 
316 // PRIVATE MEMBER FUNCTIONS
317 
318 
319 // ===========================================================
320 // EXPLICIT_INDEX_SPACE_STATE FACET
321 // ===========================================================
322 
323 // PUBLIC MEMBER FUNCTIONS
324 
325 bool
328 {
329  // Preconditions:
330 
331  require(is_ancestor_of(&xother));
332 
333  // Body:
334 
335  const reserved_primary_index_space_state& lother =
336  dynamic_cast<const reserved_primary_index_space_state&>(xother);
337 
338  bool result = primary_index_space_state::operator==(xother);
339  result = result && (_next_hub_id == lother._next_hub_id);
340 
341  // Postconditions:
342 
343  // Exit
344 
345  return result;
346 }
347 
350 deep_size(bool xinclude_shallow) const
351 {
352  // Preconditions:
353 
354  // Body:
355 
356  size_type result = sheaf::deep_size(*this, xinclude_shallow);
357 
358  // Postconditions:
359 
360  ensure(result >= 0);
361 
362  // Exit:
363 
364  return result;
365 }
366 
367 // PROTECTED MEMBER FUNCTIONS
368 
372 {
373  // Preconditions:
374 
375  require(is_ancestor_of(&xother));
376 
377  // Body:
378 
379  const reserved_primary_index_space_state& lother =
380  dynamic_cast<const reserved_primary_index_space_state&>(xother);
381 
382  _next_hub_id = lother._next_hub_id;
383 
384  (void) primary_index_space_state::operator=(xother);
385 
386  // Postconditions:
387 
388  ensure(invariant());
389  ensure((*this) == xother);
390 
391  // Exit
392 
393  return *this;
394 }
395 
396 // PRIVATE MEMBER FUNCTIONS
397 
398 
399 // ===========================================================
400 // INDEX SPACE FACET
401 // ===========================================================
402 
403 // PUBLIC MEMBER FUNCTIONS
404 
405 // PROTECTED MEMBER FUNCTIONS
406 
407 // PRIVATE MEMBER FUNCTIONS
408 
409 
410 // ===========================================================
411 // HANDLE POOL FACET
412 // ===========================================================
413 
414 // PUBLIC MEMBER FUNCTIONS
415 
419 {
420  // Preconditions:
421 
422  // Body:
423 
424  size_type result = handles().ct();
425 
426  // Postconditions:
427 
428  ensure(result >= 0);
429 
430  // Exit:
431 
432  return result;
433 }
434 
438 {
439  // Preconditions:
440 
441  // Body:
442 
443  size_type result = sheaf::deep_size(handles(), true);
444 
445  // Postconditions:
446 
447  ensure(result >= 0);
448 
449  // Exit:
450 
451  return result;
452 }
453 
457 {
458  // Preconditions:
459 
460  // Body:
461 
462  reserved_primary_index_space_handle& result = handles().get();
463  attach(result);
464 
465  // Postconditions:
466 
467  ensure(result.is_attached());
468 
469  // Exit:
470 
471  return result;
472 }
473 
474 void
477 {
478  // Preconditions:
479 
480  require(allocated_id_space(xid_space));
481 
482  // Body:
483 
484  // Detach the handle.
485 
486  xid_space.detach();
487 
488  // Release the handle to the pool.
489 
490  handles().release(reinterpret_cast<reserved_primary_index_space_handle&>(xid_space));
491 
492  // Postconditions:
493 
494  ensure(is_basic_query);
495 
496  // Exit:
497 
498  return;
499 }
500 
501 bool
503 allocated_id_space(const index_space_handle& xid_space) const
504 {
505  // Preconditions:
506 
507  // Body:
508 
509  const reserved_primary_index_space_handle* lid_space =
510  dynamic_cast<const reserved_primary_index_space_handle*>(&xid_space);
511 
512  bool result = (lid_space != 0) && handles().allocated(*lid_space);
513 
514  // Postconditions:
515 
516  ensure(is_basic_query);
517 
518  // Exit:
519 
520  return result;
521 }
522 
523 // PROTECTED MEMBER FUNCTIONS
524 
525 // PRIVATE MEMBER FUNCTIONS
526 
528 sheaf::reserved_primary_index_space_state::
529 handles()
530 {
531  // Preconditions:
532 
533  // Body:
534 
536 
537  // Postconditions:
538 
539  ensure(is_basic_query);
540 
541  // Exit:
542 
543  return result;
544 }
545 
546 
547 // ===========================================================
548 // ITERATOR POOL FACET
549 // ===========================================================
550 
551 // PUBLIC MEMBER FUNCTIONS
552 
556 {
557  // Preconditions:
558 
559  // Body:
560 
561  size_type result = iterators().ct();
562 
563  // Postconditions:
564 
565  ensure(result >= 0);
566 
567  // Exit:
568 
569  return result;
570 }
571 
575 {
576  // Preconditions:
577 
578  // Body:
579 
580  size_type result = sheaf::deep_size(iterators(), true);
581 
582  // Postconditions:
583 
584  ensure(result >= 0);
585 
586  // Exit:
587 
588  return result;
589 }
590 
594 {
595  // Preconditions:
596 
597  // Body:
598 
599  reserved_primary_index_space_iterator& result = iterators().get();
600  attach(result);
601 
602  // Postconditions:
603 
604  ensure(result.is_attached());
605 
606  // Exit:
607 
608  return result;
609 }
610 
611 void
614 {
615  // Preconditions:
616 
617  require(allocated_iterator(xitr));
618 
619  // Body:
620 
621  // Detach the iterator.
622 
623  xitr.detach();
624 
625  // Release the iterator to the pool.
626 
627  iterators().release(reinterpret_cast<reserved_primary_index_space_iterator&>(xitr));
628 
629  // Postconditions:
630 
631  ensure(is_basic_query);
632 
633  // Exit:
634 
635  return;
636 }
637 
638 bool
641 {
642  // Preconditions:
643 
644  // Body:
645 
647  dynamic_cast<const reserved_primary_index_space_iterator*>(&xitr);
648 
649  bool result = (litr != 0) && iterators().allocated(*litr);
650 
651  // Postconditions:
652 
653  ensure(is_basic_query);
654 
655  // Exit:
656 
657  return result;
658 }
659 
660 // PROTECTED MEMBER FUNCTIONS
661 
662 // PRIVATE MEMBER FUNCTIONS
663 
665 sheaf::reserved_primary_index_space_state::
666 iterators()
667 {
668  // Preconditions:
669 
670  // Body:
671 
673 
674  // Postconditions:
675 
676  ensure(is_basic_query);
677 
678  // Exit:
679 
680  return result;
681 }
682 
683 
684 // ===========================================================
685 // FACTORY FACET
686 // ===========================================================
687 
688 // PUBLIC MEMBER FUNCTIONS
689 
690 const std::string&
692 class_name() const
693 {
694  static const std::string result("reserved_primary_index_space_state");
695  return result;
696 }
697 
700 clone() const
701 {
702  // Preconditions:
703 
704  // Body:
705 
707 
708  // Postconditions:
709 
710  ensure(result != 0);
711  ensure(is_same_type(result));
712 
713  // Exit:
714 
715  return result;
716 }
717 
718 // PROTECTED MEMBER FUNCTIONS
719 
720 // PRIVATE MEMBER FUNCTIONS
721 
722 bool
723 sheaf::reserved_primary_index_space_state::
724 make_prototype()
725 {
726  // Preconditions:
727 
728  // Body:
729 
731 
732  id_space_factory().insert_prototype(lproto);
733 
734  // Postconditions:
735 
736  // Exit:
737 
738  return true;
739 }
740 
741 
742 // ===========================================================
743 // ANY FACET
744 // ===========================================================
745 
746 // PUBLIC MEMBER FUNCTIONS
747 
748 bool
750 is_ancestor_of(const any *other) const
751 {
752  // Preconditions:
753 
754  require(other != 0);
755 
756  // Body:
757 
758  // True if other conforms to this
759 
760  bool result = dynamic_cast<const reserved_primary_index_space_state*>(other) != 0;
761 
762  // Postconditions:
763 
764  // Exit:
765 
766  return result;
767 }
768 
769 bool
771 invariant() const
772 {
773  bool result = true;
774 
775  if(invariant_check())
776  {
777  // Prevent recursive calls to invariant
778 
780 
781  // Must satisfy base class invariant
782 
784 
785  // Invariances for this class:
786 
787  // Finished, turn invariant checking back on.
788 
790  }
791 
792  // Exit
793 
794  return result;
795 }
796 
797 // PROTECTED MEMBER FUNCTIONS
798 
799 // PRIVATE MEMBER FUNCTIONS
800 
801 
802 // ===========================================================
803 // NON-MEMBER FUNCTIONS
804 // ===========================================================
805 
806 size_t
807 sheaf::
808 deep_size(const reserved_primary_index_space_state& xn, bool xinclude_shallow)
809 {
810  // Preconditions:
811 
812  // Body:
813 
814  size_t result = xinclude_shallow ? sizeof(xn) : 0;
815 
816  // Add any contributions from the parent class.
817 
818  const primary_index_space_state& ixn = static_cast<const primary_index_space_state&>(xn);
819  result += deep_size(ixn, false);
820 
821  // Postconditions:
822 
823  ensure(result >= 0);
824 
825  // Exit
826 
827  return result;
828 }
virtual reserved_primary_index_space_state * clone() const
Virtual constructor; create a new instance of the same type at this.
static size_type handle_pool_deep_size()
The deep size of the handle 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 const std::string & class_name() const
The name of this class.
A computed implementation of abstract class primary_index_space_state. This representation is used fo...
virtual bool is_persistent() const
True if this id space should be written to disk.
static size_type handle_pool_ct()
The number of handles in the pool.
pod_type new_hub_id(pod_type xhub_id)
Allocate the hub id xhub_id in this space. Returns the allocated id in the hub id space...
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
The id space family for this (const version).
pod_type _next_hub_id
The next available hub id for allocation.
virtual reserved_primary_index_space_state & operator=(const explicit_index_space_state &xother)
Assignment operator.
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.
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.
Abstract base class with useful features for all objects.
Definition: any.h:39
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
std::string name() const
Name of this space.
bool is_hub_id_available(pod_type xhub_id) const
True if and only if the hub id xhub_id is available for allocation.
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.
static reserved_primary_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, pod_type xoffset, size_type xct)
Create a new reserved primary id space in the id space family xid_space at the next available id spac...
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
An iterator over a reserved term in the primary sum id space in which the equivlence between the ids ...
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...
pod_type new_id()
Allocate the next available id in this space. Returns the allocated id in the hub id space...
virtual index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool.
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 is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
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...
static size_type iterator_pool_deep_size()
The deep size of the iterator pool.
bool is_next_id_available() const
True if and only if the next id is available for allocation.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
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.
An implementation of class primary_index_space_handle that has a reserved primary id space state...
pod_type next_hub_id() const
The next available hub id for allocation.
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.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual size_type deep_size(bool xinclude_shallow) const
The deep size of this.
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
static size_type iterator_pool_ct()
The number of iterators in the pool.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
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.
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.
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.
pod_type next_hub_id() const
The next available hub id for allocation.
virtual bool is_attached() const
True if this iterator is attached to a state.
virtual void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
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
virtual bool contains(pod_type xid) const
True if this space contains id xid.
pod_type _offset
The offset into the hub id space.