SheafSystem  0.0.0.0
hub_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/hub_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_set_iterator.h"
27 #include "SheafSystem/primary_sum_index_space_state.h"
28 
29 // ===========================================================
30 // HUB_INDEX_SPACE_ITERATOR FACET
31 // ===========================================================
32 
33 // PUBLIC MEMBER FUNCTIONS
34 
37  : _mbr_itr(0)
38 {
39  // Preconditions:
40 
41  // Body:
42 
43  // Postconditions:
44 
45  ensure(invariant());
46  ensure(!is_attached());
47 
48  // Exit:
49 
50  return;
51 }
52 
55 {
56  // Preconditions:
57 
58  // Body:
59 
60  *this = xother;
61 
62  // Postconditions:
63 
64  ensure(invariant());
65  ensure((*this) == xother);
66 
67  // Exit:
68 
69  return;
70 }
71 
74  pod_type xindex)
75  : _mbr_itr(0)
76 {
77  // Preconditions:
78 
79  require(conforms_to_state(xid_spaces, xindex));
80 
81  // Body:
82 
83  attach_to(xid_spaces, xindex);
84 
85  // Postconditions:
86 
87  ensure(invariant());
88  ensure(is_attached());
89  ensure(&id_spaces() == &xid_spaces);
90  ensure(index() == xindex);
91 
92  // Exit:
93 
94  return;
95 }
96 
99  const std::string& xname)
100  : _mbr_itr(0)
101 {
102  // Preconditions:
103 
104  require(conforms_to_state(xid_spaces, xname));
105 
106  // Body:
107 
108  attach_to(xid_spaces, xname);
109 
110  // Postconditions:
111 
112  ensure(invariant());
113  ensure(is_attached());
114  ensure(&id_spaces() == &xid_spaces);
115  ensure(name() == xname);
116 
117  // Exit:
118 
119  return;
120 }
121 
125 {
126  // Preconditions:
127 
128  // Body:
129 
130  if(xother.is_attached())
131  {
132  _mbr_itr = new interval_set_iterator(*xother._mbr_itr);
133  _rem = xother._rem;
134 
135  (void) explicit_index_space_iterator::operator=(xother);
136  }
137  else
138  {
139  detach();
140  }
141 
142  // Postconditions:
143 
144  ensure(invariant());
145  ensure((*this) == xother);
146 
147  // Exit
148 
149  return *this;
150 }
151 
154 {
155  // Preconditions:
156 
157  // Body:
158 
159  if(_mbr_itr != 0)
160  {
161  delete _mbr_itr;
162  }
163 
164  // Postconditions:
165 
166  // Exit:
167 
168  return;
169 }
170 
171 // PROTECTED MEMBER FUNCTIONS
172 
173 // PRIVATE MEMBER FUNCTIONS
174 
175 
176 // ===========================================================
177 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
178 // ===========================================================
179 
180 // PUBLIC MEMBER FUNCTIONS
181 
182 // PROTECTED MEMBER FUNCTIONS
183 
184 // PRIVATE MAMBER FUNCTIONS
185 
186 
187 // ===========================================================
188 // INDEX_SPACE_ITERATOR FACET
189 // ===========================================================
190 
191 // PUBLIC MEMBER FUNCTIONS
192 
196 {
197  // Preconditions:
198 
199  require(is_ancestor_of(&xother));
200 
201  // Body:
202 
203  const hub_index_space_iterator& lother =
204  dynamic_cast<const hub_index_space_iterator&>(xother);
205 
206 
207  (void) this->operator=(lother);
208 
209  // Postconditions:
210 
211  ensure(invariant());
212  ensure((*this) == xother);
213 
214  // Exit
215 
216  return *this;
217 }
218 
219 bool
221 operator==(const index_space_iterator& xother) const
222 {
223  // Preconditions:
224 
225  require(is_ancestor_of(&xother));
226 
227  // Body:
228 
229  bool result = explicit_index_space_iterator::operator==(xother);
230  if(result && is_attached())
231  {
232  const hub_index_space_iterator& lother =
233  dynamic_cast<const hub_index_space_iterator&>(xother);
234 
235  result = result && (*_mbr_itr == *lother._mbr_itr);
236  result = result && (_rem == lother._rem);
237  }
238 
239  // Postconditions:
240 
241  ensure(invariant());
242 
243  // Exit
244 
245  return result;
246 }
247 
250 clone() const
251 {
252  hub_index_space_iterator* result;
253 
254  // Preconditions:
255 
256  // Body:
257 
258  result = new hub_index_space_iterator(*this);
259 
260  // Postconditions:
261 
262  ensure(result != 0);
263  ensure(is_same_type(result));
264  ensure(*result == *this);
265 
266  // Exit:
267 
268  return result;
269 }
270 
271 // PROTECTED MEMBER FUNCTIONS
272 
273 // PRIVATE MAMBER FUNCTIONS
274 
275 
276 // ===========================================================
277 // ITERATOR FACET
278 // ===========================================================
279 
280 // PUBLIC MEMBER FUNCTIONS
281 
282 void
285 {
286  // Preconditions:
287 
288  require(is_attached());
289  require(!is_done());
290 
291  // Body:
292 
293  define_old_variable(pod_type old_pod = pod());
294  define_old_variable(pod_type old_hub_pod = hub_pod());
295 
296  // Iterate to the next member.
297 
298  _mbr_itr->next();
299 
300  if(_mbr_itr->is_done())
301  {
302  // The iteration is done.
303 
304  invalidate_ids();
305  }
306  else
307  {
308  // Still iterating over member ids;
309  // set the ids.
310 
311  _pod = _mbr_itr->item();
312  _hub_pod = _pod;
313  }
314 
315  // Postconditions:
316 
317  ensure(invariant());
318  ensure(is_done() || pod() > old_pod);
319  ensure(is_done() || hub_pod() > old_hub_pod);
320 
321  // Exit:
322 
323  return;
324 }
325 
326 void
329 {
330  // Preconditions:
331 
332  require(is_attached());
333 
334  // Body:
335 
336  _mbr_itr->reset();
337 
338  if(_mbr_itr->is_done())
339  {
340  // The member set is empty, the iteration is done.
341 
342  invalidate_ids();
343  }
344  else
345  {
346  // There's at least one entry, the iteration is not done.
347 
348  _is_done = false;
349  _pod = _mbr_itr->item();
350  _hub_pod = _pod;
351  }
352 
353  // Postconditions:
354 
355  ensure(invariant());
356 
357  // Exit:
358 
359  return;
360 }
361 
362 // PROTECTED MEMBER FUNCTIONS
363 
364 // PRIVATE MEMBER FUNCTIONS
365 
366 
367 // ===========================================================
368 // HANDLE FACET
369 // ===========================================================
370 
371 // PUBLIC MEMBER FUNCTIONS
372 
373 void
376 {
377  // Preconditions:
378 
379  require(xstate != 0);
380  require(conforms_to_state(xstate));
381 
382  // Body:
383 
384  // Assign the state information.
385 
387  reinterpret_cast<primary_sum_index_space_state*>(xstate);
388 
389  if(_mbr_itr != 0)
390  {
391  delete _mbr_itr;
392  }
393 
394  _state = xstate;
395  _mbr_itr = lstate->_members.iterator(true);
396  _rem = &lstate->_rem;
397 
398  // Start at the first id.
399 
400  reset();
401 
402  // Postconditions:
403 
404  ensure(invariant());
405  ensure(is_attached());
406  ensure(&state() == xstate);
407 
408  // Exit:
409 
410  return;
411 }
412 
413 bool
416 {
417  // Preconditions:
418 
419  // Body:
420 
421  bool result = (dynamic_cast<primary_sum_index_space_state*>(xstate) != 0);
422 
423  // Postconditions:
424 
425  ensure(is_basic_query);
426 
427  // Exit:
428 
429  return result;
430 }
431 
432 // PROTECTED MEMBER FUNCTIONS
433 
434 // PRIVATE MAMBER FUNCTIONS
435 
436 
437 // ===========================================================
438 // ANY FACET
439 // ===========================================================
440 
441 // PUBLIC MEMBER FUNCTIONS
442 
443 bool
445 is_ancestor_of(const any *other) const
446 {
447  // Preconditions:
448 
449  require(other != 0);
450 
451  // Body:
452 
453  // True if other conforms to this
454 
455  bool result =
456  dynamic_cast<const hub_index_space_iterator*>(other) != 0;
457 
458  // Postconditions:
459 
460  return result;
461 }
462 
463 bool
465 invariant() const
466 {
467  bool result = true;
468 
469  if(invariant_check())
470  {
471  // Must satisfy base class invariant
472 
474 
475  // Prevent recursive calls to invariant
476 
478 
479  // Invariances for this class:
480 
481  invariance(is_done() || pod() == hub_pod());
482 
483  // Finished, turn invariant checking back on.
484 
486  }
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 
502 
An iterator over the integers in an interval_set.
virtual pod_type index() const
Index of this space.
virtual void next()
Makes item() the next id in the iteration.
An abstract iterator over the ids of an id space.
rem_type _rem
The reflexive reduction of the rem map.
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.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
virtual bool invariant() const
Class invariant.
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
interval_set_iterator * _mbr_itr
Iterator over the members.
void reset()
Restarts the iteration.
hub_index_space_iterator()
Default constructor.
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.
interval_set _members
Set of members in the domain of this map.
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).
pod_type item() const
The current integer of the iteration.
virtual hub_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual void reset()
Restarts the iteration.
hub_index_space_iterator & operator=(const hub_index_space_iterator &xother)
Assignment operator.
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 bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
void next()
Makes item() the next integer in the iteration.
interval_set_iterator * iterator(bool xis_full_iterator) const
Iterator for this set. If xis_full_iterator, iterate over the members in the set. Otherwise...
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
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...
explicit_index_space_state * _state
The explicit state.
virtual bool invariant() const
Class invariant.
virtual void detach()
Detach this iterator form its state, if any.
An implementation of class sum_index_space_state intended for use as the sum of the primary id spaces...
pod_type _pod
The current id in the iteration.
Factory and container for a family of id spaces.
An iterator over an id space which is the sum of the primary id spaces.
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
pod_index_type pod_type
The "plain old data" index type for this.
const rem_type * _rem
The reflexive reduction of the rem map.
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
bool is_done() const
True if iteration is finished.
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().