SheafSystem  0.0.0.0
interval_index_space_handle.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/interval_index_space_handle.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/index_space_family.h"
24 #include "SheafSystem/interval_index_space_state.h"
25 #include "SheafSystem/scoped_index.h"
26 
27 // ===========================================================
28 // SPACE FACTORY FACET
29 // ===========================================================
30 
31 // PUBLIC MEMBER FUNCTIONS
32 
36  const std::string& xname,
37  bool xis_persistent,
38  bool xmerge_mode)
39 {
40  // Preconditions:
41 
42  require(!xname.empty());
43  require(!xid_spaces.contains(xname));
44 
45  // Body:
46 
49  xname,
50  xis_persistent,
51  xmerge_mode);
52 
53  // Postconditions:
54 
55  ensure(&result.id_spaces() == &xid_spaces);
56  ensure(xid_spaces.contains(xname));
57  ensure(result.conforms_to_state(xname));
58 
59  ensure(result.is_persistent() == xis_persistent);
60  ensure(result.name() == xname);
61 
62  ensure(result.merge_mode() >= xmerge_mode);
63 
64  // Exit:
65 
66  return result;
67 }
68 
69 // PROTECTED MEMBER FUNCTIONS
70 
71 // PRIVATE MEMBER FUNCTIONS
72 
73 
74 // ===========================================================
75 // INTERVAL_INDEX_SPACE_HANDLE FACET
76 // ===========================================================
77 
78 // PUBLIC MEMBER FUNCTIONS
79 
83 {
84  // Preconditions:
85 
86  // Body:
87 
88  // Postconditions:
89 
90  ensure(invariant());
91 
92  // Exit:
93 
94  return;
95 }
96 
99 {
100  // Preconditions:
101 
102  // Body:
103 
104  (*this) = xother;
105 
106  // Postconditions:
107 
108  ensure(invariant());
109  ensure((*this) == xother);
110 
111  // Exit:
112 
113  return;
114 }
115 
118  pod_type xindex)
119 {
120  // Preconditions:
121 
122  require(xid_spaces.contains(xindex));
123  require(conforms_to_state(xid_spaces, xindex));
124 
125  // Body:
126 
127  attach_to(xid_spaces, xindex);
128 
129  // Postconditions:
130 
131  ensure(invariant());
132  ensure(is_attached());
133  ensure(&id_spaces() == &xid_spaces);
134  ensure(index() == xindex);
135 
136  // Exit:
137 
138  return;
139 }
140 
143  const std::string& xname)
144 {
145  // Preconditions:
146 
147  require(xid_spaces.contains(xname));
148  require(conforms_to_state(xid_spaces, xname));
149 
150  // Body:
151 
152  attach_to(xid_spaces, xname);
153 
154  // Postconditions:
155 
156  ensure(invariant());
157  ensure(is_attached());
158  ensure(&id_spaces() == &xid_spaces);
159  ensure(name() == xname);
160 
161  // Exit:
162 
163  return;
164 }
165 
169 {
170  // Preconditions:
171 
172  require(xother.is_attached() ? conforms_to_state(xother) : true);
173 
174  // Body:
175 
176  attach_to(xother);
177 
178  // Postconditions:
179 
180  ensure(invariant());
181  ensure((*this) == xother);
182 
183  // Exit:
184 
185  return *this;
186 }
187 
190 {
191  // Preconditions:
192 
193  // Body:
194 
195  // nothing to do.
196 
197  // Postconditions:
198 
199  // Exit:
200 
201  return;
202 }
203 
204 // PROTECTED MEMBER FUNCTIONS
205 
208 {
209  // Preconditions:
210 
211  // Body:
212 
213  attach_to(&xstate);
214 
215  // Postconditions:
216 
217  ensure(invariant());
218  ensure(is_attached());
219  ensure(&state() == &xstate);
220 
221  // Exit:
222 
223  return;
224 }
225 
229 {
230  // Preconditions:
231 
232  require(is_attached());
233 
234  // Body:
235 
237  reinterpret_cast<interval_index_space_state&>(*_state);
238 
239  // Postconditions:
240 
241  ensure(is_basic_query);
242 
243  // Exit:
244 
245  return result;
246 }
247 
250 state() const
251 {
252  // Preconditions:
253 
254  require(is_attached());
255 
256  // Body:
257 
258  const interval_index_space_state& result =
259  reinterpret_cast<const interval_index_space_state&>(*_state);
260 
261  // Postconditions:
262 
263  ensure(is_basic_query);
264 
265  // Exit:
266 
267  return result;
268 }
269 
270 // PRIVATE MEMBER FUNCTIONS
271 
272 
273 // ===========================================================
274 // INTERVAL INDEX SPACE FACET
275 // ===========================================================
276 
277 // PUBLIC MEMBER FUNCTIONS
278 
279 void
282  pod_type xend,
283  const scoped_index& xhub_begin,
284  const scoped_index& xhub_end)
285 {
286  // Preconditions:
287 
288  require(is_attached());
289  require(xhub_begin.in_scope());
290  require(xhub_end.in_scope());
291  require(precondition_of(insert_interval(xbegin, xend.
292  xhub_begin.hub_pod(),
293  xhub_end.hub_pod())));
294 
295  // Body:
296 
297  insert_interval(xbegin, xend,
298  xhub_begin.hub_pod(), xhub_end.hub_pod());
299 
300  // Postconditions:
301 
302  ensure(postcondition_of(insert_interval(xbegin, xend,
303  xhub_begin.hub_pod(),
304  xhub_end.hub_pod())));
305 
306  // Exit:
307 
308  return;
309 }
310 
311 void
314  pod_type xend,
315  pod_type xhub_begin,
316  pod_type xhub_end)
317 {
318  // Preconditions:
319 
320  require(is_attached());
321  require(xhub_begin <= xhub_end);
322  require(xbegin <= xend);
323  require((xhub_end - xhub_begin) == (xend - xbegin));
324 
325  // Body:
326 
327  define_old_variable(pod_type old_ct = ct());
328 
329  state().insert_interval(xbegin, xend, xhub_begin, xhub_end);
330 
331  // Postconditions:
332 
333  ensure(ct() >= old_ct);
334  ensure_for_range(pod_type i=xhub_begin, i<=xhub_end, ++i, pod(i) == xbegin+(i-xhub_begin));
335  ensure_for_range(pod_type i=xbegin, i<=xend, ++i, hub_pod(i) == xhub_begin+(i-xbegin));
336 
337  // Exit:
338 
339  return;
340 }
341 
342 void
344 push_interval(const scoped_index& xhub_begin, const scoped_index& xhub_end)
345 {
346  // Preconditions:
347 
348  require(xhub_begin.in_scope());
349  require(xhub_end.in_scope());
350  require(precondition_of(push_interval(xhub_begin.hub_pod(),
351  xhub_end.hub_pod())));
352 
353  // Body:
354 
355  push_interval(xhub_begin.hub_pod(), xhub_end.hub_pod());
356 
357  // Postconditions:
358 
359  ensure(postcondition_of(push_interval(xhub_begin.hub_pod(),
360  xhub_end.hub_pod())));
361 
362  // Exit:
363 
364  return;
365 }
366 
367 void
369 push_interval(pod_type xhub_begin, pod_type xhub_end)
370 {
371  // Preconditions:
372 
373  require(xhub_begin <= xhub_end);
374 
375  // Body:
376 
377  // Define old variables.
378 
379  define_old_variable(pod_type old_ct = ct());
380  define_old_variable(pod_type old_next_id = next_id());
381 
382  state().push_interval(xhub_begin, xhub_end);
383 
384  // Postconditions:
385 
386  ensure(ct() == old_ct + (xhub_end - xhub_begin + 1));
387  ensure(next_id() == old_next_id + (xhub_end - xhub_begin + 1));
388  ensure_for_range(pod_type i=xhub_begin, i<=xhub_end, ++i, pod(i) == old_next_id+(i-xhub_begin));
389 
390  // Exit:
391 
392  return;
393 }
394 
397 remove_interval(const scoped_index& xhub_begin,
398  const scoped_index& xhub_end)
399 {
400  // Preconditions:
401 
402  require(is_attached());
403  require(xhub_begin.in_scope());
404  require(xhub_end.in_scope());
405  require(precondition_of(remove_hub_interval(xhub_begin.hub_pod(),
406  xhub_end.hub_pod())));
407 
408  // Body:
409 
410  size_type result = remove_hub_interval(xhub_begin.hub_pod(),
411  xhub_end.hub_pod());
412 
413  // Postconditions:
414 
415  ensure(postcondition_of(remove_hub_interval(xhub_begin.hub_pod(),
416  xhub_end.hub_pod())));
417 
418  // Exit:
419 
420  return result;
421 }
422 
425 remove_hub_interval(pod_type xhub_begin, pod_type xhub_end)
426 {
427  // Preconditions:
428 
429  require(is_attached());
430  require(xhub_begin <= xhub_end);
431 
432  // Body:
433 
434  size_type result = state().remove_hub_interval(xhub_begin, xhub_end);
435 
436  // Postconditions:
437 
438  ensure_for_range(pod_type i=xhub_begin, i<=xhub_end, ++i, !contains_hub(i));
439 
440  // Exit
441 
442  return result;
443 }
444 
448 {
449  // Preconditions:
450 
451  require(is_attached());
452  require(xbegin <= xend);
453 
454  // Body:
455 
456  size_type result = state().remove_interval(xbegin, xend);
457 
458  // Postconditions:
459 
460  ensure_for_range(pod_type i=xbegin, i<=xend, ++i, !contains(i));
461 
462  // Exit
463 
464  return result;
465 }
466 
469 interval_begin(const scoped_index& xid) const
470 {
471  // Preconditions:
472 
473  require(is_attached());
474  require(xid.in_scope());
475  require(precondition_of(interval_begin(xid.hub_pod())));
476 
477  // Body:
478 
479  pod_type result = interval_begin(xid.hub_pod());
480 
481  // Postconditions:
482 
483  ensure(postcondition_of(interval_begin(xid.hub_pod())));
484 
485  // Exit:
486 
487  return result;
488 }
489 
492 interval_begin(pod_type xhub_id) const
493 {
494  // Preconditions:
495 
496  require(is_attached());
497 
498  // Body:
499 
500  pod_type result = state().interval_begin(xhub_id);
501 
502  // Postconditions:
503 
504  ensure(!is_valid(result) || contains_hub(result));
505 
506  // Exit:
507 
508  return result;
509 }
510 
513 interval_end(const scoped_index& xid) const
514 {
515  // Preconditions:
516 
517  require(is_attached());
518  require(xid.in_scope());
519  require(precondition_of(interval_end(xid.hub_pod())));
520 
521  // Body:
522 
523  pod_type result = interval_end(xid.hub_pod());
524 
525  // Postconditions:
526 
527  ensure(postcondition_of(interval_end(xid.hub_pod())));
528 
529  // Exit:
530 
531  return result;
532 }
533 
536 interval_end(pod_type xhub_id) const
537 {
538  // Preconditions:
539 
540  require(is_attached());
541 
542  // Body:
543 
544  pod_type result = state().interval_end(xhub_id);
545 
546  // Postconditions:
547 
548  ensure(!is_valid(result) || contains_hub(result));
549 
550  // Exit:
551 
552  return result;
553 }
554 
555 bool
557 merge_mode() const
558 {
559  // Preconditions:
560 
561  require(is_attached());
562 
563  // Body:
564 
565  bool result = state().merge_mode();
566 
567  // Postcondtions:
568 
569  ensure(is_basic_query);
570 
571  // Exit:
572 
573  return result;
574 }
575 
576 void
578 put_merge_mode(bool xvalue)
579 {
580  // Preconditions:
581 
582  require(is_attached());
583 
584  // Body:
585 
586  state().put_merge_mode(xvalue);
587 
588  // Postconditions:
589 
590  ensure(merge_mode() == xvalue);
591 
592  // Exit:
593 
594  return;
595 }
596 
597 // PROTECTED MEMBER FUNCTIONS
598 
599 // PRIVATE MEMBER FUNCTIONS
600 
601 
602 // ===========================================================
603 // SCATTERED_INSERTION_INDEX_SPACE_HANDLE FACET
604 // ===========================================================
605 
606 // PUBLIC MEMBER FUNCTIONS
607 
608 // PROTECTED MEMBER FUNCTIONS
609 
610 // PRIVATE MEMBER FUNCTIONS
611 
612 
613 // ===========================================================
614 // MAP REPRESENTATION FACET
615 // ===========================================================
616 
617 // PUBLIC MEMBER FUNCTIONS
618 
619 void
621 print_map_rep(std::ostream& xos) const
622 {
623  // Preconditions:
624 
625  // Body:
626 
627  state().print_map_rep(xos);
628 
629  // Postconditions:
630 
631  ensure(is_basic_query);
632 
633  // Exit:
634 
635  return;
636 }
637 
638 // PROTECTED MEMBER FUNCTIONS
639 
640 // PRIVATE MEMBER FUNCTIONS
641 
642 
643 // ===========================================================
644 // INDEX_SPACE_HANDLE FACET
645 // ===========================================================
646 
647 // PUBLIC MEMBER FUNCTIONS
648 
652 {
653  // Preconditions:
654 
655  require(xother.is_attached() ? conforms_to_state(xother) : true);
656 
657  // Body:
658 
659  attach_to(xother);
660 
661  // Postconditions:
662 
663  ensure(invariant());
664  ensure((*this) == xother);
665 
666  // Exit:
667 
668  return *this;
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  ensure(*result == *this);
686 
687  // Exit:
688 
689  return result;
690 }
691 
692 // PROTECTED MEMBER FUNCTIONS
693 
694 // PRIVATE MEMBER FUNCTIONS
695 
696 
697 // ===========================================================
698 // HANDLE FACET
699 // ===========================================================
700 
701 // PUBLIC MEMBER FUNCTIONS
702 
703 bool
706  pod_type xlocal_id) const
707 {
708  // Preconditions:
709 
710  require(xhost.contains(xlocal_id));
711 
712  // Body:
713 
714  bool result =
715  (dynamic_cast<interval_index_space_state*>(state(xhost, xlocal_id)) != 0);
716 
717  // Postconditions:
718 
719  ensure(is_basic_query);
720 
721  // Exit:
722 
723  return result;
724 }
725 
726 // PROTECTED MEMBER FUNCTIONS
727 
728 // PRIVATE MEMBER FUNCTIONS
729 
730 
731 // ===========================================================
732 // ANY FACET
733 // ===========================================================
734 
735 // PUBLIC MEMBER FUNCTIONS
736 
737 bool
739 is_ancestor_of(const any *other) const
740 {
741  // Preconditions:
742 
743  require(other != 0);
744 
745  // Body:
746 
747  // True if other conforms to this
748 
749  bool result = dynamic_cast<const interval_index_space_handle*>(other) != 0;
750 
751  // Postconditions:
752 
753  // Exit:
754 
755  return result;
756 }
757 
758 bool
760 invariant() const
761 {
762  bool result = true;
763 
764  if(invariant_check())
765  {
766  // Prevent recursive calls to invariant
767 
769 
770  // Must satisfy base class invariant
771 
773 
774  // Invariances for this class:
775 
776  // Finished, turn invariant checking back on.
777 
779  }
780 
781  // Exit
782 
783  return result;
784 }
785 
786 // PROTECTED MEMBER FUNCTIONS
787 
788 // PRIVATE MEMBER FUNCTIONS
789 
790 
791 // ===========================================================
792 // NON-MEMBER FUNCTIONS
793 // ===========================================================
794 
795 
An implementation of class scattered_insertion_index_space_handle that has a interval id space state...
An STL map implementation of class scattered_insertion_index_space_state optimized to efficiently rep...
pod_type interval_end(const scoped_index &xid) const
The end of the interval containing xid.hub_pod. synonym for internval_end(xid.hub_pod()).
virtual void attach_to(pod_type xindex)
Attach to the state with index xindex in the id space family id_spaces().
virtual pod_type index() const
Index of this space.
bool in_scope() const
True if and only if scope() contains an entry for pod().
Definition: scoped_index.h:584
pod_type next_id() const
The id inserted by the next call to push_back.
pod_type interval_end(const scoped_index &xid) const
The end of the interval containing xid.hub_pod. synonym for internval_end(xid.hub_pod()).
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 remove_hub_interval(pod_type xhub_begin, pod_type xhub_end)
Removes the equivalance associated with the interval [xhub_begin, xhub_end]. Returns the number of en...
bool merge_mode() const
True if and only if compatible intervals should be merged.
static interval_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, bool xis_persistent, bool xmerge_mode)
Create a new interval id space in the id space family xid_space at the next available id space index ...
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...
void put_merge_mode(bool xvalue)
Sets merge_mode() to xvalue.
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 bool is_attached() const =0
True if this handle is attached to a state.
pod_type interval_begin(const scoped_index &xid) const
The beginning of the interval containing xid.hub_pod(). synonym for internval_begin(xid.hub_pod()).
void print_map_rep(std::ostream &xos) const
Inserts the map representation into ostream xos.
size_type remove_interval(const scoped_index &xhub_begin, const scoped_index &xhub_end)
Removes the equivalance associated with the interval [xhub_begin.hub_pod(), xhub_end.hub_pod()]. synonym for remove_hub_interval(xhub_begin.hub_pod(), xhub_end.hub_pod()). Returns the number of entries actually removed, if any.
virtual pod_type pod(pod_type xid) const
The pod index in this space equivalent to xid in the hub id space.
pod_type interval_begin(const scoped_index &xid) const
The beginning of the interval containing xid.hub_pod(). synonym for internval_begin(xid.hub_pod()).
Abstract base class with useful features for all objects.
Definition: any.h:39
bool merge_mode() const
True if and only if compatible intervals should be merged.
void insert_interval(pod_type xbegin, pod_type xend, const scoped_index &xhub_begin, const scoped_index &xhub_end)
Make the closed interval [xbegin, xend] equivalent to [xbegin.hub_pod(), xhub_end.hub_pod()]. synonym for insert_interval(xbegin, xend, xhub_begin.hub_pod(), xhub_end.hub_pod()).
std::string name() const
Name of this space.
pod_index_type pod_type
The "plain old data" index type for this.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
virtual bool is_attached() const
True if this handle is attached to a state.
interval_index_space_handle & operator=(const interval_index_space_handle &xother)
Assignment operator; attach this handle to the state of xother. synonym for attach_to(xother).
void insert_interval(pod_type xbegin, pod_type xend, const scoped_index &xhub_begin, const scoped_index &xhub_end)
Make the closed interval [xbegin, xend] equivalent to [xbegin.hub_pod(), xhub_end.hub_pod()]. synonym for insert_interval(xbegin, xend, xhub_begin.hub_pod(), xhub_end.hub_pod()).
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
void push_interval(const scoped_index &xhub_begin, const scoped_index &xhub_end)
Push the closed interval [xhub_begin.hub_pod(), xhub_end.hub_pod()] to the end of this space...
size_type remove_hub_interval(pod_type xhub_begin, pod_type xhub_end)
Removes the equivalance associated with the interval [xhub_begin, xhub_end]. Returns the number of en...
A collection of id space states. This is a virtual class with provides an interface for accessing the...
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 contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual bool invariant() const
Class invariant.
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...
void put_merge_mode(bool xvalue)
Sets merge_mode() to xvalue.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual interval_index_space_handle * clone() const
Virtual constructor, makes a new instance of the same type as this. If the handle is attached...
interval_index_space_state & state()
The interval id space state (mutable version).
A handle for a scattered_insertion_index_space_state.
bool contains(pod_type xid) const
True, if this contains an id space with id xid.
virtual size_type ct() const
The number of members.
Factory and container for a family of id spaces.
void push_interval(const scoped_index &xhub_begin, const scoped_index &xhub_end)
Push the closed interval [xhub_begin.hub_pod(), xhub_end.hub_pod()] to the end of this 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
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
size_type remove_interval(const scoped_index &xhub_begin, const scoped_index &xhub_end)
Removes the equivalance associated with the interval [xhub_begin.hub_pod(), xhub_end.hub_pod()]. synonym for remove_hub_interval(xhub_begin.hub_pod(), xhub_end.hub_pod()). Returns the number of entries actually removed, if any.
static interval_index_space_handle new_space(index_space_family &xid_spaces, const std::string &xname, bool xis_persistent, bool xmerge_mode)
Create a new interval id space in the id space family xid_space with name xname, persistence xis_pers...
virtual bool contains(pod_type xid) const
True if this space contains id xid.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710
void print_map_rep(std::ostream &xos) const
Inserts the map representation into ostream xos.