/
IndexSet.ts
230 lines (188 loc) · 5.32 KB
/
IndexSet.ts
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
/*
* Copyright 2021 Cognite AS
*/
import { IntermediateIndexNode } from './IntermediateIndexNode';
import { LeafIndexNode } from './LeafIndexNode';
import { NumericRange } from '../NumericRange';
export type IndexNode = IntermediateIndexNode | LeafIndexNode;
export class IndexSet {
private rootNode?: IndexNode;
constructor(values?: Iterable<number>);
constructor(values?: NumericRange);
constructor(values?: Iterable<number> | NumericRange) {
if (values == undefined) {
this.rootNode = undefined;
} else if (NumericRange.isNumericRange(values)) {
this.addRange(values);
} else {
for (const index of values) {
this.add(index);
}
}
}
forEachRange(visitor: (range: NumericRange) => void): void {
if (this.rootNode) {
this.rootNode.traverse(visitor);
}
}
add(index: number): void {
const range = new NumericRange(index, 1);
this.addRange(range);
}
addRange(range: NumericRange): void {
if (this.rootNode) {
this.rootNode = this.rootNode.addRange(range);
} else {
this.rootNode = new LeafIndexNode(range);
}
}
remove(index: number): void {
const range = new NumericRange(index, 1);
this.removeRange(range);
}
removeRange(range: NumericRange): void {
if (this.rootNode) {
this.rootNode = this.rootNode.removeRange(range);
}
// Do nothing if root is empty
}
contains(index: number): boolean {
if (this.rootNode) {
return this.rootNode.contains(index);
}
return false;
}
get count(): number {
if (this.rootNode) {
return this.rootNode.count;
}
return 0;
}
toRangeArray(): NumericRange[] {
const ranges: NumericRange[] = [];
this.forEachRange(range => {
ranges.push(range);
});
return ranges;
}
toIndexArray(): number[] {
const result: number[] = [];
if (this.rootNode) {
this.forEachRange(range => {
range.forEach(num => {
result.push(num);
});
});
}
return result;
}
toPlainSet(): Set<number> {
const arr: number[] = this.toIndexArray();
const st = new Set(arr);
return st;
}
// NB: Assumes that this.ranges() are in order from left to right
invertedRanges(): NumericRange[] {
const originalRanges = this.toRangeArray();
const newRanges: NumericRange[] = [];
for (let i = 0; i < originalRanges.length - 1; i++) {
if (originalRanges[i].toInclusive + 1 >= originalRanges[i + 1].from) {
// Should not happen, but let's be safe
continue;
}
newRanges.push(
NumericRange.createFromInterval(originalRanges[i].toInclusive + 1, originalRanges[i + 1].from - 1)
);
}
return newRanges;
}
unionWith(otherSet: IndexSet): IndexSet {
if (this.rootNode) {
otherSet.forEachRange(range => {
this.rootNode = this.rootNode!.addRange(range);
});
} else {
this.rootNode = otherSet.rootNode;
}
return this;
}
differenceWith(otherSet: IndexSet): IndexSet {
if (this.rootNode) {
otherSet.forEachRange(range => {
this.rootNode = this.rootNode?.removeRange(range);
});
}
return this;
}
hasIntersectionWith(otherSet: IndexSet | Set<number>): boolean {
if (otherSet instanceof IndexSet) {
if (this.rootNode === undefined || otherSet.rootNode === undefined) {
return false;
}
return this.rootNode.hasIntersectionWith(otherSet.rootNode);
} else {
for (const index of otherSet) {
if (this.contains(index)) {
return true;
}
}
return false;
}
}
hasIntersectionWithMap(otherMap: Map<number, number>): boolean {
if (!this.rootNode) {
return false;
}
for (const index of otherMap.keys()) {
if (this.rootNode.contains(index)) {
return true;
}
}
return false;
}
intersectWith(otherSet: IndexSet): IndexSet {
if (this.rootNode && otherSet.rootNode) {
// Tackle endpoints
// Remove left bounds outside input set
if (this.rootNode.range.from < otherSet.rootNode.range.from) {
const leftBoundRange = NumericRange.createFromInterval(
this.rootNode.range.from,
otherSet.rootNode.range.from - 1
);
this.rootNode = this.rootNode.removeRange(leftBoundRange);
if (!this.rootNode) {
return this;
}
}
// Remove right bounds outside input set
if (this.rootNode.range.toInclusive > otherSet.rootNode.range.toInclusive) {
const rightBoundRange = NumericRange.createFromInterval(
otherSet.rootNode.range.toInclusive + 1,
this.rootNode.range.toInclusive
);
this.rootNode = this.rootNode.removeRange(rightBoundRange);
}
// Invert otherSet ranges and remove them
const invRanges = otherSet.invertedRanges();
invRanges.forEach(range => {
if (this.rootNode) {
this.rootNode = this.rootNode.removeRange(range);
}
});
} else if (this.rootNode) {
// Otherset is empty, set this to empty as well
this.rootNode = undefined;
}
return this;
}
clear(): void {
this.rootNode = undefined;
}
clone(): IndexSet {
const st: IndexSet = new IndexSet();
if (this.rootNode) {
st.rootNode = this.rootNode.clone();
}
return st;
}
}