SheafSystem  0.0.0.0
implicit_entry_map_iterator.impl.h
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 #ifndef IMPLICIT_ENTRY_MAP_ITERATOR_IMPL_H
22 #define IMPLICIT_ENTRY_MAP_ITERATOR_IMPL_H
23 
24 #ifndef IMPLICIT_ENTRY_MAP_ITERATOR_H
25 #include "SheafSystem/implicit_entry_map_iterator.h"
26 #endif
27 
28 #ifndef ASSERT_CONTRACT_H
29 #include "SheafSystem/assert_contract.h"
30 #endif
31 
32 #ifndef IMPLICIT_ENTRY_MAP_H
33 #include "SheafSystem/implicit_entry_map.h"
34 #endif
35 
36 #ifndef RC_PTR_H
37 #include "SheafSystem/rc_ptr.h"
38 #endif
39 
40 namespace sheaf
41 {
42 
43 // =============================================================================
44 // IMPLICIT_ENTRY_MAP_ITERATOR FACET
45 // =============================================================================
46 
47 // PUBLIC MEMBER FUNCTIONS
48 
49 template <typename E, typename I>
52 {
53  // Preconditions:
54 
55  // Body:
56 
57  _map = &xmap;
58  reset();
59 
60  // Postconditions:
61 
62  // Exit:
63 
64  return;
65 };
66 
67 template <typename E, typename I>
70 {
71  // Preconditions:
72 
73  // Body:
74 
75  // Nothing to do.
76 
77  // Postconditions:
78 
79  // Exit:
80 
81  return;
82 };
83 
84 template <typename E, typename I>
85 E&
88 {
89  // Preconditions:
90 
91  require(!is_done());
92 
93  // Body:
94 
95  if(_item == 0)
96  {
97  // The first time the implicit value was requested, use the
98  // map to construct it.
99 
100  _item = &_map->value(_id);
101  }
102 
103  E& result = *_item;
104 
105  // Postconditions:
106 
107  // Exit:
108 
109  return result;
110 };
111 
112 template <typename E, typename I>
113 const E&
115 item() const
116 {
117  // Preconditions:
118 
119  require(!is_done());
120 
121  // Body:
122 
123  if(_item == 0)
124  {
125  // The first time the implicit value was requested, use the
126  // map to construct it.
127 
128  _item = &_map->value(_id);
129  }
130 
131  const E& result = *_item;
132 
133  // Postconditions:
134 
135  // Exit:
136 
137  return result;
138 };
139 
140 template <typename E, typename I>
143 id() const
144 {
145  // Preconditions:
146 
147  require(!is_done());
148 
149  // Body:
150 
151  pod_type result = _id;
152 
153  // Postconditions:
154 
155  // Exit:
156 
157  return result;
158 };
159 
160 template <typename E, typename I>
161 void
164 {
165  // Preconditions:
166 
167  require(!is_done());
168 
169  // Body:
170 
171  if(_explicit_value_itr != _map->_explicit_value_map.end())
172  {
173  // Still iterating over the explicit values.
174 
175  _id = _explicit_value_itr->first;
176  _item = &**_explicit_value_itr->second;
177 
178  _explicit_value_itr++;
179  }
180  else if(_interval_itr != _map->_interval_map.end())
181  {
182  // Find the next implicit value that is not an explicit value.
183  // Explicit values trumps implicit values.
184 
185  do
186  {
187  if(is_valid(_end))
188  {
189  // Already in an interval, increment to the next id.
190 
191  ++_id;
192 
193  // Check if the interval has been deleted.
194 
198 
199  if(!_map->contains_implicit_entry(_id))
200  {
201  // The interval has been deleted, move to the end and continue.
202 
203  _id = _end;
204  }
205  }
206  else
207  {
208  // Starting with the first interval.
209 
210  assertion(_interval_itr->second == 0);
211 
212  _id = _interval_itr->first;
213  _interval_itr++;
214 
215  // $$SCRIBBLE: There should not be a hole at the end or
216  // back-to-back holes. This should be enforced by the delete
217  // operation in implicit_entry_map.
218 
219  assertion(_interval_itr != _map->_interval_map.end());
220  assertion(_interval_itr->second != 0);
221 
222  _end = _interval_itr->first;
223  }
224 
225  if(_id == _end)
226  {
227  // Move to the next interval.
228 
229  _interval_itr++;
230 
231  if(_interval_itr != _map->_interval_map.end())
232  {
233  if(_interval_itr->second == 0)
234  {
235  // There is a hole in the interval map, start iterating
236  // from the end of the hole.
237 
238  _id = _interval_itr->first;
239  _interval_itr++;
240 
241  // $$SCRIBBLE: There should not be a hole at the end or
242  // back-to-back holes. This should be enforced by the delete
243  // operation in implicit_entry_map.
244 
245  assertion(_interval_itr != _map->_interval_map.end());
246  assertion(_interval_itr->second != 0);
247  }
248 
249  // Set the end of the current interval.
250 
251  _end = _interval_itr->first;
252  }
253  else
254  {
255  // No more intervals to iterate over, the iteration is done.
256 
257  _id = invalid_pod_index();
258  }
259  }
260  }
261  while(is_valid(_id) && _map->contains_explicit_entry(_id));
262 
263  // Void out the item.
264 
265  _item = 0;
266  }
267  else
268  {
269  // Reached the end of the explicit values and there are no implicit
270  // values, the iteration is done.
271 
272  _id = invalid_pod_index();
273  }
274 
275  // Postconditions:
276 
277  // Exit:
278 
279  return;
280 };
281 
282 template <typename E, typename I>
283 bool
285 is_done() const
286 {
287  // Preconditions:
288 
289  // Body:
290 
291  bool result = !is_valid(_id);
292 
293  // Postconditions:
294 
295  // Exit:
296 
297  return result;
298 };
299 
300 template <typename E, typename I>
301 void
304 {
305  // Preconditions:
306 
307  // Body:
308 
309  _id = 0; // Needs to be some valid value so the precondition of next() passes.
310  _end = invalid_pod_index();
311 
312  _explicit_value_itr = _map->_explicit_value_map.begin();
313  _interval_itr = _map->_interval_map.begin();
314 
315  next();
316 
317  // Postconditions:
318 
319  ensure(invariant());
320 
321  // Exit:
322 
323  return;
324 };
325 
326 // PROTECTED MEMBER FUNCTIONS
327 
328 template <typename E, typename I>
331 {
332  // Preconditions:
333 
334  // Body:
335 
336  *this = xother;
337 
338  // Postconditions:
339 
340  // Exit:
341 
342  return;
343 };
344 
345 // PRIVATE MEMBER FUNCTIONS
346 
347 
348 // =============================================================================
349 // ANY FACET
350 // =============================================================================
351 
352 // PUBLIC MEMBER FUNCTIONS
353 
354 template <typename E, typename I>
355 bool
357 is_ancestor_of(const any* other) const
358 {
359  // Preconditions:
360 
361  require(other != 0);
362 
363  // Body:
364 
365  bool result = dynamic_cast< const implicit_entry_map_iterator<E, I>* >(other) != 0;
366 
367  // Postconditions:
368 
369  // Exit:
370 
371  return result;
372 };
373 
374 template <typename E, typename I>
377 clone() const
378 {
379  // Preconditions:
380 
381  // Body:
382 
384 
385  // Postconditions:
386 
387  ensure(result != 0);
388  ensure(is_same_type(result));
389 
390  // Exit:
391 
392  return result;
393 };
394 
395 template <typename E, typename I>
399 {
400  // Preconditions:
401 
402  // Body:
403 
404  _map = xother._map;
405  _id = xother._id;
406  _item = xother._item;
407  _explicit_value_itr = xother._explicit_value_itr;
408  _interval_itr = xother._interval_itr;
409  _end = xother._end;
410 
411  // Postconditions:
412 
413  ensure(invariant());
414 
415  // Exit:
416 
417  return *this;
418 };
419 
420 template <typename E, typename I>
421 bool
423 invariant() const
424 {
425  bool result = true;
426 
427  if(invariant_check())
428  {
429  // Must satisfy base class invariant
430 
431  invariance(any::invariant());
432 
433  // Prevent recursive calls to invariant
434 
435  disable_invariant_check();
436 
437  // Invariances for this class:
438 
440 
441  // Finished, turn invariant checking back on.
442 
443  enable_invariant_check();
444  }
445 
446  // Exit
447 
448  return result;
449 };
450 
451 // PROTECTED MEMBER FUNCTIONS
452 
453 // PRIVATE MEMBER FUNCTIONS
454 
455 
456 // =============================================================================
457 // NON-MEMBER FUNCTIONS
458 // =============================================================================
459 
460 } // namespace sheaf
461 
462 #endif // ifndef IMPLICIT_ENTRY_MAP_IMPL_H
463 
464 
465 
466 
467 
468 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
void next()
Makes item() the next integer in the iteration.
void reset()
Restarts the iteration.
Abstract base class with useful features for all objects.
Definition: any.h:39
A map in which the entries may be implicit.
implicit_entry_map_iterator()
Default constructor; disabled.
~implicit_entry_map_iterator()
Destructor.
An iterator over the entries in an implicit_entry_map. This iteration is NOT order preserving...
E & item()
The current value of the iteration (mutable version).
virtual implicit_entry_map_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this.
pod_type id() const
The current id of the value of the iteration.
implicit_entry_map_iterator & operator=(const implicit_entry_map_iterator &xother)
Assignment operator.
bool is_done() const
True if iteration is finished.
Namespace for the sheaves component of the sheaf system.
virtual bool invariant() const
Class invariant.
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
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
implicit_entry_map< E, I >::pod_type pod_type
The "plain old data" index type for this.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.