-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTypedRangeSet.cs
More file actions
556 lines (496 loc) · 19.6 KB
/
Copy pathTypedRangeSet.cs
File metadata and controls
556 lines (496 loc) · 19.6 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright 2018 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace CommonUtil {
/// <summary>
/// Compact representation of a set of typed integers that tend to be adjacent.
/// We expect there to be relatively few different types of things.
///
/// <para>The default enumeration is a series of integers, not a series of ranges. Use
/// RangeListIterator to get the latter.</para>
///
/// <para>Most operations operate in log(N) time, where N is the number of
/// regions.</para>
/// </summary>
public class TypedRangeSet : IEnumerable<TypedRangeSet.Tuple> {
/// <summary>
/// List of ranges, in sorted order.
/// </summary>
private List<TypedRange> mRangeList = new List<TypedRange>();
/// <summary>
/// Number of values in the set.
/// </summary>
public int Count { get; private set; }
/// <summary>
/// Returns the number of Range elements in the list.
/// </summary>
public int RangeCount { get { return mRangeList.Count; } }
/// <summary>
/// Represents a contiguous range of values.
/// </summary>
public struct TypedRange {
/// <summary>
/// Lowest value (inclusive).
/// </summary>
public int Low { get; set; }
/// <summary>
/// Highest value (inclusive).
/// </summary>
public int High { get; set; }
/// <summary>
/// Value type in this range.
/// </summary>
public int Type { get; set; }
public TypedRange(int low, int high, int type) {
Debug.Assert(low <= high);
Low = low;
High = high;
Type = type;
}
public bool Contains(int val) {
return (val >= Low && val <= High);
}
}
/// <summary>
/// Value + type pair. Returned from foreach enumerator.
/// </summary>
public struct Tuple {
public int Value;
public int Type;
public Tuple(int value, int type) {
Value = value;
Type = type;
}
public static bool operator ==(Tuple a, Tuple b) {
return a.Value == b.Value && a.Type == b.Type;
}
public static bool operator !=(Tuple a, Tuple b) {
return !(a == b);
}
public override bool Equals(object obj) {
return obj is Tuple && this == (Tuple)obj;
}
public override int GetHashCode() {
return Value ^ Type;
}
public override string ToString() {
return Value + " (" + Type + ")";
}
}
/// <summary>
/// Iterator definition.
/// </summary>
private class TypedRangeSetIterator : IEnumerator<Tuple> {
/// <summary>
/// The TypedRangeSet we're iterating over.
/// </summary>
private TypedRangeSet mSet;
// Index of current Range element in mSet.mRangeList.
private int mListIndex = -1;
// Current range, extracted from mRangeList.
private TypedRange mCurrentRange;
// Current value in mCurrentRange.
private int mCurrentVal;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="set">TypedRangeSet to iterate over.</param>
public TypedRangeSetIterator(TypedRangeSet set) {
mSet = set;
Reset();
}
// IEnumerator: current element
public object Current {
get {
if (mListIndex < 0) {
// not started
return null;
}
return new Tuple(mCurrentVal, mCurrentRange.Type);
}
}
// IEnumerator<Tuple>
Tuple IEnumerator<Tuple>.Current {
get {
return (Tuple)Current;
}
}
/// <summary>
/// Puts the next range in the list in mCurrentRange.
/// </summary>
/// <returns>True on success, false if we reached the end of the list.</returns>
private bool GetNextRange() {
mListIndex++; // increments to 0 on first invocation
if (mListIndex == mSet.mRangeList.Count) {
// no more ranges
return false;
}
mCurrentRange = mSet.mRangeList[mListIndex];
mCurrentVal = mCurrentRange.Low;
return true;
}
// IEnumerator: move to the next element, returning false if there isn't one
public bool MoveNext() {
if (mListIndex < 0) {
// just started
return GetNextRange();
} else {
// iterating within range object
mCurrentVal++;
if (mCurrentVal > mCurrentRange.High) {
// finished with this one, move on to the next
return GetNextRange();
} else {
return true;
}
}
}
// IEnumerator: reset state
public void Reset() {
mListIndex = -1;
}
// IEnumerator<Tuple>
public void Dispose() {
mSet = null;
}
}
/// <summary>
/// Constructor. Creates an empty set.
/// </summary>
public TypedRangeSet() {
Count = 0;
}
/// <summary>
/// Returns an enumerator that iterates through the range list, returning Range objects.
/// </summary>
public IEnumerator<TypedRange> RangeListIterator {
get { return mRangeList.GetEnumerator(); }
}
/// <summary>
/// Removes all values from the set.
/// </summary>
public void Clear() {
mRangeList.Clear();
Count = 0;
}
// IEnumerable: get an enumerator instance that returns integer values
public IEnumerator GetEnumerator() {
return new TypedRangeSetIterator(this);
}
// IEnumerable<Tuple>
IEnumerator<Tuple> IEnumerable<Tuple>.GetEnumerator() {
return (IEnumerator<Tuple>)GetEnumerator();
}
/// <summary>
/// Finds the range that contains "val", or an appropriate place in the list to
/// insert a new range.
/// </summary>
/// <param name="val">Value to find.</param>
/// <returns>The index of the matching element, or a negative value indicating
/// the index to insert at. 2C doesn't support negative 0, so the insertion
/// index will be incremented before negation.</returns>
private int FindValue(int val) {
int low = 0;
int high = mRangeList.Count - 1;
while (low <= high) {
int mid = (low + high) / 2;
TypedRange midRange = mRangeList[mid];
if (midRange.Contains(val)) {
// found it
return mid;
} else if (val < midRange.Low) {
// too big, move the high end in
high = mid - 1;
} else if (val > midRange.High) {
// too small, move the low end in
low = mid + 1;
} else {
// WTF... list not sorted?
throw new Exception("Bad binary search");
}
}
// Not found, insert before "low".
return -(low + 1);
}
/// <summary>
/// Determines whether val is a member of the set.
/// </summary>
/// <param name="val">Value to check.</param>
/// <returns>True if the value is a member of the set.</returns>
public bool Contains(int val) {
return (FindValue(val) >= 0);
}
#if false
/// <summary>
/// Finds a range that contains searchVal, or identifies the one that immediately
/// follows. The caller can determine which by checking to see if range.Low is
/// greater than searchVal.
/// </summary>
/// <param name="searchVal">Value to find.</param>
/// <param name="range">Result.</param>
/// <returns>True if a valid range was returned.</returns>
public bool GetContainingOrSubsequentRange(int searchVal, out TypedRange range) {
int index = FindValue(searchVal);
if (index >= 0) {
// found a range that contains val
range = mRangeList[index];
return true;
}
// No matching range, so the index of the insertion point was returned. The
// indexed range will have a "low" value that is greater than searchVal. If
// we've reached the end of the list, the index will be past the end.
index = -index - 1;
if (index >= mRangeList.Count) {
// reached the end of the list
range = new TypedRange(-128, -128, -128);
return false;
}
range = mRangeList[index];
return true;
}
#endif
/// <summary>
/// Gets the type of the specified value.
/// </summary>
/// <param name="val">Value to query.</param>
/// <param name="type">Receives the type, or -1 if the value is not in the set.</param>
/// <returns>True if the value is in the set.</returns>
public bool GetType(int val, out int type) {
int listIndex = FindValue(val);
if (listIndex >= 0) {
type = mRangeList[listIndex].Type;
return true;
} else {
type = -1;
return false;
}
}
/// <summary>
/// Adds or changes a value to the set. If the value is already present and has
/// a matching type, nothing changes.
/// </summary>
/// <param name="val">Value to add.</param>
/// <param name="type">Value's type.</param>
public void Add(int val, int type) {
int listIndex = FindValue(val);
if (listIndex >= 0) {
// Value is present in set, check type.
if (mRangeList[listIndex].Type == type) {
// It's a match, do nothing.
return;
}
// Wrong type. Remove previous entry, then fall through to add new.
Remove(val);
listIndex = FindValue(val); // get insertion point
}
Count++;
if (mRangeList.Count == 0) {
// Empty list, skip the gymnastics.
mRangeList.Add(new TypedRange(val, val, type));
return;
}
// Negate and decrement to get insertion index. This value may == Count if
// the value is higher than all current members.
listIndex = -listIndex - 1;
if (listIndex > 0 && mRangeList[listIndex - 1].High == val - 1 &&
mRangeList[listIndex - 1].Type == type) {
// Expand prior range. Check to see if it blends into next as well.
if (listIndex < mRangeList.Count && mRangeList[listIndex].Low == val + 1 &&
mRangeList[listIndex].Type == type) {
// Combine ranges.
TypedRange prior = mRangeList[listIndex - 1];
TypedRange next = mRangeList[listIndex];
Debug.Assert(prior.High + 2 == next.Low);
prior.High = next.High;
mRangeList[listIndex - 1] = prior;
mRangeList.RemoveAt(listIndex);
} else {
// Nope, just expand the prior range.
TypedRange prior = mRangeList[listIndex - 1];
Debug.Assert(prior.High == val - 1);
prior.High = val;
mRangeList[listIndex - 1] = prior;
}
} else if (listIndex < mRangeList.Count && mRangeList[listIndex].Low == val + 1 &&
mRangeList[listIndex].Type == type) {
// Expand next range.
TypedRange next = mRangeList[listIndex];
Debug.Assert(next.Low == val + 1);
next.Low = val;
mRangeList[listIndex] = next;
} else {
// Nothing adjacent, add a new single-entry element.
mRangeList.Insert(listIndex, new TypedRange(val, val, type));
}
}
/// <summary>
/// Adds a range of contiguous values to the set.
/// </summary>
/// <param name="low">Lowest value (inclusive).</param>
/// <param name="high">Highest value (inclusive).</param>
/// <param name="type">Value type.</param>
public void AddRange(int low, int high, int type) {
// There's probably some very efficient way to do this. Keeping it simple for now.
// (TODO: do a quick check to see if there's anything overlapping; if not, just
// create a new item and insert it into the list. Should handle the common case.)
Debug.Assert(low <= high); // adding an empty set is valid but weird
for (int i = low; i <= high; i++) {
Add(i, type);
}
}
/// <summary>
/// Removes a value from the set. If the value is not present, nothing changes.
/// </summary>
/// <param name="val">Value to remove.</param>
public void Remove(int val) {
int listIndex = FindValue(val);
if (listIndex < 0) {
// not found
return;
}
Count--;
TypedRange rng = mRangeList[listIndex];
if (rng.Low == val && rng.High == val) {
// Single-value range. Remove.
mRangeList.RemoveAt(listIndex);
} else if (rng.Low == val) {
// We're at the low end, reduce range.
rng.Low = val + 1;
mRangeList[listIndex] = rng;
} else if (rng.High == val) {
// We're at the high end, reduce range.
rng.High = val - 1;
mRangeList[listIndex] = rng;
} else {
// We're in the middle, split the range.
TypedRange next = new TypedRange(val + 1, rng.High, rng.Type);
rng.High = val - 1;
mRangeList[listIndex] = rng;
mRangeList.Insert(listIndex + 1, next);
}
}
public void DebugDump(string name) {
Debug.WriteLine(name + " has " + RangeCount + " ranges");
IEnumerator<TypedRange> iter = RangeListIterator;
while (iter.MoveNext()) {
TypedRange rng = iter.Current;
Debug.WriteLine("[+{0:x6},+{1:x6}] ({2:x2})", rng.Low, rng.High, rng.Type);
}
}
/// <summary>
/// Internal test function.
/// </summary>
private static bool CheckTypedRangeSet(TypedRangeSet set, int expectedRanges,
Tuple[] expected) {
if (set.RangeCount != expectedRanges) {
Debug.WriteLine("Expected " + expectedRanges + " ranges, got " +
set.RangeCount);
return false;
}
// Compare actual vs. expected. If we have more actual than expected we'll
// throw on the array access.
int expIndex = 0;
foreach (TypedRangeSet.Tuple val in set) {
if (val != expected[expIndex]) {
Debug.WriteLine("Expected " + expected[expIndex] + ", got " + val);
return false;
}
expIndex++;
}
// See if we have more expected than actual.
if (expIndex != expected.Length) {
Debug.WriteLine("Expected " + expected.Length + " elements, found " + expIndex);
return false;
}
// The count is maintained separately, so check it.
if (set.Count != expected.Length) {
Debug.WriteLine("Expected Count=" + expected.Length + ", got " + set.Count);
return false;
}
return true;
}
/// <summary>
/// Executes unit tests.
/// </summary>
/// <returns>True if all goes well.</returns>
public static bool Test() {
bool result = true;
TypedRangeSet one = new TypedRangeSet();
one.Add(7, 100);
one.Add(5, 100);
one.Add(3, 100);
one.Add(9, 100);
one.Add(7, 100);
one.Add(8, 100);
one.Add(2, 100);
one.Add(4, 100);
result &= CheckTypedRangeSet(one, 2, new Tuple[] {
new Tuple(2, 100),
new Tuple(3, 100),
new Tuple(4, 100),
new Tuple(5, 100),
new Tuple(7, 100),
new Tuple(8, 100),
new Tuple(9, 100) });
one.Remove(2);
one.Remove(9);
one.Remove(4);
result &= CheckTypedRangeSet(one, 3, new Tuple[] {
new Tuple(3, 100),
new Tuple(5, 100),
new Tuple(7, 100),
new Tuple(8, 100) });
one.Clear();
one.Add(1, 200);
one.Add(3, 100);
one.Add(7, 100);
one.Add(5, 100);
one.Add(9, 100);
one.Add(6, 100);
one.Add(8, 100);
one.Add(6, 200);
one.Add(2, 200);
one.Add(4, 300);
one.Add(4, 100);
result &= CheckTypedRangeSet(one, 4, new Tuple[] {
new Tuple(1, 200),
new Tuple(2, 200),
new Tuple(3, 100),
new Tuple(4, 100),
new Tuple(5, 100),
new Tuple(6, 200),
new Tuple(7, 100),
new Tuple(8, 100),
new Tuple(9, 100) });
one.Add(6, 100);
result &= CheckTypedRangeSet(one, 2, new Tuple[] {
new Tuple(1, 200),
new Tuple(2, 200),
new Tuple(3, 100),
new Tuple(4, 100),
new Tuple(5, 100),
new Tuple(6, 100),
new Tuple(7, 100),
new Tuple(8, 100),
new Tuple(9, 100) });
Debug.WriteLine("TypedRangeSet: test complete (ok=" + result + ")");
return result;
}
}
}