SheafSystem  0.0.0.0
crg_interval.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/crg_interval.h"
22 
23 #include "SheafSystem/arg_list.h"
24 #include "SheafSystem/assert_contract.h"
25 #include "SheafSystem/factory.h"
26 #include "SheafSystem/index_space_family.h"
27 #include "SheafSystem/index_space_iterator.h"
28 #include "SheafSystem/list_index_space_handle.h"
29 #include "SheafSystem/list_index_space_state.h"
30 #include "SheafSystem/poset_crg_state.h"
31 #include "SheafSystem/poset_path.h"
32 
33 using namespace std;
34 
35 // ===========================================================
36 // CRG_INTERVAL FACET
37 // ===========================================================
38 
39 // PUBLIC DATA MEMBERS
40 
43  : _id_spaces(0),
44  _id_spaces_initialized(false),
45  _local_id_space_initialized(false),
46  _lower_covers_initialized(false),
47  _lower_covers_begin(invalid_pod_index()),
48  _upper_covers_initialized(false),
49  _upper_covers_begin(invalid_pod_index())
50 {
51  // Preconditions:
52 
53  // Body:
54 
55  // Postconditions:
56 
57  ensure(invariant());
58  ensure(!is_initialized());
59 }
60 
63 {
64  // Preconditions:
65 
66  // Body:
67 
68  // Delete explicit overrides.
69 
70  const explicit_cover_map_type& llower_cover_map = explicit_cover_map(LOWER);
71  explicit_cover_map_type::const_iterator itr;
72  for(itr = llower_cover_map.begin();
73  itr != llower_cover_map.end();
74  ++itr)
75  {
76  _id_spaces->delete_space(itr->second);
77  }
78 
79  const explicit_cover_map_type& lupper_cover_map = explicit_cover_map(UPPER);
80  for(itr = lupper_cover_map.begin();
81  itr != lupper_cover_map.end();
82  ++itr)
83  {
84  _id_spaces->delete_space(itr->second);
85  }
86 
87  // Postconditions:
88 
89  // Exit:
90 
91  return;
92 }
93 
94 // PROTECTED DATA MEMBERS
95 
96 // PRIVATE DATA MEMBERS
97 
98 
99 // ===========================================================
100 // INTERVAL FACET
101 // ===========================================================
102 
103 // PUBLIC DATA MEMBERS
104 
107 begin() const
108 {
109  // Preconditions:
110 
111  require(local_id_space_initialized());
112 
113  // Body:
114 
115  return _local_id_space.offset();
116 }
117 
118 void
120 begin(scoped_index& result) const
121 {
122  // Preconditions:
123 
124  require(local_id_space_initialized());
125 
126  // Body:
127 
128  result.put(hub_id_space(), begin());
129 
130  // Postconditions:
131 
132  ensure(result.is_hub_scope());
133 
134  // Exit:
135 
136  return;
137 }
138 
141 end() const
142 {
143  // Preconditions:
144 
145  require(local_id_space_initialized());
146 
147  // Body:
148 
150 }
151 
152 void
154 end(scoped_index& result) const
155 {
156  // Preconditions:
157 
158  require(local_id_space_initialized());
159 
160  // Body:
161 
162  result.put(hub_id_space(), end());
163 
164  // Postconditions:
165 
166  ensure(result.is_hub_scope());
167 
168  // Exit:
169 
170  return;
171 }
172 
175 size() const
176 {
177  // Preconditions:
178 
179  require(local_id_space_initialized());
180 
181  // Body:
182 
183  return _local_id_space.ct();
184 }
185 
186 bool
189 {
190  // Preconditions:
191 
192  require(local_id_space_initialized());
193 
194  // Body:
195 
196  return _local_id_space.contains_hub(xindex);
197 }
198 
199 bool
201 contains_member(const scoped_index& xindex) const
202 {
203  // Preconditions:
204 
205  require(local_id_space_initialized());
206 
207  // Body:
208 
209  return contains_member(xindex.hub_pod());
210 }
211 
212 // PROTECTED DATA MEMBERS
213 
214 // PRIVATE DATA MEMBERS
215 
216 
217 // ===========================================================
218 // ID SPACE FAMILY FACET
219 // ===========================================================
220 
221 // PUBLIC DATA MEMBERS
222 
225 id_spaces() const
226 {
227  // Preconditions:
228 
229  require(id_spaces_initialized());
230 
231  // Body:
232 
233  return *_id_spaces;
234 }
235 
239 {
240  // Preconditions:
241 
242  require(id_spaces_initialized());
243 
244  // Body:
245 
246  return _id_spaces->hub_id_space();
247 }
248 
249 void
252 {
253  // Preconditions:
254 
255  require(!id_spaces_initialized());
256 
257  // Body:
258 
259  _id_spaces = &xid_spaces;
260  _id_spaces_initialized = true;
261 
262  // Postconditions:
263 
264  ensure(id_spaces_initialized());
265 
266  // Exit:
267 
268  return;
269 }
270 
271 bool
274 {
275  return _id_spaces_initialized;
276 }
277 
278 // PROTECTED DATA MEMBERS
279 
280 // PRIVATE DATA MEMBERS
281 
282 
283 // ===========================================================
284 // LOCAL ID SPACE FACET
285 // ===========================================================
286 
287 // PUBLIC DATA MEMBERS
288 
289 std::string
292 {
293  // Preconditions:
294 
295  // Body:
296 
297  string result(poset_path::make_reserved_name("", xindex, "_local_ids"));
298 
299  // Postconditions:
300 
301  ensure(!result.empty());
302 
303  // Exit:
304 
305  return result;
306 }
307 
311 {
312  // Preconditions:
313 
314  require(local_id_space_initialized());
315 
316  // Body:
317 
318  return _local_id_space;
319 }
320 
321 void
324 {
325  // Preconditions:
326 
327  require(!local_id_space_initialized());
328 
329  // Body:
330 
331  // Initialize the local id space.
332 
333  _local_id_space.attach_to(xlocal_id_space);
335 
336  // Postconditions:
337 
338  ensure(local_id_space_initialized());
339 
340  // Exit:
341 
342  return;
343 }
344 
345 bool
348 {
350 }
351 
352 // PROTECTED DATA MEMBERS
353 
354 // PRIVATE DATA MEMBERS
355 
356 
357 // ===========================================================
358 // COVER_SET FACET
359 // ===========================================================
360 
361 // PUBLIC DATA MEMBERS
362 
365 cover_id_space_id(bool xlower, pod_index_type xmbr_index) const
366 {
367  // Preconditions:
368 
369  require(contains_member(xmbr_index));
370  require(covers_initialized(xlower));
371 
372  // Body:
373 
374  pod_index_type result;
375 
376  explicit_cover_map_type::const_iterator litr =
377  explicit_cover_map(xlower).find(xmbr_index);
378 
379  if(litr != explicit_cover_map(xlower).end())
380  {
381  result = litr->second;
382  }
383  else
384  {
385  pod_index_type lpod = _local_id_space.pod(xmbr_index);
386  result = xlower ? _lower_covers_begin + lpod : _upper_covers_begin + lpod;
387  }
388 
389  // Postconditions:
390 
391  // Exit:
392 
393  return result;
394 }
395 
396 bool
398 covers_initialized(bool xlower) const
399 {
400  // Preconditions:
401 
402  // Body:
403 
404  bool result = xlower ? _lower_covers_initialized : _upper_covers_initialized;
405 
406  // Postconditions:
407 
408  ensure(is_basic_query);
409 
410  // Exit:
411 
412  return result;
413 }
414 
415 void
418 {
419  // Preconditions:
420 
421  require(id_spaces_initialized());
422  require(local_id_space_initialized());
423  require(!covers_initialized(LOWER));
424  require(!covers_initialized(UPPER));
425 
426  // Body:
427 
430 
431  // Postconditions:
432 
433  ensure(covers_initialized(LOWER));
434  ensure(covers_initialized(UPPER));
435 
436  // Exit:
437 
438  return;
439 }
440 
441 // PROTECTED DATA MEMBERS
442 
443 void
446 {
447  // Preconditions:
448 
449  require(id_spaces_initialized());
450  require(local_id_space_initialized());
451  require(!covers_initialized(LOWER));
452 
453  // Body:
454 
455  is_abstract();
456 
457  // Posconditions:
458 
459  ensure(covers_initialized(LOWER));
460 
461  // Exit:
462 
463  return;
464 }
465 
466 void
469 {
470  // Preconditions:
471 
472  require(id_spaces_initialized());
473  require(local_id_space_initialized());
474  require(!covers_initialized(UPPER));
475 
476  // Body:
477 
478  is_abstract();
479 
480  // Posconditions:
481 
482  ensure(covers_initialized(UPPER));
483 
484  // Exit:
485 
486  return;
487 }
488 
489 // PRIVATE DATA MEMBERS
490 
491 
492 // ===========================================================
493 // EXPLICIT COVER SET FACET
494 // ===========================================================
495 
496 // PUBLIC DATA MEMBERS
497 
498 bool
500 cover_is_explicit(bool xlower, pod_index_type xmbr_index) const
501 {
502  // Preconditions:
503 
504  require(contains_member(xmbr_index));
505 
506  // Body:
507 
509 }
510 
513 force_explicit_cover(bool xlower, pod_index_type xmbr_index, bool xinitialize)
514 {
515  // Preconditions:
516 
517  require(contains_member(xmbr_index));
518 
519  // Body:
520 
521  pod_index_type result = cover_id_space_id(xlower, xmbr_index);
522 
524  {
525  // Id space is not explicit. Construct an interval id space in its place.
526 
527  list_index_space_handle lid_space =
529 
530  result = lid_space.index();
531 
532  if(xinitialize)
533  {
534  // Initialize the ids of the cover.
535 
536  index_space_iterator& litr =
537  _id_spaces->get_id_space_iterator(cover_id_space_id(xlower, xmbr_index));
538 
539  while(!litr.is_done())
540  {
541  lid_space.push_back(litr.hub_pod());
542  litr.next();
543  }
544 
546  }
547 
548  // Set the explicit override in the interval.
549 
550  explicit_cover_map(xlower)[xmbr_index] = result;
551  }
552  else
553  {
554  // Explicit cover already exists, return the id.
555  }
556 
557  // Postconditions:
558 
559  ensure(id_spaces().contains(result));
560  ensure(cover_is_explicit(xlower, xmbr_index));
561 
562  // Exit:
563 
564  return result;
565 }
566 
567 std::string
569 explicit_cover_name(bool xlower, pod_index_type xmbr_index)
570 {
571  // Preconditions:
572 
573  // Body:
574 
575  string suffix = xlower ? "_lower_cover" : "_upper_cover";
576 
577  string result = poset_path::make_reserved_name("explicit_", xmbr_index, suffix);
578 
579  // Postconditions:
580 
581  ensure(!result.empty());
582 
583  // Exit:
584 
585  return result;
586 }
587 
588 std::string
590 explicit_cover_name(bool xlower, const scoped_index& xmbr_index)
591 {
592  // Preconditions:
593 
594  // Body:
595 
596  return explicit_cover_name(xlower, xmbr_index.hub_pod());
597 }
598 
599 // PROTECTED DATA MEMBERS
600 
601 // PRIVATE DATA MEMBERS
602 
603 
604 // ===========================================================
605 // FACTORY FACET
606 // ===========================================================
607 
608 // PUBLIC DATA MEMBERS
609 
613 {
614  static factory<crg_interval> result;
615 
616  return result;
617 }
618 
621 new_interval(const std::string& xname)
622 {
623  // Preconditions:
624 
625  require(interval_factory().contains_prototype(xname));
626 
627  // Body:
628 
629  crg_interval* result = interval_factory().new_instance(xname);
630 
631  // Postconditions:
632 
633  ensure(result != 0);
634  ensure(result->class_name() == xname);
635 
636  // Exit:
637 
638  return result;
639 }
640 
641 void
643 initialize(const namespace_poset& xnamespace)
644 {
645  // Preconditions:
646 
647  // Body:
648 
649  _id_spaces_initialized = true;
653 
654  // Postconditions:
655 
656  ensure(is_initialized());
657 
658  // Exit:
659 
660  return;
661 }
662 
663 bool
666 {
667  bool result =
672 
673  return result;
674 }
675 
676 const std::string&
678 class_name() const
679 {
680  static const string result("crg_interval");
681  return result;
682 }
683 
684 // PROTECTED DATA MEMBERS
685 
686 // PRIVATE DATA MEMBERS
687 
688 
689 // ===========================================================
690 // ANY FACET
691 // ===========================================================
692 
693 // PUBLIC DATA MEMBERS
694 
695 bool
697 is_ancestor_of(const any* other) const
698 {
699 
700  // Preconditions:
701 
702  require(other != 0);
703 
704  // Body:
705 
706  // True if other conforms to this
707 
708  bool result = dynamic_cast<const crg_interval*>(other) != 0;
709 
710  // Postconditions:
711 
712  return result;
713 }
714 
717 clone() const
718 {
719  crg_interval* result = 0;
720 
721  // Preconditions:
722 
723  // Body:
724 
725  is_abstract();
726 
727  // Postconditions:
728 
729  ensure(result != 0);
730  ensure(is_same_type(result));
731 
732  // Exit:
733 
734  return result;
735 }
736 
737 bool
739 invariant() const
740 {
741  bool result = true;
742 
743  if(invariant_check())
744  {
745  // Prevent recursive calls to invariant
746 
748 
749  // Must satisfy base class invariant
750 
751  invariance(any::invariant());
752 
753  // Invariances for this class:
754 
755  invariance(local_id_space_initialized() ? begin() == local_id_space().offset() : true);
756  invariance(local_id_space_initialized() ? end() == local_id_space().offset() + local_id_space().end() : true);
757 
758  // Finished, turn invariant checking back on.
759 
761  }
762 
763  // Exit
764 
765  return result;
766 }
767 
768 // PROTECTED DATA MEMBERS
769 
770 // PRIVATE DATA MEMBERS
771 
772 
773 // ===========================================================
774 // NON-MEMBER FUNCTIONS
775 // ===========================================================
776 
777 std::ostream&
778 sheaf::operator << (std::ostream& os, const crg_interval& m)
779 {
780 
781  os << "interval begin: " << m.begin()
782  << " interval end: " << m.end()
783  << " interval size: " << m.size()
784  << endl;
785 
786  return os;
787 }
788 
789 size_t
790 sheaf::deep_size(const crg_interval& xp, bool xinclude_shallow)
791 {
792  size_t result;
793 
794  // Preconditions:
795 
796  // Body:
797 
798  result = xinclude_shallow ? sizeof(xp) : 0;
799 
800  // Postconditions:
801 
802  ensure(result >= 0);
803 
804  // Exit
805 
806  return result;
807 }
808 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
void initialize(const namespace_poset &xnamespace)
Sets is_initialized() == true; dummy routine provided to satisfy factory template.
virtual void attach_to(pod_type xindex)
Attach to the state with index xindex in the id space family id_spaces().
index_space_family * _id_spaces
The id space family of this interval.
Definition: crg_interval.h:186
virtual pod_type index() const
Index of this space.
crg_interval()
Default Constructor; constructs an uninitialized interval.
Definition: crg_interval.cc:42
An abstract iterator over the ids of an id space.
const hub_index_space_handle & hub_id_space() const
The hub id space of this interval.
The default name space; a poset which contains other posets as members.
pod_index_type _lower_covers_begin
The offset into the id space family for the interval of lower covers.
Definition: crg_interval.h:283
bool _lower_covers_initialized
True if and only if the lower cover is initialized.
Definition: crg_interval.h:288
bool _local_id_space_initialized
True if local id space has been initialized.
Definition: crg_interval.h:237
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 list_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, bool xis_persistent)
Create a new list id space in the id space family xid_space at the next available id space index with...
explicit_cover_map_type & explicit_cover_map(bool xlower)
The lower (xlower true) or upper (xlower false) map to explicit cover id spaces.
Definition: crg_interval.h:359
void initialize_covers()
Initialize the upper and lower covers for this interval.
An map implementation of class scattered_insertion_index_space_handle. This representation is intende...
STL namespace.
bool _upper_covers_initialized
True if and only if the upper cover is initialized.
Definition: crg_interval.h:298
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
primary_index_space_handle _local_id_space
The id space of this interval.
Definition: crg_interval.h:232
pod_index_type begin() const
The beginning of the member interval; this emulates members in the interval [begin(), end()), includes interval member.
virtual void next()=0
Makes id() the next id in the iteration.
virtual void initialize_lower_covers()=0
Initialize the lower covers for this interval.
virtual ~crg_interval()
Destructor.
Definition: crg_interval.cc:62
static std::string explicit_cover_name(bool xlower, const scoped_index &xmbr_index)
Creates a name for the explicit cover id space for member with index, xmbr_index. ...
void initialize_local_id_space(const index_space_handle &xlocal_id_space)
Sets the local id space to xlocal_id_space and initializes local ids.
An implementation of class sum_index_space_handle that has a primary sum id space state...
pod_index_type cover_id_space_id(bool xlower, pod_index_type xmbr_index) const
The id for the lower (xlower true) or upper (xlower false) cover id space with index xmbr_index...
index_space_iterator & get_id_space_iterator(const std::string &xname) const
Allocates an id space iterator from the iterator pool attached to state with name xname...
void initialize_id_spaces(index_space_family &xid_spaces)
Set the index space family to xid_spaces.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
An implementation of class explicit_index_space_handle that has a primary id space state...
bool is_hub_scope() const
True if and only if the id space of this is the hub id space.
Definition: scoped_index.h:575
bool local_id_space_initialized() const
True if local id space has been initialized.
virtual bool invariant() const
Class invariant.
Abstract base class with useful features for all objects.
Definition: any.h:39
pod_index_type _upper_covers_begin
The offset into the id space family for the interval of upper covers.
Definition: crg_interval.h:293
bool handle_conforms_to_state(const std::string &xname) const
True if handle of type T conforms to the state with name xname.
Abstract emulator for an interval of cover relation graph members.
Definition: crg_interval.h:70
bool is_done() const
True if iteration is finished.
pod_index_type end() const
The end of the member interval.
A factory for instanting descendants of an abstract type T, given the class name of the descendant...
Definition: eval_family.h:49
void release_id_space_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
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.
virtual void initialize_upper_covers()=0
Initialize the upper cover for this interval.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
bool _id_spaces_initialized
True if the id space family has been initialized.
Definition: crg_interval.h:191
const bool UPPER
Selector for upper cover.
Definition: sheaf.h:72
void put(const index_space_handle &xid_space, pod_type xpod)
Set the scope to id space, xid_space and pod() to xpod.
Definition: scoped_index.h:332
virtual bool is_initialized() const
True if fully initialized.
bool contains_member(pod_index_type xindex) const
True if and only if the index xindex is contained in this interval.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
const index_space_family & id_spaces() const
The id space family for this interval.
void delete_space(pod_type xid)
Delete the id space with index xid.
const bool LOWER
Selector for lower cover.
Definition: sheaf.h:67
static crg_interval * new_interval(const std::string &xname)
Creates a default constructed instance of the the class with name xname.
size_type size() const
The number of members in the member interval.
virtual crg_interval * clone() const =0
Virtual constructor, makes a new instance of the same type as this.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
static factory< crg_interval > & interval_factory()
A factory for making descendants of this class.
static std::string local_id_space_name(pod_index_type xindex)
The reserved name for the local id space of the ramge member with index xindex.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
bool id_spaces_initialized() const
True if the index space family has been initialized.
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
A handle for a scattered_insertion_index_space_state.
virtual pod_type end() const
Ending id of this space.
void push_back(const scoped_index &xhub_id)
Make the next id in this space equivalent to xhub_id in the hub id space. synonym for push_back(xhub_...
virtual size_type ct() const
The number of members.
virtual const std::string & class_name() const
The name of this class; provided to satisfy factory template.
Factory and container for a family of id spaces.
const primary_index_space_handle & local_id_space() const
The local id space of this interval.
bool cover_is_explicit(bool xlower, pod_index_type xmbr_index) const
True if and only if the lower (xlower true) or upper (xlower false) cover with index xmbr_index is ex...
unordered::unordered_map< pod_index_type, pod_index_type > explicit_cover_map_type
The type of the explicit cover maps..
Definition: crg_interval.h:344
pod_type offset() const
The offset into the hub id space.
bool covers_initialized(bool xlower) const
True if and only if the lower (xlower true) or upper (xlower false) covers has been initialized for t...
pod_index_type force_explicit_cover(bool xlower, pod_index_type xmbr_index, bool xinitialize=true)
The explicit lower (xlower true) or upper (xlower false) cover id space of the member with index xmbr...
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
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
static std::string make_reserved_name(const std::string &xprefix, const size_t &xindex, const std::string &xsuffix)
Creates a string reserved_prefix()_xprefix_xindex_xsuffix.
Definition: poset_path.cc:733
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710