diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-10-30 15:12:16 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-11-02 01:17:35 +0100 |
commit | 64e39b8d3f858117f460973b894cba94efe9c4b0 (patch) | |
tree | 5600794a6a15974113b0c183b6af6e8b7fcb2fde | |
parent | 54111d1c67254c86ffced22bbc4299b96ba796d1 (diff) | |
download | algeria-64e39b8d3f858117f460973b894cba94efe9c4b0.tar.gz |
Add "map" functions.
-rw-r--r-- | rules.js | 92 |
1 files changed, 92 insertions, 0 deletions
@@ -5473,6 +5473,22 @@ function array_insert(array, index, item) { return array } +function array_remove_pair(array, index) { + let n = array.length + for (let i = index + 2; i < n; ++i) + array[i - 2] = array[i] + array.length = n - 2 +} + +function array_insert_pair(array, index, key, value) { + for (let i = array.length; i > index; i -= 2) { + array[i] = array[i-2] + array[i+1] = array[i-1] + } + array[index] = key + array[index+1] = value +} + function set_clear(set) { set.length = 0 } @@ -5548,6 +5564,82 @@ function is_subset_with_multiplicity(multiset, subset) { return !subset.some(val => (occurrences(subset, val) > occurrences(multiset, val))) } +function map_clear(map) { + map.length = 0 +} + +function map_has(map, key) { + let a = 0 + let b = (map.length >> 1) - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = map[m<<1] + if (key < x) + b = m - 1 + else if (key > x) + a = m + 1 + else + return true + } + return false +} + +function map_get(map, key, missing) { + let a = 0 + let b = (map.length >> 1) - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = map[m<<1] + if (key < x) + b = m - 1 + else if (key > x) + a = m + 1 + else + return map[(m<<1)+1] + } + return missing +} + +function map_set(map, key, value) { + let a = 0 + let b = (map.length >> 1) - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = map[m<<1] + if (key < x) + b = m - 1 + else if (key > x) + a = m + 1 + else { + map[(m<<1)+1] = value + return + } + } + array_insert_pair(map, a<<1, key, value) +} + +function map_delete(map, item) { + let a = 0 + let b = (map.length >> 1) - 1 + while (a <= b) { + let m = (a + b) >> 1 + let x = map[m<<1] + if (item < x) + b = m - 1 + else if (item > x) + a = m + 1 + else { + array_remove_pair(map, m<<1) + return + } + } +} + +function map_for_each(map, f) { + for (let i = 0; i < map.length; i += 2) + f(map[i], map[i+1]) +} + // Fast deep copy for objects without cycles function object_copy(original) { if (Array.isArray(original)) { |