SheafSystem  0.0.0.0
section_component_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 section_component_iterator
19 
20 #include "SheafSystem/section_component_iterator.h"
21 
22 #include "SheafSystem/poset_state_handle.h"
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/section_space_schema_member.h"
25 #include "SheafSystem/section_space_schema_poset.h"
26 
27 using namespace std;
28 using namespace fiber_bundle; // Workaround for MS C++ bug.
29 
32 {
33 
34  // Preconditions:
35 
36 
37  // Body:
38 
39  _anchor = 0;
40  _item = 0;
41 
42  // Postconditions:
43 
44  ensure(invariant());
45  ensure(!is_initialized());
46 
47  // Exit
48 
49  return;
50 }
51 
52 
55 {
56 
57  // Preconditions:
58 
59  require(is_ancestor_of(&xother));
60 
61  // Body:
62 
63  section_component_iterator& lother = const_cast<section_component_iterator&>(xother);
64 
65  if(lother.is_initialized())
66  {
67  _item = lother._item->clone();
68  _anchor = lother._anchor->clone();
69  }
70  else
71  {
72  _item = 0;
73  _anchor = 0;
74  }
75  _itr = lother._itr;
76 
77  // Postconditions:
78 
79  ensure(invariant());
80  ensure(is_initialized() == xother.is_initialized());
81  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
82  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
83  ensure(unexecutable(this is first member of iteration or is_done()));
84 
85  // Exit
86 
87  return;
88 }
89 
90 
94 {
95 
96  // Preconditions:
97 
98  require(is_ancestor_of(&xother));
99 
100  // Body:
101 
102  section_component_iterator& lother = const_cast<section_component_iterator&>(xother);
103 
104  if(is_initialized())
105  {
106  _item->detach_from_state();
107  delete _item;
108  _item = 0;
109 
110  _anchor->detach_from_state();
111  delete _anchor;
112  _anchor = 0;
113  }
114 
115  if(lother._anchor != 0)
116  {
117  _item = lother._item->clone();
118 
119  _anchor = lother._anchor->clone();
120  _anchor->attach_to_state(lother._anchor);
121  }
122 
123  _itr = lother._itr;
124 
125  // Postconditions:
126 
127  ensure(invariant());
128  ensure(is_initialized() == xother.is_initialized());
129  ensure(is_initialized() ? anchor().is_same_state(&xother.anchor()) : true);
130  ensure(is_initialized() ? item().is_same_type(&xother.item()) : true);
131  ensure(unexecutable(this is first member of iteration or is_done()));
132 
133  // Exit
134 
135  return *this;
136 }
137 
138 
141 {
142 
143  // Preconditions:
144 
145  // Body:
146 
147  if(_anchor != 0)
148  {
149  _anchor->detach_from_state();
150  delete _anchor;
151  }
152 
153  if(_item != 0)
154  {
155  _item->detach_from_state();
156  delete _item;
157  }
158 
159  // Postconditions:
160 
161  // Exit:
162 
163  return;
164 }
165 
166 
167 
168 bool
170 is_ancestor_of(const any* xother) const
171 {
172  bool result;
173 
174  // Preconditions:
175 
176  // Body:
177 
178  result = dynamic_cast<const section_component_iterator*>(xother) != 0;
179 
180  // Postconditions:
181 
182  // Exit
183 
184  return result;
185 }
186 
187 
188 bool
190 invariant() const
191 {
192  bool result = true;
193 
194  // Preconditions:
195 
196  // Body:
197 
198  result = result && any::invariant();
199 
200  if(invariant_check())
201  {
202  disable_invariant_check();
203 
204  result = result && ( is_initialized() == (_anchor != 0) );
205  result = result && ( is_initialized() == (_item != 0) );
206  result = result && ( is_initialized() == (_itr.is_initialized()) );
207 
208  // Finished, turn invariant checking back on.
209 
210  enable_invariant_check();
211  }
212 
213  // Postconditions:
214 
215  // Exit
216 
217  return result;
218 }
219 
220 
221 
224 {
225 
226  // Preconditions:
227 
228  require(item_is_ancestor_of(xanchor));
229  require(xanchor.state_is_read_accessible());
230 
231 
232  // Body:
233 
234  // Make sure !is_initialized, then just reset.
235 
236  _anchor = 0;
237  _item = 0;
238  put_anchor(&xanchor);
239  reset();
240 
241  // Postconditions:
242 
243  ensure(invariant());
244  ensure(is_initialized());
245  ensure(anchor().is_same_state(&xanchor));
246  ensure(anchor().is_same_type(&xanchor));
247  ensure(unexecutable(postorder - have visited all children of this));
248  ensure(unexecutable(!is_done() implies this is first member));
249  ensure(item().is_attached() == !is_done());
250 
251  return;
252 }
253 
254 
255 
256 
257 // ITERATOR FACET
258 
259 
260 
261 bool
264 {
265  bool result;
266 
267  // Preconditions:
268 
269  // Body:
270 
271  result = _item != 0;
272 
273  // Postconditions:
274 
275  // Exit
276 
277  return result;
278 }
279 
280 
281 
285 {
286  // Preconditions:
287 
288  require(is_initialized());
289 
290  // Body:
291 
292  section_space_schema_member& result = *_anchor;
293 
294  // Postconditions:
295 
296  // Exit
297 
298  return result;
299 }
300 
303 anchor() const
304 {
305  // Preconditions:
306 
307  require(is_initialized());
308 
309  // Body:
310 
311  section_space_schema_member& result = *_anchor;
312 
313  // Postconditions:
314 
315  // Exit
316 
317  return result;
318 }
319 
320 
321 
322 bool
324 is_done() const
325 {
326  bool result;
327 
328  // Preconditions:
329 
330  require(is_initialized());
331 
332  // Body:
333 
334  result = _itr.is_done();
335 
336  // Postconditions:
337 
338  // Exit
339 
340  return result;
341 }
342 
343 
344 void
347 {
348 
349  // Preconditions:
350 
351  require(is_initialized());
352  require(!is_done());
353 
354  // Body:
355 
356  _itr.next();
357  update_item();
358 
359  // Postconditions:
360 
361  ensure(invariant());
362  ensure(!is_done() == item().is_attached());
363  ensure(unexecutable(postorder - have visited all of the members of the strict down set
364  of item()));
365 
366  // Exit
367 
368  return;
369 }
370 
371 
372 void
374 reset(bool xreset_markers)
375 {
376  // Preconditions:
377 
378  require(is_initialized());
379  require(anchor().state_is_read_accessible());
380 
381  // Body:
382 
383  _itr.reset(xreset_markers);
384  update_item();
385 
386  // Postconditions:
387 
388  ensure(invariant());
389  ensure(is_initialized());
390  ensure(item().is_attached() == !is_done());
391 
392  // Exit
393 
394  return;
395 }
396 
397 void
400 {
401 
402  // Preconditions:
403 
404  require(item_is_ancestor_of(*xanchor));
405  require(xanchor->state_is_read_accessible());
406  require(xanchor->host()->is_schematized(false));
407 
408  // Body:
409 
410  // Reset the anchor.
411 
412  reset_anchor(*xanchor);
413 
414  // Set the anchor and filter of the iterator.
415 
416  string ldof_sp_name = schema_poset_member::dof_subposet_name("top", false);
417  _itr.put_anchor(&(xanchor->fiber_schema()));
418  _itr.put_filter(ldof_sp_name);
419  reset_item();
420 
421  // Postconditions:
422 
423  ensure(invariant());
424  ensure(is_initialized());
425  ensure(anchor().is_same_state(xanchor));
426  ensure(anchor().is_same_type(xanchor));
427  ensure(item().is_attached() == !is_done());
428 
429  return;
430 }
431 
432 int
434 ct(bool xreset)
435 {
436  int result = 0;
437 
438  // Preconditions:
439 
440  require(is_initialized());
441  require(xreset ? anchor().state_is_read_accessible(): true);
442 
443  // Body:
444 
445  result = _itr.ct(xreset);
446  update_item();
447 
448  // Postconditions:
449 
450  ensure(result >= 0);
451  ensure(is_done());
452 
453  // Exit
454 
455  return result;
456 }
457 
458 bool
461 {
462  bool result;
463 
464  // Preconditions:
465 
466  require(is_initialized());
467  require(anchor().state_is_read_accessible());
468  require(anchor().host()->contains_member(xhub_id));
469 
470  // Body:
471 
472  pod_index_type lbase_space_id = anchor().host()->get_base_space_id_from_index(xhub_id);
473  pod_index_type lfiber_schema_id = anchor().host()->get_fiber_schema_id_from_index(xhub_id);
474 
475  result = (anchor().base_space_id() == lbase_space_id) && _itr.has_visited(lfiber_schema_id);
476 
477  // Postconditions:
478 
479  // Exit:
480 
481  return result;
482 }
483 
484 bool
486 has_visited(const scoped_index& xid) const
487 {
488  bool result;
489 
490  // Preconditions:
491 
492  require(is_initialized());
493  require(anchor().state_is_read_accessible());
494  require(anchor().host()->contains_member(xid));
495 
496  // Body:
497 
498  return has_visited(xid.hub_pod());
499 }
500 
501 
502 bool
505 {
506  bool result;
507 
508  // Preconditions:
509 
510  require(is_initialized());
511  require(anchor().state_is_read_accessible());
512  require(item_is_ancestor_of(xmbr));
513  require(xmbr.is_attached());
514  require(anchor().host()->is_same_state(xmbr.host()));
515 
516  // Body:
517 
518  result = (anchor().base_space_id() ==~ xmbr.base_space_id()) &&
519  _itr.has_visited(&(xmbr.fiber_schema()));
520 
521  // Postconditions:
522 
523  // Exit:
524 
525  return result;
526 }
527 
528 
529 void
531 put_has_visited(pod_index_type xhub_id, bool xvalue)
532 {
533 
534  // Preconditions:
535 
536  require(is_initialized());
537  require(anchor().state_is_read_accessible());
538  require(anchor().host()->contains_member(xhub_id));
539  require(anchor().base_space_id() == anchor().host()->get_base_space_id_from_index(xhub_id));
540 
541  // Body:
542 
543  _itr.put_has_visited(_anchor->host()->get_fiber_schema_id_from_index(xhub_id), xvalue);
544 
545  // Postconditions:
546 
547  ensure(has_visited(xhub_id) == xvalue);
548 
549  // Exit:
550 
551  return;
552 }
553 
554 void
556 put_has_visited(const scoped_index& xid, bool xvalue)
557 {
558 
559  // Preconditions:
560 
561  require(is_initialized());
562  require(anchor().state_is_read_accessible());
563  require(anchor().host()->contains_member(xid));
564  require(anchor().base_space_id() == anchor().host()->get_base_space_id_from_index(xid.hub_pod()));
565 
566  // Body:
567 
568  put_has_visited(xid.hub_pod(), xvalue);
569 
570  // Postconditions:
571 
572  ensure(has_visited(xid) == xvalue);
573 
574  // Exit:
575 
576  return;
577 }
578 
579 // MEMBER ITERATOR FACET
580 
584 {
585  // Preconditions:
586 
587  require(is_initialized());
588 
589  // Body:
590 
591  section_space_schema_member& result = *_item;
592 
593  // Postconditions:
594 
595  // Exit
596 
597  return result;
598 }
599 
602 item() const
603 {
604  // Preconditions:
605 
606  require(is_initialized());
607 
608  // Body:
609 
610  section_space_schema_member& result = *_item;
611 
612  // Postconditions:
613 
614  // Exit
615 
616  return result;
617 }
618 
619 
620 
621 bool
624 {
625  bool result;
626 
627  // Preconditions:
628 
629  // Body:
630 
631  // Always true in this class;
632  // intended to be redefined in descendants.
633 
634  result = true;
635 
636  // Postconditions:
637 
638  // Exit
639 
640  return result;
641 }
642 
643 const sheaf::scoped_index&
645 index() const
646 {
647  // Preconditions:
648 
649  require(is_initialized());
650 
651  // Body:
652 
653  const scoped_index& result = _item->index();
654 
655  // Postconditions:
656 
657  ensure(result == item().index());
658 
659  // Exit
660 
661  return result;
662 }
663 
664 // PROTECTED MEMBER FUNCTIONS
665 
666 void
669 {
670  // Preconditions:
671 
672  require(item_is_ancestor_of(xanchor));
673  require(xanchor.state_is_read_accessible());
674 
675  // Body:
676 
677  if(_anchor == 0)
678  {
679  // Create the anchor handle
680 
681  _anchor = xanchor.clone();
682  }
683 
684  // Attach the handle.
685 
686  _anchor->attach_to_state(&xanchor);
687 
688  // Postconditions:
689 
690  // Can't use anchor() in postconditions because
691  // can't satisfy is_initialized() precondition yet.
692 
693  ensure(_anchor != 0);
694  ensure(_anchor->is_attached());
695  ensure(_anchor->is_same_state(&xanchor));
696  ensure(_anchor->is_same_type(&xanchor));
697 
698  // Exit
699 
700  return;
701 }
702 
703 void
706 {
707  // Preconditions:
708 
709  require(is_initialized());
710 
711  // Body:
712 
713  if(_itr.is_done())
714  {
715  _item->detach_from_state();
716  }
717  else
718  {
722 
723  _item->attach_to_state(_anchor->host(), _anchor->base_space_id(), _itr.index().pod());
724  }
725 
726  // Postconditions:
727 
728  ensure(item().is_attached() == !is_done());
729 
730  // Exit
731 
732  return;
733 }
734 
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 void update_item()
Attaches the item handle to the current index, or detaches the item handle if is_done.
bool is_initialized() const
True if this has been initialized for iteration with respect to a specific anchor.
virtual void next()
Makes this the next member of the subset.
section_space_schema_poset * host() const
The poset which this is a handle to a component of.
schema_poset_member & fiber_schema()
The fiber schema component of this (mutable version).
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
section_space_schema_member * _item
The member handle for the current item in the iteration.
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...
postorder_iterator _itr
The index iterator used to implement the iteration.
virtual section_component_iterator & operator=(const section_component_iterator &xother)
Assignment operator.
void put_anchor(const section_space_schema_member *xanchor)
Sets anchor to member xanchor.
STL namespace.
section_component_iterator()
Default constructor; creates an unattached iterator, with and all-pass filter.
section_space_schema_member & item()
The the current member of the iteration (mutable version).
virtual bool has_visited(pod_index_type xhub_id) const
True if this has already visited hub id xhub_id.
section_space_schema_member & anchor()
The schema member whose downset is being iterated over; the top member of the domain of iteration (mu...
virtual 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...
Abstract base class with useful features for all objects.
Definition: any.h:39
virtual void reset_anchor(const section_space_schema_member &xanchor)
Creates anchor if needed and attaches it to xanchor. Abstract in this class; intended to be redefined...
virtual bool item_is_ancestor_of(const section_space_schema_member &xmbr) const
True if xmbr conforms to the type of item of 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 non-void state.
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
virtual const scoped_index & index() const
The index of the current member of the iteration.
Iterates in postorder over components of a section_space_schema_member anchor. The components of a se...
bool is_done() const
True if iteration finished.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
A client handle for a poset member which has been prepared for use as a schema for a section space...
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...
Namespace for the fiber_bundles component of the sheaf system.
virtual void attach_to_state(pod_index_type xbase_space_id, pod_index_type xfiber_schema_id)=0
Attach to the state in host() with component ids xbase_id and xfiber_schema_id.
section_space_schema_member * _anchor
The schema member whose downset is being iterated over; the top member of the domain of iteration...
section_space_schema_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...
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710