SheafSystem  0.0.0.0
singleton_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/singleton_implicit_index_space_iterator.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/singleton_index_space_interval.h"
25 #include "SheafSystem/index_space_family.h"
26 
27 // ===========================================================
28 // SINGLETON_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  _hub_begin = xother._hub_begin;
128 
129  (void) implicit_index_space_iterator::operator=(xother);
130  }
131  else
132  {
133  detach();
134  }
135 
136  // Postconditions:
137 
138  ensure(invariant());
139  ensure((*this) == xother);
140 
141  // Exit
142 
143  return *this;
144 }
145 
148 {
149  // Preconditions:
150 
151  // Body:
152 
153  // Nothing to do;
154 
155  // Postconditions:
156 
157  // Exit:
158 
159  return;
160 }
161 
162 // PROTECTED MEMBER FUNCTIONS
163 
164 // PRIVATE MEMBER FUNCTIONS
165 
166 
167 // ===========================================================
168 // IMPLICIT_INDEX_SPACE_ITERATOR FACET
169 // ===========================================================
170 
171 // PUBLIC MEMBER FUNCTIONS
172 
173 // PROTECTED MEMBER FUNCTIONS
174 
175 // PRIVATE MAMBER FUNCTIONS
176 
177 
178 // ===========================================================
179 // INDEX_SPACE_ITERATOR FACET
180 // ===========================================================
181 
182 // PUBLIC MEMBER FUNCTIONS
183 
187 {
188  // Preconditions:
189 
190  require(is_ancestor_of(&xother));
191 
192  // Body:
193 
195  dynamic_cast<const singleton_implicit_index_space_iterator&>(xother);
196 
197  (void) this->operator=(lother);
198 
199  // Postconditions:
200 
201  ensure(invariant());
202  ensure((*this) == xother);
203 
204  // Exit
205 
206  return *this;
207 }
208 
209 bool
211 operator==(const index_space_iterator& xother) const
212 {
213  // Preconditions:
214 
215  require(is_ancestor_of(&xother));
216 
217  // Body:
218 
219  bool result = implicit_index_space_iterator::operator==(xother);
220  if(result && is_attached())
221  {
223  dynamic_cast<const singleton_implicit_index_space_iterator&>(xother);
224 
225  result = result && (_hub_begin == lother._hub_begin);
226  }
227 
228  // Postconditions:
229 
230  ensure(invariant());
231 
232  // Exit
233 
234  return result;
235 }
236 
239 clone() const
240 {
242 
243  // Preconditions:
244 
245  // Body:
246 
247  result = new singleton_implicit_index_space_iterator(*this);
248 
249  // Postconditions:
250 
251  ensure(result != 0);
252  ensure(is_same_type(result));
253  ensure(*result == *this);
254 
255  // Exit:
256 
257  return result;
258 }
259 
260 // PROTECTED MEMBER FUNCTIONS
261 
262 // PRIVATE MAMBER FUNCTIONS
263 
264 
265 // ===========================================================
266 // ITERATOR FACET
267 // ===========================================================
268 
269 // PUBLIC MEMBER FUNCTIONS
270 
271 void
274 {
275  // Preconditions:
276 
277  require(is_attached());
278  require(!is_done());
279 
280  // Body:
281 
282  define_old_variable(pod_type old_pod = pod());
283  define_old_variable(pod_type old_hub_pod = hub_pod());
284 
285  // Only one entry, the iteration is done.
286 
287  invalidate_ids();
288 
289  // Postconditions:
290 
291  ensure(invariant());
292  ensure(is_done() || pod() != old_pod);
293  ensure(is_done() || hub_pod() != old_hub_pod);
294 
295  // Exit:
296 
297  return;
298 }
299 
300 void
303 {
304  // Preconditions:
305 
306  require(is_attached());
307 
308  // Body:
309 
310  _is_done = false;
311 
312  // Start at begin.
313 
314  _pod = 0;
316 
317  // Postconditions:
318 
319  ensure(invariant());
320 
321  // Exit:
322 
323  return;
324 }
325 
326 // PROTECTED MEMBER FUNCTIONS
327 
328 // PRIVATE MEMBER FUNCTIONS
329 
330 
331 // ===========================================================
332 // HANDLE FACET
333 // ===========================================================
334 
335 // PUBLIC MEMBER FUNCTIONS
336 
337 void
339 attach_to(const index_space_collection& xhost, pod_type xlocal_id)
340 {
341  // Preconditions:
342 
343  require(xhost.contains(xlocal_id));
344  require(conforms_to_state(xhost, xlocal_id));
345 
346  // Body:
347 
348  // Assign the state information.
349 
350  _host = &xhost;
351  _local_id = xlocal_id;
352 
353  // Find the section of the singleton that represents this id space.
354 
355  const singleton_index_space_interval* lhost =
356  reinterpret_cast<const singleton_index_space_interval*>(_host);
357 
358  _hub_begin = xlocal_id + lhost->hub_offset();
359 
360  // Reset the iterator.
361 
362  reset();
363 
364  // Postconditions:
365 
366  ensure(invariant());
367  ensure(is_attached());
368  ensure(&host() == &xhost);
369  ensure(index() == xhost.family_scope(xlocal_id));
370 
371  // Exit:
372 
373  return;
374 }
375 
376 bool
379  pod_type xlocal_id) const
380 {
381  // Preconditions:
382 
383  // Body:
384 
385  bool result = (dynamic_cast<const singleton_index_space_interval*>(&xhost) != 0);
386 
387  // Postconditions:
388 
389  ensure(is_basic_query);
390 
391  // Exit:
392 
393  return result;
394 }
395 
396 // PROTECTED MEMBER FUNCTIONS
397 
398 // PRIVATE MAMBER FUNCTIONS
399 
400 
401 // ===========================================================
402 // ANY FACET
403 // ===========================================================
404 
405 // PUBLIC MEMBER FUNCTIONS
406 
407 bool
409 is_ancestor_of(const any *other) const
410 {
411  // Preconditions:
412 
413  require(other != 0);
414 
415  // Body:
416 
417  // True if other conforms to this
418 
419  bool result = dynamic_cast<const singleton_implicit_index_space_iterator*>(other) != 0;
420 
421  // Postconditions:
422 
423  // Exit:
424 
425  return result;
426 }
427 
428 bool
430 invariant() const
431 {
432  bool result = true;
433 
434  if(invariant_check())
435  {
436  // Must satisfy base class invariant
437 
439 
440  // Prevent recursive calls to invariant
441 
443 
444  // Invariances for this class:
445 
446  // Finished, turn invariant checking back on.
447 
449  }
450 
451  // Exit
452 
453  return result;
454 }
455 
456 // PROTECTED MEMBER FUNCTIONS
457 
458 // PRIVATE MEMBER FUNCTIONS
459 
460 
461 // ===========================================================
462 // NON-MEMBER FUNCTIONS
463 // ===========================================================
464 
An abstract iterator over the ids of an id space.
An implementation of index_space_interval for an interval of implicit id spaces with a single id and ...
bool _is_done
True if the iteration is finished.
pod_type hub_offset() const
The offset from local id to hub id.
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.
virtual singleton_implicit_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 operator==(const index_space_iterator &xother) const
True if this is equivalent to xother.
virtual const index_space_collection & host() const
The host collection.
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.
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...
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
const index_space_family & id_spaces() const
The id space family for this (const version).
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.
singleton_implicit_index_space_iterator & operator=(const singleton_implicit_index_space_iterator &xother)
Assignment operator.
An implementation of implicit_index_space_iterator for an implicit id space in an singleton_index_spa...
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
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 void next()
Makes item() the next 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.
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
pod_type hub_pod() const
The current unglued hub id in the iteration. synonym for unglued_hub_pod().
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...