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 | 1×
1×
1×
1×
1×
1×
344×
344×
344×
344×
1×
8693×
2×
8691×
8691×
8691×
8691×
1×
46×
46×
46×
46×
167×
167×
167×
167×
167×
167×
46×
1×
1250×
2×
1248×
1248×
1248×
1248×
1248×
1×
45×
45×
45×
45×
10×
2×
8×
8×
1×
69×
69×
2213×
1×
2709×
2709×
5974×
1×
10×
10×
10×
10×
10×
1×
155×
155×
155×
155×
155×
1×
165×
165×
149×
143×
5×
154×
1×
11×
1×
173×
173×
1×
| goog.provide('ol.structs.RBush');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.ext.rbush');
goog.require('ol.extent');
/**
* Wrapper around the RBush by Vladimir Agafonkin.
*
* @constructor
* @param {number=} opt_maxEntries Max entries.
* @see https://github.com/mourner/rbush
* @struct
* @template T
*/
ol.structs.RBush = function(opt_maxEntries) {
/**
* @private
*/
this.rbush_ = ol.ext.rbush(opt_maxEntries);
/**
* A mapping between the objects added to this rbush wrapper
* and the objects that are actually added to the internal rbush.
* @private
* @type {Object.<number, Object>}
*/
this.items_ = {};
Eif (goog.DEBUG) {
/**
* @private
* @type {number}
*/
this.readers_ = 0;
}
};
/**
* Insert a value into the RBush.
* @param {ol.Extent} extent Extent.
* @param {T} value Value.
*/
ol.structs.RBush.prototype.insert = function(extent, value) {
if (goog.DEBUG && this.readers_) {
throw new Error('Can not insert value while reading');
}
var item = [
extent[0],
extent[1],
extent[2],
extent[3],
value
];
this.rbush_.insert(item);
// remember the object that was added to the internal rbush
goog.asserts.assert(
!goog.object.containsKey(this.items_, goog.getUid(value)),
'uid (%s) of value (%s) already exists', goog.getUid(value), value);
this.items_[goog.getUid(value)] = item;
};
/**
* Bulk-insert values into the RBush.
* @param {Array.<ol.Extent>} extents Extents.
* @param {Array.<T>} values Values.
*/
ol.structs.RBush.prototype.load = function(extents, values) {
Iif (goog.DEBUG && this.readers_) {
throw new Error('Can not insert values while reading');
}
goog.asserts.assert(extents.length === values.length,
'extens and values must have same length (%s === %s)',
extents.length, values.length);
var items = new Array(values.length);
for (var i = 0, l = values.length; i < l; i++) {
var extent = extents[i];
var value = values[i];
var item = [
extent[0],
extent[1],
extent[2],
extent[3],
value
];
items[i] = item;
goog.asserts.assert(
!goog.object.containsKey(this.items_, goog.getUid(value)),
'uid (%s) of value (%s) already exists', goog.getUid(value), value);
this.items_[goog.getUid(value)] = item;
}
this.rbush_.load(items);
};
/**
* Remove a value from the RBush.
* @param {T} value Value.
* @return {boolean} Removed.
*/
ol.structs.RBush.prototype.remove = function(value) {
if (goog.DEBUG && this.readers_) {
throw new Error('Can not remove value while reading');
}
var uid = goog.getUid(value);
goog.asserts.assert(goog.object.containsKey(this.items_, uid),
'uid (%s) of value (%s) does not exist', uid, value);
// get the object in which the value was wrapped when adding to the
// internal rbush. then use that object to do the removal.
var item = this.items_[uid];
delete this.items_[uid];
return this.rbush_.remove(item) !== null;
};
/**
* Update the extent of a value in the RBush.
* @param {ol.Extent} extent Extent.
* @param {T} value Value.
*/
ol.structs.RBush.prototype.update = function(extent, value) {
var uid = goog.getUid(value);
goog.asserts.assert(goog.object.containsKey(this.items_, uid),
'uid (%s) of value (%s) does not exist', uid, value);
var item = this.items_[uid];
if (!ol.extent.equals(item.slice(0, 4), extent)) {
if (goog.DEBUG && this.readers_) {
throw new Error('Can not update extent while reading');
}
this.remove(value);
this.insert(extent, value);
}
};
/**
* Return all values in the RBush.
* @return {Array.<T>} All.
*/
ol.structs.RBush.prototype.getAll = function() {
var items = this.rbush_.all();
return items.map(function(item) {
return item[4];
});
};
/**
* Return all values in the given extent.
* @param {ol.Extent} extent Extent.
* @return {Array.<T>} All in extent.
*/
ol.structs.RBush.prototype.getInExtent = function(extent) {
var items = this.rbush_.search(extent);
return items.map(function(item) {
return item[4];
});
};
/**
* Calls a callback function with each value in the tree.
* If the callback returns a truthy value, this value is returned without
* checking the rest of the tree.
* @param {function(this: S, T): *} callback Callback.
* @param {S=} opt_this The object to use as `this` in `callback`.
* @return {*} Callback return value.
* @template S
*/
ol.structs.RBush.prototype.forEach = function(callback, opt_this) {
Eif (goog.DEBUG) {
++this.readers_;
try {
return this.forEach_(this.getAll(), callback, opt_this);
} finally {
--this.readers_;
}
} else {
return this.forEach_(this.getAll(), callback, opt_this);
}
};
/**
* Calls a callback function with each value in the provided extent.
* @param {ol.Extent} extent Extent.
* @param {function(this: S, T): *} callback Callback.
* @param {S=} opt_this The object to use as `this` in `callback`.
* @return {*} Callback return value.
* @template S
*/
ol.structs.RBush.prototype.forEachInExtent =
function(extent, callback, opt_this) {
Eif (goog.DEBUG) {
++this.readers_;
try {
return this.forEach_(this.getInExtent(extent), callback, opt_this);
} finally {
--this.readers_;
}
} else {
return this.forEach_(this.getInExtent(extent), callback, opt_this);
}
};
/**
* @param {Array.<T>} values Values.
* @param {function(this: S, T): *} callback Callback.
* @param {S=} opt_this The object to use as `this` in `callback`.
* @private
* @return {*} Callback return value.
* @template S
*/
ol.structs.RBush.prototype.forEach_ = function(values, callback, opt_this) {
var result;
for (var i = 0, l = values.length; i < l; i++) {
result = callback.call(opt_this, values[i]);
if (result) {
return result;
}
}
return result;
};
/**
* @return {boolean} Is empty.
*/
ol.structs.RBush.prototype.isEmpty = function() {
return goog.object.isEmpty(this.items_);
};
/**
* Remove all values from the RBush.
*/
ol.structs.RBush.prototype.clear = function() {
this.rbush_.clear();
this.items_ = {};
};
/**
* @param {ol.Extent=} opt_extent Extent.
* @return {!ol.Extent} Extent.
*/
ol.structs.RBush.prototype.getExtent = function(opt_extent) {
// FIXME add getExtent() to rbush
return this.rbush_.data.bbox;
};
|