SheafSystem  0.0.0.0
interval_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/interval_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/interval_index_space_state.h"
27 
28 // ===========================================================
29 // INTERVAL_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 
120 {
121  // Preconditions:
122 
123  // Body:
124 
125  // Nothing to do;
126 
127  // Postconditions:
128 
129  // Exit:
130 
131  return;
132 }
133 
137 {
138  // Preconditions:
139 
140  // Body:
141 
142  if(xother.is_attached())
143  {
144  _to_hub = xother._to_hub;
145  _itr = xother._itr;
147  _interval_end = xother._interval_end;
148 
149  (void) explicit_index_space_iterator::operator=(xother);
150  }
151  else
152  {
153  detach();
154  }
155 
156  // Postconditions:
157 
158  ensure(invariant());
159  ensure((*this) == xother);
160 
161  // Exit
162 
163  return *this;
164 }
165 
166 void
169 {
170  // Preconditions:
171 
172  require(!is_done());
173 
174  // Body:
175 
176  pod_type linterval_begin = _itr->first + 1;
177  ++_itr;
178  _is_done = (_itr == _to_hub->end());
179 
180  if(!_is_done)
181  {
182  // Found the next interval.
183 
184  if(!is_valid(_itr->second))
185  {
186  // The current interval is not mapped;
187  // we're going to skip it, but first
188  // set the beginning of the next interval.
189 
190  linterval_begin = _itr->first + 1;
191 
192  // Now skip the current intreval.
193 
194  ++_itr;
195 
196  // Last interval can not be unmapped,
197  // so there must be another one and
198  // there can not be two unmapped intervals in a row.
199 
200  assertion(_itr != _to_hub->end());
201  assertion(is_valid(_itr->second));
202  }
203 
204  // Update the interval ids.
205 
206  update_interval_ids(linterval_begin);
207  }
208  else
209  {
210  // There is no next interval, we're done.
211 
213  }
214 
215  // Postconditions:
216 
217  ensure(invariant());
218 
219  // Exit:
220 
221  return;
222 }
223 
227 {
228  return _interval_begin;
229 }
230 
234 {
235  return _interval_end;
236 }
237 
238 // PROTECTED MEMBER FUNCTIONS
239 
240 void
243 {
244  // Preconditions:
245 
246  require(!is_done());
247  require(is_valid(xinterval_begin));
248 
249  // Body:
250 
251  // Assign the interval extrema.
252 
253  _interval_begin = xinterval_begin;
254  _interval_end = _itr->first;
255 
256  // Set the current ids.
257 
259  _hub_pod = _itr->second - (_interval_end - _interval_begin);
260 
261  // Postconditions:
262 
263  ensure(is_valid(pod()));
264  ensure(is_valid(hub_pod()));
265  ensure(is_valid(interval_begin()));
266  ensure(is_valid(interval_end()));
267 
268  // Exit:
269 
270  return;
271 }
272 
273 void
276 {
277  // Preconditions:
278 
279  // Body:
280 
283 
284  invalidate_ids();
285 
286  // Postconditions:
287 
288  // Exit:
289 
290  return;
291 }
292 
293 // PRIVATE MEMBER FUNCTIONS
294 
295 
296 // ===========================================================
297 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
298 // ===========================================================
299 
300 // PUBLIC MEMBER FUNCTIONS
301 
302 // PROTECTED MEMBER FUNCTIONS
303 
304 // PRIVATE MAMBER FUNCTIONS
305 
306 
307 // ===========================================================
308 // INDEX_SPACE_ITERATOR FACET
309 // ===========================================================
310 
311 // PUBLIC MEMBER FUNCTIONS
312 
316 {
317  // Preconditions:
318 
319  require(is_ancestor_of(&xother));
320 
321  // Body:
322 
323  const interval_index_space_iterator& lother =
324  dynamic_cast<const interval_index_space_iterator&>(xother);
325 
326  (void) this->operator=(lother);
327 
328  // Postconditions:
329 
330  ensure(invariant());
331  ensure((*this) == xother);
332 
333  // Exit
334 
335  return *this;
336 }
337 
338 bool
340 operator==(const index_space_iterator& xother) const
341 {
342  // Preconditions:
343 
344  require(is_ancestor_of(&xother));
345 
346  // Body:
347 
348  bool result = explicit_index_space_iterator::operator==(xother);
349  if(result && is_attached())
350  {
351  const interval_index_space_iterator& lother =
352  dynamic_cast<const interval_index_space_iterator&>(xother);
353 
354  result = result && (_to_hub == lother._to_hub);
355  result = result && (_itr == lother._itr);
356  result = result && (_interval_begin == lother._interval_begin);
357  result = result && (_interval_end == lother._interval_end);
358  }
359 
360  // Postconditions:
361 
362  ensure(invariant());
363 
364  // Exit
365 
366  return result;
367 }
368 
371 clone() const
372 {
374 
375  // Preconditions:
376 
377  // Body:
378 
379  result = new interval_index_space_iterator(*this);
380 
381  // Postconditions:
382 
383  ensure(result != 0);
384  ensure(is_same_type(result));
385  ensure(*result == *this);
386 
387  // Exit:
388 
389  return result;
390 }
391 
392 // PROTECTED MEMBER FUNCTIONS
393 
394 // PRIVATE MAMBER FUNCTIONS
395 
396 
397 // ===========================================================
398 // ITERATOR FACET
399 // ===========================================================
400 
401 // PUBLIC MEMBER FUNCTIONS
402 
403 void
406 {
407  // Preconditions:
408 
409  require(is_attached());
410  require(!is_done());
411 
412  // Body:
413 
414  define_old_variable(pod_type old_pod = pod());
415  define_old_variable(pod_type old_hub_pod = hub_pod());
416 
417  // Iterate to the next id.
418 
419  ++_pod;
420 
421  if(_pod > _interval_end)
422  {
423  // At the end of the current interval;
424  // advance to the next interval.
425 
426  next_interval();
427  }
428  else
429  {
430  // Still in the same interval;
431  // increment hub id.
432 
433  ++_hub_pod;
434  }
435 
436  // Postconditions:
437 
438  ensure(invariant());
439  ensure(is_done() || pod() != old_pod);
440  ensure(is_done() || hub_pod() != old_hub_pod);
441 
442  // Exit:
443 
444  return;
445 }
446 
447 void
450 {
451  // Preconditions:
452 
453  require(is_attached());
454 
455  // Body:
456 
457  _itr = _to_hub->begin();
458 
459  if(_itr != _to_hub->end())
460  {
461  // There's at least one entry, we're not done.
462 
463  _is_done = false;
464 
465  // Found end of implicit 0-th interval;
466  // mapped value must be invalid.
467 
468  assertion(!is_valid(_itr->second));
469 
470  // The beginning of the first interval is the next id.
471 
472  pod_type linterval_begin = _itr->first + 1;
473 
474  // Find the end of the first interval.
475 
476  ++_itr;
477 
478  // There can not be just a single entry.
479  // and there can not be two unmapped intervals in a row.
480 
481  assertion(_itr != _to_hub->end());
482  assertion(is_valid(_itr->second));
483 
484  // Update the interval ids.
485 
486  update_interval_ids(linterval_begin);
487  }
488  else
489  {
490  // The map is empty, we're done.
491 
492  invalidate_ids();
493  }
494 
495  // Postconditions:
496 
497  ensure(invariant());
498 
499  // Exit:
500 
501  return;
502 }
503 
504 // PROTECTED MEMBER FUNCTIONS
505 
506 // PRIVATE MEMBER FUNCTIONS
507 
508 
509 // ===========================================================
510 // HANDLE FACET
511 // ===========================================================
512 
513 // PUBLIC MEMBER FUNCTIONS
514 
515 void
518 {
519  // Preconditions:
520 
521  require(xstate != 0);
522  require(conforms_to_state(xstate));
523 
524  // Body:
525 
526  // Assign the state information.
527 
528  _state = xstate;
529  _to_hub = &reinterpret_cast<interval_index_space_state*>(_state)->_to_range;
530 
531  // Reset the iterator.
532 
533  reset();
534 
535  // Postconditions:
536 
537  ensure(invariant());
538  ensure(is_attached());
539  ensure(&state() == xstate);
540  ensure(!is_done() ? pod() >= 0 : true);
541  ensure(!is_done() ? is_valid(hub_pod()) : true);
542 
543  // Exit:
544 
545  return;
546 }
547 
548 bool
551 {
552  // Preconditions:
553 
554  // Body:
555 
556  bool result = (dynamic_cast<interval_index_space_state*>(xstate) != 0);
557 
558  // Postconditions:
559 
560  ensure(is_basic_query);
561 
562  // Exit:
563 
564  return result;
565 }
566 
567 // PROTECTED MEMBER FUNCTIONS
568 
569 // PRIVATE MAMBER FUNCTIONS
570 
571 
572 // ===========================================================
573 // ANY FACET
574 // ===========================================================
575 
576 // PUBLIC MEMBER FUNCTIONS
577 
578 bool
580 is_ancestor_of(const any *other) const
581 {
582  // Preconditions:
583 
584  require(other != 0);
585 
586  // Body:
587 
588  // True if other conforms to this
589 
590  bool result = dynamic_cast<const interval_index_space_iterator*>(other) != 0;
591 
592  // Postconditions:
593 
594  // Exit:
595 
596  return result;
597 }
598 
599 bool
601 invariant() const
602 {
603  bool result = true;
604 
605  if(invariant_check())
606  {
607  // Must satisfy base class invariant
608 
610 
611  // Prevent recursive calls to invariant
612 
614 
615  // Invariances for this class:
616 
617  // Finished, turn invariant checking back on.
618 
620  }
621 
622  // Exit
623 
624  return result;
625 }
626 
627 // PROTECTED MEMBER FUNCTIONS
628 
629 // PRIVATE MEMBER FUNCTIONS
630 
631 
632 // ===========================================================
633 // NON-MEMBER FUNCTIONS
634 // ===========================================================
635 
An STL map implementation of class scattered_insertion_index_space_state optimized to efficiently rep...
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.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
void next_interval()
Advances the iteration to the beginning of the next interval.
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
virtual bool invariant() const
Class invariant.
pod_type _interval_end
The end of the current interval.
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.
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...
pod_type interval_begin() const
The begin of the current interval.
bool is_done() const
True if iteration is finished.
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
pod_type _interval_begin
The begin of the current interval.
const index_space_family & id_spaces() const
The id space family for this (const version).
pod_type interval_end() const
The end of the current interval.
void update_interval_ids(pod_type xinterval_begin)
Update the interval ids with the beginning interval id, xinterval_begin.
virtual void next()
Makes item() the next id in the iteration.
An iterator over an id space in which the equivalence between the ids in the space and the hub id spa...
virtual void reset()
Restarts the iteration.
pod_type _hub_pod
The current hub id in the iteration.
virtual interval_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
to_hub_type::const_iterator _itr
The stl iterator over ids.
void invalidate_interval_ids()
Invalidate the interval ids.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
explicit_index_space_state * _state
The explicit state.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual bool invariant() const
Class invariant.
virtual void detach()
Detach this iterator form its state, if any.
interval_index_space_iterator & operator=(const interval_index_space_iterator &xother)
Assignment operator.
pod_type _pod
The current id in the iteration.
const to_hub_type * _to_hub
The interval to hub ids.
Factory and container for a family of id spaces.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
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
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
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().