SheafSystem  0.0.0.0
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/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/primary_index_space_state.h"
27 
28 // ===========================================================
29 // PRIMARY_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  _offset = xother._offset;
129  _ct = xother._ct;
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  // No action required.
156 
157  // Postconditions:
158 
159  // Exit:
160 
161  return;
162 }
163 
166 offset() const
167 {
168  // Preconditions:
169 
170  require(is_attached());
171 
172  // Body:
173 
174  pod_type result = _offset;
175 
176  // Postconditions:
177 
178  ensure(is_basic_query);
179 
180  // Exit:
181 
182  return result;
183 }
184 
187 ct() const
188 {
189  // Preconditions:
190 
191  require(is_attached());
192 
193  // Body:
194 
195  size_type result = _ct;
196 
197  // Postconditions:
198 
199  ensure(is_basic_query);
200 
201  // Exit:
202 
203  return result;
204 }
205 
206 // PROTECTED MEMBER FUNCTIONS
207 
208 // PRIVATE MEMBER FUNCTIONS
209 
210 
211 // ===========================================================
212 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
213 // ===========================================================
214 
215 // PUBLIC MEMBER FUNCTIONS
216 
217 // PROTECTED MEMBER FUNCTIONS
218 
219 // PRIVATE MAMBER FUNCTIONS
220 
221 
222 // ===========================================================
223 // INDEX_SPACE_ITERATOR FACET
224 // ===========================================================
225 
226 // PUBLIC MEMBER FUNCTIONS
227 
231 {
232  // Preconditions:
233 
234  require(is_ancestor_of(&xother));
235 
236  // Body:
237 
238  const primary_index_space_iterator& lother =
239  dynamic_cast<const primary_index_space_iterator&>(xother);
240 
241  (void) this->operator=(lother);
242 
243  // Postconditions:
244 
245  ensure(invariant());
246  ensure((*this) == xother);
247 
248  // Exit
249 
250  return *this;
251 }
252 
253 bool
255 operator==(const index_space_iterator& xother) const
256 {
257  // Preconditions:
258 
259  require(is_ancestor_of(&xother));
260 
261  // Body:
262 
263  bool result = explicit_index_space_iterator::operator==(xother);
264  if(result && is_attached())
265  {
266  const primary_index_space_iterator& lother =
267  dynamic_cast<const primary_index_space_iterator&>(xother);
268 
269  result = result && (_offset == lother._offset);
270  result = result && (_ct == lother._ct);
271  }
272 
273  // Postconditions:
274 
275  ensure(invariant());
276 
277  // Exit
278 
279  return result;
280 }
281 
284 clone() const
285 {
287 
288  // Preconditions:
289 
290  // Body:
291 
292  result = new primary_index_space_iterator(*this);
293 
294  // Postconditions:
295 
296  ensure(result != 0);
297  ensure(is_same_type(result));
298  ensure(*result == *this);
299 
300  // Exit:
301 
302  return result;
303 }
304 
305 // PROTECTED MEMBER FUNCTIONS
306 
307 // PRIVATE MAMBER FUNCTIONS
308 
309 
310 // ===========================================================
311 // ITERATOR FACET
312 // ===========================================================
313 
314 // PUBLIC MEMBER FUNCTIONS
315 
316 void
319 {
320  // Preconditions:
321 
322  require(is_attached());
323  require(!is_done());
324 
325  // Body:
326 
327  define_old_variable(pod_type old_pod = pod());
328  define_old_variable(pod_type old_hub_pod = hub_pod());
329 
330  // Increment to the next entry.
331 
332  ++_pod;
333  if(_pod == _ct)
334  {
335  // The iteration is done.
336 
337  invalidate_ids();
338  }
339  else
340  {
341  ++_hub_pod;
342  }
343 
344  // Postconditions:
345 
346  ensure(invariant());
347  ensure(is_done() || pod() == (old_pod + 1));
348  ensure(is_done() || hub_pod() == (old_hub_pod + 1));
349 
350  // Exit:
351 
352  return;
353 }
354 
355 void
358 {
359  // Preconditions:
360 
361  require(is_attached());
362 
363  // Body:
364 
365  if(_ct == 0)
366  {
367  // The map is empty, the iteration is done.
368 
369  invalidate_ids();
370  }
371  else
372  {
373  // There's at least one entry, the iteration is not done.
374 
375  _is_done = false;
376 
377  _pod = 0;
378  _hub_pod = _offset;
379  }
380 
381  // Postconditions:
382 
383  ensure(invariant());
384 
385  // Exit:
386 
387  return;
388 }
389 
390 // PROTECTED MEMBER FUNCTIONS
391 
392 // PRIVATE MAMBER FUNCTIONS
393 
394 
395 // ===========================================================
396 // HANDLE FACET
397 // ===========================================================
398 
399 // PUBLIC MEMBER FUNCTIONS
400 
401 void
404 {
405  // Preconditions:
406 
407  require(xstate != 0);
408  require(conforms_to_state(xstate));
409 
410  // Body:
411 
412  // Assign the state information.
413 
414  _state = xstate;
415  _offset = reinterpret_cast<primary_index_space_state*>(_state)->offset();
416  _ct = xstate->ct();
417 
418  // Reset the iterator.
419 
420  reset();
421 
422  // Postconditions:
423 
424  ensure(invariant());
425  ensure(is_attached());
426  ensure(&state() == xstate);
427 
428  // Exit:
429 
430  return;
431 }
432 
433 bool
436 {
437  // Preconditions:
438 
439  // Body:
440 
441  bool result = (dynamic_cast<primary_index_space_state*>(xstate) != 0);
442 
443  // Postconditions:
444 
445  ensure(is_basic_query);
446 
447  // Exit:
448 
449  return result;
450 }
451 
452 // PROTECTED MEMBER FUNCTIONS
453 
454 // PRIVATE MAMBER FUNCTIONS
455 
456 
457 // ===========================================================
458 // ANY FACET
459 // ===========================================================
460 
461 // PUBLIC MEMBER FUNCTIONS
462 
463 bool
465 is_ancestor_of(const any *other) const
466 {
467  // Preconditions:
468 
469  require(other != 0);
470 
471  // Body:
472 
473  // True if other conforms to this
474 
475  bool result = dynamic_cast<const primary_index_space_iterator*>(other) != 0;
476 
477  // Postconditions:
478 
479  // Exit:
480 
481  return result;
482 }
483 
484 bool
486 invariant() const
487 {
488  bool result = true;
489 
490  if(invariant_check())
491  {
492  // Must satisfy base class invariant
493 
495 
496  // Prevent recursive calls to invariant
497 
499 
500  // Invariances for this class:
501 
502  // Finished, turn invariant checking back on.
503 
505  }
506 
507  // Exit
508 
509  return result;
510 }
511 
512 // PROTECTED MEMBER FUNCTIONS
513 
514 // PRIVATE MEMBER FUNCTIONS
515 
516 
517 // ===========================================================
518 // NON-MEMBER FUNCTIONS
519 // ===========================================================
pod_type offset() const
The offset into the hub id space.
virtual void next()
Makes item() the next id in the iteration.
virtual void reset()
Restarts the iteration.
size_type ct() const
The number of members.
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.
pod_type _offset
The offset into the hub id space.
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
virtual bool invariant() const
Class invariant.
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.
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).
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
virtual primary_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
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
A computed implementation class explicit_index_space_state that is used for the terms of the primary ...
size_type _ct
The number of entries to iterate over.
primary_index_space_iterator & operator=(const primary_index_space_iterator &xother)
Assignment operator.
size_type ct() const
The number of entries to iterate over.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
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.
pod_type _pod
The current id in the iteration.
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...
Factory and container for a family of id spaces.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
pod_index_type pod_type
The "plain old data" index type for this.
An iterator over a gathered id space in which the equivlence between the ids in the space and the hub...
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().