SheafSystem  0.0.0.0
reserved_primary_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/reserved_primary_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/reserved_primary_index_space_state.h"
27 
28 // ===========================================================
29 // RESERVED_PRIMARY_INDEX_SPACE_ITERATOR FACET
30 // ===========================================================
31 
32 // PUBLIC MEMBER FUNCTIONS
33 
36  : _is_active_only(true)
37 {
38  // Preconditions:
39 
40  // Body:
41 
42  // Postconditions:
43 
44  ensure(invariant());
45  ensure(!is_attached());
46 
47  // Exit:
48 
49  return;
50 }
51 
54 {
55  // Preconditions:
56 
57  // Body:
58 
59  *this = xother;
60 
61  // Postconditions:
62 
63  ensure(invariant());
64  ensure((*this) == xother);
65 
66  // Exit:
67 
68  return;
69 }
70 
73  pod_type xindex)
74  : _is_active_only(true)
75 {
76  // Preconditions:
77 
78  require(conforms_to_state(xid_spaces, xindex));
79 
80  // Body:
81 
82  attach_to(xid_spaces, xindex);
83 
84  // Postconditions:
85 
86  ensure(invariant());
87  ensure(is_attached());
88  ensure(&id_spaces() == &xid_spaces);
89  ensure(index() == xindex);
90 
91  // Exit:
92 
93  return;
94 }
95 
98  const std::string& xname)
99  : _is_active_only(true)
100 {
101  // Preconditions:
102 
103  require(conforms_to_state(xid_spaces, xname));
104 
105  // Body:
106 
107  attach_to(xid_spaces, xname);
108 
109  // Postconditions:
110 
111  ensure(invariant());
112  ensure(is_attached());
113  ensure(&id_spaces() == &xid_spaces);
114  ensure(name() == xname);
115 
116  // Exit:
117 
118  return;
119 }
120 
124 {
125  // Preconditions:
126 
127  // Body:
128 
129  if(xother.is_attached())
130  {
131  _offset = xother._offset;
132  _end = xother._end;
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  // No action required.
160 
161  // Postconditions:
162 
163  // Exit:
164 
165  return;
166 }
167 
170 offset() const
171 {
172  // Preconditions:
173 
174  require(is_attached());
175 
176  // Body:
177 
178  pod_type result = _offset;
179 
180  // Postconditions:
181 
182  ensure(is_basic_query);
183 
184  // Exit:
185 
186  return result;
187 }
188 
189 bool
192 {
193  // Preconditions:
194 
195  // Body:
196 
197  bool result = _is_active_only;
198 
199  // Postconditions:
200 
201  ensure(is_basic_query);
202 
203  // Exit:
204 
205  return result;
206 }
207 
208 void
210 put_is_active_only(bool xis_active_only)
211 {
212  // Preconditions:
213 
214  require(is_attached());
215 
216  // Body:
217 
218  _is_active_only = xis_active_only;
219 
221  reinterpret_cast<reserved_primary_index_space_state*>(_state);
222 
223  _end = _is_active_only ? lstate->next_hub_id() - _offset : lstate->ct();
224 
225  // Postconditions:
226 
227  ensure(is_active_only() == xis_active_only);
228 
229  // Exit:
230 
231  return;
232 }
233 
234 // PROTECTED MEMBER FUNCTIONS
235 
236 // PRIVATE MEMBER FUNCTIONS
237 
238 
239 // ===========================================================
240 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
241 // ===========================================================
242 
243 // PUBLIC MEMBER FUNCTIONS
244 
245 // PROTECTED MEMBER FUNCTIONS
246 
247 // PRIVATE MAMBER FUNCTIONS
248 
249 
250 // ===========================================================
251 // INDEX_SPACE_ITERATOR FACET
252 // ===========================================================
253 
254 // PUBLIC MEMBER FUNCTIONS
255 
259 {
260  // Preconditions:
261 
262  require(is_ancestor_of(&xother));
263 
264  // Body:
265 
267  dynamic_cast<const reserved_primary_index_space_iterator&>(xother);
268 
269  (void) this->operator=(lother);
270 
271  // Postconditions:
272 
273  ensure(invariant());
274  ensure((*this) == xother);
275 
276  // Exit
277 
278  return *this;
279 }
280 
281 bool
283 operator==(const index_space_iterator& xother) const
284 {
285  // Preconditions:
286 
287  require(is_ancestor_of(&xother));
288 
289  // Body:
290 
291  bool result = explicit_index_space_iterator::operator==(xother);
292  if(result && is_attached())
293  {
295  dynamic_cast<const reserved_primary_index_space_iterator&>(xother);
296 
297  result = result && (_offset == lother._offset);
298  result = result && (_end == lother._end);
299  result = result && (_is_active_only == lother._is_active_only);
300  }
301 
302  // Postconditions:
303 
304  ensure(invariant());
305 
306  // Exit
307 
308  return result;
309 }
310 
313 clone() const
314 {
316 
317  // Preconditions:
318 
319  // Body:
320 
321  result = new reserved_primary_index_space_iterator(*this);
322 
323  // Postconditions:
324 
325  ensure(result != 0);
326  ensure(is_same_type(result));
327  ensure(*result == *this);
328 
329  // Exit:
330 
331  return result;
332 }
333 
334 // PROTECTED MEMBER FUNCTIONS
335 
336 // PRIVATE MAMBER FUNCTIONS
337 
338 
339 // ===========================================================
340 // ITERATOR FACET
341 // ===========================================================
342 
343 // PUBLIC MEMBER FUNCTIONS
344 
345 void
348 {
349  // Preconditions:
350 
351  require(is_attached());
352  require(!is_done());
353 
354  // Body:
355 
356  define_old_variable(pod_type old_pod = pod());
357  define_old_variable(pod_type old_hub_pod = hub_pod());
358 
359  // Increment to the next entry.
360 
361  ++_pod;
362  if(_pod == _end)
363  {
364  // The iteration is done.
365 
366  invalidate_ids();
367  }
368  else
369  {
370  ++_hub_pod;
371  }
372 
373  // Postconditions:
374 
375  ensure(invariant());
376  ensure(is_done() || pod() == (old_pod + 1));
377  ensure(is_done() || hub_pod() == (old_hub_pod + 1));
378 
379  // Exit:
380 
381  return;
382 }
383 
384 void
387 {
388  // Preconditions:
389 
390  require(is_attached());
391 
392  // Body:
393 
394  if(_end == 0 || !is_valid(_end))
395  {
396  // The map is empty, the iteration is done.
397 
398  invalidate_ids();
399  }
400  else
401  {
402  // There's at least one entry, the iteration is not done.
403 
404  _is_done = false;
405 
406  _pod = 0;
407  _hub_pod = _offset;
408  }
409 
410  // Postconditions:
411 
412  ensure(invariant());
413 
414  // Exit:
415 
416  return;
417 }
418 
419 // PROTECTED MEMBER FUNCTIONS
420 
421 // PRIVATE MAMBER FUNCTIONS
422 
423 
424 // ===========================================================
425 // HANDLE FACET
426 // ===========================================================
427 
428 // PUBLIC MEMBER FUNCTIONS
429 
430 void
433 {
434  // Preconditions:
435 
436  require(xstate != 0);
437  require(conforms_to_state(xstate));
438 
439  // Body:
440 
441  // Assign the state information.
442 
444  reinterpret_cast<reserved_primary_index_space_state*>(xstate);
445 
446  _state = xstate;
447  _offset = lstate->offset();
448  _end = _is_active_only ? lstate->next_hub_id() - _offset : lstate->ct();
449 
450  // Reset the iterator.
451 
452  reset();
453 
454  // Postconditions:
455 
456  ensure(invariant());
457  ensure(is_attached());
458  ensure(&state() == xstate);
459 
460  // Exit:
461 
462  return;
463 }
464 
465 bool
468 {
469  // Preconditions:
470 
471  // Body:
472 
473  bool result = (dynamic_cast<reserved_primary_index_space_state*>(xstate) != 0);
474 
475  // Postconditions:
476 
477  ensure(is_basic_query);
478 
479  // Exit:
480 
481  return result;
482 }
483 
484 // PROTECTED MEMBER FUNCTIONS
485 
486 // PRIVATE MAMBER FUNCTIONS
487 
488 
489 // ===========================================================
490 // ANY FACET
491 // ===========================================================
492 
493 // PUBLIC MEMBER FUNCTIONS
494 
495 bool
497 is_ancestor_of(const any *other) const
498 {
499  // Preconditions:
500 
501  require(other != 0);
502 
503  // Body:
504 
505  // True if other conforms to this
506 
507  bool result = dynamic_cast<const reserved_primary_index_space_iterator*>(other) != 0;
508 
509  // Postconditions:
510 
511  // Exit:
512 
513  return result;
514 }
515 
516 bool
518 invariant() const
519 {
520  bool result = true;
521 
522  if(invariant_check())
523  {
524  // Must satisfy base class invariant
525 
527 
528  // Prevent recursive calls to invariant
529 
531 
532  // Invariances for this class:
533 
534  // Finished, turn invariant checking back on.
535 
537  }
538 
539  // Exit
540 
541  return result;
542 }
543 
544 // PROTECTED MEMBER FUNCTIONS
545 
546 // PRIVATE MEMBER FUNCTIONS
547 
548 
549 // ===========================================================
550 // NON-MEMBER FUNCTIONS
551 // ===========================================================
size_type ct() const
The number of members.
bool _is_active_only
True if and only the active interval of the id space is iterated over. Otherwise, all the ids in the ...
virtual pod_type index() const
Index of this space.
reserved_primary_index_space_iterator & operator=(const reserved_primary_index_space_iterator &xother)
Assignment operator.
An abstract iterator over the ids of an id space.
bool _is_done
True if the iteration is finished.
A computed implementation of abstract class primary_index_space_state. This representation is used fo...
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 operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
virtual reserved_primary_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
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 void next()
Makes item() the next id in the iteration.
explicit_index_space_state & state()
The explicit state (mutable version).
pod_type offset() const
The offset into the hub id space.
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.
An immutable abstract state for a space of alternate integer identifiers (aliases) for a subset of th...
pod_type offset() const
The offset into the hub id space.
const index_space_family & id_spaces() const
The id space family for this (const version).
An iterator over a reserved term in the primary sum id space in which the equivlence between the ids ...
bool is_active_only() const
True if and only the active interval of the id space is iterated over. Otherwise, all the ids in the ...
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 void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
pod_type next_hub_id() const
The next available hub id for allocation.
explicit_index_space_state * _state
The explicit state.
virtual bool invariant() const
Class invariant.
void put_is_active_only(bool xis_active_only)
Set the iteration mode. If xis_active_only, only the active interval of the id space is iterated over...
virtual void detach()
Detach this iterator form its state, if any.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
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
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().