SheafSystem  0.0.0.0
poset_traverser.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_TRAVERSER
19 
20 #ifndef POSET_TRAVERSER_H
21 #include "SheafSystem/poset_traverser.h"
22 #endif
23 
24 #include "SheafSystem/assert_contract.h"
25 
26 #ifndef POSET_H
27 #include "SheafSystem/poset.h"
28 #endif
29 
30 #ifndef POSET_MEMBER_H
31 #include "SheafSystem/total_poset_member.h"
32 #endif
33 
34 #ifndef POSET_MEMBER_ITERATOR_H
35 #include "SheafSystem/poset_member_iterator.h"
36 #endif
37 
38 #ifndef BIT_VECTOR_H
39 #include "SheafSystem/zn_to_bool.h"
40 #endif
41 
43 bool
45 invariant() const
46 {
47  bool result = true;
48 
49  result = result && (_host != 0);
50  result = result && (_host->state_is_read_accessible());
51 
52  result = result && (_visited != 0);
53  result = result && ( (_anchor != 0) ? _visited->ub() >= member_index_ub().pod() : true);
54 
55  result = result && ( (_anchor != 0) ? _anchor->is_attached() : true);
56  result = result && ( (_anchor != 0) ? _anchor->host() == _host : true);
57 
58  return result;
59 }
60 
61 
62 
63 
65 bool
67 has_same_host(const poset_component* other) const
68 {
69  bool result;
70 
71  // Preconditions:
72 
73  // Body:
74 
75  result = (other != 0) && (other->host() == host());
76 
77  // Postconditions:
78 
79  ensure(result == ( (other != 0) && (other->host() == host()) ));
80 
81  // Exit
82 
83  return result;
84 }
85 
90 {
91  abstract_poset_member* result;
92 
93  // Preconditions:
94 
95  // Body:
96 
97  result = _anchor;
98 
99  // Postconditions:
100 
101  // Exit:
102 
103  return result;
104 }
105 
106 
107 
111 anchor() const
112 {
113  abstract_poset_member* result;
114 
115  // Preconditions:
116 
117  // Body:
118 
119  result = _anchor;
120 
121  // Postconditions:
122 
123  // Exit:
124 
125  return result;
126 }
127 
131 {
132  // Preconditions:
133 
134  require(xhost != 0);
135  require(xhost->state_is_read_accessible());
136 
137  // body:
138 
139  _host = const_cast<poset_state_handle*>(xhost);
140  _visited = new zn_to_bool(member_index_ub().pod());
141  _anchor = 0;
142  _in_down_set = true;
143 
144 
145  // postconditions:
146 
147  ensure(invariant());
148 }
149 
150 
154 {
155 
156  // body:
157 
158  delete _visited;
159 }
160 
161 
163 void
165 traverse(const abstract_poset_member* xanchor, bool down, bool reset_markers)
166 {
167  // Preconditions:
168 
169  require(has_same_host(xanchor));
170  require(xanchor->state_is_read_accessible());
171 
172  // Body:
173 
174  _anchor = const_cast<abstract_poset_member*>(xanchor);
175  _in_down_set = down;
176 
177  // Make sure there are enough visited markers
178 
180 
181  // make sure the visited markers are all off
182 
183  if(reset_markers)
185 
186  // execute traversal defined by descendant
187 
189 
190  // forget pointer to anchor
191 
192  _anchor = 0;
193 
194  // Postconditions:
195 
196  ensure(invariant());
197  ensure(postcondition_of(private_traverse()));
198  ensure(anchor() == 0);
199 
200  // Exit:
201 
202  return;
203 }
204 
205 
206 
208 bool
211 {
212 
213  // Preconditions:
214 
215  require(xmbr != 0);
216  require(xmbr->is_attached());
217  require(xmbr->host() == host());
218 
219  // body:
220 
221  bool result = (*_visited)[xmbr->index().pod()];
222 
223  return result;
224 }
225 
226 
227 
232 {
233  // Preconditions:
234 
235  // Body:
236 
237  scoped_index result = _host->member_index_ub();
238 
239  // Postconditions:
240 
241  // Exit
242 
243  return result;
244 }
245 
246 
247 
248 bool
251 {
252  bool result;
253 
254  // Preconditions:
255 
256  // Body:
257 
258  result = _visited->is_false();
259 
260  // Postconditions:
261 
262  ensure(result == unexecutable(no member is marked visited));
263 
264  // Exit
265 
266  return result;
267 }
268 
269 
270 
271 bool
274 {
275  bool result = true;
276 
277  // Preconditions:
278 
279  // Body:
280 
282 
283  while(result && !itr.is_done())
284  {
285  result = result && (*_visited)[itr.index().pod()];
286 
287  itr.next();
288  }
289 
290 
291  // Postconditions:
292 
293  ensure(unexecutable(result if and only if every member is marked visited))
294  ;
295 
296  // Exit
297 
298  return result;
299 }
300 
301 
302 
303 void
306 {
307 
308  // Preconditions:
309 
310  // None.
311 
312  // Body:
313 
315 
316  // Postconditions:
317 
318  ensure(no_members_visited());
319 
320  // Exit:
321 
322  return;
323 }
324 
325 
326 
327 void
330 {
331 
332  // preconditions:
333 
334  require(xmbr != 0);
335  require(xmbr->is_attached());
336  require(xmbr->host() == _anchor->host());
337 
338  // body:
339 
340  _visited->force(xmbr->index().pod(), true);
341 
342  // postconditions:
343 
344  ensure(has_been_visited(xmbr));
345 }
bool no_members_visited() const
True if no members have been visited.
poset_state_handle * host() const
The poset which this is a handle to a component of.
virtual bool invariant() const
Class invariant.
const pod_type & pod() const
The "plain old data" storage of this; the value in the external id space.
Definition: scoped_index.h:672
bool has_been_visited(const abstract_poset_member *xmbr) const
True if xmbr has been previously visited.
void make_false_sa()
Constant function false, self-allocated. Note on terminology: "false" is a keyword, so this function can not be just "false"
Definition: zn_to_bool.cc:955
zn_to_bool * _visited
The markers for previously visited members.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
bool _in_down_set
True if traversing down from anchor().
void traverse(const abstract_poset_member *xanchor, bool down, bool reset_markers=true)
Traverse the down set (down == true) or up set (down == false) of xanchor. If reset_markers, reset all visited markers to false before begining.
A client handle for a general, abstract partially order set.
const scoped_index & index() const
The current item in the subset.
virtual abstract_poset_member * anchor()
The member which is the starting point of this traversal (mutable verison).
virtual void private_traverse()=0
Implements the traversal.
int ub() const
The upper bound for the domain index.
Definition: zn_to_bool.cc:214
void mark_members_not_visited()
Sets the visited markers false for all members.
Features shared by poset_member and subposet. Subposet and poset_member objects can be attached...
const scoped_index & index() const
The index of the component state this handle is attached to.
~poset_traverser()
Destructor.
void ensure_visited_ub()
Ensures the visited bit vector is large enough.
void next()
Makes item the next member of the subset.
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
Definition: zn_to_bool.h:52
bool has_same_host(const poset_component *other) const
True if other is not void and is attached to the same host as 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.
poset_traverser(const poset_state_handle *xanchor)
Constructor.
abstract_poset_member * _anchor
The member which is the starting point of this traversal.
bool all_members_visited() const
True if all members have been visited.
Iterates over the subset of Zn defined by the characteristic function host().
void mark_visited(const abstract_poset_member *xmbr)
Sets the visited marker true for xmbr.
poset_state_handle * host()
The poset being traversed (mutable version).
bool is_false() const
True if function is false evrywhere on domain.
Definition: zn_to_bool.cc:735
virtual scoped_index member_index_ub() const
The upper bound on the member_index;.
bool is_done() const
True if iteration finished.
poset_state_handle * _host
The poset being traversed.
scoped_index member_index_ub() const
The upper bound on the member index.
An abstract client handle for a member of a poset.
index_iterator member_iterator() const
void force(int i, bool value)
Sets the i-th member to value, extends the upper bound if necessary.
Definition: zn_to_bool.cc:607