SheafSystem  0.0.0.0
hash_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/hash_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/hash_index_space_state.h"
27 
28 // ===========================================================
29 // HASH_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 
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 {
75  // Preconditions:
76 
77  require(conforms_to_state(xid_spaces, xindex));
78 
79  // Body:
80 
81  attach_to(xid_spaces, xindex);
82 
83  // Postconditions:
84 
85  ensure(invariant());
86  ensure(is_attached());
87  ensure(&id_spaces() == &xid_spaces);
88  ensure(index() == xindex);
89 
90  // Exit:
91 
92  return;
93 }
94 
97  const std::string& xname)
98 {
99  // Preconditions:
100 
101  require(conforms_to_state(xid_spaces, xname));
102 
103  // Body:
104 
105  attach_to(xid_spaces, xname);
106 
107  // Postconditions:
108 
109  ensure(invariant());
110  ensure(is_attached());
111  ensure(&id_spaces() == &xid_spaces);
112  ensure(name() == xname);
113 
114  // Exit:
115 
116  return;
117 }
118 
122 {
123  // Preconditions:
124 
125  // Body:
126 
127  if(xother.is_attached())
128  {
129  _to_hub = xother._to_hub;
130  _itr = xother._itr;
131 
132  (void) explicit_index_space_iterator::operator=(xother);
133  }
134  else
135  {
136  detach();
137  }
138 
139  // Postconditions:
140 
141  ensure(invariant());
142  ensure((*this) == xother);
143 
144  // Exit
145 
146  return *this;
147 }
148 
151 {
152  // Preconditions:
153 
154  // Body:
155 
156  // Nothing to do;
157 
158  // Postconditions:
159 
160  // Exit:
161 
162  return;
163 }
164 
165 // PROTECTED MEMBER FUNCTIONS
166 
167 // PRIVATE MEMBER FUNCTIONS
168 
169 
170 // ===========================================================
171 // EXPLICIT_INDEX_SPACE_ITERATOR FACET
172 // ===========================================================
173 
174 // PUBLIC MEMBER FUNCTIONS
175 
176 // PROTECTED MEMBER FUNCTIONS
177 
178 // PRIVATE MAMBER FUNCTIONS
179 
180 
181 // ===========================================================
182 // INDEX_SPACE_ITERATOR FACET
183 // ===========================================================
184 
185 // PUBLIC MEMBER FUNCTIONS
186 
190 {
191  // Preconditions:
192 
193  require(is_ancestor_of(&xother));
194 
195  // Body:
196 
197  const hash_index_space_iterator& lother =
198  dynamic_cast<const hash_index_space_iterator&>(xother);
199 
200  (void) this->operator=(lother);
201 
202  // Postconditions:
203 
204  ensure(invariant());
205  ensure((*this) == xother);
206 
207  // Exit
208 
209  return *this;
210 }
211 
212 bool
214 operator==(const index_space_iterator& xother) const
215 {
216  // Preconditions:
217 
218  require(is_ancestor_of(&xother));
219 
220  // Body:
221 
222  bool result = explicit_index_space_iterator::operator==(xother);
223  if(result && is_attached())
224  {
225  const hash_index_space_iterator& lother =
226  dynamic_cast<const hash_index_space_iterator&>(xother);
227 
228  result = result && (_to_hub == lother._to_hub);
229  result = result && (_itr == lother._itr);
230  }
231 
232  // Postconditions:
233 
234  ensure(invariant());
235 
236  // Exit
237 
238  return result;
239 }
240 
243 clone() const
244 {
246 
247  // Preconditions:
248 
249  // Body:
250 
251  result = new hash_index_space_iterator(*this);
252 
253  // Postconditions:
254 
255  ensure(result != 0);
256  ensure(is_same_type(result));
257  ensure(*result == *this);
258 
259  // Exit:
260 
261  return result;
262 }
263 
264 // PROTECTED MEMBER FUNCTIONS
265 
266 // PRIVATE MAMBER FUNCTIONS
267 
268 
269 // ===========================================================
270 // ITERATOR FACET
271 // ===========================================================
272 
273 // PUBLIC MEMBER FUNCTIONS
274 
275 void
278 {
279  // Preconditions:
280 
281  require(is_attached());
282  require(!is_done());
283 
284  // Body:
285 
286  define_old_variable(pod_type old_pod = pod());
287  define_old_variable(pod_type old_hub_pod = hub_pod());
288 
289  // Move to the next id.
290 
291  ++_itr;
292 
293  if(_itr != _to_hub->end())
294  {
295  // Found another entry in the map, assign pod values.
296 
297  _pod = _itr->first;
298  _hub_pod = _itr->second;
299  }
300  else
301  {
302  // Done with the iteration.
303 
304  invalidate_ids();
305  }
306 
307  // Postconditions:
308 
309  ensure(invariant());
310  ensure(is_done() || pod() != old_pod);
311  ensure(is_done() || hub_pod() != old_hub_pod);
312 
313  // Exit:
314 
315  return;
316 }
317 
318 void
321 {
322  // Preconditions:
323 
324  require(is_attached());
325 
326  // Body:
327 
328  _itr = _to_hub->begin();
329 
330  if(_itr != _to_hub->end())
331  {
332  // There's at least one entry, we're not done.
333 
334  _is_done = false;
335 
336  // Assign pod values.
337 
338  _pod = _itr->first;
339  _hub_pod = _itr->second;
340  }
341  else
342  {
343  // The map is empty, we're done.
344 
345  invalidate_ids();
346  }
347 
348  // Postconditions:
349 
350  ensure(invariant());
351 
352  // Exit:
353 
354  return;
355 }
356 
357 // PROTECTED MEMBER FUNCTIONS
358 
359 // PRIVATE MEMBER FUNCTIONS
360 
361 
362 // ===========================================================
363 // HANDLE FACET
364 // ===========================================================
365 
366 // PUBLIC MEMBER FUNCTIONS
367 
368 void
371 {
372  // Preconditions:
373 
374  require(xstate != 0);
375  require(conforms_to_state(xstate));
376 
377  // Body:
378 
379  // Assign the state information.
380 
381  _state = xstate;
382  _to_hub = &reinterpret_cast<hash_index_space_state*>(_state)->_to_range;
383 
384  // Reset the iterator.
385 
386  reset();
387 
388  // Postconditions:
389 
390  ensure(invariant());
391  ensure(is_attached());
392  ensure(&state() == xstate);
393  ensure(!is_done() ? pod() >= 0 : true);
394  ensure(!is_done() ? is_valid(hub_pod()) : true);
395 
396  // Exit:
397 
398  return;
399 }
400 
401 bool
404 {
405  // Preconditions:
406 
407  // Body:
408 
409  bool result = (dynamic_cast<hash_index_space_state*>(xstate) != 0);
410 
411  // Postconditions:
412 
413  ensure(is_basic_query);
414 
415  // Exit:
416 
417  return result;
418 }
419 
420 // PROTECTED MEMBER FUNCTIONS
421 
422 // PRIVATE MAMBER FUNCTIONS
423 
424 
425 // ===========================================================
426 // ANY FACET
427 // ===========================================================
428 
429 // PUBLIC MEMBER FUNCTIONS
430 
431 bool
433 is_ancestor_of(const any *other) const
434 {
435  // Preconditions:
436 
437  require(other != 0);
438 
439  // Body:
440 
441  // True if other conforms to this
442 
443  bool result = dynamic_cast<const hash_index_space_iterator*>(other) != 0;
444 
445  // Postconditions:
446 
447  // Exit:
448 
449  return result;
450 }
451 
452 bool
454 invariant() const
455 {
456  bool result = true;
457 
458  if(invariant_check())
459  {
460  // Must satisfy base class invariant
461 
463 
464  // Prevent recursive calls to invariant
465 
467 
468  // Invariances for this class:
469 
470  // Finished, turn invariant checking back on.
471 
473  }
474 
475  // Exit
476 
477  return result;
478 }
479 
480 // PROTECTED MEMBER FUNCTIONS
481 
482 // PRIVATE MEMBER FUNCTIONS
483 
484 
485 // ===========================================================
486 // NON-MEMBER FUNCTIONS
487 // ===========================================================
virtual bool invariant() const
Class invariant.
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 invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
virtual void attach_to(explicit_index_space_state *xstate)
Attach to the explicit id space state xstate.
explicit_index_space_state & state()
The explicit state (mutable version).
to_hub_type::const_iterator _itr
The stl iterator over ids.
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 bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
pod_type _hub_pod
The current hub id in the iteration.
virtual void next()
Makes item() the next 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
An hash map implementation of class scattered_insertion_index_space_state. This representation is int...
hash_index_space_iterator & operator=(const hash_index_space_iterator &xother)
Assignment operator.
virtual hash_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
hash_index_space_iterator()
Default constructor.
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...
An implementation of class index_space_iterator for an explicit_index_space_state.
explicit_index_space_state * _state
The explicit state.
virtual bool invariant() const
Class invariant.
An iterator over an id space in which the equivalence between the ids in the space and the hub id spa...
virtual void detach()
Detach this iterator form its state, if any.
virtual void reset()
Restarts the iteration.
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().
const to_hub_type * _to_hub
The hash to hub ids.