MCPcopy Create free account
hub / github.com/microsoft/SandDance / SortedIndex

Function SortedIndex

docs/tests/v2/es6/js/sanddance.js:125972–126067  ·  view source on GitHub ↗

* Maintains a list of values, sorted by key.

()

Source from the content-addressed store, hash-verified

125970 * Maintains a list of values, sorted by key.
125971 */
125972function SortedIndex() {
125973 var index = (0, _arrays.array32)(0),
125974 value = [],
125975 size = 0;
125976
125977 function insert(key, data, base) {
125978 if (!data.length) return [];
125979 var n0 = size,
125980 n1 = data.length,
125981 addv = Array(n1),
125982 addi = (0, _arrays.array32)(n1),
125983 oldv,
125984 oldi,
125985 i;
125986
125987 for (i = 0; i < n1; ++i) {
125988 addv[i] = key(data[i]);
125989 addi[i] = i;
125990 }
125991
125992 addv = sort(addv, addi);
125993
125994 if (n0) {
125995 oldv = value;
125996 oldi = index;
125997 value = Array(n0 + n1);
125998 index = (0, _arrays.array32)(n0 + n1);
125999 merge(base, oldv, oldi, n0, addv, addi, n1, value, index);
126000 } else {
126001 if (base > 0) for (i = 0; i < n1; ++i) {
126002 addi[i] += base;
126003 }
126004 value = addv;
126005 index = addi;
126006 }
126007
126008 size = n0 + n1;
126009 return {
126010 index: addi,
126011 value: addv
126012 };
126013 }
126014
126015 function remove(num, map) {
126016 // map: index -> remove
126017 var n = size,
126018 idx,
126019 i,
126020 j; // seek forward to first removal
126021
126022 for (i = 0; !map[index[i]] && i < n; ++i); // condense index and value arrays
126023
126024
126025 for (j = i; i < n; ++i) {
126026 if (!map[idx = index[i]]) {
126027 index[j] = idx;
126028 value[j] = value[i];
126029 ++j;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected