SheafSystem  0.0.0.0
poset_dof_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 poset_dof_iterator
19 
20 
21 
22 #include "SheafSystem/poset_dof_iterator.h"
23 
24 #include "SheafSystem/assert_contract.h"
25 #include "SheafSystem/poset_state_handle.h"
26 #include "SheafSystem/schema_poset_member.h"
27 
28 using namespace std;
29 
32 {
33 
34  // Preconditions:
35 
36 
37  // Body:
38 
39  _anchor = 0;
40  _item = 0;
41  _is_table_dof = false;
42 
43  // Postconditions:
44 
45  ensure(invariant());
46  ensure(!is_initialized());
47 
48  // Exit
49 
50  return;
51 }
52 
53 
56 {
57 
58  // Preconditions:
59 
60  require(is_ancestor_of(&xother));
61 
62  // Body:
63 
64  poset_dof_iterator& lother = const_cast<poset_dof_iterator&>(xother);
65 
66  if(lother.is_initialized())
67  {
68  _item = lother._item->clone();
69  _anchor = lother._anchor->clone();
70  }
71  else
72  {
73  _item = 0;
74  _anchor = 0;
75  }
76  _itr = lother._itr;
77  _is_table_dof = lother._is_table_dof;
78 
79  // Postconditions:
80 
81  ensure(invariant());
82  ensure(is_initialized() == xother.is_initialized());
83  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
84  ensure(is_initialized() ? anchor().version() == xother.anchor().version() : true);
85  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
86  ensure(is_table_dof() == xother.is_table_dof());
87 
88  // Exit
89 
90  return;
91 }
92 
93 
97 {
98 
99  // Preconditions:
100 
101  require(is_ancestor_of(&xother));
102  require(xother.is_done());
103 
104  // Body:
105 
106  poset_dof_iterator& lother = const_cast<poset_dof_iterator&>(xother);
107 
108  if(is_initialized())
109  {
110  _item->detach_from_state();
111  delete _item;
112  _item = 0;
113 
114  _anchor->detach_from_state();
115  delete _anchor;
116  _anchor = 0;
117  }
118 
119  if(lother._anchor != 0)
120  {
121  _item = lother._item->clone();
122 
123  _anchor = lother._anchor->clone();
124  _anchor->attach_to_state(lother._anchor);
125  }
126 
127  _itr = lother._itr;
128  _is_table_dof = lother._is_table_dof;
129 
130  // Postconditions:
131 
132  ensure(invariant());
133  ensure(is_initialized() == xother.is_initialized());
134  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
135  ensure(is_initialized() ? anchor().version() == xother.anchor().version() : true);
136  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
137  ensure(is_table_dof() == xother.is_table_dof());
138 
139  // Exit
140 
141  return *this;
142 }
143 
144 
147 {
148 
149  // Preconditions:
150 
151  // Body:
152 
153  if(_anchor != 0)
154  {
155  _anchor->detach_from_state();
156  delete _anchor;
157  }
158 
159  if(_item != 0)
160  {
161  _item->detach_from_state();
162  delete _item;
163  }
164 
165  // Postconditions:
166 
167  // Exit:
168 
169  return;
170 }
171 
172 
173 
174 bool
176 is_ancestor_of(const any* xother) const
177 {
178  bool result;
179 
180  // Preconditions:
181 
182  // Body:
183 
184  result = dynamic_cast<const poset_dof_iterator*>(xother) != 0;
185 
186  // Postconditions:
187 
188  // Exit
189 
190  return result;
191 }
192 
193 
196 clone() const
197 {
199 
200  // Preconditions:
201 
202  // Body:
203 
204  result = new poset_dof_iterator;
205 
206  // Postconditions:
207 
208  ensure(result != 0);
209 
210  // Exit
211 
212  return result;
213 }
214 
215 bool
217 invariant() const
218 {
219  bool result = true;
220 
221  // Preconditions:
222 
223  // Body:
224 
225  invariance( any::invariant() );
226 
227  if(invariant_check())
228  {
229  disable_invariant_check();
230 
231  invariance( is_initialized() == (_anchor != 0) );
232  invariance( is_initialized() == (_item != 0) );
233  invariance( is_initialized() == (_itr.is_initialized()) );
234 
235  // Finished, turn invariant checking back on.
236 
237  enable_invariant_check();
238  }
239 
240  // Postconditions:
241 
242  // Exit
243 
244  return result;
245 }
246 
247 
248 
250 poset_dof_iterator(const sheaf::schema_poset_member& xanchor, bool xis_table_dof, int xversion)
251 {
252 
253  // Preconditions:
254 
255  require(item_is_ancestor_of(xanchor));
256  require(xanchor.state_is_read_accessible());
257  require(xanchor.has_version(xversion));
258 
259 
260  // Body:
261 
262  // Make sure !is_initialized, then set the anchor and reset.
263 
264  _anchor = 0;
265  _item = 0;
266  _is_table_dof = xis_table_dof;
267  put_anchor(&xanchor, xversion);
268  reset();
269 
270  // Postconditions:
271 
272  ensure(invariant());
273  ensure(is_initialized());
274  ensure(anchor().is_same_state(&xanchor));
275  ensure(anchor().is_same_type(&xanchor));
276  ensure(unexecutable(postorder - have visited all children of this));
277  ensure(unexecutable(!is_done() implies this is first member));
278  ensure(item().is_attached() == !is_done());
279  ensure(is_table_dof() == xis_table_dof);
280  ensure(anchor().version() == xanchor.unaliased_version(xversion));
281 
282  return;
283 }
284 
285 
286 
287 
288 // ITERATOR FACET
289 
290 
291 
292 bool
295 {
296  bool result;
297 
298  // Preconditions:
299 
300  // Body:
301 
302  result = (_anchor != 0);
303  result = result && (_item != 0);
304  result = result && _itr.is_initialized();
305 
306  // Postconditions:
307 
308  // Exit
309 
310  return result;
311 }
312 
313 
314 
318 {
319  // Preconditions:
320 
321  require(is_initialized());
322 
323  // Body:
324 
325  schema_poset_member& result = *_anchor;
326 
327  // Postconditions:
328 
329  // Exit
330 
331  return result;
332 }
333 
334 
335 
338 anchor() const
339 {
340  // Preconditions:
341 
342  require(is_initialized());
343 
344  // Body:
345 
346  schema_poset_member& result = *_anchor;
347 
348  // Postconditions:
349 
350  // Exit
351 
352  return result;
353 }
354 
355 
356 
357 void
359 put_anchor(const sheaf::schema_poset_member* xanchor, int xversion)
360 {
361  // Preconditions:
362 
363  require(xanchor != 0);
364  require(item_is_ancestor_of(*xanchor));
365  require(xanchor->state_is_read_accessible());
366  require(xanchor->host()->is_schematized(false));
367  require(xanchor->has_version(xversion));
368 
369 
370  // Body:
371 
372  if(_anchor == 0)
373  {
374  // Create the anchor handle
375 
376  _anchor = xanchor->clone();
377  }
378 
379  // Attach the handle to the same state and
380  // (possibly aliased) version as xanchor.
381 
382  _anchor->attach_to_state(xanchor);
383 
384  // Set the version, making sure it is unaliased.
385 
386  _anchor->put_version(xversion, true);
387 
388  // Reset the index iterator.
389 
390  string ldof_sp_name = schema_poset_member::dof_subposet_name("top", _is_table_dof);
391  _itr.put_anchor(_anchor);
392  _itr.put_filter(ldof_sp_name);
393 
394  // Reset the item
395 
396  reset_item();
397 
398  // Force the iterator to be done.
399 
400  force_is_done();
401 
402  // Postconditions:
403 
404  ensure(invariant());
405  ensure(is_initialized());
406  ensure(is_done());
407  ensure(anchor().is_same_state(xanchor));
408  ensure(anchor().is_same_type(xanchor));
409  ensure(anchor().version() == xanchor->unaliased_version(xversion));
410 
411  // Exit:
412 
413 }
414 
415 void
418 {
419  // Preconditions:
420 
421  require(is_initialized());
422  require(anchor().host()->contains_member(xhub_id));
423 
424  // Body:
425 
426  define_old_variable(int old_anchor_version = anchor().version());
427 
428  // Attach the handle to the state with index xhub_id.
429 
430  _anchor->attach_to_state(xhub_id);
431 
432  // Reset the index iterator anchor
433 
434  _itr.put_anchor(xhub_id);
435 
436  // Reset the item
437 
438  reset_item();
439 
440  // Force the iterator to be done.
441 
442  force_is_done();
443 
444  // Postconditions:
445 
446  ensure(invariant());
447  ensure(is_initialized());
448  ensure(is_done());
449  ensure(anchor().index() == xhub_id);
450  ensure(anchor().version() == old_anchor_version);
451 
452  // Exit:
453 
454 }
455 
456 void
459 {
460  // Preconditions:
461 
462  require(is_initialized());
463  require(anchor().host()->contains_member(xid));
464 
465  // Body:
466 
467  define_old_variable(int old_anchor_version = anchor().version());
468 
469  put_anchor(xid.hub_pod());
470 
471  // Postconditions:
472 
473  ensure(invariant());
474  ensure(is_initialized());
475  ensure(is_done());
476  ensure(anchor().index() ==~ xid);
477  ensure(anchor().version() == old_anchor_version);
478 
479  // Exit:
480 
481 }
482 
483 bool
485 is_done() const
486 {
487  bool result;
488 
489  // Preconditions:
490 
491  require(is_initialized());
492 
493  // Body:
494 
495  result = _itr.is_done();
496 
497  // Postconditions:
498 
499  // Exit
500 
501  return result;
502 }
503 
504 void
507 {
508  // Preconditions:
509 
510  require(is_initialized());
511 
512  // Body:
513 
514  _itr.force_is_done();
515  _item->detach_from_state();
516 
517  // Postconditions:
518 
519  ensure(invariant());
520  ensure(is_done());
521 
522  // Exit:
523 }
524 
525 void
528 {
529 
530  // Preconditions:
531 
532  require(is_initialized());
533  require(!is_done());
534 
535  // Body:
536 
537  _itr.next();
538  update_item();
539 
540  // Postconditions:
541 
542  ensure(invariant());
543  ensure(!is_done() == item().is_attached());
544  ensure(unexecutable(postorder - have visited all of the members of the strict down set
545  of item()));
546 
547  // Exit
548 
549  return;
550 }
551 
552 void
554 reset(bool xreset_markers)
555 {
556  // Preconditions:
557 
558  require(is_initialized());
559  require(anchor().state_is_read_accessible());
560 
561  // Body:
562 
563  define_old_variable(int old_anchor_version = anchor().version());
564 
565  _itr.reset(xreset_markers);
566  update_item();
567 
568  // Postconditions:
569 
570  ensure(invariant());
571  ensure(is_initialized());
572  ensure(item().is_attached() == !is_done());
573  ensure(anchor().version() == old_anchor_version);
574 
575  // Exit
576 
577  return;
578 }
579 
580 int
582 ct(bool xreset)
583 {
584  int result = 0;
585 
586  // Preconditions:
587 
588  require(is_initialized());
589  require(xreset ? anchor().state_is_read_accessible(): true);
590 
591  // Body:
592 
593  result = _itr.ct(xreset);
594  update_item();
595 
596  // Postconditions:
597 
598  ensure(result >= 0);
599  ensure(is_done());
600 
601  // Exit
602 
603  return result;
604 }
605 
606 bool
609 {
610  bool result;
611 
612  // Preconditions:
613 
614  require(is_initialized());
615  require(anchor().state_is_read_accessible());
616  require(anchor().host()->contains_member(xhub_id));
617 
618  // Body:
619 
620  result = _itr.has_visited(xhub_id);
621 
622  // Postconditions:
623 
624  // Exit:
625 
626  return result;
627 }
628 
629 
630 bool
632 has_visited(const scoped_index& xid) const
633 {
634  bool result;
635 
636  // Preconditions:
637 
638  require(is_initialized());
639  require(anchor().state_is_read_accessible());
640  require(anchor().host()->contains_member(xid));
641 
642  // Body:
643 
644  return has_visited(xid.hub_pod());
645 }
646 
647 bool
650 {
651  bool result;
652 
653  // Preconditions:
654 
655  require(is_initialized());
656  require(anchor().state_is_read_accessible());
657  require(item_is_ancestor_of(xmbr));
658  require(xmbr.is_attached());
659  require(anchor().host()->is_same_state(xmbr.host()));
660 
661  // Body:
662 
663  result = _itr.has_visited(&xmbr);
664 
665  // Postconditions:
666 
667  // Exit:
668 
669  return result;
670 }
671 
672 
673 void
675 put_has_visited(pod_index_type xhub_id, bool xvalue)
676 {
677 
678  // Preconditions:
679 
680  require(is_initialized());
681  require(anchor().state_is_read_accessible());
682  require(anchor().host()->contains_member(xhub_id));
683 
684  // Body:
685 
686  _itr.put_has_visited(xhub_id, xvalue);
687 
688  // Postconditions:
689 
690  ensure(has_visited(xhub_id) == xvalue);
691 
692  // Exit:
693 
694  return;
695 }
696 
697 void
699 put_has_visited(const scoped_index& xid, bool xvalue)
700 {
701 
702  // Preconditions:
703 
704  require(is_initialized());
705  require(anchor().state_is_read_accessible());
706  require(anchor().host()->contains_member(xid));
707 
708  // Body:
709 
710  put_has_visited(xid.hub_pod(), xvalue);
711 
712  // Postconditions:
713 
714  ensure(has_visited(xid) == xvalue);
715 
716  // Exit:
717 
718  return;
719 }
720 
721 // MEMBER ITERATOR FACET
722 
726 {
727  // Preconditions:
728 
729  require(is_initialized());
730 
731  // Body:
732 
733  schema_poset_member& result = *_item;
734 
735  // Postconditions:
736 
737  // Exit
738 
739  return result;
740 }
741 
742 
743 
746 item() const
747 {
748  // Preconditions:
749 
750  require(is_initialized());
751 
752  // Body:
753 
754  schema_poset_member& result = *_item;
755 
756  // Postconditions:
757 
758  // Exit
759 
760  return result;
761 }
762 
763 
764 
765 
766 
767 bool
770 {
771  bool result;
772 
773  // Preconditions:
774 
775  // Body:
776 
777  // Always true in this class;
778  // intended to be redefined in descendants.
779 
780  result = true;
781 
782  // Postconditions:
783 
784  // Exit
785 
786  return result;
787 }
788 
789 const sheaf::scoped_index&
792 {
793  // Preconditions:
794 
795  require(is_initialized());
796 
797  // Body:
798 
799  define_old_variable(scoped_index result(_item->index()));
800 
801  // Postconditions:
802 
803  ensure(result == item().index());
804 
805  // Exit
806 
807  return _item->index();
808 }
809 
810 bool
813 {
814  bool result;
815 
816  // Preconditions:
817 
818  // Body:
819 
820  result = _is_table_dof;
821 
822  // Postconditions:
823 
824  // Exit
825 
826  return result;
827 }
828 
829 // PROTECTED MEMBER FUNCTIONS
830 
831 void
834 {
835  // Preconditions:
836 
837  // Body:
838 
839  if(_item == 0)
840  {
841  // Create the item handle
842 
843  _item = new schema_poset_member;
844  }
845 
846  // Attach the handle.
847 
848  update_item();
849 
850  // Postconditions:
851 
852  ensure(item().is_attached() == !is_done());
853 
854  // Exit
855 
856  return;
857 }
858 
859 void
862 {
863  // Preconditions:
864 
865  require(is_initialized());
866 
867  // Body:
868 
869  if(_itr.is_done())
870  {
871  _item->detach_from_state();
872  }
873  else
874  {
875  _item->attach_to_state(_itr.anchor().host(), _itr.index());
876  }
877 
878  // Postconditions:
879 
880  ensure(item().is_attached() == !is_done());
881 
882  // Exit
883 
884  return;
885 }
886 
virtual void force_is_done()
Force the iterator to be done.
poset_state_handle * host() const
The poset which this is a handle to a component of.
virtual poset_dof_iterator * clone() const
Make a new instance of the same type as this.
int unaliased_version(int xversion) const
The actual version associated with (possibly aliased) version xversion in this.
int version(bool xunalias=true) const
The (possibly aliased) version of this component. The version of the host used when evaluating proper...
virtual void update_item()
Attaches the item handle to the current index, or detaches the item handle if is_done.
virtual void next()
Makes this the next member of the subset.
virtual void reset(bool xreset_markers=RESET)
Restarts the iteration over the down set of anchor() If xreset_markers, set !has_visited for all memb...
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
schema_poset_member & item()
The current member of the iteration (mutable version).
virtual void reset_item()
Creates item if needed and attaches it to the current index.
STL namespace.
virtual bool is_done() const
True if iteration finished.
virtual void put_has_visited(pod_index_type xhub_id, bool xvalue)
Set the visisted marker for hub id xhub_id to xvalue. Intended for use reseting iterator without havi...
schema_poset_member * clone(bool xnew_state, bool xauto_access=true) const
Make a new handle instance of current. Attach the new instance to a new state if xnew_state is true...
Abstract base class with useful features for all objects.
Definition: any.h:39
poset_dof_iterator()
Default constructor; creates an unattached iterator, with and all-pass filter.
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.
virtual ~poset_dof_iterator()
Destructor.
virtual bool is_initialized() const
True if this has been initialized for iteration with respect to a specific anchor.
virtual bool invariant() const
The class invariant.
virtual const scoped_index & index()
The index of the current member of the iteration.
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
virtual bool has_visited(pod_index_type xhub_id) const
True if this has already visited hub id xhub_id.
bool is_table_dof() const
True if iterating over table dofs.
schema_poset_member & anchor()
The schema member whose downset is being iterated over; the top member of the domain of iteration (mu...
virtual bool is_schematized(bool xauto_access) const
True if this poset has been prepared for use as a schema, that is, if the top member has been schemat...
Iterates in postorder over dofs of a schema member anchor. Attaches a handle of type schema_poset_mem...
postorder_iterator _itr
The index iterator used to implement the iteration.
bool _is_table_dof
True if iterating over table dofs.
schema_poset_member * _anchor
The schema member whose downset is being iterated over; the top member of the domain of iteration...
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual poset_dof_iterator & operator=(const poset_dof_iterator &xother)
Assignment operator.
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.
virtual 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 bool item_is_ancestor_of(const schema_poset_member &xmbr) const
True if xmbr conforms to the type of item of this.
schema_poset_member * _item
The member handle for the current item in the iteration.
A client handle for a poset member which has been prepared for use as a schema.
virtual void put_anchor(const schema_poset_member *xanchor, int xversion)
Set anchor() to xversion of *xanchor. Forces reset of markers, since the host may be different...
bool has_version(int xversion) const
True if xversion is a valid version for this.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710