SheafSystem  0.0.0.0
interval_set_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 //
19 
20 #include "SheafSystem/interval_set_iterator.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 
24 // ===========================================================
25 // INTERVAL_SET_ITERATOR FACET
26 // ===========================================================
27 
28 // PUBLIC MEMBER FUNCTIONS
29 
31 interval_set_iterator(const interval_set& xset, bool xis_member_iterator)
32 {
33  // Preconditions:
34 
35  // Body:
36 
37  _set = &xset;
38  _map = &xset._interval_map;
39  _is_member_iterator = xis_member_iterator;
40 
41  reset();
42 
43  // Postconditions:
44 
45  ensure(invariant());
46 
47  // Exit:
48 
49  return;
50 }
51 
54 {
55  // Preconditions:
56 
57  // Body:
58 
59  // Nothing to do;
60 
61  // Postconditions:
62 
63  // Exit:
64 
65  return;
66 }
67 
70 set() const
71 {
72  return *_set;
73 }
74 
75 bool
78 {
79  return _is_member_iterator;
80 }
81 
84 item() const
85 {
86  // Preconditions:
87 
88  require(!is_done());
89 
90  // Body:
91 
92  // Postconditions:
93 
94  // Exit:
95 
96  return _item;
97 }
98 
99 void
102 {
103  // Preconditions:
104 
105  require(!is_done());
106 
107  // Body:
108 
109  ++_item;
110 
111  if(_item > _ub)
112  {
113  ++_map_itr;
114 
115  if(_map_itr != _map->end())
116  {
117  next_interval();
118  }
119  else
120  {
123  }
124  }
125 
126  // Postconditions:
127 
128  ensure(invariant());
129 
130  // Exit:
131 
132  return;
133 }
134 
135 bool
137 is_done() const
138 {
139  bool result;
140 
141  // Preconditions:
142 
143  // Body:
144 
145  result = (_map_itr == _map->end());
146 
147  // Postconditions:
148 
149  // Exit:
150 
151  return result;
152 }
153 
154 void
157 {
158  // Preconditions:
159 
160  // Body:
161 
162  _map_itr = _map->begin();
163 
164  if(_map_itr != _map->end())
165  {
167  {
168  ++_map_itr;
169  }
170 
171  next_interval();
172  }
173 
174  // Postconditions:
175 
176  ensure(invariant());
177 
178  // Exit:
179 
180  return;
181 }
182 
183 // PROTECTED MEMBER FUNCTIONS
184 
187 {
188  // Preconditions:
189 
190  // Body:
191 
192  *this = xother;
193 
194  // Postconditions:
195 
196  ensure(invariant());
197 
198  // Exit:
199 
200  return;
201 }
202 
203 void
206 {
207  // Preconditions:
208 
209  require(!is_done());
210 
211  // Body:
212 
213  _item = _map_itr->first+1;
214  ++_map_itr;
215 
216  if(_map_itr != _map->end())
217  {
218  _ub = _map_itr->first;
219  }
220  else
221  {
224  }
225 
226  // Postconditions:
227 
228  ensure(is_done() || is_member_iterator() == is_member_interval());
229 
230  // Exit:
231 
232  return;
233 }
234 
235 bool
238 {
239  return (_map_itr->second);
240 }
241 
242 // PRIVATE MEMBER FUNCTIONS
243 
244 
245 // ===========================================================
246 // ANY FACET
247 // ===========================================================
248 
249 // PUBLIC MEMBER FUNCTIONS
250 
251 bool
253 is_ancestor_of(const any *other) const
254 {
255  // Preconditions:
256 
257  require(other != 0);
258 
259  // Body:
260 
261  // True if other conforms to this
262 
263  bool result = dynamic_cast<const interval_set_iterator*>(other) != 0;
264 
265  // Postconditions:
266 
267  // Exit:
268 
269  return result;
270 }
271 
274 clone() const
275 {
276  interval_set_iterator* result = 0;
277 
278  // Preconditions:
279 
280  // Body:
281 
283 
284  // Postconditions:
285 
286  ensure(result != 0);
287  ensure(is_same_type(result));
288  ensure(set() == result->set());
289  ensure(is_member_iterator() == result->is_member_iterator());
290 
291  // Exit:
292 
293  return result;
294 }
295 
299 {
300  // Preconditions:
301 
302  // Body:
303 
304  _set = xother._set;
306  _item = xother._item;
307  _ub = xother._ub;
308  _map = xother._map;
309  _map_itr = xother._map_itr;
310 
311  // Postconditions:
312 
313  ensure(invariant());
314 
315  // Exit
316 
317  return *this;
318 }
319 
320 bool
322 operator==(const interval_set_iterator& xother) const
323 {
324  // Preconditions:
325 
326  // Body:
327 
328  bool result = (_set == xother._set);
329  result = result && (_is_member_iterator == xother._is_member_iterator);
330  result = result && (_item == xother._item);
331  result = result && (_ub == xother._ub);
332  result = result && (_map == xother._map);
333  result = result && (_map_itr == xother._map_itr);
334 
335  // Postconditions:
336 
337  // Exit:
338 
339  return result;
340 }
341 
342 bool
344 invariant() const
345 {
346  bool result = true;
347 
348  if(invariant_check())
349  {
350  // Must satisfy base class invariant
351 
352  invariance(any::invariant());
353 
354  // Prevent recursive calls to invariant
355 
357 
358  // Invariances for this class:
359 
360  invariance(is_done() || (is_member_iterator() == is_member_interval()));
361 
362  // Finished, turn invariant checking back on.
363 
365  }
366 
367  // Exit
368 
369  return result;
370 }
371 
372 // PROTECTED MEMBER FUNCTIONS
373 
374 // PRIVATE MEMBER FUNCTIONS
375 
376 
377 // ===========================================================
378 // NON-MEMBER FUNCTIONS
379 // ===========================================================
380 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
An iterator over the integers in an interval_set.
Set of integers optimized for when the integers are concentrated in closed intervals.
Definition: interval_set.h:70
pod_type _ub
The upper bound of the current interval.
virtual bool operator==(const interval_set_iterator &xother) const
True if this is equivalent to xother.
interval_set_iterator()
Default constructor; disabled.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
void reset()
Restarts the iteration.
virtual bool invariant() const
Class invariant.
interval_set_iterator & operator=(const interval_set_iterator &xother)
Assignment operator.
bool is_member_interval() const
True, if the current interval is an interval of members.
const map_type * _map
The interval map.
Abstract base class with useful features for all objects.
Definition: any.h:39
map_iterator_type _map_itr
The interval map iterator.
const interval_set * _set
The interval_set this is iterating over.
void next_interval()
Assign the item to the beginning of the next interval.
pod_type item() const
The current integer of 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
bool _is_member_iterator
True, if iterating over the members of the set.
void next()
Makes item() the next integer in the iteration.
interval_set::pod_type pod_type
The "plain old data" index type for this.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
const interval_set & set() const
The interval set this is iterating over.
bool is_member_iterator() const
True, if iterating over members.
pod_type _item
The current integer in the iteration.
SHEAF_DLL_SPEC pod_index_type invalid_pod_index()
The invalid pod index value.
Definition: pod_types.cc:31
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
bool is_done() const
True if iteration is finished.
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual interval_set_iterator * clone() const
Virtual constructor, makes a new instance of the same type as this.