-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranger_iterator_impl.cpp
More file actions
63 lines (54 loc) · 1.18 KB
/
ranger_iterator_impl.cpp
File metadata and controls
63 lines (54 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "ranger.h"
/* Frankly, the user would be better served by the tremendously simpler:
*
* for (auto &rr : myranger.forest)
* for (int i : rr)
* process_int(i);
*/
void ranger::iterator::mk_valid()
{
if (!rit_valid) {
rit = sit->begin();
rit_valid = true;
}
}
ranger::int_type ranger::iterator::operator*()
{
mk_valid();
return *rit;
}
ranger::iterator &ranger::iterator::operator++()
{
mk_valid();
if (++rit == sit->end()) {
++sit;
rit_valid = false;
}
return *this;
}
ranger::iterator &ranger::iterator::operator--()
{
mk_valid();
if (rit == sit->begin()) {
--sit;
rit = sit->end();
--rit;
}
return *this;
}
bool ranger::iterator::operator==(iterator &it)
{
if (sit != it.sit)
return false;
if (!rit_valid && !it.rit_valid)
return true;
// at this point neither sit nor it.sit points to the end() of its set,
// thus it's OK to call mk_valid() on each, which may dereference *sit
mk_valid();
it.mk_valid();
return rit == it.rit;
}
bool ranger::iterator::operator!=(iterator &it)
{
return !(*this == it);
}