SheafSystem  0.0.0.0
index_equivalence_class.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/index_equivalence_class.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/hub_index_space_handle.h"
25 #include "SheafSystem/index_space_family_iterator.h"
26 #include "SheafSystem/index_space_family.h"
27 #include "SheafSystem/index_space_iterator.h"
28 
29 #include "SheafSystem/block.impl.h"
30 
31 // ===========================================================
32 // INDEX_EQUIVALENCE_CLASS FACET
33 // ===========================================================
34 
35 // PUBLIC MEMBER FUNCTIONS
36 
39  bool xonly_persistent,
40  bool xsort_ids)
41 {
42  // Preconditions:
43 
44  // Body:
45 
46  const hub_index_space_handle& lhub_id_space = xid_spaces.hub_id_space();
47 
48  size_type lct = is_valid(lhub_id_space.end()) ? lhub_id_space.end() : 0;
51 
52  index_space_family_iterator lid_space_itr(xid_spaces, xonly_persistent);
53 
54  // The first id space should be the hub id space. This assumption will
55  // ensure the the first id in the equivalence lists is the hub id.
56 
57  assertion(lid_space_itr.only_persistent() || !lid_space_itr.is_done());
58  assertion(lid_space_itr.only_persistent() || lid_space_itr.index() == xid_spaces.hub_id_space().index());
59 
60  member_type litem;
61 
62  while(!lid_space_itr.is_done())
63  {
64  index_space_iterator& lid_itr = lid_space_itr.get_id_space_iterator();
65 
66  // Set the id space index.
67 
68  litem.first = lid_itr.index();
69 
70  while(!lid_itr.is_done())
71  {
72  // Add the current id to the end of the equivalence list for
73  // the representive id.
74 
75  pod_type lhub_id = lid_itr.hub_pod();
76  if(lhub_id_space.contains(lhub_id))
77  {
78  // Only insert ids that are in the hub id space for storage
79  // efficiency and to avoid an array out-of-bounds problem when
80  // the last term of the hub id space is a reserved term that
81  // is not full.
82 
83  litem.second = lid_itr.pod();
84  _equivalence_lists[lhub_id].push_back(litem);
85  }
86 
87  // Increment to the next id.
88 
89  lid_itr.next();
90  }
91 
92  lid_space_itr.release_id_space_iterator(lid_itr);
93  lid_space_itr.next();
94  }
95 
96  // The equivalence lists are built, sort them if requested by the user.
97 
98  if(xsort_ids)
99  {
100  for(pod_type i = 0; i < lct; i++)
101  {
103  }
104  }
105 
106  // Postconditions:
107 
108  // Exit:
109 
110  return;
111 }
112 
115 {
116  // Preconditions:
117 
118  // Body:
119 
120  // nothing to do.
121 
122  // Postconditions:
123 
124  // Exit:
125 
126  return;
127 }
128 
132 {
133  // Preconditions:
134 
135  // Body:
136 
137  size_type lct = xother._equivalence_lists.ct();
138 
141 
142  for(pod_type i = 0; i < lct; i++)
143  {
144  list_type& lother_list = xother._equivalence_lists[i];
145 
146  list_type::const_iterator litr;
147  for(litr = lother_list.begin(); litr != lother_list.end(); ++litr)
148  {
149  _equivalence_lists[i].push_back(*litr);
150  }
151  }
152 
153  // Postconditions:
154 
155  // Exit:
156 
157  return *this;
158 }
159 
163 {
164  // Preconditions:
165 
166  require(contains_rep_id(xrep_id));
167 
168  // Body:
169 
170  const list_type& result = _equivalence_lists[xrep_id];
171 
172  // Postconditions:
173 
174  // Exit:
175 
176  return result;
177 }
178 
181 equivalence_list(const scoped_index& xrep_id) const
182 {
183  // Preconditions:
184 
185  require(contains_rep_id(xrep_id));
186 
187  // Body:
188 
189  return equivalence_list(xrep_id.hub_pod());
190 }
191 
195 {
196  // Preconditions:
197 
198  require(contains_rep_id(xrep_id));
199 
200  // Body:
201 
202  size_type result = _equivalence_lists[xrep_id].size();
203 
204  // Postconditions:
205 
206  // Exit:
207 
208  return result;
209 }
210 
213 equivalence_ct(const scoped_index& xrep_id) const
214 {
215  // Preconditions:
216 
217  require(contains_rep_id(xrep_id));
218 
219  // Body:
220 
221  return equivalence_ct(xrep_id.hub_pod());
222 }
223 
224 bool
227 {
228  // Preconditions:
229 
230  // Body:
231 
232  bool result = (xrep_id >= 0) && (xrep_id < _equivalence_lists.ct());
233 
234  // Postconditions:
235 
236  ensure(is_basic_query);
237 
238  // Exit:
239 
240  return result;
241 }
242 
243 bool
245 contains_rep_id(const scoped_index& xrep_id) const
246 {
247  // Preconditions:
248 
249  // Body:
250 
251  return contains_rep_id(xrep_id.hub_pod());
252 }
253 
254 bool
256 id_space_less_than(const member_type& xitem1, const member_type& xitem2)
257 {
258  // Preconditions:
259 
260  // Body:
261 
262  bool result = (xitem1.first < xitem2.first);
263 
264  // Postconditions:
265 
266  // Exit:
267 
268  return result;
269 }
270 
271 // PROTECTED MEMBER FUNCTIONS
272 
273 // PRIVATE MEMBER FUNCTIONS
274 
275 
276 // ===========================================================
277 // NON-MEMBER FUNCTIONS
278 // ===========================================================
279 
index_equivalence_class & operator=(const index_equivalence_class &xother)
Assignment operator.
bool contains_rep_id(pod_index_type xrep_id) const
True if this class contains the representive id, xrep_id.
void release_id_space_iterator(index_space_iterator &xitr) const
Returns the id space iterator xitr to the iterator pool.
index_type ub() const
The upper bound on the storage array. The number of items current allocated in the storage array...
virtual pod_type index() const
Index of this space.
size_type ct() const
The number of items currently in use.
An abstract iterator over the ids of an id space.
pod_index_type pod_type
The "plain old data" index type for this.
pod_type pod() const
The current id in the iteration.
void reserve(index_type xub)
Makes ub() at least xub; if new storage is allocated, it is uninitialized.
size_type equivalence_ct(pod_index_type xrep_id) const
The number of ids in the equivalence list for the representive id, xrep_id.
virtual ~index_equivalence_class()
Destructor.
virtual void next()=0
Makes id() the next id in the iteration.
std::pair< pod_type, pod_type > member_type
The member type for the equivalence id list. The first value in the pair is the index of the id space...
An implementation of class sum_index_space_handle that has a primary sum id space state...
An abstract iterator over the id spaces of a interval.
block< list_type > _equivalence_lists
The block of equivalence id lists for a given representive id.
static bool id_space_less_than(const member_type &xitem1, const member_type &xitem2)
True if the id space of xitem1 is less than the id space of xitem2.
void push_back(const_reference_type item)
Insert item at the end of the items in the auto_block.
bool is_done() const
True if iteration is finished.
void set_ct(size_type xct)
Sets ct() == xct.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
bool only_persistent() const
True if only the persistent id spaces are iterated over.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
virtual pod_type index() const =0
Index of this space.
std::list< member_type > list_type
The equivalence id list type for this.
pod_type index() const
The index of the current id space in the iteration.
const list_type & equivalence_list(pod_index_type xrep_id) const
The equivalence list for the representive id, xrep_id.
void next()
Makes id_space() the next id space in the iteration.
index_equivalence_class()
Default constructor; disabled.
index_space_iterator & get_id_space_iterator() const
Allocates an id space iterator from the iterator pool attached to the current id space state...
Data structure containing the id equivalence class for ab id space family. This class is used as a te...
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual pod_type end() const
Ending id of this space.
Factory and container for a family of id spaces.
bool is_done() const
True if iteration is finished.
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
virtual bool contains(pod_type xid) const
True if this space contains id xid.
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710