forked from shinout/interval-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntervalTree.js
288 lines (238 loc) · 5.95 KB
/
IntervalTree.js
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
var SortedList = require('sortedlist');
/**
* IntervalTree
*
* @param (object) data:
* @param (number) center:
* @param (object) options:
* center:
*
**/
function IntervalTree(center, options) {
options || (options = {});
this.startKey = options.startKey || 0; // start key
this.endKey = options.endKey || 1; // end key
this.intervalHash = {}; // id => interval object
this.pointTree = new SortedList({ // b-tree of start, end points
compare: function(a, b) {
if (a == null) return -1;
if (b == null) return 1;
var c = a[0]- b[0];
return (c > 0) ? 1 : (c == 0) ? 0 : -1;
}
});
this._autoIncrement = 0;
// index of the root node
if (!center || typeof center != 'number') {
throw new Error('you must specify center index as the 2nd argument.');
}
this.root = new Node(center, this);
}
/**
* publid methods
**/
/**
* add new range
**/
IntervalTree.prototype.add = function(data, id) {
if (this.intervalHash[id]) {
throw new Error('id ' + id + ' is already registered.');
}
if (id == undefined) {
while (this.intervalHash[this._autoIncrement]) {
this._autoIncrement++;
}
id = this._autoIncrement;
}
var itvl = new Interval(data, id, this.startKey, this.endKey);
this.pointTree.insert([itvl.start, id]);
this.pointTree.insert([itvl.end, id]);
this.intervalHash[id] = itvl;
this._autoIncrement++;
_insert.call(this, this.root, itvl);
};
/**
* search
*
* @param (integer) val:
* @return (array)
**/
IntervalTree.prototype.search = function(val1, val2) {
var ret = [];
if (typeof val1 != 'number') {
throw new Error(val1 + ': invalid input');
}
if (val2 == undefined) {
_pointSearch.call(this, this.root, val1, ret);
}
else if (typeof val2 == 'number') {
_rangeSearch.call(this, val1, val2, ret);
}
else {
throw new Error(val1 + ',' + val2 + ': invalid input');
}
return ret;
};
/**
* remove:
**/
IntervalTree.prototype.remove = function(interval_id) {
};
/**
* private methods
**/
/**
* _insert
**/
function _insert(node, itvl) {
if (itvl.end < node.idx) {
if (!node.left) {
node.left = new Node((itvl.start + itvl.end) / 2, this);
}
return _insert.call(this, node.left, itvl);
}
if (node.idx < itvl.start) {
if (!node.right) {
node.right = new Node((itvl.start + itvl.end) / 2, this);
}
return _insert.call(this, node.right, itvl);
}
return node.insert(itvl);
}
/**
* _pointSearch
* @param (Node) node
* @param (integer) idx
* @param (Array) arr
**/
function _pointSearch(node, idx, arr) {
if (!node) return;
if (idx < node.idx) {
node.starts.every(function(itvl) {
var bool = (itvl.start <= idx);
if (bool) arr.push(itvl.result());
return bool;
});
return _pointSearch.call(this, node.left, idx, arr);
}
else if (idx > node.idx) {
node.ends.every(function(itvl) {
var bool = (itvl.end >= idx);
if (bool) arr.push(itvl.result());
return bool;
});
return _pointSearch.call(this, node.right, idx, arr);
}
// exact equal
else {
node.starts.map(function(itvl) { arr.push(itvl.result()) });
}
}
/**
* _rangeSearch
* @param (integer) start
* @param (integer) end
* @param (Array) arr
**/
function _rangeSearch(start, end, arr) {
if (end - start <= 0) {
throw new Error('end must be greater than start. start: ' + start + ', end: ' + end);
}
var resultHash = {};
var wholeWraps = [];
_pointSearch.call(this, this.root, (start + end) >> 1, wholeWraps, true);
wholeWraps.forEach(function(result) {
resultHash[result.id] = true;
});
var idx1 = this.pointTree.bsearch([start, null]);
var pointTreeArray = this.pointTree;
while (idx1 >= 0 && pointTreeArray[idx1][0] == start) {
idx1--;
}
var idx2 = this.pointTree.bsearch([end, null]);
if (idx2 >= 0)
{
var len = pointTreeArray.length -1;
while (idx2 <= len && pointTreeArray[idx2][0] <= end) {
idx2++;
}
pointTreeArray.slice(idx1 + 1, idx2).forEach(function(point) {
var id = point[1];
resultHash[id] = true;
}, this);
Object.keys(resultHash).forEach(function(id) {
var itvl = this.intervalHash[id];
arr.push(itvl.result(start, end));
}, this);
}
}
/**
* subclasses
*
**/
/**
* Node : prototype of each node in a interval tree
*
**/
function Node(idx) {
this.idx = idx;
this.starts = new SortedList({
compare: function(a, b) {
if (a == null) return -1;
if (b == null) return 1;
var c = a.start - b.start;
return (c > 0) ? 1 : (c == 0) ? 0 : -1;
}
});
this.ends = new SortedList({
compare: function(a, b) {
if (a == null) return -1;
if (b == null) return 1;
var c = a.end - b.end;
return (c < 0) ? 1 : (c == 0) ? 0 : -1;
}
});
};
/**
* insert an Interval object to this node
**/
Node.prototype.insert = function(interval) {
this.starts.insert(interval);
this.ends.insert(interval);
};
/**
* Interval : prototype of interval info
**/
function Interval(data, id, s, e) {
this.id = id;
this.start = data[s];
this.end = data[e];
this.data = data;
if (typeof this.start != 'number' || typeof this.end != 'number') {
throw new Error('start, end must be number. start: ' + this.start + ', end: ' + this.end);
}
if ( this.start >= this.end) {
throw new Error('start must be smaller than end. start: ' + this.start + ', end: ' + this.end);
}
}
/**
* get result object
**/
Interval.prototype.result = function(start, end) {
var ret = {
id : this.id,
data : this.data
};
if (typeof start == 'number' && typeof end == 'number') {
/**
* calc overlapping rate
**/
var left = Math.max(this.start, start);
var right = Math.min(this.end, end);
var lapLn = right - left;
ret.rate1 = lapLn / (end - start);
ret.rate2 = lapLn / (this.end - this.start);
}
return ret;
};
module.exports = IntervalTree;