-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranger.h
More file actions
107 lines (77 loc) · 3.08 KB
/
ranger.h
File metadata and controls
107 lines (77 loc) · 3.08 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <set>
#include <iosfwd>
struct ranger {
struct range;
struct iterator;
typedef int int_type;
typedef std::set<range> set_type;
typedef set_type::iterator set_iterator;
ranger() {};
ranger(const std::initializer_list<range> &il);
set_iterator insert(range r);
set_iterator erase(range r);
std::pair<set_iterator, bool> find(int_type x) const;
bool contains(int_type x) const { return find(x).second; }
bool empty() const { return forest.empty(); }
void clear() { forest.clear(); }
inline iterator begin() const;
inline iterator end() const;
friend std::ostream &operator<<(std::ostream &os, const ranger &r);
// the state of our ranger
set_type forest;
};
struct ranger::range {
struct iterator;
typedef ranger::int_type int_type;
range(int_type e) : _start(0), _end(e) {}
range(int_type s, int_type e) : _start(s), _end(e) {}
int_type size() const { return _end - _start; }
bool contains(int_type x) const { return _start <= x && x < _end; }
bool contains(const range &r) const
{ return _start <= r._start && r._end < _end; }
// only for use in our disjoint ranger forest context
bool operator< (const range &r2) const { return _end < r2._end; }
inline iterator begin() const;
inline iterator end() const;
friend std::ostream &operator<<(std::ostream &os, const range &rr);
// data members; a valid range in ranger forest context has _start < _end
int_type _start;
int_type _end;
};
struct ranger::range::iterator {
typedef ranger::int_type int_type;
typedef int_type value_type;
iterator() : i(0) {}
iterator(int_type n) : i(n) {}
int_type operator*() const { return i; }
iterator operator+(int_type n) const { return i+n; }
iterator operator-(int_type n) const { return i-n; }
iterator &operator++() { ++i; return *this; }
iterator &operator--() { --i; return *this; }
iterator operator++(int) { return i++; }
iterator operator--(int) { return i--; }
// takes care of rel ops :D
operator int_type() const { return i; }
// this is both the iterator "position" and the value
int_type i;
};
struct ranger::iterator {
typedef int_type value_type;
iterator(set_iterator si) : sit(si), rit_valid(0) {}
iterator() : rit_valid(0) {}
int_type operator*();
iterator &operator++();
iterator &operator--();
bool operator==(iterator &it);
bool operator!=(iterator &it);
private:
void mk_valid();
set_iterator sit;
range::iterator rit;
bool rit_valid;
};
inline ranger::iterator ranger::begin() const { return forest.begin(); }
inline ranger::iterator ranger::end() const { return forest.end(); }
inline ranger::range::iterator ranger::range::begin() const { return _start; }
inline ranger::range::iterator ranger::range::end() const { return _end; }