SheafSystem  0.0.0.0
index_equivalence_iterator.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_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/index_space_family.h"
25 
26 // ===========================================================
27 // INDEX_EQUIVALENCE_ITERATOR FACET
28 // ===========================================================
29 
30 // PUBLIC MEMBER FUNCTIONS
31 
34  pod_index_type xrep_id,
35  bool xonly_persistent,
36  bool xsort_ids)
37  : _equivalence_class(xid_spaces, xonly_persistent, xsort_ids)
38 {
39  // Preconditions:
40 
41  require(is_valid(xrep_id) ? xid_spaces.hub_id_space().contains(xrep_id) : true);
42 
43  // Body:
44 
45  _id_spaces = &xid_spaces;
46  _only_persistent = xonly_persistent;
47  _is_sorted = xsort_ids;
48  _rep_id = xrep_id;
49 
50  reset();
51 
52  // Postconditions:
53 
54  ensure(invariant());
55  ensure(&id_spaces() == &xid_spaces);
56  ensure(rep_id() == xrep_id);
57  ensure(only_persistent() == xonly_persistent);
58  ensure(is_sorted() == xsort_ids);
59 
60  // Exit:
61 
62  return;
63 }
64 
67  const scoped_index& xrep_id,
68  bool xonly_persistent,
69  bool xsort_ids)
70  : _equivalence_class(xid_spaces, xonly_persistent, xsort_ids)
71 {
72  // Preconditions:
73 
74  require(xrep_id.is_valid() ? xid_spaces.hub_id_space().contains(xrep_id.hub_pod()) : true);
75 
76  // Body:
77 
78  _id_spaces = &xid_spaces;
79  _only_persistent = xonly_persistent;
80  _is_sorted = xsort_ids;
81  _rep_id = xrep_id.hub_pod();
82 
83  reset();
84 
85  // Postconditions:
86 
87  ensure(invariant());
88  ensure(&id_spaces() == &xid_spaces);
89  ensure(rep_id() == xrep_id.hub_pod());
90  ensure(only_persistent() == xonly_persistent);
91  ensure(is_sorted() == xsort_ids);
92 
93  // Exit:
94 
95  return;
96 }
97 
100  bool xonly_persistent,
101  bool xsort_ids)
102  : _equivalence_class(xid_spaces, xonly_persistent, xsort_ids)
103 {
104  // Preconditions:
105 
106  // Body:
107 
108  _id_spaces = &xid_spaces;
109  _only_persistent = xonly_persistent;
110  _is_sorted = xsort_ids;
112 
113  reset();
114 
115  // Postconditions:
116 
117  ensure(invariant());
118  ensure(&id_spaces() == &xid_spaces);
119  ensure(only_persistent() == xonly_persistent);
120  ensure(is_sorted() == xsort_ids);
121 
122  // Exit:
123 
124  return;
125 }
126 
129 {
130  // Preconditions:
131 
132  // Body:
133 
134  // Nothing to do.
135 
136  // Postconditions:
137 
138  // Exit:
139 
140  return;
141 }
142 
145 id_spaces() const
146 {
147  return *_id_spaces;
148 }
149 
152 rep_id() const
153 {
154  return _rep_id;
155 }
156 
157 void
159 rep_id(scoped_index& result) const
160 {
161  result.put(_id_spaces->hub_id_space(), _rep_id);
162 }
163 
164 bool
167 {
168  return _only_persistent;
169 }
170 
171 bool
173 is_sorted() const
174 {
175  return _is_sorted;
176 }
177 
178 void
181 {
182  // Preconditions:
183 
184  // Body:
185 
186  _rep_id = xrep_id;
187 
188  // Postconditions:
189 
190  ensure(invariant());
191  ensure(rep_id() == xrep_id);
192 
193  // Exit:
194 
195  return;
196 }
197 
198 void
200 put_rep_id(const scoped_index& xrep_id)
201 {
202  // Preconditions:
203 
204  require(xrep_id.is_valid() ? id_spaces().hub_id_space().contains(xrep_id.hub_pod()) : true);
205  // Body:
206 
207  _rep_id = xrep_id.hub_pod();
208 
209  // Postconditions:
210 
211  ensure(invariant());
212  ensure(rep_id() == xrep_id.hub_pod());
213 
214  // Exit:
215 
216  return;
217 }
218 
221 item() const
222 {
223  // Preconditions:
224 
225  require(!is_done());
226 
227  // Body:
228 
229  const member_type& result = _item;
230 
231  // Postconditions:
232 
233  // Exit:
234 
235  return result;
236 }
237 
238 bool
240 is_done() const
241 {
242  // Preconditions:
243 
244  // Body:
245 
246  bool result = _is_done;
247 
248  // Postconditions:
249 
250  ensure(is_basic_query);
251 
252  // Exit:
253 
254  return result;
255 }
256 
259 ct() const
260 {
261  // Preconditions:
262 
263  // Body:
264 
265  size_type result =
266  is_valid(_rep_id) ?
268 
269  // Postconditions:
270 
271  ensure(result >= 0);
272 
273  // Exit:
274 
275  return result;
276 }
277 
278 void
281 {
282  // Preconditions:
283 
284  require(!is_done());
285 
286  // Body:
287 
288  define_old_variable(pod_index_type old_space_index = item().first);
289 
290  // Increment the list iterator.
291 
292  _itr++;
293 
294  // Assign the item.
295 
296  if(_itr == _end)
297  {
298  _is_done = true;
299  }
300  else
301  {
302  _item = *_itr;
303  }
304 
305  // Postconditions:
306 
307  ensure(invariant());
308  //ensure(!is_done() ? item().first > old_space_index : true);
309  // @error equivalence class not sorted by id space;
310  // underlying hash map not ordered.
311  ensure(!is_done() ? item().first != old_space_index : true);
312 
313  // Exit:
314 
315  return;
316 }
317 
318 void
321 {
322  // Preconditions:
323 
324  // Body:
325 
326  _is_done = false;
327 
328  if(is_valid(_rep_id))
329  {
330  // Initialize the iterator to the beginning of the list and
331  // initialize the end of the iterator.
332 
333  const index_equivalence_class::list_type& lequivalence_list =
335 
336  _itr = lequivalence_list.begin();
337  _end = lequivalence_list.end();
338 
339  // Assign the item.
340 
341  if(_itr == _end)
342  {
343  _is_done = true;
344  }
345  else
346  {
347  _item = *_itr;
348  }
349  }
350  else
351  {
352  // The rep id is not valid, iteration is finished.
353 
354  _is_done = true;
355  }
356 
357  // Postconditions:
358 
359  ensure(invariant());
360 
361  // Exit:
362 
363  return;
364 }
365 
366 // PROTECTED MEMBER FUNCTIONS
367 
368 // PRIVATE MEMBER FUNCTIONS
369 
370 
371 // ===========================================================
372 // ANY FACET
373 // ===========================================================
374 
375 // PUBLIC MEMBER FUNCTIONS
376 
377 bool
379 is_ancestor_of(const any *other) const
380 {
381  // Preconditions:
382 
383  require(other != 0);
384 
385  // Body:
386 
387  // True if other conforms to this
388 
389  bool result = dynamic_cast<const index_equivalence_iterator*>(other) != 0;
390 
391  // Postconditions:
392 
393  // Exit:
394 
395  return result;
396 }
397 
400 clone() const
401 {
403 
404  // Preconditions:
405 
406  // Body:
407 
410  _is_sorted);
411 
412  // Postconditions:
413 
414  ensure(result != 0);
415  ensure(is_same_type(result));
416  ensure(&result->id_spaces() == &id_spaces());
417  ensure(result->rep_id() == rep_id());
418  ensure(result->only_persistent() == only_persistent());
419  ensure(result->is_sorted() == is_sorted());
420 
421  // Exit:
422 
423  return result;
424 }
425 
429 {
430  // Preconditions:
431 
432  // Body:
433 
434  _id_spaces = xother._id_spaces;
435  _rep_id = xother._rep_id;
437  _is_sorted = xother._is_sorted;
439  _itr = xother._itr;
440  _end = xother._end;
441  _item = xother._item;
442  _is_done = xother._is_done;
443 
444  // Postconditions:
445 
446  ensure(invariant());
447 
448  // Exit:
449 
450  return *this;
451 }
452 
453 bool
455 invariant() const
456 {
457  bool result = true;
458 
459  // Preconditions:
460 
461  // Body:
462 
463  if(invariant_check())
464  {
465  // Must satisfy base class invariant
466 
467  invariance(any::invariant());
468 
469  // Prevent recursive calls to invariant
470 
472 
473  // Must satisfy base class invariant
474 
478 
479  invariance(is_valid(rep_id()) ? id_spaces().hub_id_space().contains(rep_id()) : true);
480 
481  // Finished, turn invariant checking back on.
482 
484  }
485 
486  // Postconditions:
487 
488  // Exit
489 
490  return result;
491 }
492 
493 // PROTECTED MEMBER FUNCTIONS
494 
495 // PRIVATE MEMBER FUNCTIONS
496 
497 
498 // ===========================================================
499 // NON-MEMBER FUNCTIONS
500 // ===========================================================
501 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
pod_index_type _rep_id
The representative of this equivalence class.
const member_type & item() const
The current equivalence class member.
bool is_valid() const
True if this is a valid id.
Definition: scoped_index.h:832
void next()
Makes item() the next item in the equivalence class.
index_equivalence_class::member_type member_type
The member type for the equivalence id list. The first value in the pair is the index of the id space...
void put_rep_id(pod_index_type xrep_id)
Sets rep_id() to xrep_id.
bool _is_sorted
True, if the ids are sorted in increasing space id order.
const index_space_family & id_spaces() const
The id space family of this equivalence class.
size_type equivalence_ct(pod_index_type xrep_id) const
The number of ids in the equivalence list for the representive id, xrep_id.
index_equivalence_iterator()
Default constructor; disabled.
size_type ct() const
The number of items in the current equivalence list being iterated over.
bool only_persistent() const
True, if only persistent id spaces should be iterated over.
Abstract base class with useful features for all objects.
Definition: any.h:39
bool is_done() const
True if iteration is finished.
virtual index_equivalence_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this.
pod_index_type rep_id() const
The representative id in the top id space of this equivalence class.
index_equivalence_iterator & operator=(const index_equivalence_iterator &xother)
Assignment operator.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
iterator_type _itr
The current equivalence list iterator.
void put(const index_space_handle &xid_space, pod_type xpod)
Set the scope to id space, xid_space and pod() to xpod.
Definition: scoped_index.h:332
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
iterator_type _end
The end of the current list begin iterated over.
std::list< member_type > list_type
The equivalence id list type for this.
virtual bool invariant() const
Class invariant.
const list_type & equivalence_list(pod_index_type xrep_id) const
The equivalence list for the representive id, xrep_id.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
bool _only_persistent
True, if only persistent id spaces should be iterated over.
bool _is_done
True if iteration is finished.
An iterator over members of an id equivalence class.
index_equivalence_class _equivalence_class
The equivalence class to iterate over.
Factory and container for a family of id spaces.
const index_space_family * _id_spaces
The id space family to iterate over.
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
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
const hub_index_space_handle & hub_id_space() const
The hub id space of this family.
member_type _item
The current equivalence class member.
virtual bool contains(pod_type xid) const
True if this space contains id xid.
bool is_sorted() const
True, if the ids are sorted in increasing space id order.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710