SheafSystem  0.0.0.0
index_iterator.cc
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 
18 // Implementation for class index_iterator
19 
20 #include "SheafSystem/index_iterator.h"
21 #include "SheafSystem/assert_contract.h"
22 
23 #include "SheafSystem/std_bitset.h"
24 #include "SheafSystem/std_limits.h"
25 #include "SheafSystem/zn_to_bool.h"
26 
30 {
31  // Preconditions:
32 
33  // Body:
34 
35 
36  // Initialize data members;
37  // default initialization on _index is ok.
38 
39  _host = 0;
40  _delete_host = false;
41  _word_index = 0;
42  _word_ub = 0;
43  _word = 0;
44  _bit_index = 0;
45  _bit_ub = 0;
46  _item = 0;
47  _next_item = 0;
48  _item_ub = 0;
49 
50  // Postcondition
51 
52  ensure(is_done());
53 
54  return;
55 }
56 
59 index_iterator(const zn_to_bool* xhost, const index_space_handle& xid_space, bool xdelete_host)
60 {
61  // Preconditions:
62 
63  require(xhost != 0);
64 
65  // Body:
66 
68 
69  reset(xhost, xid_space, xdelete_host);
70 
71  // Postcondition
72 
73  return;
74 }
75 
79 {
80  // Preconditions:
81 
82 
83  // Body:
84 
89 
90  if(_delete_host)
91  {
92  delete _host;
93  }
94 
95  // Postconditions:
96 
97 
98  // Exit:
99 
100  return;
101 }
102 
103 
104 
106 const sheaf::zn_to_bool*
108 host() const
109 {
110  return _host;
111 }
112 
114 int
117 {
118  int result;
119 
120  // Preconditions:
121 
122  require(!is_done());
123 
124  // Body:
125 
126  result = _item;
127 
128  // Postconditions:
129 
130  ensure((*host())[result]);
131 
132  // Exit
133 
134  return result;
135 }
136 
137 
138 
139 const sheaf::scoped_index&
141 index() const
142 {
143  // Preconditions:
144 
145  require(!is_done());
146 
147  // Body:
148 
149 
150  define_old_variable(const scoped_index& result = _index);
151 
152  _index = _item;
153 
154  // Postconditions:
155 
156  ensure((*host())[result.pod()]);
157 
158  // Exit
159 
160  return _index;
161 }
162 
163 
164 
165 
167 bool
169 is_done() const
170 {
171  bool result;
172 
173  // Preconditions:
174 
175  // Body:
176 
177  // You're done when there are no more items to be examined.
178 
179  result = (_item >= _item_ub);
180 
181  // Postconditions:
182 
183  // None - is_done is a primitive query.
184  // In particular, note that it is next() that ensures
185  // the semantics of item(), not is_done()
186 
187  // Exit
188 
189  return result;
190 }
191 
192 
193 
195 bool
197 is_last() const
198 {
199  bool result;
200 
201  // Preconditions:
202 
203  // Body:
204 
205  result = ((_next_item >= _item_ub) && (_item < _item_ub));
206 
207  // Postconditions:
208 
209  ensure(unexecutable(result implies is_done() after next()));
210 
211  // Exit
212 
213  return result;
214 }
215 
217 inline
218 void
221 {
222  // Preconditions:
223 
224  require(!is_done());
225 
226  // Body:
227 
228  bool lfound = false;
229  while(!lfound)
230  {
231  _next_item++;
232  if(_next_item >= _item_ub)
233  break;
234 
235  assertion(_next_item < _item_ub);
236 
237  _bit_index++;
238 
239  if(_bit_index >= _bit_ub)
240  {
241  // Move to the 0th bit of the next word
242 
243  _bit_index = 0;
244  _word_index++;
245 
246  assertion(_word_index < _word_ub); // implied by _next_item < _item_ub
247  }
248 
249  lfound = (*host())[_next_item];
250 
251  }
252 
253  assertion(lfound || (_next_item == _item_ub));
254 
255  // Postcondition
256 
257  ensure((_next_item < _item_ub) ? (*host())[_next_item] : true);
258 
259  return;
260 }
261 
263 void
266 {
267  // Preconditions:
268 
269  require(!is_done());
270 
271  // Body:
272 
273  define_old_variable(int old_item = _item);
274 
275  _item = _next_item;
276  if(_next_item < _item_ub)
277  {
278  find_next_item();
279  }
280 
281  // Postcondition
282 
283  ensure(!is_done() ? item() > old_item : true);
284  ensure(!is_done() ? (*host())[item()] : true);
285 
286  return;
287 }
288 
290 void
293 {
294  // Preconditions:
295 
296  // Body:
297 
298  require(host() != 0);
299 
300  // Initialize host independent data members
301 
302  _word_index = -1;
303  _bit_index = _bit_ub - 1; // == -1 mod _bit_ub
304  _item = -1;
305  _next_item = -1;
306 
307  // Find the first subset member
308 
309  find_next_item();
310  next();
311 
312  // Postcondition
313 
314  ensure(!is_done() ? (*host())[item()] : true);
315 
316  return;
317 }
318 
320 void
322 reset(const zn_to_bool* xhost, const index_space_handle& xid_space, bool xdelete_host)
323 {
324  // Preconditions:
325 
326  require(xhost != 0);
327 
328  // Body:
329 
331 
332 
333  // Initialize host dependent data members
334 
335  _host = xhost;
336  _index.put_scope(xid_space);
337  _delete_host = xdelete_host;
338  _word_ub = _host->_word_ub;
339  // _bit_ub = _host->_bits_per_word;
340  _bit_ub = zn_to_bool::BITS_PER_WORD;
341  _item_ub = _host->_ub;
342 
343  // Perform same-host reset
344 
345  reset();
346 
347  // Postcondition
348 
349  ensure(!is_done() ? (*host())[_item] : true);
350 
351  return;
352 }
353 
const scoped_index & index() const
The current item in the subset.
int item()
OBSOLETE: use index(). The current item in the subset.
An abstract handle to a space of alternate integer identifiers (aliases) for a subset of a hub set of...
const zn_to_bool * host() const
The subset this is iterating over.
int _word_ub
The upper bound for the word index.
index_iterator()
Creates an instance with host() == 0.
void next()
Makes item the next member of the subset.
scoped_index _index
The current index in the iteration.
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
Definition: zn_to_bool.h:52
int _item_ub
The upper bound in item.
void put_scope(const index_space_handle &xid_space)
Sets the scope to xid_space.
Definition: scoped_index.h:441
void find_next_item()
Finds the next item in the iteration.
unsigned int _word
The current word.
bool _delete_host
True if _host should be deleted when this is deleted.
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
int _bit_ub
The upper bound on the bit index.
const zn_to_bool * _host
The subset this is iterating over.
int _item
The current item in the iteration.
int _bit_index
The index of the current bit.
bool is_done() const
True if iteration finished.
virtual ~index_iterator()
Destructor; not virtual so this class should not be used as a base class.
int _next_item
The next item in the iteration.
void reset()
Restarts the iteration, makes item the first member of the subset.
int _word_index
The index of the word containing the current bit.
bool is_last() const
True if iteration finished after next call to next()