File: RangeSet.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (180 lines) | stat: -rw-r--r-- 3,549 bytes parent folder | download | duplicates (3)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "stdafx.h"
#include "RangeSet.h"
#include "Core/CloneEnv.h"

namespace storm {
	namespace server {

		RangeSet::RangeSet() {
			parts = new (this) Array<Range>();
		}

		RangeSet::RangeSet(Range r) {
			parts = new (this) Array<Range>();
			if (!r.empty())
				parts->push(r);
		}

		void RangeSet::deepCopy(CloneEnv *env) {
			cloned(parts, env);
		}

		Bool RangeSet::intersects(Range a, Range b) {
			return a.from <= b.to
				&& b.from <= a.to;
		}

		void RangeSet::insert(Range insert) {
			if (insert.empty())
				return;

			Nat pos = findUpper(insert.from);
			if (pos > 0)
				pos--;

			while (pos < parts->count()) {
				Range at = parts->at(pos);

				if (intersects(at, insert)) {
					// Remove the range in the array and continue to insert the expanded 'insert'.
					insert = merge(at, insert);
					parts->remove(pos);
				} else if (insert.to < at.from) {
					// 'insert' is after 'at'. We have found the correct place!
					break;
				} else {
					// Keep looking...
					pos++;
				}
			}

			if (pos >= parts->count())
				parts->push(insert);
			else
				parts->insert(pos, insert);
		}

		void RangeSet::insert(RangeSet *src) {
			for (Nat i = 0; i < src->count(); i++)
				insert(src->at(i));
		}

		void RangeSet::remove(Range remove) {
			if (remove.empty())
				return;

			Nat pos = findUpper(remove.from);
			if (pos > 0) {
				// left.from <= remove.from.
				Range &left = parts->at(pos - 1);
				if (left.from >= remove.from && left.to <= remove.to) {
					// Remove it.
					parts->remove(--pos);
				} else if (left.from >= remove.from) {
					// Shorten the beginning.
					left.from = remove.to;
				} else if (left.to > remove.from) {
					// Shorten the end.
					left.to = remove.from;
				}
			}

			while (pos < parts->count()) {
				Range &at = parts->at(pos);

				if (remove.to <= at.from) {
					// Done. We're past any sections that will possibly intersect.
					break;
				} else if (at.to <= remove.to) {
					// Remove this one entirely.
					parts->remove(pos);
				} else {
					// Shorten the beginning.
					at.from = remove.to;
				}
			}
		}

		Bool RangeSet::has(Nat point) const {
			Nat pos = findUpper(point);
			if (pos == 0)
				return false;

			Range r = parts->at(--pos);
			return r.contains(point);
		}

		Range RangeSet::cover(Nat point) const {
			Nat pos = findUpper(point);
			if (pos == 0)
				return Range();

			Range r = parts->at(--pos);
			if (r.contains(point))
				return r;
			else
				return Range();
		}

		Range RangeSet::cover(Range range) const {
			Nat pos = findUpper(range.from);
			if (pos > 0)
				pos--;

			while (pos < parts->count()) {
				Range at = parts->at(pos);

				if (intersects(at, range)) {
					range = merge(at, range);
					pos++;
				} else if (range.to < at.from) {
					break;
				} else {
					pos++;
				}
			}

			return range;
		}

		Range RangeSet::nearest(Nat point) const {
			Nat pos = findUpper(point);

			Range result;
			if (pos > 0) {
				result = parts->at(pos - 1);
			}
			if (pos < parts->count()) {
				Range c = parts->at(pos);
				if (result.empty())
					result = c;
				else if (c.distance(point) < result.distance(point))
					result = c;
			}

			return result;
		}

		void RangeSet::toS(StrBuf *to) const {
			parts->toS(to);
		}

		Nat RangeSet::findUpper(Nat v) const {
			Nat from = 0;
			Nat to = parts->count();

			while (from < to) {
				Nat mid = (to - from) / 2 + from;

				if (parts->at(mid).from <= v) {
					from = mid + 1;
				} else {
					to = mid;
				}
			}

			return from;
		}

	}
}