SheafSystem  0.0.0.0
sum_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/sum_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/scoped_index.h"
26 #include "SheafSystem/std_limits.h"
27 
28 using namespace std;
29 
30 // ===========================================================
31 // SUM_INDEX_SPACE_STATE FACET
32 // ===========================================================
33 
34 // PUBLIC MEMBER FUNCTIONS
35 
36 // PROTECTED MEMBER FUNCTIONS
37 
41 {
42  // Preconditions:
43 
44  // Body:
45 
46  // Initialize the sum to term map so that
47  // anything before first term maps to invalid
48  // and anything above last term maps to invalid.
49 
52 
53  // Postconditions:
54 
55  ensure(invariant());
56  ensure(is_empty());
57 
58  // Exit:
59 
60  return;
61 }
62 
65 {
66  // Preconditions:
67 
68  // Body:
69 
70  // Release the terms
71 
72  for(pod_type i = 0; i < _terms.ub(); ++i)
73  {
74  if(_terms[i] != 0)
75  {
77  }
78  }
79 
80  // Postconditions:
81 
82  // Exit:
83 
84  return;
85 }
86 
87 // PRIVATE MEMBER FUNCTIONS
88 
89 
90 // ===========================================================
91 // SUM INDEX SPACE FACET
92 // ===========================================================
93 
94 // PUBLIC MEMBER FUNCTIONS
95 
96 void
99 {
100  // Preconditions:
101 
102  require(contains_hub(xhub_id));
103 
104  // Body:
105 
106  result.clear();
107 
108  // Find the representative member.
109 
110  pod_type lrep_id = rep(xhub_id);
111 
112  // Equivalence class always contains the representative itself,
113  // but we don't store that, so insert it as first member.
114 
115  result.push_back(lrep_id);
116 
117  // Insert the rest of the equivalence class, may be empty.
118 
119  typedef rem_inv_type::const_iterator itr_type;
120  pair<itr_type, itr_type> lrange = _rem_inv.equal_range(lrep_id);
121 
122  for(itr_type litr = lrange.first; litr != lrange.second; ++litr)
123  {
124  result.push_back(litr->second);
125  }
126 
127  // Postconditions:
128 
129  ensure(!result.empty());
130 
131  // Exit:
132 
133  return;
134 }
135 
138 equivalence_ct(pod_type xhub_id) const
139 {
140  // Preconditions:
141 
142  require(contains_hub(xhub_id));
143 
144  // Body:
145 
146  // Find the representative member.
147 
148  pod_type lrep_id = rep(xhub_id);
149 
150  // Get the equivalence count; add one for the rep itself.
151 
152  size_type result = _rem_inv.count(lrep_id) + 1;
153 
154  // Postconditions:
155 
156  ensure(result > 0);
157 
158  // Exit:
159 
160  return result;
161 }
162 
163 bool
165 contains_rep(pod_type xhub_id) const
166 {
167  // Preconditions:
168 
169  // Body:
170 
171  is_abstract();
172 
173  bool result = true; // Just to silence compiler warnings.
174 
175  // Postconditions:
176 
177  ensure(result ? contains_hub(xhub_id) : true);
178 
179  // Exit:
180 
181  return result;
182 }
183 
184 bool
187 {
188  // Preconditions:
189 
190  // Body:
191 
192  // A rep is "unique" if it is the only member of its equivalence class.
193  // _rem_inv contains the inverse of the reflexive reduction of the rem function.
194  // So _rem_inv contains an entry for sum id s if and only if s is the rep
195  // for an equivalence class that contains more that just s itself.
196  // Thus s is unique if s is a rep and _rem_inv does not contain an entry for s.
197 
198  bool result = contains_rep(xhub_id) && (_rem_inv.find(xhub_id) == _rem_inv.end());
199 
200  // Postconditions:
201 
202  ensure(result ? contains_hub(xhub_id) : true);
203  ensure(result ? contains_rep(xhub_id) : true);
204 
205  // Exit:
206 
207  return result;
208 }
209 
212 rep(pod_type xhub_id) const
213 {
214  // Preconditions:
215 
216  require(contains_hub(xhub_id));
217 
218  // Body:
219 
220  is_abstract();
221 
222  pod_type result = 0; // Just to silence compiler warnings.
223 
224  // Postconditions:
225 
226  ensure(is_basic_query);
227 
228  // Exit:
229 
230  return true;
231 }
232 
235 host_rep(pod_type xhub_id) const
236 {
237  // Preconditions:
238 
239  require(contains_hub(xhub_id));
240 
241  // Body:
242 
243  is_abstract();
244 
245  pod_type result = 0; // Just to silence compiler warnings.
246 
247  // Postconditions:
248 
249  ensure(contains(result));
250 
251  // Exit:
252 
253  return result;
254 }
255 
259 {
260  // Preconditions:
261 
262  // Body:
263 
264  is_abstract();
265 
266  pod_type result = 0; // Just to silence compiler warnings.
267 
268  // Postconditions:
269 
270  ensure(is_basic_query);
271 
272  // Exit:
273 
274  return result;
275 }
276 
279 term_id_end() const
280 {
281  // Preconditions:
282 
283  // Body:
284 
285  is_abstract();
286 
287  pod_type result = 0; // Just to silence compiler warnings.
288 
289  // Postconditions:
290 
291  ensure(is_basic_query);
292 
293  // Exit:
294 
295  return result;
296 }
297 
298 bool
301 {
302  // Preconditions:
303 
304  // Body:
305 
306  is_abstract();
307 
308  bool result = true; // Just to silence compiler warnings.
309 
310  // Postconditions:
311 
312  ensure(is_basic_query);
313 
314  // Exit:
315 
316  return result;
317 }
318 
321 term(pod_type xi) const
322 {
323  // Preconditions:
324 
325  require(contains_term_id(xi));
326 
327  // Body:
328 
329  const index_space_handle& result = *_terms[xi];
330 
331  // Postconditions:
332 
333  ensure(is_basic_query);
334 
335  // Exit:
336 
337  return result;
338 }
339 
342 term_of(pod_type xid) const
343 {
344  // Preconditions:
345 
346  require(contains(xid));
347 
348  // Body:
349 
350  const index_space_handle& result = term(term_id(pod(xid)));
351 
352  // Postconditions:
353 
354  ensure(is_basic_query);
355 
356  // Exit:
357 
358  return result;
359 }
360 
363 term_of(const scoped_index& xid) const
364 {
365  // Preconditions:
366 
367  require(xid.in_scope());
368  require(contains(xid));
369 
370  // Body:
371 
372  const index_space_handle& result = term_of(xid.hub_pod());
373 
374  // Postconditions:
375 
376  ensure(is_basic_query);
377 
378  // Exit:
379 
380  return result;
381 }
382 
385 last_term() const
386 {
387  // Preconditions:
388 
389  require(!is_empty());
390 
391  // Body:
392 
393  // _terms.ct() is not maintained in gluable_sum_index_space_state,
394  // so can't use _terms.back().
395 
396  const index_space_handle& result = *_terms[term_id_end() - 1];
397 
398  // Postconditions:
399 
400  ensure(is_basic_query);
401 
402  // Exit:
403 
404  return result;
405 }
406 
410 {
411  // Preconditions:
412 
413  require(contains_term_id(xi));
414 
415  // Body:
416 
417  pod_type result = _term_begin[xi];
418 
419  // Postconditions:
420 
421  ensure(is_basic_query);
422 
423  // Exit:
424 
425  return result;
426 }
427 
431 {
432  // Preconditions:
433 
434  require(contains_term_id(xi));
435 
436  // Body:
437 
438  pod_type result = _term_end[xi];
439 
440  // Postconditions:
441 
442  ensure(is_basic_query);
443 
444  // Exit:
445 
446  return result;
447 }
448 
452 {
453  // Preconditions:
454 
455  require(contains_term_id(xi));
456 
457  // Body:
458 
459  pod_type result = hub_pod(term_begin(xi));
460 
461  // Postconditions:
462 
463  ensure(is_basic_query);
464 
465  // Exit:
466 
467  return result;
468 }
469 
473 {
474  // Preconditions:
475 
476  require(contains_term_id(xi));
477 
478  // Body:
479 
480  pod_type result = hub_pod(term_end(xi));
481 
482  // Postconditions:
483 
484  ensure(is_basic_query);
485 
486  // Exit
487 
488  return result;
489 }
490 
493 term_id(pod_type xid) const
494 {
495  // Preconditions:
496 
497  // Body:
498 
499  pod_type result = _sum_to_term_map.upper_bound(xid)->second;
500 
501  // Postconditions:
502 
503  ensure(!is_valid(result) || contains_term_id(result));
504 
505  // Exit:
506 
507  return result;
508 }
509 
510 void
512 binary(pod_type xindex, pod_type& xterm_id, pod_type& xlocal_id) const
513 {
514  // Preconditions:
515 
516  require(contains_hub(xindex));
517 
518  // Body:
519 
520  xterm_id = _sum_to_term_map.upper_bound(xindex)->second;
521  xlocal_id = xindex - _term_begin[xterm_id];
522 
523  // Postconditions:
524 
525  ensure(contains_term_id(xterm_id));
526  ensure(term(xterm_id).contains(xlocal_id));
527 
528  // Exit:
529 
530  return;
531 }
532 
533 void
535 simple(pod_type xterm_id, pod_type xlocal_id, pod_type& xindex) const
536 {
537  // Preconditions:
538 
539  require(contains_term_id(xterm_id));
540  require(term(xterm_id).contains(xlocal_id));
541 
542  // Body:
543 
544  xindex = _term_begin[xterm_id] + xlocal_id;
545 
546  // Postconditions:
547 
548  ensure(contains_hub(xindex));
549 
550  // Exit:
551 
552  return;
553 }
554 
555 void
558 {
559  // Preconditions:
560 
561  // Body:
562 
563  // Release the terms
564 
565  for(pod_type i = 0; i < _terms.ub(); ++i)
566  {
567  if(_terms[i] != 0)
568  {
570  }
571  }
572 
573  // Clear the data structures
574 
575  _rem_inv.clear();
576  _terms.clear();
577  _term_begin.clear();
578  _term_end.clear();
579  _sum_to_term_map.clear();
580  _ct = 0;
581 
583 
584  // Postconditions:
585 
586  ensure(invariant());
587  ensure(is_empty());
588 
589  // Exit:
590 
591  return;
592 }
593 
594 // PROTECTED MEMBER FUNCTIONS
595 
596 // PRIVATE MEMBER FUNCTIONS
597 
598 
599 // ===========================================================
600 // EXPLICIT_INDEX_SPACE_STATE FACET
601 // ===========================================================
602 
603 // PUBLIC MEMBER FUNCTIONS
604 
605 bool
608 {
609  // Preconditions:
610 
611  require(is_ancestor_of(&xother));
612 
613  // Body:
614 
615  const sum_index_space_state& lother =
616  dynamic_cast<const sum_index_space_state&>(xother);
617 
618  bool result = explicit_index_space_state::operator==(xother);
621 // result = result && (_rem_inv == lother._rem_inv);
622  result = result && (_terms == lother._terms);
623  result = result && (_term_begin == lother._term_begin);
624  result = result && (_term_end == lother._term_end);
625  result = result && (_sum_to_term_map == lother._sum_to_term_map);
626 
627  // Postconditions:
628 
629  // Exit
630 
631  return result;
632 }
633 
636 deep_size(bool xinclude_shallow) const
637 {
638  // Preconditions:
639 
640  // Body:
641 
642  size_type result = sheaf::deep_size(*this, xinclude_shallow);
643 
644  // Postconditions:
645 
646  ensure(result >= 0);
647 
648  // Exit:
649 
650  return result;
651 }
652 
653 // PROTECTED MEMBER FUNCTIONS
654 
658 {
659  // Preconditions:
660 
661  require(is_ancestor_of(&xother));
662 
663  // Body:
664 
665  const sum_index_space_state& lother =
666  dynamic_cast<const sum_index_space_state&>(xother);
667 
668  _rem_inv = lother._rem_inv;
669  _terms = lother._terms;
670  _term_begin = lother._term_begin;
671  _term_end = lother._term_end;
673 
674  (void) explicit_index_space_state::operator=(xother);
675 
676  // Postconditions:
677 
678  ensure(invariant());
679  ensure((*this) == xother);
680 
681  // Exit
682 
683  return *this;
684 }
685 
686 // PRIVATE MEMBER FUNCTIONS
687 
688 
689 // ===========================================================
690 // INDEX SPACE FACET
691 // ===========================================================
692 
693 // PUBLIC MEMBER FUNCTIONS
694 
695 // PROTECTED MEMBER FUNCTIONS
696 
697 // PRIVATE MEMBER FUNCTIONS
698 
699 
700 // ===========================================================
701 // FACTORY FACET
702 // ===========================================================
703 
704 // PUBLIC MEMBER FUNCTIONS
705 
706 const std::string&
708 class_name() const
709 {
710  static const std::string result("sum_index_space_state");
711  return result;
712 }
713 
714 // PROTECTED MEMBER FUNCTIONS
715 
716 // PRIVATE MEMBER FUNCTIONS
717 
718 
719 // ===========================================================
720 // ANY FACET
721 // ===========================================================
722 
723 // PUBLIC MEMBER FUNCTIONS
724 
725 bool
727 is_ancestor_of(const any *other) const
728 {
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 sum_index_space_state*>(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 sum_index_space_state& xn, bool xinclude_shallow)
787 {
788  // Preconditions:
789 
790  // Body:
791 
792  size_t result = xinclude_shallow ? sizeof(xn) : 0;
793 
794  // Add any contributions from the parent class.
795 
796  const explicit_index_space_state& ixn = static_cast<const explicit_index_space_state&>(xn);
797  result += deep_size(ixn, false);
798 
799  // Add memory associated in member _rem_inv.
800 
802  result += deep_size<pod_index_type, pod_index_type, rem_inv_policy_type>(xn._rem_inv, false);
803 
804  // Add memory associated in member _terms
805 
810 
811  // Add memory associated in member _term_begin.
812 
813  result += deep_size(xn._term_begin, false);
814 
815  // Add memory associated in member _term_end.
816 
817  result += deep_size(xn._term_end, false);
818 
819  // Add memory associated in member _sum_to_term_map.
820 
821  typedef no_deep_size_policy<sum_index_space_state::sum_to_term_type> sum_to_term_policy_type;
822  result += deep_size<pod_index_type, pod_index_type, sum_to_term_policy_type>(xn._sum_to_term_map, false);
823 
824  // Postconditions:
825 
826  ensure(result >= 0);
827 
828  // Exit
829 
830  return result;
831 }
pod_type term_begin(pod_type xi) const
Beginning id of the xi-th term.
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...
virtual sum_index_space_state & operator=(const explicit_index_space_state &xother)
Assignment operator.
index_type ub() const
The upper bound on the storage array. The number of items current allocated in the storage array...
bool in_scope() const
True if and only if scope() contains an entry for pod().
Definition: scoped_index.h:584
pod_type term_id(pod_type xid) const
The id of the term containing id xid.
void release_id_space(index_space_handle &xid_space) const
Returns the id space handle xid_space to the handle pool.
virtual bool contains_term_id(pod_type xi) const =0
True if this map contains the xi-th term.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
virtual bool invariant() const
Class invariant.
STL namespace.
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
const index_space_family & id_spaces() const
The id space family for this (const version).
block< pod_type > _term_end
The end of the interval in the sum id space for each term.
virtual bool operator==(const explicit_index_space_state &xother) const
True if this is equivalent to xother.
virtual pod_type host_rep(pod_type xhub_id) const =0
The host id associated with the representative member of the equivalence class of hub id xhub_id...
virtual const std::string & class_name() const
The name of this class.
const index_space_handle & term(pod_type xi) const
The xi-th term.
virtual pod_type term_id_end() const =0
Ending index of the terms in this sum.
Abstract base class with useful features for all objects.
Definition: any.h:39
void binary(pod_type xindex, pod_type &xterm_id, pod_type &xlocal) const
Converts simple index xindex to its binary components xterm_id and xlocal_id.
virtual pod_type pod(pod_type xid) const =0
The pod index in this space 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...
std::list< pod_type > equivalence_class_type
The type used to represent an equivalence class.
sum_index_space_state()
Default constructor.
Do not call deep_size on either the key or value.
Definition: deep_size.h:54
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
An abstract id space of alternate integer identifiers which is the sum (disjoint union) of two or mor...
const index_space_handle & term_of(pod_type xid) const
The term containing id xid.
void invalidate_extrema()
Invalidate the extrema.
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.
terms_type _terms
The terms of this sum.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
pod_type term_hub_end(pod_type xi) const
Ending hub id of the xi-th term.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
const index_space_handle & last_term() const
The last term.
void simple(pod_type xterm_id, pod_type xlocal_id, pod_type &xindex) const
Converts binary components xterm_id, xlocal_id to simple index xindex.
friend SHEAF_DLL_SPEC size_t deep_size(const sum_index_space_state &xn, bool xinclude_shallow)
The deep size of sum_index_space_state& xn.
pod_type term_end(pod_type xi) const
Ending id of the xi-th term.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
SHEAF_DLL_SPEC void max(const vd &x0, vd_value_type &xresult, bool xauto_access)
Maximum component of x0, pre-allocated version.
Definition: vd.cc:2097
virtual bool contains_rep(pod_type xhub_id) const =0
True if and only if hub id xhub_id is the representative member of its equivalence class...
void equivalence_class(pod_type xhub_id, equivalence_class_type &result) const
The equivalence class associated with the hub id xhub_id.
pod_type term_hub_begin(pod_type xi) const
Beginning hub id of the xi-th term.
virtual ~sum_index_space_state()
Destructor.
size_type _ct
The number of members.
bool contains_unique_rep(pod_type xhub_id) const
True if and only if hub id xhub_id is the only member of its equivalence class.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual bool contains(pod_type xid) const =0
True if this space contains id xid.
pod_index_type pod_type
The "plain old data" index type for this.
virtual pod_type rep(pod_type xhub_id) const =0
The representative member of the equivalence class of hub id xhub_id.
block< pod_type > _term_begin
The beginning of the interval in the sum id space for each term.
virtual pod_type term_id_begin() const =0
Beginning index of the terms in this sum.
sum_to_term_type _sum_to_term_map
The term id corresponding to each sum id.
virtual bool invariant() const
Class invariant.
virtual void clear()
Deletes all entries.
bool is_empty() const
True if there are no ids in the space.
void clear()
Remove all items.
rem_inv_type _rem_inv
The inverse rem map.
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
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
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 equivalence_ct(pod_type xhub_id) const
The number of members of the equivalence class of hub id xhub_id.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710