aws-crt
Version:
NodeJS bindings to the aws-c-* libraries
77 lines • 2.37 kB
JavaScript
;
/* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
class Node {
constructor(key, value, children = new Map()) {
this.key = key;
this.value = value;
this.children = children;
}
}
exports.Node = Node;
var TrieOp;
(function (TrieOp) {
TrieOp[TrieOp["Insert"] = 0] = "Insert";
TrieOp[TrieOp["Delete"] = 1] = "Delete";
TrieOp[TrieOp["Find"] = 2] = "Find";
})(TrieOp = exports.TrieOp || (exports.TrieOp = {}));
;
class Trie {
constructor(split) {
this.root = new Node();
if (typeof (split) === 'string') {
const delimeter = split;
split = (key) => {
return key.split(delimeter);
};
}
this.split_key = split;
}
find_node(key, op) {
const parts = this.split_key(key);
let current = this.root;
let parent = undefined;
for (const part in parts) {
let child = current.children.get(part);
if (!child) {
if (op == TrieOp.Insert) {
current.children.set(part, child = new Node(part));
}
else {
return undefined;
}
}
parent = current;
current = child;
}
if (parent && op == TrieOp.Delete) {
parent.children.delete(current.key);
}
return current;
}
insert(key, value) {
let node = this.find_node(key, TrieOp.Insert);
node.value = value;
}
remove(key) {
this.find_node(key, TrieOp.Delete);
}
find(key) {
const node = this.find_node(key, TrieOp.Find);
return node ? node.value : undefined;
}
}
exports.Trie = Trie;
//# sourceMappingURL=trie.js.map