SheafSystem  0.0.0.0
array_implicit_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_implicit_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/array_index_space_interval.h"
25 #include "SheafSystem/index_space_family.h"
26 
27 // ===========================================================
28 // ARRAY_IMPLICIT_INDEX_SPACE_ITERATOR FACET
29 // ===========================================================
30 
31 // PUBLIC MEMBER FUNCTIONS
32 
35 {
36  // Preconditions:
37 
38  // Body:
39 
40  // Postconditions:
41 
42  ensure(invariant());
43  ensure(!is_attached());
44 
45  // Exit:
46 
47  return;
48 }
49 
52 {
53  // Preconditions:
54 
55  // Body:
56 
57  *this = xother;
58 
59  // Postconditions:
60 
61  ensure(invariant());
62  ensure((*this) == xother);
63 
64  // Exit:
65 
66  return;
67 }
68 
71  pod_type xindex)
72 {
73  // Preconditions:
74 
75  require(conforms_to_state(xid_spaces, xindex));
76 
77  // Body:
78 
79  attach_to(xid_spaces, xindex);
80 
81  // Postconditions:
82 
83  ensure(invariant());
84  ensure(is_attached());
85  ensure(&id_spaces() == &xid_spaces);
86  ensure(index() == xindex);
87 
88  // Exit:
89 
90  return;
91 }
92 
95  const std::string& xname)
96 {
97  // Preconditions:
98 
99  require(conforms_to_state(xid_spaces, xname));
100 
101  // Body:
102 
103  attach_to(xid_spaces, xname);
104 
105  // Postconditions:
106 
107  ensure(invariant());
108  ensure(is_attached());
109  ensure(&id_spaces() == &xid_spaces);
110  ensure(name() == xname);
111 
112  // Exit:
113 
114  return;
115 }
116 
120 {
121  // Preconditions:
122 
123  // Body:
124 
125  if(xother.is_attached())
126  {
127  _to_hub = xother._to_hub;
128  _end = xother._end;
129 
130  (void) implicit_index_space_iterator::operator=(xother);
131  }
132  else
133  {
134  detach();
135  }
136 
137  // Postconditions:
138 
139  ensure(invariant());
140  ensure((*this) == xother);
141 
142  // Exit
143 
144  return *this;
145 }
146 
149 {
150  // Preconditions:
151 
152  // Body:
153 
154  // Nothing to do;
155 
156  // Postconditions:
157 
158  // Exit:
159 
160  return;
161 }
162 
163 // PROTECTED MEMBER FUNCTIONS
164 
165 // PRIVATE MEMBER FUNCTIONS
166 
167 
168 // ===========================================================
169 // IMPLICIT_INDEX_SPACE_ITERATOR FACET
170 // ===========================================================
171 
172 // PUBLIC MEMBER FUNCTIONS
173 
174 // PROTECTED MEMBER FUNCTIONS
175 
176 // PRIVATE MAMBER FUNCTIONS
177 
178 
179 // ===========================================================
180 // INDEX_SPACE_ITERATOR FACET
181 // ===========================================================
182 
183 // PUBLIC MEMBER FUNCTIONS
184 
188 {
189  // Preconditions:
190 
191  require(is_ancestor_of(&xother));
192 
193  // Body:
194 
196  dynamic_cast<const array_implicit_index_space_iterator&>(xother);
197 
198  (void) this->operator=(lother);
199 
200  // Postconditions:
201 
202  ensure(invariant());
203  ensure((*this) == xother);
204 
205  // Exit
206 
207  return *this;
208 }
209 
210 bool
212 operator==(const index_space_iterator& xother) const
213 {
214  // Preconditions:
215 
216  require(is_ancestor_of(&xother));
217 
218  // Body:
219 
220  bool result = implicit_index_space_iterator::operator==(xother);
221  if(result && is_attached())
222  {
224  dynamic_cast<const array_implicit_index_space_iterator&>(xother);
225 
226  result = result && (_to_hub == lother._to_hub);
227  result = result && (_end == lother._end);
228  }
229 
230  // Postconditions:
231 
232  ensure(invariant());
233 
234  // Exit
235 
236  return result;
237 }
238 
241 clone() const
242 {
244 
245  // Preconditions:
246 
247  // Body:
248 
249  result = new array_implicit_index_space_iterator(*this);
250 
251  // Postconditions:
252 
253  ensure(result != 0);
254  ensure(is_same_type(result));
255  ensure(*result == *this);
256 
257  // Exit:
258 
259  return result;
260 }
261 
262 // PROTECTED MEMBER FUNCTIONS
263 
264 // PRIVATE MAMBER FUNCTIONS
265 
266 
267 // ===========================================================
268 // ITERATOR FACET
269 // ===========================================================
270 
271 // PUBLIC MEMBER FUNCTIONS
272 
273 void
276 {
277  // Preconditions:
278 
279  require(is_attached());
280  require(!is_done());
281 
282  // Body:
283 
284  define_old_variable(pod_type old_pod = pod());
285  define_old_variable(pod_type old_hub_pod = hub_pod());
286 
287  // Skip forward until we're done or we find a valid hub id.
288 
289  do
290  {
291  ++_pod;
292 
293  if(_pod == _end)
294  {
295  // Done with the iteration.
296 
297  invalidate_ids();
298  break;
299  }
300  }
301  while(!is_valid(_hub_pod = _to_hub[_pod]));
302 
303  // Postconditions:
304 
305  ensure(invariant());
306  ensure(is_done() || pod() != old_pod);
307  ensure(is_done() || hub_pod() != old_hub_pod);
308 
309  // Exit:
310 
311  return;
312 }
313 
314 void
317 {
318  // Preconditions:
319 
320  require(is_attached());
321 
322  // Body:
323 
324  _is_done = false;
325 
326  // Start at begin.
327 
328  _pod = 0;
329 
330  while(!is_valid(_hub_pod = _to_hub[_pod]))
331  {
332  _pod++;
333 
334  if(_pod == _end)
335  {
336  // Reached the end entry before finding a valid hub id;
337  // invalidate the ids and quit.
338 
339  invalidate_ids();
340  break;
341  }
342  }
343 
344  // Postconditions:
345 
346  ensure(invariant());
347 
348  // Exit:
349 
350  return;
351 }
352 
353 // PROTECTED MEMBER FUNCTIONS
354 
355 // PRIVATE MEMBER FUNCTIONS
356 
357 
358 // ===========================================================
359 // HANDLE FACET
360 // ===========================================================
361 
362 // PUBLIC MEMBER FUNCTIONS
363 
364 void
366 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
367 {
368  // Preconditions:
369 
370  require(xhost.contains(xlocal_id));
371  require(conforms_to_state(xhost, xlocal_id));
372  require(reinterpret_cast<const array_index_space_interval&>(xhost).ids_per_space() > 0);
373 
374  // Body:
375 
376  // Assign the state information.
377 
378  _host = &xhost;
379  _local_id = xlocal_id;
380 
381  // Find the section of the array that represents this id space.
382 
383  const array_index_space_interval* lhost =
384  reinterpret_cast<const array_index_space_interval*>(_host);
385 
386  _to_hub = &lhost->hub_ids()[_local_id * lhost->ids_per_space()];
387  _end = lhost->ids_per_space();
388 
389  // Reset the iterator.
390 
391  reset();
392 
393  // Postconditions:
394 
395  ensure(invariant());
396  ensure(is_attached());
397  ensure(&host() == &xhost);
398  ensure(index() == xhost.family_scope(xlocal_id));
399 
400  // Exit:
401 
402  return;
403 }
404 
405 bool
408  pod_type xlocal_id) const
409 {
410  // Preconditions:
411 
412  // Body:
413 
414  bool result = (dynamic_cast<const array_index_space_interval*>(&xhost) != 0);
415 
416  // Postconditions:
417 
418  ensure(is_basic_query);
419 
420  // Exit:
421 
422  return result;
423 }
424 
425 // PROTECTED MEMBER FUNCTIONS
426 
427 // PRIVATE MAMBER FUNCTIONS
428 
429 
430 // ===========================================================
431 // ANY FACET
432 // ===========================================================
433 
434 // PUBLIC MEMBER FUNCTIONS
435 
436 bool
438 is_ancestor_of(const any *other) const
439 {
440  // Preconditions:
441 
442  require(other != 0);
443 
444  // Body:
445 
446  // True if other conforms to this
447 
448  bool result = dynamic_cast<const array_implicit_index_space_iterator*>(other) != 0;
449 
450  // Postconditions:
451 
452  // Exit:
453 
454  return result;
455 }
456 
457 bool
459 invariant() const
460 {
461  bool result = true;
462 
463  if(invariant_check())
464  {
465  // Must satisfy base class invariant
466 
468 
469  // Prevent recursive calls to invariant
470 
472 
473  // Invariances for this class:
474 
475  // Finished, turn invariant checking back on.
476 
478  }
479 
480  // Exit
481 
482  return result;
483 }
484 
485 // PROTECTED MEMBER FUNCTIONS
486 
487 // PRIVATE MEMBER FUNCTIONS
488 
489 
490 // ===========================================================
491 // NON-MEMBER FUNCTIONS
492 // ===========================================================
493 
An abstract iterator over the ids of an id space.
bool _is_done
True if the iteration is finished.
const block< pod_type > & hub_ids() const
The array of hub ids.
pod_type pod() const
The current id in the iteration.
virtual pod_type family_scope(pod_type xlocal_id) const =0
Id relative to the id space family equivalent to the local id xlocal_id.
void invalidate_ids()
Set is_done() and invalidate pod() and hub_pod().
virtual const index_space_collection & host() const
The host collection.
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 pod_type index() const
Index of this space.
virtual void detach()
Detach this iterator form its state, if any.
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.
const index_space_family & id_spaces() const
The id space family for this (const version).
An implementation of implicit_index_space_iterator for an implicit id space in an array_index_space_i...
virtual void attach_to(const index_space_collection &xhost, pod_type xlocal_id)
Attach to the state with local scope id, xlocal_id in the host id space collection xhost...
size_type ids_per_space() const
The number of ids per id space.
pod_type _hub_pod
The current hub id in the iteration.
A collection of id space states. This is a virtual class with provides an interface for accessing the...
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 contains(pod_type xlocal_id) const =0
True if this collection contains the id space with local id xlocal_id.
virtual bool invariant() const
Class invariant.
virtual bool is_attached() const
True if this iterator is attached to a state.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
array_implicit_index_space_iterator & operator=(const array_implicit_index_space_iterator &xother)
Assignment operator.
const index_space_collection * _host
The host collection.
pod_type _local_id
The id of this space in the local host id space.
pod_type _pod
The current id in the iteration.
virtual bool conforms_to_state(const index_space_collection &xhost, pod_type xlocal_id) const
True if this conforms to the handle type required by the state with local scope id xlocal_id in the h...
Factory and container for a family of id spaces.
virtual array_implicit_index_space_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this. If the iterator is attached...
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
virtual bool operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
An implementation of index_space_interval for an interval of implicit id spaces in which the hub ids ...
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
virtual void next()
Makes item() the next id in the iteration.