SheafSystem  0.0.0.0
poset_dft.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_DFT
19 
20 
21 #ifndef POSET_DFT_H
22 #include "SheafSystem/poset_dft.h"
23 #endif
24 
25 #include "SheafSystem/abstract_poset_member.h"
26 #include "SheafSystem/assert_contract.h"
27 #include "SheafSystem/poset.h"
28 #include "SheafSystem/total_poset_member.h"
29 #include "SheafSystem/index_space_iterator.h"
30 #include "SheafSystem/zn_to_bool.h"
31 
35  : poset_traverser(xhost)
36 {
37 
38  // Preconditions
39 
40  require(precondition_of(poset_traverser));
41 
42  // body:
43 
44  _descend = true;
45 
46  // postconditions:
47 
48  ensure(invariant());
49 }
50 
51 
55 {
56 
57  // Delete the poset members on the free members stack
58 
59  while(!_free_mbrs.empty())
60  {
61  delete (_free_mbrs.top());
62  _free_mbrs.pop();
63  }
64 
65 }
66 
67 
68 void
71 {
72 
73  // Preconditions: (from poset_traverser)
74 
75  require(_anchor != 0);
76 
77  // Body:
78 
80 
81  // Postconditions:
82 
83  ensure(unexecutable(traversal truncated or all members <= anchor() visited));
84 
85  // Exit:
86 
87  return;
88 }
89 
90 
91 void
94 {
95 
96  // Preconditions:
97 
98  require(xmbr != 0);
99 
100  // Body:
101 
102  // This is the first time the traversal has visited this member,
103  // mark it and execute the previsit action
104 
105  mark_visited(xmbr);
106  previsit_action(xmbr);
107  ensure_visited_ub(); // action may have changed size of poset
108 
109  // check to see if previsit_action decided to continue the descent
110 
111  if(_descend)
112  {
113  // continue dft, descend into lower cover
114 
123 
124  // Assuming that there are no disconnected members,
125  // there are then four cases when the cover of a member can be empty
126  // 1) the upper cover of the top
127  // 2) the upper cover of the greatest_bottom_jem
128  // 3) the lower cover of the bottom
129  // 4) the lower cover of a minimal jim
130  //
131  // Cases 1 and 3 are handled properly by the lack of explicit links.
132  // Cases 4 can be dealt with by substituting a single global object
133  // _implied_lower_cover of type list_cover_set for xmbr.cover
134  // The _implied_lower_cover has one member, the greatest_bottom_jem.
135  // Case 2 must be handled specially. Instead of a cover set iterator,
136  // we can use the member iterator for host()->jims() and only follow
137  // jims with empty lower covers.
138 
139 
140  // get an member for the other end of a link
141 
142  abstract_poset_member* linked_mbr;
143  if(_free_mbrs.empty())
144  {
145  // No free iterators, have to make one.
146 
147  linked_mbr = _anchor->clone();
148  }
149  else
150  {
151  // Just get one from the free stack.
152 
153  linked_mbr = _free_mbrs.top();
154  _free_mbrs.pop();
155  }
156 
157  // Have to set version to CCR and must be attached
158  // to some member to do this, bottom will do.
159 
160  linked_mbr->attach_to_state(&(host()->bottom()));
161  linked_mbr->put_version(COARSEST_COMMON_REFINEMENT_VERSION);
162 
163  // Initialize itr for lower (upper) cover of xmbr.
164 
165  index_space_iterator& link_itr =
167 
168  // Iterate over lower (upper) cover of xmbr
169 
170  while(!link_itr.is_done())
171  {
172  // Attach to the member at the other end of the link.
173  // Must use the host, index signature because linked_mbr
174  // may have been deleted or otherwise unattached in
175  // postvisit or link actions.
176 
177  linked_mbr->attach_to_state(host(), link_itr.hub_pod());
178 
179  // Increment the iterator directly after attaching to state.
180  // This moves the iterator past the current linked member
181  // so it can be deleted in the post_visit action.
182 
183  link_itr.next();
184 
185  if (! has_been_visited(linked_mbr))
186  {
187  // First time we've seen this member, recur.
188 
189  recursive_dft(linked_mbr);
190  }
191 
192  link_action(xmbr, linked_mbr);
193  ensure_visited_ub(); // Action may have changed size of poset.
194  }
195 
197 
198  // Finished with the poset member;
199  // detach it and push it onto the stack,
200  // so we won't have to create one next time.
201 
202  linked_mbr->detach_from_state();
203  _free_mbrs.push(linked_mbr);
204 
205  } // end if _descend
206  else
207  {
208  // Reset _descend to default (true).
209 
210  _descend = true;
211  }
212 
213  // Finished with the cover of xmbr;
214  // execute the postvisit_action.
215 
216  postvisit_action(xmbr);
217  ensure_visited_ub(); // Action may have changed size of poset.
218 
219  // Postconditions:
220 
221  ensure(unexecutable(traversal truncated or all members <= xmbr visited));
222 
223  // Exit:
224 
225  return;
226 }
227 
228 
229 
virtual void put_version(int xversion, bool xunalias=false)
Sets version to (possibly aliased) xversion. If unalias == true, set version to the actual version al...
virtual bool invariant() const
Class invariant.
poset_dft(const poset_state_handle *xhost)
Constructor.
Definition: poset_dft.cc:34
An abstract iterator over the ids of an id space.
index_space_iterator & get_cover_id_space_iterator(bool xlower, pod_index_type xmbr_hub_id) const
Allocates an iterator for the lower (xlower true) or upper (xlower false) cover id space of the membe...
bool has_been_visited(const abstract_poset_member *xmbr) const
True if xmbr has been previously visited.
bool _in_down_set
True if traversing down from anchor().
A client handle for a general, abstract partially order set.
Abstract traverser (internal iterator) for poset.
virtual abstract_poset_member * anchor()
The member which is the starting point of this traversal (mutable verison).
const scoped_index & index() const
The index of the component state this handle is attached to.
virtual void next()=0
Makes id() the next id in the iteration.
void ensure_visited_ub()
Ensures the visited bit vector is large enough.
bool is_done() const
True if iteration is finished.
bool _descend
If true on return from previsit_action, descend into lower cover previsit_action can truncate dft by ...
Definition: poset_dft.h:70
abstract_poset_member * _anchor
The member which is the starting point of this traversal.
std::stack< abstract_poset_member * > _free_mbrs
Storage for abstract_poset_member objects, so we can reuse them, rather than creating one for each me...
Definition: poset_dft.h:76
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...
virtual void detach_from_state()
Detach this handle from its state, if any.
void private_traverse()
Implements the traversal.
Definition: poset_dft.cc:70
void mark_visited(const abstract_poset_member *xmbr)
Sets the visited marker true for xmbr.
void recursive_dft(abstract_poset_member *xmbr)
Definition: poset_dft.cc:93
poset_state_handle * host()
The poset being traversed (mutable version).
~poset_dft()
Destructor.
Definition: poset_dft.cc:54
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.
void release_cover_id_space_iterator(index_space_iterator &xcover_itr) const
Returns xcover_itr to the pool of id space iterators.
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().