SheafSystem  0.0.0.0
list_index_space_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/list_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/explicit_index_space_handle.h"
25 #include "SheafSystem/index_space_family.h"
26 #include "SheafSystem/list_index_space_state.h"
27 
28 // ===========================================================
29 // LIST_INDEX_SPACE_ITERATOR FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36 {
37  // Preconditions:
38 
39  // Body:
40 
41  // Postconditions:
42 
43  ensure(invariant());
44  ensure(!is_attached());
45 
46  // Exit:
47 
48  return;
49 }
50 
53 {
54  // Preconditions:
55 
56  // Body:
57 
58  *this = xother;
59 
60  // Postconditions:
61 
62  ensure(invariant());
63  // ensure((*this) == xother);
64 
65  // Exit:
66 
67  return;
68 }
69 
72  pod_type xindex)
73 {
74  // Preconditions:
75 
76  require(conforms_to_state(xid_spaces, xindex));
77 
78  // Body:
79 
80  attach_to(xid_spaces, xindex);
81 
82  // Postconditions:
83 
84  ensure(invariant());
85  ensure(is_attached());
86  ensure(&id_spaces() == &xid_spaces);
87  ensure(index() == xindex);
88 
89  // Exit:
90 
91  return;
92 }
93 
96  const std::string& xname)
97 {
98  // Preconditions:
99 
100  require(conforms_to_state(xid_spaces, xname));
101 
102  // Body:
103 
104  attach_to(xid_spaces, xname);
105 
106  // Postconditions:
107 
108  ensure(invariant());
109  ensure(is_attached());
110  ensure(&id_spaces() == &xid_spaces);
111  ensure(name() == xname);
112 
113  // Exit:
114 
115  return;
116 }
117 
121 {
122  // Preconditions:
123 
124  // Body:
125 
126  if(xother.is_attached())
127  {
128  _to_hub = xother._to_hub;
129  _itr = xother._itr;
130 
131  (void) explicit_index_space_iterator::operator=(xother);
132  }
133  else
134  {
135  detach();
136  }
137 
138  // Postconditions:
139 
140  ensure(invariant());
141  // ensure((*this) == xother);
142 
143  // Exit
144 
145  return *this;
146 }
147 
150 {
151  // Preconditions:
152 
153  // Body:
154 
155  // Nothing to do;
156 
157  // Postconditions:
158 
159  // Exit:
160 
161  return;
162 }
163 
164 // PROTECTED MEMBER FUNCTIONS
165 
166 // PRIVATE MEMBER FUNCTIONS
167 
168 
169 // ===========================================================
170 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
171 // ===========================================================
172 
173 // PUBLIC MEMBER FUNCTIONS
174 
175 // PROTECTED MEMBER FUNCTIONS
176 
177 // PRIVATE MAMBER FUNCTIONS
178 
179 
180 // ===========================================================
181 // INDEX_SPACE_ITERATOR FACET
182 // ===========================================================
183 
184 // PUBLIC MEMBER FUNCTIONS
185 
189 {
190  // Preconditions:
191 
192  require(is_ancestor_of(&xother));
193 
194  // Body:
195 
196  const list_index_space_iterator& lother =
197  dynamic_cast<const list_index_space_iterator&>(xother);
198 
199  (void) this->operator=(lother);
200 
201  // Postconditions:
202 
203  ensure(invariant());
204  // ensure((*this) == xother);
205 
206  // Exit
207 
208  return *this;
209 }
210 
211 bool
213 operator==(const index_space_iterator& xother) const
214 {
215  // Preconditions:
216 
217  require(is_ancestor_of(&xother));
218 
219  // Body:
220 
221  bool result = explicit_index_space_iterator::operator==(xother);
222  if(result && is_attached())
223  {
224  const list_index_space_iterator& lother =
225  dynamic_cast<const list_index_space_iterator&>(xother);
226 
227  result = result && (_to_hub == lother._to_hub);
228  result = result && (_itr == lother._itr);
229  }
230 
231  // Postconditions:
232 
233  ensure(invariant());
234 
235  // Exit
236 
237  return result;
238 }
239 
242 clone() const
243 {
245 
246  // Preconditions:
247 
248  // Body:
249 
250  result = new list_index_space_iterator(*this);
251 
252  // Postconditions:
253 
254  ensure(result != 0);
255  ensure(is_same_type(result));
256  ensure(*result == *this);
257 
258  // Exit:
259 
260  return result;
261 }
262 
263 // PROTECTED MEMBER FUNCTIONS
264 
265 // PRIVATE MAMBER FUNCTIONS
266 
267 
268 // ===========================================================
269 // ITERATOR FACET
270 // ===========================================================
271 
272 // PUBLIC MEMBER FUNCTIONS
273 
274 void
277 {
278  // Preconditions:
279 
280  require(is_attached());
281  require(!is_done());
282 
283  // Body:
284 
285  define_old_variable(pod_type old_pod = pod());
286  define_old_variable(pod_type old_hub_pod = hub_pod());
287 
288  ++_itr;
289  if(_itr == _to_hub->end())
290  {
291  // Done with iteration.
292 
293  invalidate_ids();
294  }
295  else
296  {
297  _pod++;
298  _hub_pod = *_itr;
299  }
300 
301 
302 // do
303 // {
304 // // Move to the next id.
305 
306 // ++_itr;
307 // if(_itr == _to_hub->end())
308 // {
309 // // Done with iteration.
310 
311 // invalidate_ids();
312 // break;
313 // }
314 
315 // _pod++;
316 // _hub_pod = *_itr;
317 // }
318 // while(!is_valid(_hub_pod));
319 
320  // Postconditions:
321 
322  ensure(invariant());
323  ensure(is_done() || pod() != old_pod);
324  ensure(is_done() || hub_pod() != old_hub_pod);
325 
326  // Exit:
327 
328  return;
329 }
330 
331 void
334 {
335  // Preconditions:
336 
337  require(is_attached());
338 
339  // Body:
340 
341  _itr = _to_hub->begin();
342  if(_itr == _to_hub->end())
343  {
344  // The id space is empty.
345 
346  invalidate_ids();
347  }
348  else
349  {
350  // The id space is not empty;
351  // initialize the iteration.
352 
353  _pod = 0;
354  _hub_pod = *_itr;
355  _is_done = false;
356  }
357 
358 
359 // _itr = _to_hub->begin();
360 
361 // _pod = 0;
362 
363 // while(true)
364 // {
365 // if(_itr == _to_hub->end())
366 // {
367 // // No entries found, invalidate the iterator.
368 
369 // invalidate_ids();
370 
371 // break;
372 // }
373 // else if(is_valid(*_itr))
374 // {
375 // // The first entry was found, initialize iterator.
376 
377 // _is_done = false;
378 // _hub_pod = *_itr;
379 
380 // break;
381 // }
382 // else
383 // {
384 // // The entry is empty continue to the next entry.
385 
386 // ++_pod;
387 // ++_itr;
388 // }
389 // }
390 
391  // Postconditions:
392 
393  ensure(invariant());
394 
395  // Exit:
396 
397  return;
398 }
399 
400 // PROTECTED MEMBER FUNCTIONS
401 
402 // PRIVATE MEMBER FUNCTIONS
403 
404 
405 // ===========================================================
406 // HANDLE FACET
407 // ===========================================================
408 
409 // PUBLIC MEMBER FUNCTIONS
410 
411 void
414 {
415  // Preconditions:
416 
417  require(xstate != 0);
418  require(conforms_to_state(xstate));
419 
420  // Body:
421 
422  // Assign the state information.
423 
424  _state = xstate;
425  _to_hub = &reinterpret_cast<list_index_space_state*>(_state)->_to_range;
426 
427  // Reset the iterator.
428 
429  reset();
430 
431  // Postconditions:
432 
433  ensure(invariant());
434  ensure(is_attached());
435  ensure(&state() == xstate);
436  ensure(!is_done() ? pod() >= 0 : true);
437  ensure(!is_done() ? is_valid(hub_pod()) : true);
438 
439  // Exit:
440 
441  return;
442 }
443 
444 bool
447 {
448  // Preconditions:
449 
450  // Body:
451 
452  bool result = (dynamic_cast<list_index_space_state*>(xstate) != 0);
453 
454  // Postconditions:
455 
456  ensure(is_basic_query);
457 
458  // Exit:
459 
460  return result;
461 }
462 
463 // PROTECTED MEMBER FUNCTIONS
464 
465 // PRIVATE MAMBER FUNCTIONS
466 
467 
468 // ===========================================================
469 // ANY FACET
470 // ===========================================================
471 
472 // PUBLIC MEMBER FUNCTIONS
473 
474 bool
476 is_ancestor_of(const any *other) const
477 {
478  // Preconditions:
479 
480  require(other != 0);
481 
482  // Body:
483 
484  // True if other conforms to this
485 
486  bool result = dynamic_cast<const list_index_space_iterator*>(other) != 0;
487 
488  // Postconditions:
489 
490  // Exit:
491 
492  return result;
493 }
494 
495 bool
497 invariant() const
498 {
499  bool result = true;
500 
501  if(invariant_check())
502  {
503  // Must satisfy base class invariant
504 
506 
507  // Prevent recursive calls to invariant
508 
510 
511  // Invariances for this class:
512 
513  // Finished, turn invariant checking back on.
514 
516  }
517 
518  // Exit
519 
520  return result;
521 }
522 
523 // PROTECTED MEMBER FUNCTIONS
524 
525 // PRIVATE MEMBER FUNCTIONS
526 
527 
528 // ===========================================================
529 // NON-MEMBER FUNCTIONS
530 // ===========================================================
virtual pod_type index() const
Index of this space.
An abstract iterator over the ids of an id space.
bool _is_done
True if the iteration is finished.
pod_type pod() const
The current id in the iteration.
An list implementation of class gathered_insertion_index_space_state. This representation is intended...
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
virtual void next()
Makes item() the next id in the iteration.
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
explicit_index_space_state & state()
The explicit state (mutable version).
Abstract base class with useful features for all objects.
Definition: any.h:39
std::string name() const
Name of this space.
bool is_done() const
True if iteration is finished.
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
const index_space_family & id_spaces() const
The id space family for this (const version).
to_hub_type * _to_hub
The map to hub ids.
An iterator over an id space in which the equivalence between the ids in the space and the hub id spa...
pod_type _hub_pod
The current hub id in the iteration.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual list_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
list_index_space_iterator & operator=(const list_index_space_iterator &xother)
Assignment operator.
virtual bool invariant() const
Class invariant.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
list_index_space_iterator()
Default constructor.
explicit_index_space_state * _state
The explicit state.
virtual bool invariant() const
Class invariant.
virtual void reset()
Restarts the iteration.
virtual void detach()
Detach this iterator form its state, if any.
pod_type _pod
The current id in the iteration.
Factory and container for a family of id spaces.
pod_index_type pod_type
The "plain old data" index type for this.
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
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
to_hub_type::iterator _itr
The stl iterator over ids.
virtual bool conforms_to_state(explicit_index_space_state *xstate) const
True if this conforms to the iterator type required by the explicit id space state xstate...
virtual bool is_attached() const
True if this iterator is attached to a state.
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().