@calcit/ternary-tree
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -13,2 +13,3 @@ import { TernaryTreeMap, TernaryTreeMapHashEntry } from "./types"; | ||
export declare function mapGet<K, T>(originalTree: TernaryTreeMap<K, T>, item: K): T; | ||
export declare function mapGetDefault<K, T>(originalTree: TernaryTreeMap<K, T>, item: K, v0: T): T; | ||
export declare function checkMapStructure<K, V>(tree: TernaryTreeMap<K, V>): boolean; | ||
@@ -15,0 +16,0 @@ export declare function assocMap<K, T>(tree: TernaryTreeMap<K, T>, key: K, item: T, disableBalancing?: boolean): TernaryTreeMap<K, T>; |
@@ -405,2 +405,55 @@ import { TernaryTreeKind, hashGenerator } from "./types"; | ||
} | ||
export function mapGetDefault(originalTree, item, v0) { | ||
let hx = hashGenerator(item); | ||
let tree = originalTree; | ||
whileLoop: while (tree != null) { | ||
if (tree.kind === TernaryTreeKind.ternaryTreeLeaf) { | ||
for (let pair of tree.elements) { | ||
if (dataEqual(pair[0], item)) { | ||
return pair[1]; | ||
} | ||
} | ||
return v0; | ||
} | ||
// echo "looking for: ", hx, " ", item, " in ", tree.formatInline | ||
if (tree.left != null) { | ||
if (tree.left.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.left.hash === hx) { | ||
tree = tree.left; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
else if (hx >= tree.left.minHash && hx <= tree.left.maxHash) { | ||
tree = tree.left; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
if (tree.middle != null) { | ||
if (tree.middle.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.middle.hash === hx) { | ||
tree = tree.middle; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
else if (hx >= tree.middle.minHash && hx <= tree.middle.maxHash) { | ||
tree = tree.middle; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
if (tree.right != null) { | ||
if (tree.right.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.right.hash === hx) { | ||
tree = tree.right; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
else if (hx >= tree.right.minHash && hx <= tree.right.maxHash) { | ||
tree = tree.right; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
return v0; | ||
} | ||
return v0; | ||
} | ||
// leaves on the left has smaller hashes | ||
@@ -407,0 +460,0 @@ // TODO check sizes, hashes |
import { hashGenerator } from "./types"; | ||
import { test, check, cmp, deepEqual, justDisplay } from "./utils"; | ||
import { initTernaryTreeMap, toHashSortedPairs, merge, mergeSkip, formatMapInline, assocMap, dissocMap, contains, mapGet, toPairs, initEmptyTernaryTreeMap, sameMapShape, checkMapStructure, mapLen, mapEqual, toKeys, toPairsArray, mapMapValues, } from "./map"; | ||
import { initTernaryTreeMap, toHashSortedPairs, merge, mergeSkip, formatMapInline, assocMap, dissocMap, contains, mapGet, toPairs, initEmptyTernaryTreeMap, sameMapShape, checkMapStructure, mapLen, mapEqual, toKeys, toPairsArray, mapMapValues, mapGetDefault, } from "./map"; | ||
export let runMapTests = () => { | ||
@@ -25,2 +25,3 @@ test("init map", () => { | ||
check(deepEqual(mapGet(data10, "1"), 11)); | ||
check(deepEqual(mapGetDefault(data10, "111", 0), 0)); | ||
// check(deepEqual(mapGet(data10, "11"), null)); // should throws error | ||
@@ -27,0 +28,0 @@ let emptyData = new Map(); |
{ | ||
"name": "@calcit/ternary-tree", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "devDependencies": { |
@@ -429,3 +429,59 @@ import { TernaryTreeMap, TernaryTreeKind, TernaryTreeMapTheLeaf, TernaryTreeMapTheBranch, RefInt, Hash, hashGenerator, TernaryTreeMapHashEntry } from "./types"; | ||
} | ||
export function mapGetDefault<K, T>(originalTree: TernaryTreeMap<K, T>, item: K, v0: T): T { | ||
let hx = hashGenerator(item); | ||
let tree = originalTree; | ||
whileLoop: while (tree != null) { | ||
if (tree.kind === TernaryTreeKind.ternaryTreeLeaf) { | ||
for (let pair of tree.elements) { | ||
if (dataEqual(pair[0], item)) { | ||
return pair[1]; | ||
} | ||
} | ||
return v0; | ||
} | ||
// echo "looking for: ", hx, " ", item, " in ", tree.formatInline | ||
if (tree.left != null) { | ||
if (tree.left.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.left.hash === hx) { | ||
tree = tree.left; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} else if (hx >= tree.left.minHash && hx <= tree.left.maxHash) { | ||
tree = tree.left; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
if (tree.middle != null) { | ||
if (tree.middle.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.middle.hash === hx) { | ||
tree = tree.middle; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} else if (hx >= tree.middle.minHash && hx <= tree.middle.maxHash) { | ||
tree = tree.middle; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
if (tree.right != null) { | ||
if (tree.right.kind == TernaryTreeKind.ternaryTreeLeaf) { | ||
if (tree.right.hash === hx) { | ||
tree = tree.right; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} else if (hx >= tree.right.minHash && hx <= tree.right.maxHash) { | ||
tree = tree.right; | ||
continue whileLoop; // notice, it jumps to while loop | ||
} | ||
} | ||
return v0; | ||
} | ||
return v0; | ||
} | ||
// leaves on the left has smaller hashes | ||
@@ -432,0 +488,0 @@ // TODO check sizes, hashes |
@@ -24,2 +24,3 @@ import { hashGenerator } from "./types"; | ||
mapMapValues, | ||
mapGetDefault, | ||
} from "./map"; | ||
@@ -54,2 +55,3 @@ | ||
check(deepEqual(mapGet(data10, "1"), 11)); | ||
check(deepEqual(mapGetDefault(data10, "111", 0), 0)); | ||
// check(deepEqual(mapGet(data10, "11"), null)); // should throws error | ||
@@ -56,0 +58,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
465418
8831