MCPcopy Index your code
hub / github.com/microsoft/SandDance / SortedIndex

Function SortedIndex

docs/app/js/sanddance-app.js:141253–141316  ·  view source on GitHub ↗

* Maintains a list of values, sorted by key.

()

Source from the content-addressed store, hash-verified

141251/**
141252 * Maintains a list of values, sorted by key.
141253 */ function SortedIndex() {
141254 let index = array32(0), value = [], size = 0;
141255 function insert(key, data, base) {
141256 if (!data.length) return [];
141257 const n0 = size, n1 = data.length, addi = array32(n1);
141258 let addv = Array(n1), oldv, oldi, i;
141259 for(i = 0; i < n1; ++i){
141260 addv[i] = key(data[i]);
141261 addi[i] = i;
141262 }
141263 addv = sort(addv, addi);
141264 if (n0) {
141265 oldv = value;
141266 oldi = index;
141267 value = Array(n0 + n1);
141268 index = array32(n0 + n1);
141269 merge(base, oldv, oldi, n0, addv, addi, n1, value, index);
141270 } else {
141271 if (base > 0) for(i = 0; i < n1; ++i)addi[i] += base;
141272 value = addv;
141273 index = addi;
141274 }
141275 size = n0 + n1;
141276 return {
141277 index: addi,
141278 value: addv
141279 };
141280 }
141281 function remove(num, map) {
141282 // map: index -> remove
141283 const n = size;
141284 let idx, i, j; // seek forward to first removal
141285 for(i = 0; !map[index[i]] && i < n; ++i); // condense index and value arrays
141286 for(j = i; i < n; ++i)if (!map[idx = index[i]]) {
141287 index[j] = idx;
141288 value[j] = value[i];
141289 ++j;
141290 }
141291 size = n - num;
141292 }
141293 function reindex(map) {
141294 for(let i = 0, n = size; i < n; ++i)index[i] = map[index[i]];
141295 }
141296 function bisect(range, array4) {
141297 let n;
141298 if (array4) n = array4.length;
141299 else {
141300 array4 = value;
141301 n = size;
141302 }
141303 return [
141304 (0, _d3Array.bisectLeft)(array4, range[0], 0, n),
141305 (0, _d3Array.bisectRight)(array4, range[1], 0, n)
141306 ];
141307 }
141308 return {
141309 insert: insert,
141310 remove: remove,

Callers 2

initFunction · 0.70
reinitFunction · 0.70

Calls 1

array32Function · 0.70

Tested by

no test coverage detected