SheafSystem  0.0.0.0
array_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/array_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/array_index_space_state.h"
27 
28 // ===========================================================
29 // ARRAY_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  _end = xother._end;
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 array_index_space_iterator& lother =
197  dynamic_cast<const array_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 array_index_space_iterator& lother =
225  dynamic_cast<const array_index_space_iterator&>(xother);
226 
227  result = result && (_to_hub == lother._to_hub);
228  result = result && (_end == lother._end);
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 array_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  // Skip forward until we're done or we find a valid hub id.
289 
290  do
291  {
292  ++_pod;
293 
294  if(_pod == _end)
295  {
296  // Done with the iteration.
297 
298  invalidate_ids();
299  break;
300  }
301  }
302  while(!is_valid(_hub_pod = _to_hub->item(_pod)));
303 
304  // Postconditions:
305 
306  ensure(invariant());
307  ensure(is_done() || pod() != old_pod);
308  ensure(is_done() || hub_pod() != old_hub_pod);
309 
310  // Exit:
311 
312  return;
313 }
314 
315 void
318 {
319  // Preconditions:
320 
321  require(is_attached());
322 
323  // Body:
324 
325  if(!is_valid(_end) || (_end == 0))
326  {
327  // The map is empty, we're done.
328 
329  invalidate_ids();
330  }
331  else
332  {
333  // There's at least one entry, we're not done.
334 
335  _is_done = false;
336 
337  // Start at begin.
338 
339  _pod = 0;
340 
341  while(!is_valid(_hub_pod = _to_hub->item(_pod)))
342  {
343  _pod++;
344 
345  if(_pod == _end)
346  {
347  // Reached the end entry before finding a valid hub id;
348  // invalidate the ids and quit.
349 
350  invalidate_ids();
351  break;
352  }
353  }
354  }
355 
356  // Postconditions:
357 
358  ensure(invariant());
359 
360  // Exit:
361 
362  return;
363 }
364 
365 // PROTECTED MEMBER FUNCTIONS
366 
367 // PRIVATE MEMBER FUNCTIONS
368 
369 
370 // ===========================================================
371 // HANDLE FACET
372 // ===========================================================
373 
374 // PUBLIC MEMBER FUNCTIONS
375 
376 void
379 {
380  // Preconditions:
381 
382  require(xstate != 0);
383  require(conforms_to_state(xstate));
384 
385  // Body:
386 
387  // Assign the state information.
388 
389  _state = xstate;
390  _to_hub = &reinterpret_cast<array_index_space_state*>(_state)->_to_range;
391  _end = xstate->end();
392 
393  // Reset the iterator.
394 
395  reset();
396 
397  // Postconditions:
398 
399  ensure(invariant());
400  ensure(is_attached());
401  ensure(&state() == xstate);
402  ensure(!is_done() ? pod() >= 0 : true);
403  ensure(!is_done() ? is_valid(hub_pod()) : true);
404 
405  // Exit:
406 
407  return;
408 }
409 
410 bool
413 {
414  // Preconditions:
415 
416  // Body:
417 
418  bool result = (dynamic_cast<array_index_space_state*>(xstate) != 0);
419 
420  // Postconditions:
421 
422  ensure(is_basic_query);
423 
424  // Exit:
425 
426  return result;
427 }
428 
429 // PROTECTED MEMBER FUNCTIONS
430 
431 // PRIVATE MAMBER FUNCTIONS
432 
433 
434 // ===========================================================
435 // ANY FACET
436 // ===========================================================
437 
438 // PUBLIC MEMBER FUNCTIONS
439 
440 bool
442 is_ancestor_of(const any *other) const
443 {
444  // Preconditions:
445 
446  require(other != 0);
447 
448  // Body:
449 
450  // True if other conforms to this
451 
452  bool result = dynamic_cast<const array_index_space_iterator*>(other) != 0;
453 
454  // Postconditions:
455 
456  // Exit:
457 
458  return result;
459 }
460 
461 bool
463 invariant() const
464 {
465  bool result = true;
466 
467  if(invariant_check())
468  {
469  // Must satisfy base class invariant
470 
472 
473  // Prevent recursive calls to invariant
474 
476 
477  // Invariances for this class:
478 
479  // Finished, turn invariant checking back on.
480 
482  }
483 
484  // Exit
485 
486  return result;
487 }
488 
489 // PROTECTED MEMBER FUNCTIONS
490 
491 // PRIVATE MEMBER FUNCTIONS
492 
493 
494 // ===========================================================
495 // NON-MEMBER FUNCTIONS
496 // ===========================================================
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
const to_hub_type * _to_hub
The array to hub ids.
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.
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
pod_type end() const
Ending id of this space.
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 invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
An array implementation of class scattered_insertion_index_space_state. This representation is intend...
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 _end
The end of this id space.
virtual void reset()
Restarts the iteration.
array_index_space_iterator & operator=(const array_index_space_iterator &xother)
Assignment operator.
reference_type item(index_type xindex) const
The item at index xindex.
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 array_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual bool invariant() const
Class invariant.
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 next()
Makes item() the next id in the iteration.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
An iterator over an id space in which the equivalence between the ids in the space and the hub id spa...
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.
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().