SheafSystem  0.0.0.0
offset_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/offset_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/offset_index_space_handle.h"
27 #include "SheafSystem/offset_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  offset_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  offset_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 // OFFSET_INDEX_SPACE_STATE FACET
134 // ===========================================================
135 
136 // PUBLIC MEMBER FUNCTIONS
137 
138 // PROTECTED MEMBER FUNCTIONS
139 
143  _offset(0)
144 {
145  // Preconditions:
146 
147  // Body:
148 
149  _ct = 0;
150  _begin = 0;
151  _end = 0;
152 
153  // Postconditions:
154 
155  ensure(invariant());
156  ensure(is_empty());
157  ensure(offset() == 0);
158  ensure(begin() == 0);
159  ensure(end() == 0);
160 
161  // Exit:
162 
163  return;
164 }
165 
168 {
169  // Preconditions:
170 
171  // Body:
172 
173  // nothing to do.
174 
175  // Postconditions:
176 
177  // Exit:
178 
179  return;
180 }
181 
182 // PRIVATE MEMBER FUNCTIONS
183 
184 
185 // ===========================================================
186 // OFFSET INDEX SPACE FACET
187 // ===========================================================
188 
189 // PUBLIC MEMBER FUNCTIONS
190 
193 offset() const
194 {
195  // Preconditions:
196 
197  // Body:
198 
199  pod_type result = _offset;
200 
201  // Postconditions:
202 
203  ensure(is_basic_query);
204 
205  // Exit:
206 
207  return result;
208 }
209 
210 // PROTECTED MEMBER FUNCTIONS
211 
212 // PRIVATE MEMBER FUNCTIONS
213 
214 
215 // ===========================================================
216 // EXPLICIT_INDEX_SPACE_STATE FACET
217 // ===========================================================
218 
219 // PUBLIC MEMBER FUNCTIONS
220 
221 bool
224 {
225  // Preconditions:
226 
227  require(is_ancestor_of(&xother));
228 
229  // Body:
230 
231  const offset_index_space_state& lother =
232  dynamic_cast<const offset_index_space_state&>(xother);
233 
234  bool result = explicit_index_space_state::operator==(xother);
235  result = result && (_offset == lother._offset);
236 
237  // Postconditions:
238 
239  // Exit
240 
241  return result;
242 }
243 
246 deep_size(bool xinclude_shallow) const
247 {
248  // Preconditions:
249 
250  // Body:
251 
252  size_type result = sheaf::deep_size(*this, xinclude_shallow);
253 
254  // Postconditions:
255 
256  ensure(result >= 0);
257 
258  // Exit:
259 
260  return result;
261 }
262 
263 // PROTECTED MEMBER FUNCTIONS
264 
268 {
269  // Preconditions:
270 
271  require(is_ancestor_of(&xother));
272 
273  // Body:
274 
275  const offset_index_space_state& lother =
276  dynamic_cast<const offset_index_space_state&>(xother);
277 
278  _offset = lother._offset;
279 
280  (void) explicit_index_space_state::operator=(xother);
281 
282  // Postconditions:
283 
284  ensure(invariant());
285  ensure((*this) == xother);
286 
287  // Exit
288 
289  return *this;
290 }
291 
292 // PRIVATE MEMBER FUNCTIONS
293 
294 
295 // ===========================================================
296 // INDEX SPACE FACET
297 // ===========================================================
298 
299 // PUBLIC MEMBER FUNCTIONS
300 
301 bool
303 contains(pod_type xid) const
304 {
305  // Preconditions:
306 
307  // Body:
308 
309  bool result = (0 <= xid) && (xid < _end);
310 
311  // Postconditions:
312 
313  ensure(is_basic_query);
314 
315  // Exit
316 
317  return result;
318 }
319 
320 bool
323 {
324  // Preconditions:
325 
326  // Body:
327 
328  bool result = (_offset <= xhub_id) && (xhub_id < _offset + _end);
329 
330  // Postconditions:
331 
332  ensure(is_basic_query);
333 
334  // Exit
335 
336  return result;
337 }
338 
341 pod(pod_type xhub_id) const
342 {
343  // Preconditions:
344 
345  // Body:
346 
347  pod_type result =
348  contains_hub(xhub_id) ? xhub_id - _offset : invalid_pod_index();
349 
350  // Postconditions:
351 
352  ensure(!is_valid(result) || contains(result));
353 
354  // Exit
355 
356  return result;
357 }
358 
362 {
363  // Preconditions:
364 
365  // Body:
366 
367  pod_type result = contains(xid) ? xid + _offset : invalid_pod_index();
368 
369  // Postconditions:
370 
371  ensure(!is_valid(result) || contains_unglued_hub(result));
372 
373  // Exit:
374 
375  return result;
376 }
377 
378 // PROTECTED MEMBER FUNCTIONS
379 
380 // PRIVATE MEMBER FUNCTIONS
381 
382 
383 // ===========================================================
384 // HANDLE POOL FACET
385 // ===========================================================
386 
387 // PUBLIC MEMBER FUNCTIONS
388 
392 {
393  // Preconditions:
394 
395  // Body:
396 
397  size_type result = handles().ct();
398 
399  // Postconditions:
400 
401  ensure(result >= 0);
402 
403  // Exit:
404 
405  return result;
406 }
407 
411 {
412  // Preconditions:
413 
414  // Body:
415 
416  size_type result = sheaf::deep_size(handles(), true);
417 
418  // Postconditions:
419 
420  ensure(result >= 0);
421 
422  // Exit:
423 
424  return result;
425 }
426 
430 {
431  // Preconditions:
432 
433  // Body:
434 
435  offset_index_space_handle& result = handles().get();
436  attach(result);
437 
438  // Postconditions:
439 
440  ensure(result.is_attached());
441 
442  // Exit:
443 
444  return result;
445 }
446 
447 void
450 {
451  // Preconditions:
452 
453  require(allocated_id_space(xid_space));
454 
455  // Body:
456 
457  // Detach the handle.
458 
459  xid_space.detach();
460 
461  // Release the handle to the pool.
462 
463  handles().release(reinterpret_cast<offset_index_space_handle&>(xid_space));
464 
465  // Postconditions:
466 
467  ensure(is_basic_query);
468 
469  // Exit:
470 
471  return;
472 }
473 
474 bool
476 allocated_id_space(const index_space_handle& xid_space) const
477 {
478  // Preconditions:
479 
480  // Body:
481 
482  const offset_index_space_handle* lid_space =
483  dynamic_cast<const offset_index_space_handle*>(&xid_space);
484 
485  bool result = (lid_space != 0) && handles().allocated(*lid_space);
486 
487  // Postconditions:
488 
489  ensure(is_basic_query);
490 
491  // Exit:
492 
493  return result;
494 }
495 
496 // PROTECTED MEMBER FUNCTIONS
497 
498 // PRIVATE MEMBER FUNCTIONS
499 
501 sheaf::offset_index_space_state::
502 handles()
503 {
504  // Preconditions:
505 
506  // Body:
507 
509 
510  // Postconditions:
511 
512  ensure(is_basic_query);
513 
514  // Exit:
515 
516  return result;
517 }
518 
519 
520 // ===========================================================
521 // ITERATOR POOL FACET
522 // ===========================================================
523 
524 // PUBLIC MEMBER FUNCTIONS
525 
529 {
530  // Preconditions:
531 
532  // Body:
533 
534  size_type result = iterators().ct();
535 
536  // Postconditions:
537 
538  ensure(result >= 0);
539 
540  // Exit:
541 
542  return result;
543 }
544 
548 {
549  // Preconditions:
550 
551  // Body:
552 
553  size_type result = sheaf::deep_size(iterators(), true);
554 
555  // Postconditions:
556 
557  ensure(result >= 0);
558 
559  // Exit:
560 
561  return result;
562 }
563 
567 {
568  // Preconditions:
569 
570  // Body:
571 
572  offset_index_space_iterator& result = iterators().get();
573  attach(result);
574 
575  // Postconditions:
576 
577  ensure(result.is_attached());
578 
579  // Exit:
580 
581  return result;
582 }
583 
584 void
587 {
588  // Preconditions:
589 
590  require(allocated_iterator(xitr));
591 
592  // Body:
593 
594  // Detach the iterator.
595 
596  xitr.detach();
597 
598  // Release the iterator to the pool.
599 
600  iterators().release(reinterpret_cast<offset_index_space_iterator&>(xitr));
601 
602  // Postconditions:
603 
604  ensure(is_basic_query);
605 
606  // Exit:
607 
608  return;
609 }
610 
611 bool
614 {
615  // Preconditions:
616 
617  // Body:
618 
619  const offset_index_space_iterator* litr =
620  dynamic_cast<const offset_index_space_iterator*>(&xitr);
621 
622  bool result = (litr != 0) && iterators().allocated(*litr);
623 
624  // Postconditions:
625 
626  ensure(is_basic_query);
627 
628  // Exit:
629 
630  return result;
631 }
632 
633 // PROTECTED MEMBER FUNCTIONS
634 
635 // PRIVATE MEMBER FUNCTIONS
636 
638 sheaf::offset_index_space_state::
639 iterators()
640 {
641  // Preconditions:
642 
643  // Body:
644 
646 
647  // Postconditions:
648 
649  ensure(is_basic_query);
650 
651  // Exit:
652 
653  return result;
654 }
655 
656 
657 // ===========================================================
658 // FACTORY FACET
659 // ===========================================================
660 
661 // PUBLIC MEMBER FUNCTIONS
662 
663 const std::string&
665 class_name() const
666 {
667  static const std::string result("offset_index_space_state");
668  return result;
669 }
670 
673 clone() const
674 {
675  // Preconditions:
676 
677  // Body:
678 
680 
681  // Postconditions:
682 
683  ensure(result != 0);
684  ensure(is_same_type(result));
685 
686  // Exit:
687 
688  return result;
689 }
690 
691 // PROTECTED MEMBER FUNCTIONS
692 
693 // PRIVATE MEMBER FUNCTIONS
694 
695 bool
696 sheaf::offset_index_space_state::
697 make_prototype()
698 {
699  // Preconditions:
700 
701  // Body:
702 
704 
705  id_space_factory().insert_prototype(lproto);
706 
707  // Postconditions:
708 
709  // Exit:
710 
711  return true;
712 }
713 
714 
715 // ===========================================================
716 // ANY FACET
717 // ===========================================================
718 
719 // PUBLIC MEMBER FUNCTIONS
720 
721 bool
723 is_ancestor_of(const any *other) const
724 {
725  // Preconditions:
726 
727  require(other != 0);
728 
729  // Body:
730 
731  // True if other conforms to this
732 
733  bool result = dynamic_cast<const offset_index_space_state*>(other) != 0;
734 
735  // Postconditions:
736 
737  // Exit:
738 
739  return result;
740 }
741 
742 bool
744 invariant() const
745 {
746  bool result = true;
747 
748  if(invariant_check())
749  {
750  // Prevent recursive calls to invariant
751 
753 
754  // Must satisfy base class invariant
755 
757 
758  // Invariances for this class:
759 
760  invariance(is_gathered());
761 
762  // Finished, turn invariant checking back on.
763 
765  }
766 
767  // Exit
768 
769  return result;
770 }
771 
772 // PROTECTED MEMBER FUNCTIONS
773 
774 // PRIVATE MEMBER FUNCTIONS
775 
776 
777 // ===========================================================
778 // NON-MEMBER FUNCTIONS
779 // ===========================================================
780 
781 size_t
782 sheaf::
783 deep_size(const offset_index_space_state& xn, bool xinclude_shallow)
784 {
785  // Preconditions:
786 
787  // Body:
788 
789  size_t result = xinclude_shallow ? sizeof(xn) : 0;
790 
791  // Add any contributions from the parent class.
792 
793  const explicit_index_space_state& ixn = static_cast<const explicit_index_space_state&>(xn);
794  result += deep_size(ixn, false);
795 
796  // Postconditions:
797 
798  ensure(result >= 0);
799 
800  // Exit
801 
802  return result;
803 }
virtual size_type deep_size(bool xinclude_shallow) const
The deep size of this.
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...
pod_type _offset
The offset into the hub id space.
static offset_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, pod_type xoffset, size_type xct)
Create a new offset id space in the id space family xid_space at the next available id space index wi...
static size_type handle_pool_ct()
The number of handles in the pool.
virtual void release_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
pod_type offset() const
The offset into the hub id space.
virtual index_space_iterator & get_iterator() const
Allocates an id space iterator from the iterator pool.
virtual pod_type index() const
Index of this space.
virtual index_space_handle & get_id_space() const
Allocates an id space handle from the handle pool.
An abstract iterator over the ids of an id space.
pod_type end() const
Ending id of this space.
virtual offset_index_space_state & operator=(const explicit_index_space_state &xother)
Assignment operator.
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 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.
An implementation of class explicit_index_space_handle that has a offset id space state...
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 offset_index_space_state * clone() const
Virtual constructor; create a new instance of the same type at this.
virtual void detach()=0
Detach this handle form its state, if any.
virtual void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
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
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.
static size_type iterator_pool_ct()
The number of iterators in the pool.
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.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
pod_type begin() const
Beginning id of this space.
virtual bool is_attached() const
True if this handle is attached to a state.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
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 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.
bool is_explicit_interval(pod_type xid)
True, if and only if the id space interval that contains index xid is an explicit interval...
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.
A computed implementation of abstract class explicit_index_space_state. This representation assumes t...
virtual bool contains(pod_type xid) const
True if this space contains id xid.
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 invariant() const
Class invariant.
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 contains_unglued_hub(pod_type xid) const
True if this space contains an id equivalent to xid in the unglued hub id space.
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
offset_index_space_state()
Default constructor.
virtual const std::string & class_name() const
The name of this class.
virtual bool invariant() const
Class invariant.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
static size_type handle_pool_deep_size()
The deep size of the handle pool.
static size_type iterator_pool_deep_size()
The deep size of the iterator pool.
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.
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.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
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
pod_type offset() const
The offset into the hub id space.