SheafSystem  0.0.0.0
filtered_depth_first_member_iterator.cc
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 
18 // Implementation for class filtered_depth_first_member_iterator
19 
20 
21 
22 #include "SheafSystem/filtered_depth_first_member_iterator.h"
23 
24 #include "SheafSystem/poset_state_handle.h"
25 #include "SheafSystem/assert_contract.h"
26 #include "SheafSystem/total_poset_member.h"
27 
28 // const bool sheaf::filtered_depth_first_member_iterator::DOWN = true;
29 // const bool sheaf::filtered_depth_first_member_iterator::UP = false;
30 // const bool sheaf::filtered_depth_first_member_iterator::RESET = true;
31 // const bool sheaf::filtered_depth_first_member_iterator::NO_RESET = false;
32 // const bool sheaf::filtered_depth_first_member_iterator::STRICT = true;
33 // const bool sheaf::filtered_depth_first_member_iterator::NOT_STRICT = false;
34 
35 
36 sheaf::filtered_depth_first_member_iterator::
37 filtered_depth_first_member_iterator(const filtered_depth_first_member_iterator& xother)
38 {
39 
40  // Preconditions:
41 
42  // Body:
43 
44  _iterator = xother._iterator->clone();
45  (*_iterator) = *(xother._iterator);
46 
47  _item = xother._item->clone();
48 
49  // Postconditions:
50 
51  ensure(invariant());
52  ensure(is_initialized() == xother.is_initialized());
53  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
54  ensure(is_initialized() ? filter().is_same_state(&xother.filter()) : true);
55  ensure(is_initialized() ? descending() == xother.descending() : true);
56  ensure(is_initialized() ? strict() == xother.strict() : true);
57  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
58  ensure(unexecutable(this is first member of iteration or is_done()));
59 
60  // Exit
61 
62  return;
63 }
64 
65 
69 {
70 
71  // Preconditions:
72 
73  require(is_ancestor_of(&xother));
74 
75  // Body:
76 
77  if(this != &(xother))
78  {
80 
81  delete _iterator;
82  _iterator = lother._iterator->clone();
83  (*_iterator) = *(lother._iterator);
84 
86  delete _item;
87  _item = lother._item->clone();
88  }
89 
90 
91  // Postconditions:
92 
93  ensure(invariant());
94  ensure(is_initialized() == xother.is_initialized());
95  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
96  ensure(is_initialized() ? filter().is_same_state(&xother.filter()) : true);
97  ensure(is_initialized() ? descending() == xother.descending() : true);
98  ensure(is_initialized() ? strict() == xother.strict() : true);
99  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
100  ensure(unexecutable(this is first member of iteration or is_done()));
101 
102  // Exit
103 
104  return *this;
105 }
106 
107 
110 {
111 
112  // Preconditions:
113 
114  // Body:
115 
116  delete _iterator;
117  _iterator = 0;
118 
120  delete _item;
121  _item = 0;
122 
123  // Postconditions:
124 
125  // Exit:
126 
127  return;
128 }
129 
130 bool
132 is_ancestor_of(const any* xother) const
133 {
134  bool result;
135 
136  // Preconditions:
137 
138  // Body:
139 
140  result = dynamic_cast<const filtered_depth_first_member_iterator*>(xother) != 0;
141 
142  // Postconditions:
143 
144  // Exit
145 
146  return result;
147 }
148 
149 bool
151 invariant() const
152 {
153  bool result = true;
154 
155  // Preconditions:
156 
157  // Body:
158 
159  result = result && any::invariant();
160 
161  if(invariant_check())
162  {
164 
165  invariance( _item != 0 );
166  invariance( _iterator != 0 );
167  invariance( is_initialized() == (_iterator->is_initialized()) );
168 
169  // Finished, turn invariant checking back on.
170 
172  }
173 
174  // Postconditions:
175 
176  // Exit
177 
178  return result;
179 }
180 
181 // ITERATOR FACET
182 
186 {
188 
189  // Preconditions:
190 
191  require(is_initialized());
192 
193  // Body:
194 
195  result = _iterator->order();
196 
197  // Postconditions:
198 
199  // Exit
200 
201  return result;
202 }
203 
204 
205 
206 bool
209 {
210  bool result;
211 
212  // Preconditions:
213 
214  // Body:
215 
216  result = _iterator->is_initialized();
217 
218  // Postconditions:
219 
220  // Exit
221 
222  return result;
223 }
224 
225 
226 
227 
228 
232 {
233  // Preconditions:
234 
235  require(is_initialized());
236 
237  // Body:
238 
240 
241  // Postconditions:
242 
243  // Exit
244 
245  return result;
246 }
247 
248 
249 
252 anchor() const
253 {
254  // Preconditions:
255 
256  require(is_initialized());
257 
258  // Body:
259 
261 
262  // Postconditions:
263 
264  // Exit
265 
266  return result;
267 }
268 
269 
270 
271 void
274 {
275  // Preconditions:
276 
277  require(xanchor!=0);
278 
279  // Body:
280 
281  define_old_variable(bool old_descending = descending());
282  define_old_variable(bool old_strict = strict());
283 
284  // Set the iterator's anchor and update the item.
285 
286  _iterator->put_anchor(xanchor);
287  update_item();
288 
289  // Postconditions:
290 
291  ensure(invariant());
292  ensure(is_initialized());
293  ensure(is_done());
294  ensure(anchor().is_same_state(xanchor));
295  ensure(anchor().is_same_type(xanchor));
296  ensure(anchor().version() == xanchor->version());
297  ensure(filter().index() == anchor().version_index());
298  ensure(descending() == old_descending);
299  ensure(strict() == old_strict);
300 
301  // Exit:
302 
303 }
304 
305 void
307 put_anchor(pod_index_type xanchor_hub_id)
308 {
309  // Preconditions:
310 
311  require(is_initialized());
312 
313  // Body:
314 
315  // Set the iterator's anchor and update the item.
316 
317  _iterator->put_anchor(xanchor_hub_id);
318  update_item();
319 
320  // Postconditions:
321 
322  ensure(invariant());
323  ensure(is_initialized());
324  ensure(is_done());
325  ensure(anchor().index() == xanchor_hub_id);
326 
327  // Exit:
328 
329 }
330 
331 void
333 put_anchor(const scoped_index& xanchor_id)
334 {
335  // Preconditions:
336 
337  require(is_initialized());
338 
339  // Body:
340 
341  put_anchor(xanchor_id.hub_pod());
342 
343  // Postconditions:
344 
345  ensure(invariant());
346  ensure(is_initialized());
347  ensure(is_done());
348  ensure(anchor().index() ==~ xanchor_id);
349 
350  // Exit:
351 
352 }
353 
354 const sheaf::subposet&
356 filter() const
357 {
358  // Preconditions:
359 
360  require(is_initialized());
361 
362  // Body:
363 
364  subposet& result = _iterator->filter();
365 
366  // Postconditions:
367 
368  // Exit
369 
370  return result;
371 }
372 
373 void
375 put_filter(const subposet& xfilter)
376 {
377 
378  // Preconditions:
379 
380  require(anchor().host()->includes_subposet(&xfilter));
381 
382  // Body:
383 
384  define_old_variable(bool old_descending = descending());
385  define_old_variable(bool old_strict = strict());
386 
387  // Set the iterator's filter and update the item
388 
389  _iterator->put_filter(xfilter);
390  update_item();
391 
392  // Postconditions:
393 
394  ensure(invariant());
395  ensure(is_initialized());
396  ensure(is_done());
397  ensure(filter().is_same_state(&xfilter));
398  ensure(descending() == old_descending);
399  ensure(strict() == old_strict);
400 
401  return;
402 }
403 
404 void
406 put_filter(const std::string& xfilter_name)
407 {
408 
409  // Preconditions:
410 
411  require(!xfilter_name.empty() ? anchor().host()->includes_subposet(xfilter_name) : true);
412 
413  // Body:
414 
415  define_old_variable(bool old_descending = descending());
416  define_old_variable(bool old_strict = strict());
417 
418  // Set the iterator's filter and update the item
419 
420  _iterator->put_filter(xfilter_name);
421  update_item();
422 
423  // Postconditions:
424 
425  ensure(invariant());
426  ensure(is_initialized());
427  ensure(is_done());
428  ensure(!xfilter_name.empty() ?
429  filter().name() == xfilter_name :
430  filter().name() == anchor().version_name());
431  ensure(descending() == old_descending);
432  ensure(strict() == old_strict);
433 
434  return;
435 }
436 
437 void
439 put_filter(pod_index_type xfilter_hub_id)
440 {
441 
442  // Preconditions:
443 
444  require(anchor().host()->includes_subposet(xfilter_hub_id));
445 
446  // Body:
447 
448  define_old_variable(bool old_descending = descending());
449  define_old_variable(bool old_strict = strict());
450 
451  // Set the iterator's filter and update the item
452 
453  _iterator->put_filter(xfilter_hub_id);
454  update_item();
455 
456  // Postconditions:
457 
458  ensure(invariant());
459  ensure(is_initialized());
460  ensure(is_done());
461  ensure(filter().index() == xfilter_hub_id);
462  ensure(descending() == old_descending);
463  ensure(strict() == old_strict);
464 
465  return;
466 }
467 
468 void
470 put_filter(const scoped_index& xfilter_id)
471 {
472 
473  // Preconditions:
474 
475  require(anchor().host()->includes_subposet(xfilter_id));
476 
477  // Body:
478 
479  define_old_variable(bool old_descending = descending());
480  define_old_variable(bool old_strict = strict());
481 
482  put_filter(xfilter_id.hub_pod());
483 
484  // Postconditions:
485 
486  ensure(invariant());
487  ensure(is_initialized());
488  ensure(is_done());
489  ensure(filter().index() ==~ xfilter_id);
490  ensure(descending() == old_descending);
491  ensure(strict() == old_strict);
492 
493  return;
494 }
495 
496 bool
498 descending() const
499 {
500  bool result;
501 
502  // Preconditions:
503 
504  // Body:
505 
506  result = _iterator->descending();
507 
508  // Postconditions:
509 
510  // Exit
511 
512  return result;
513 }
514 
515 void
517 put_descending(bool xdescending)
518 {
519  // Preconditions:
520 
521  // Body:
522 
523  _iterator->put_descending(xdescending);
524 
525  // Postconditions:
526 
527  ensure(descending() == xdescending);
528 
529  // Exit
530 
531  return;
532 }
533 
534 
535 
536 bool
538 strict() const
539 {
540  bool result;
541 
542  // Preconditions:
543 
544  // Body:
545 
546  result = _iterator->strict();
547 
548  // Postconditions:
549 
550  // Exit
551 
552  return result;
553 }
554 
555 void
557 put_strict(bool xstrict)
558 {
559  // Preconditions:
560 
561  // Body:
562 
563  _iterator->put_strict(xstrict);
564 
565  // Postconditions:
566 
567  ensure(strict() == xstrict);
568 
569  // Exit
570 
571  return;
572 }
573 
574 
575 
576 bool
578 is_done() const
579 {
580  bool result;
581 
582  // Preconditions:
583 
584  require(is_initialized());
585 
586  // Body:
587 
588  result = _iterator->is_done();
589 
590  // Postconditions:
591 
592  // Exit
593 
594  return result;
595 }
596 
597 
598 void
601 {
602 
603  // Preconditions:
604 
605  require(is_initialized());
606  require(!is_done());
607 
608  // Body:
609 
610  _iterator->next();
611  update_item();
612 
613  // Postconditions:
614 
615  ensure(invariant());
616  ensure(!is_done() == item().is_attached());
617 
618  // Exit
619 
620  return;
621 }
622 
623 
624 void
626 reset(bool xreset_markers)
627 {
628 
629  // Preconditions:
630 
631  require(is_initialized());
632  require(anchor().state_is_read_accessible());
633 
634  // Body:
635 
636  define_old_variable(int old_anchor_version = anchor().version());
637  define_old_variable(bool old_descending = descending());
638  define_old_variable(bool old_strict = strict());
639 
640  _iterator->reset(xreset_markers);
641  update_item();
642 
643  // Postconditions:
644 
645  ensure(invariant());
646  ensure(is_initialized());
647  ensure(anchor().version() == old_anchor_version);
648  ensure(filter().is_attached());
649  ensure(descending() == old_descending);
650  ensure(strict() == old_strict);
651  ensure(strict() && !is_done() ? item().index() !=~ anchor().index() : true);
652  ensure(item().is_attached() == !is_done());
653 
654  // Exit
655 
656  return;
657 }
658 
659 int
661 ct(bool xreset)
662 {
663  int result = 0;
664 
665  // Preconditions:
666 
667  require(is_initialized());
668  require(xreset ? anchor().state_is_read_accessible(): true);
669 
670  // Body:
671 
672  result = _iterator->ct();
673  update_item();
674 
675  // Postconditions:
676 
677  ensure(result >= 0);
678  ensure(is_done());
679 
680  // Exit
681 
682  return result;
683 }
684 
685 bool
688 {
689  bool result;
690 
691  // Preconditions:
692 
693  require(is_initialized());
694  require(anchor().state_is_read_accessible());
695  require(anchor().host()->contains_member(xhub_id));
696 
697  // Body:
698 
699  result = _iterator->has_visited(xhub_id);
700 
701  // Postconditions:
702 
703  // Exit:
704 
705  return result;
706 }
707 
708 bool
710 has_visited(const scoped_index& xid) const
711 {
712  bool result;
713 
714  // Preconditions:
715 
716  require(is_initialized());
717  require(anchor().state_is_read_accessible());
718  require(anchor().host()->contains_member(xid));
719 
720  // Body:
721 
722  return has_visited(xid.hub_pod());
723 }
724 
725 bool
728 {
729  bool result;
730 
731  // Preconditions:
732 
733  require(is_initialized());
734  require(anchor().state_is_read_accessible());
735  require(xmbr.is_attached());
736  require(anchor().host()->is_same_state(xmbr.host()));
737 
738  // Body:
739 
740  result = _iterator->has_visited(&xmbr);
741 
742  // Postconditions:
743 
744  // Exit:
745 
746  return result;
747 }
748 
749 void
751 put_has_visited(pod_index_type xhub_id, bool xvalue)
752 {
753 
754  // Preconditions:
755 
756  require(is_initialized());
757  require(anchor().state_is_read_accessible());
758  require(anchor().host()->contains_member(xhub_id));
759 
760  // Body:
761 
762  _iterator->put_has_visited(xhub_id, xvalue);
763 
764  // Postconditions:
765 
766  ensure(has_visited(xhub_id) == xvalue);
767 
768  // Exit:
769 
770  return;
771 }
772 
773 void
775 put_has_visited(const scoped_index& xid, bool xvalue)
776 {
777 
778  // Preconditions:
779 
780  require(is_initialized());
781  require(anchor().state_is_read_accessible());
782  require(anchor().host()->contains_member(xid));
783 
784  // Body:
785 
786  put_has_visited(xid.hub_pod(), xvalue);
787 
788  // Postconditions:
789 
790  ensure(has_visited(xid) == xvalue);
791 
792  // Exit:
793 
794  return;
795 }
796 
797 const sheaf::scoped_index&
800 {
801  // Preconditions:
802 
803  require(is_initialized());
804 
805  // Body:
806 
807  const scoped_index& result = _iterator->greater_index();
808 
809  // Postconditions:
810 
811  // Exit
812 
813  return result;
814 }
815 
816 const sheaf::scoped_index&
819 {
820  // Preconditions:
821 
822  require(is_initialized());
823 
824  // Body:
825 
826  const scoped_index& result = _iterator->lesser_index();
827 
828  // Postconditions:
829 
830  ensure(result == item().index());
831 
832  // Exit
833 
834  return result;
835 }
836 
837 // MEMBER ITERATOR FACET
838 
839 
840 
844 {
845  // Preconditions:
846 
847  require(is_initialized());
848 
849  // Body:
850 
851  abstract_poset_member& result = *_item;
852 
853  // Postconditions:
854 
855  // Exit
856 
857  return result;
858 }
859 
862 item() const
863 {
864  // Preconditions:
865 
866  require(is_initialized());
867 
868  // Body:
869 
870  abstract_poset_member& result = *_item;
871 
872  // Postconditions:
873 
874  // Exit
875 
876  return result;
877 }
878 
879 
880 
881 
882 bool
885 {
886  bool result;
887 
888  // Preconditions:
889 
890  // Body:
891 
892  // Always true in this class;
893  // intended to be redefined in descendants.
894 
895  result = true;
896 
897  // Postconditions:
898 
899  // Exit
900 
901  return result;
902 }
903 
904 
905 
906 bool
909 {
910  bool result;
911 
912  // Preconditions:
913 
914  // Body:
915 
916  // Always true in this class;
917  // intended to be redefined in descendants.
918 
919  result = true;
920 
921  // Postconditions:
922 
923  // Exit
924 
925  return result;
926 }
927 
928 // PROTECTED MEMBER FUNCTIONS
929 
930 sheaf::filtered_depth_first_member_iterator::
931 filtered_depth_first_member_iterator(filtered_depth_first_iterator* xitr,
932  abstract_poset_member* xitem)
933 {
934  // Preconditions:
935 
936  require(xitr != 0);
937  require(xitem != 0);
938 
939  // Body:
940 
941  _iterator = xitr;
942  _item = xitem;
943 
944  update_item();
945 
946  // Postconditions:
947 
948  ensure(_iterator == xitr);
949  ensure(_item == xitem);
950  ensure(!is_done() == item().is_attached());
951 
952  // Exit
953 
954  return;
955 }
956 
957 void
960 {
961  // Preconditions:
962 
963  require(is_initialized());
964 
965  // Body:
966 
967  if(_iterator->is_done())
968  {
970  }
971  else
972  {
974  }
975 
976  // Postconditions:
977 
978  ensure(!is_done() == item().is_attached());
979 
980  // Exit
981 
982  return;
983 }
984 
985 
986 sheaf::filtered_depth_first_member_iterator::
987 filtered_depth_first_member_iterator()
988 {
989  // Preconditions:
990 
991  // Body:
992 
993  // This routine should be unreachable.
994 
995  not_implemented();
996 
997  // Postconditions:
998 
999  // Exit
1000 
1001  return;
1002 }
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
const scoped_index & lesser_index() const
The index of the lesser member of the current link lesser_index() == index()
const scoped_index & greater_index() const
The index of the greater member of the current link.
virtual abstract_poset_member & anchor()
The poset member whose downset is being iterated over; the top member of the domain of iteration (mut...
poset_state_handle * host() const
The poset which this is a handle to a component of.
std::string name() const
Definition: subposet.h:796
A client handle for a subposet.
Definition: subposet.h:86
abstract_poset_member & item()
The the current member of the iteration (mutable version).
int version(bool xunalias=true) const
The (possibly aliased) version of this component. The version of the host used when evaluating proper...
virtual bool is_initialized() const
True if this has been initialized for iteration with respect to a specific anchor.
void put_filter(const subposet &xfilter)
Sets the subposet which is the filter.
filtered_depth_first_iterator * _iterator
The index iterator used to implement the iteration.
virtual void reset(bool xreset_markers=RESET)
Restarts the iteration.
depth_first_iterator::order_type order()
Iteration directions.
virtual bool includes_subposet(pod_index_type xsubposet_hub_id, bool xauto_access=true) const
True if this poset includes subposet with hub id xsubposet_hub_id.
void put_strict(bool xstrict)
Set strict() to xstrict.
virtual void update_item()
Attaches the item handle to the current index, or detaches the item handle if is_done.
virtual filtered_depth_first_member_iterator & operator=(const filtered_depth_first_member_iterator &xother)
Assignment operator.
order_type
The types of order in which the iterator will visit the members of the poset. Determines which action...
virtual bool item_is_ancestor_of(const abstract_poset_member &xmbr) const
True if xmbr conforms to the type of item of this.
void put_descending(bool xdescending)
Set descending() to xdescending.
Abstract base class with useful features for all objects.
Definition: any.h:39
bool has_visited(pod_index_type xhub_id) const
True if this has already visited member with hub id xhub_id.
bool descending() const
True if iterating over down set of anchor.
virtual abstract_poset_member & anchor()
The poset member whose downset is being iterated over; the top member of the domain of iteration (mut...
order_type order() const
The order of the iteration. Determines which actions are exported to the client.
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 non-void state.
const subposet & filter() const
The subposet which is the filter; Defines what is passed, not what is blocked.
bool descending() const
True if iterating over down set of anchor.
bool has_visited(pod_index_type xhub_id) const
True if this has already visited member with hub id xhub_id.
void next()
Makes this the next member of the subset.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
const scoped_index & lesser_index() const
The index of the lesser member of the current link.
const scoped_index & greater_index() const
The index of the greater member of the current link.
abstract_poset_member * clone(bool xnew_state, bool xauto_access=true) const
Virtual constructor; makes a new handle of the same type as this, attached to a new state (xnew_state...
abstract_poset_member * _item
The member handle for the current item in the iteration.
virtual bool anchor_is_ancestor_of(const abstract_poset_member &xmbr) const
True if xmbr conforms to the type of anchor of this.
virtual void put_anchor(const abstract_poset_member *xanchor)
Set anchor() to *xanchor.
bool is_done() const
True if iteration finished.
bool strict() const
True if iterating over xstrict up/down set of anchor.
virtual void detach_from_state()
Detach this handle from its state, if any.
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
void put_has_visited(pod_index_type xhub_id, bool xvalue)
Set the visited marker for hub id xhub_id to xvalue. Intended for use reseting iterator without havin...
virtual void reset(bool xreset_markers=true)
Restarts the iteration over the down set of anchor().
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual void next()
Makes this the next member of the iteration.
int ct(bool xreset=false)
The number of members of the iteration set, from the current member to the end, inclusive. If xreset, reset before computing the count.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
int ct(bool xreset=false)
The number of members of the iteration set, from the current member to the end, inclusive. If xreset, reset before computing the count.
virtual void put_anchor(const abstract_poset_member *xanchor)
Set anchor() to xanchor.
void attach_to_state(const namespace_poset *xns, const poset_path &xpath, bool xauto_access=true)
Attach to the state specified by path xpath in the namespace xns.
An abstract client handle for a member of a poset.
virtual filtered_depth_first_iterator * clone() const
Make a new instance of the same type as this.
void put_descending(bool xdescending)
Set descending() to xdescending.
subposet & filter()
The subposet which is the filter; Defines what is passed, not what is blocked.
std::string version_name() const
The subposet name for the filter associated with version().
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void put_filter(const subposet &filter)
Sets the subposet which is the filter.
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
void put_strict(bool xstrict)
Set strict() to xstrict.
bool strict() const
True if iterating over xstrict up/down set of anchor.
bool is_initialized() const
True if this has been initialized for iteration with respect to a specific anchor.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710
void put_has_visited(pod_index_type xhub_id, bool xvalue)
Set the visited marker for hub id xhub_id to xvalue. Intended for use reseting iterator without havin...
const scoped_index & index() const
The index of the current member of the iteration.