SheafSystem  0.0.0.0
poset_orderer.cc
Go to the documentation of this file.
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 
20 
21 #include "SheafSystem/poset_orderer.h"
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/poset_state_handle.h"
24 #include "SheafSystem/poset_member.h"
25 #include "SheafSystem/index_space_iterator.h"
26 #include "SheafSystem/subposet.h"
27 
28 // ===========================================================
29 // POSET_ORDERER FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36  : poset_dft(xhost)
37 {
38  // Preconditions:
39 
40  require(precondition_of(poset_dft::poset_dft));
41 
42  // Body:
43 
44  // create the post-visited marker
45 
46  _post_visited = new zn_to_bool(member_index_ub().pod());
47 
48  // Postconditions:
49 
50  ensure(invariant());
51 }
52 
55 {
56  // Preconditions:
57 
58  // Body:
59 
60  delete _post_visited;
61 
62  // Postconditions:
63 }
64 
65 void
67 restore_order(subposet* xlower_bound)
68 {
69  // Preconditions:
70 
71  require(xlower_bound != 0 ? has_same_host(xlower_bound) : true);
72  require(unexecutable(xlower_bound != 0 ? jrm order is independent of anything below xlower_bound : true));
73  require(unexecutable(only bottom has an empty lower cover));
74  // ensured in ensure_lattice_invariant before calling this routine
75 
76  // Body:
77 
78  _lower_bound = xlower_bound;
79 
80  // restore the order
81 
82  traverse(&(host()->top()), true);
83 
84  // Postconditions:
85 
86  ensure(unexecutable(jrm order consistent with jim order));
87 
88  // Exit
89 
90  return;
91 }
92 
93 // PROTECTED MEMBER FUNCTIONS
94 
95 // PRIVATE MEMBER FUNCTIONS
96 
97 
98 // ===========================================================
99 // POSET_DFT FACET
100 // ===========================================================
101 
102 // PUBLIC MEMBER FUNCTIONS
103 
104 // PROTECTED MEMBER FUNCTIONS
105 
106 void
109 {
110  // Preconditions:
111 
112  // Body:
113 
114  if((_lower_bound != 0) && _lower_bound->contains_member(xmbr))
115  {
116  // no need to go deeper
117 
118  _descend = false;
119  }
120 
121  // Postconditions:
122 
123  // Exit
124 
125  return;
126 }
127 
128 void
131 {
132  // Preconditions:
133 
134  require(has_same_host(xmbr));
135  require(has_been_visited(xmbr));
136 
137  // Body:
138 
139  // Note 1: order relation for jims is defined by links,
140  // so we only need to restore order for the jrms
141  //
142  // Note 2: because it ignores members with empty lower covers,
143  // the restore_order routine requires that there are no
144  // members with empty lower covers except bottom.
145 
146  if(!xmbr->is_jim())
147  {
148  // xmbr is a jrm
149 
150  // If any of the members in the upper cover of xmbr's lower cover
151  // have exactly the same lower cover, then they are join equivalent
152  // and need to be merged into the join equivalence class of xmbr
153 
154  if(!xmbr->cover_is_empty(LOWER))
155  {
156  // Xmbr's lower cover is not empty.
157  // Check all the members of the upper cover of the first member of xmbr's lower cover.
158  // Note that every member that has the same lower cover as xmbr has to appear
159  // in the upper cover of the first (and every) member of xmbr's lower cover.
160 
161  pod_index_type lfirst_lower = xmbr->first_cover_member(LOWER);
162  index_space_iterator& uc_itr = host()->get_cover_id_space_iterator(UPPER, lfirst_lower);
163 
164  while(!uc_itr.is_done())
165  {
166  // The upper cover of the first member is not empty.
167  // Check for lower cover equivalance with xmbr,
168  // but only if the traversal has completely finished with
169  // the other member. This condition prevents the merge below
170  // from messing up the traversal and keeps the order within the
171  // equivalence class compatible with the original order.
172 
173  pod_index_type other_mbr_index = uc_itr.hub_pod();
174 
175  if( has_been_post_visited(other_mbr_index) &&
176  xmbr->cover_is_equal(LOWER, other_mbr_index) )
177  {
178  // Current member of upper cover has same lower cover as xmbr.
179 
180  if(host()->is_jim(other_mbr_index))
181  {
182  // Other member is a jim.
183  // A jrm is never join-equivalent to a jim;
184  // if they have the same lower cover, then the jrm < the jim.
185  // The jim should cover the top of xmbr's eqv class.
186  // Note that the jim has already been post-visited,
187  // so the new link can not affect the traversal.
188 
189  pod_index_type xmbr_gjem = _host->greatest_jem(xmbr->index().pod());
190 
191  _host->new_link(other_mbr_index, xmbr_gjem);
192  }
193  else
194  {
195  // If two jrms have the same lower cover,
196  // they are join-equivalent.
197  // Merge their equivalence classes.
198 
199  host()->merge_jems(xmbr->index().pod(), other_mbr_index);
200 
201  // Merge stacks xmbr eqv class on top of other eqv class
202  // so other now has the correct lower cover.
203 
204  host()->copy_cover(LOWER, other_mbr_index, xmbr->index().pod());
205  }
206  }
207 
208  // Move to next member of upper cover.
209 
210  uc_itr.next();
211  }
212 
214  }
215  }
216 
217  // Postconditions:
218 
219  ensure(unexecutable(g has the proper lower cover links w.r.t.xmbr));
220 }
221 
222 // PRIVATE MEMBER FUNCTIONS
223 
224 
225 // ===========================================================
226 // POSET_TRAVERSER FACET
227 // ===========================================================
228 
229 // PUBLIC MEMBER FUNCTIONS
230 
231 // PROTECTED MEMBER FUNCTIONS
232 
233 // PRIVATE MEMBER FUNCTIONS
234 
235 
236 // ===========================================================
237 // ANY FACET
238 // ===========================================================
239 
240 // PUBLIC MEMBER FUNCTIONS
241 
242 bool
244 invariant() const
245 {
246  bool result = true;
247 
248  result = result && poset_traverser::invariant();
249  result = result && (_post_visited != 0);
250 
251 
252  return result;
253 }
254 
255 // PROTECTED MEMBER FUNCTIONS
256 
257 // PRIVATE MEMBER FUNCTIONS
258 
259 
SHEAF_DLL_SPEC void lower(const met &xmetric, const ed &xvector, ed &xresult, bool xauto_access)
Lower vector (pre-allocated version for persistent type).
Definition: met.cc:943
virtual bool invariant() const
Class invariant.
virtual bool is_jim(bool xin_current_version=true) const
True if this member is join irreducible in the current version of the host (xin_current_version == tr...
A client handle for a subposet.
Definition: subposet.h:86
virtual bool invariant() const
Class invariant.
virtual void new_link(pod_index_type xgreater, pod_index_type xlesser)
Insert a cover link from greater to lesser (that is, hub id xgreater covers hub id xlesser)...
poset_dft(const poset_state_handle *xhost)
Constructor.
Definition: poset_dft.cc:34
const pod_type & pod() const
The "plain old data" storage of this; the value in the external id space.
Definition: scoped_index.h:672
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.
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.
virtual bool contains_member(pod_index_type xmbr_hub_id) const
True if this poset contains poset member with hub id xmbr_hub_id.
Definition: subposet.cc:955
void previsit_action(abstract_poset_member *xmbr)
Previsit action for member, xmbr.
void postvisit_action(abstract_poset_member *xmbr)
Postvisit action for member, xmbr.
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.
bool cover_is_equal(bool xlower, pod_index_type xother_mbr_index) const
True if and only if the lower (xlower true) or upper (xlower false) cover of this contains the same m...
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
virtual void merge_jems(pod_index_type xjem1, pod_index_type xjem2)
Merge the join-equivalence class of hub id xjem2 under that of hub id xjem1.
bool is_done() const
True if iteration is finished.
bool has_same_host(const poset_component *other) const
True if other is not void and is attached to the same host as this.
bool _descend
If true on return from previsit_action, descend into lower cover previsit_action can truncate dft by ...
Definition: poset_dft.h:70
poset_orderer(poset_state_handle *xhost)
Constructor.
const bool UPPER
Selector for upper cover.
Definition: sheaf.h:72
pod_index_type first_cover_member(bool xlower) const
The first member of the lower (xlower true) or upper (xlower false) cover of this.
void restore_order(subposet *xlower_bound=0)
Make jrm order consistent with jim order.
~poset_orderer()
Destructor.
bool cover_is_empty(bool xlower) const
True if and only if the lower (xlower true) or upper (xlower false) cover this is empty...
const bool LOWER
Selector for lower cover.
Definition: sheaf.h:67
virtual pod_index_type greatest_jem(pod_index_type xmbr_hub_id) const
The hub id of the largest member which is join-equivalent to hub id xmbr_hub_id.
poset_state_handle * host()
The poset being traversed (mutable version).
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual bool is_jim(pod_index_type xmbr_hub_id, bool xin_current_version=true) const
True if the member with hub id xmbr_hub_id is a jim in the current version (xin_current_version == tr...
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.
Abstract traverser (internal iterator) for poset which traverses the cover relation graph in depth fi...
Definition: poset_dft.h:44
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().
void copy_cover(bool xlower, pod_index_type xmbr_hub_id, pod_index_type xother_mbr_hub_id)
Copies the lower (xlower true) or upper (xlower false) cover set of the member with hub id xmbr_hub_i...