forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntervalMap.m
222 lines (188 loc) · 5.88 KB
/
IntervalMap.m
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
//
// IntervalMap.m
// iTerm
//
// Created by George Nachman on 12/8/11.
// Copyright (c) 2011 Georgetech. All rights reserved.
//
#import "IntervalMap.h"
@interface IntervalMapElement : NSObject {
IntRange *range_;
id value_;
}
@property (nonatomic, retain) IntRange *range;
@property (nonatomic, retain) id value;
+ (IntervalMapElement *)elementWithRange:(IntRange *)range value:(id)value;
@end
@implementation IntervalMapElement
@synthesize range = range_;
@synthesize value = value_;
+ (IntervalMapElement *)elementWithRange:(IntRange *)range value:(id)value
{
IntervalMapElement *element = [[[IntervalMapElement alloc] init] autorelease];
element.range = range;
element.value = value;
return element;
}
- (void)dealloc
{
[range_ release];
[value_ release];
[super dealloc];
}
- (NSComparisonResult)compare:(IntervalMapElement *)other
{
return [[NSNumber numberWithInt:self.range->min] compare:[NSNumber numberWithInt:other.range->min]];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@=%@", self.range, self.value];
}
@end
@implementation IntRange
+ (IntRange *)rangeWithMin:(int)min size:(int)size
{
return [IntRange rangeWithMin:min limit:min + size];
}
+ (IntRange *)rangeWithMin:(int)min limit:(int)limit
{
IntRange *range = [[[IntRange alloc] init] autorelease];
range->min = min;
range->size = MAX(0, limit - min);
return range;
}
- (BOOL)isEqualToIntRange:(IntRange *)other
{
return min == other->min && size == other->size;
}
- (int)limit
{
return min + size;
}
- (BOOL)intersectsRange:(IntRange *)other
{
return min < [other limit] && [self limit] > other->min;
}
- (IntRange *)intersectionWithRange:(IntRange *)other
{
return [IntRange rangeWithMin:MAX(min, other->min)
limit:MIN([self limit], [other limit])];
}
- (NSArray *)rangesAfterSubtractingRange:(IntRange *)other
{
NSMutableArray *result = [NSMutableArray array];
// Possibilities:
// 1. 2. 3. 4. 5. 6.
// sssss | sss | sss | sss | ssss | ssss
// ooo | ooooo | ooo | ooo | oooo | oooo
if (other->min > min && [other limit] < [self limit]) {
// 1. Other is strictly inside self so return two results
[result addObject:[IntRange rangeWithMin:min limit:[other limit]]];
[result addObject:[IntRange rangeWithMin:other->min limit:[self limit]]];
} else if (other->min <= min && [other limit] >= [self limit]) {
// 2. Self is contained by other
} else if ([other limit] <= min || other->min >= [self limit]) {
// 3, 4. Disjoint
[result addObject:[IntRange rangeWithMin:min limit:[self limit]]];
} else if (other->min <= min) {
// 5. Other starts before self but overlaps
[result addObject:[IntRange rangeWithMin:[other limit] limit:[self limit]]];
} else {
// 6. Self starts before other but overlaps
[result addObject:[IntRange rangeWithMin:min limit:other->min]];
}
return result;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"[%d,%d)", min, [self limit]];
}
@end
@implementation IntervalMap
- (id)init
{
self = [super init];
if (self) {
elements_ = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[elements_ release];
[super dealloc];
}
- (NSArray *)elementsInRange:(IntRange *)range
{
NSMutableArray *result = [NSMutableArray array];
for (IntervalMapElement *elt in elements_) {
if ([elt.range intersectsRange:range]) {
[result addObject:elt];
}
}
return result;
}
- (void)insertElement:(IntervalMapElement *)elt
{
[elements_ addObject:elt];
}
- (void)setObject:(id)object forRange:(IntRange *)range
{
NSArray *overlappingElements = [self elementsInRange:range];
for (IntervalMapElement *elt in overlappingElements) {
// Remove overlapping element
[elements_ removeObject:elt];
// Break into zero or more elements that don't include any part of 'range'
// and add them back with the original value.
NSArray *fragments = [elt.range rangesAfterSubtractingRange:range];
for (IntRange *fragment in fragments) {
[self insertElement:[IntervalMapElement elementWithRange:fragment
value:elt.value]];
}
}
// Insert a new element with just 'range' and its new value.
[self insertElement:[IntervalMapElement elementWithRange:range value:object]];
}
- (NSArray *)ranges:(NSArray *)orig bySubtractingRange:(IntRange *)sub
{
NSMutableArray *result = [NSMutableArray array];
for (IntRange *range in orig) {
NSArray *fragments = [range rangesAfterSubtractingRange:sub];
if (fragments.count) {
[result addObjectsFromArray:fragments];
}
}
return result;
}
- (void)incrementNumbersBy:(int)amount inRange:(IntRange *)range
{
NSArray *elts = [self elementsInRange:range];
NSArray *newRanges = [NSArray arrayWithObject:range];
for (IntervalMapElement *e in elts) {
NSNumber *n = e.value;
n = [NSNumber numberWithInt:[n intValue] + amount];
IntRange *intersection = [e.range intersectionWithRange:range];
[self setObject:n forRange:intersection];
newRanges = [self ranges:newRanges bySubtractingRange:e.range];
}
for (IntRange *newRange in newRanges) {
[self setObject:[NSNumber numberWithInt:amount] forRange:newRange];
}
}
- (NSArray *)allValues
{
NSMutableArray *result = [NSMutableArray array];
for (IntervalMapElement *e in elements_) {
[result addObject:e.value];
}
return result;
}
- (NSString *)description
{
NSMutableString *result = [NSMutableString string];
for (IntervalMapElement *e in elements_) {
[result appendFormat:@"%@ ", [e description]];
}
return result;
}
@end