@denox/cube
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -91,6 +91,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -97,0 +97,0 @@ *entities() { |
@@ -93,6 +93,6 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -99,0 +99,0 @@ *entries() { |
@@ -70,3 +70,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
*keys() { | ||
yield* __classPrivateFieldGet(this, _LFU_queue, "f")[Symbol.iterator](); | ||
yield* __classPrivateFieldGet(this, _LFU_queue, "f"); | ||
} | ||
@@ -79,3 +79,3 @@ *values() { | ||
*entries() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -82,0 +82,0 @@ forEach(callbackFn, thisArg) { |
@@ -136,6 +136,6 @@ // This is a special kind of linked list, where there is a map that keeps track of the nodes | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -142,0 +142,0 @@ *entities() { |
@@ -13,2 +13,4 @@ import Heap from "./heap.js"; | ||
import Channel from "./channel.js"; | ||
export { Buffer, Channel, CircularList, FQueue, Heap, LFU, LinkedList, LRU, PubSub, Queue, Stack, Trie, }; | ||
import QuadTree from "./qtree.js"; | ||
import BloomFilter from "./bloom.js"; | ||
export { BloomFilter, Buffer, Channel, CircularList, FQueue, Heap, LFU, LinkedList, LRU, PubSub, QuadTree, Queue, Stack, Trie, }; |
@@ -43,16 +43,13 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
__classPrivateFieldGet(this, _PubSub_buffer, "f").forEach(([topic, context]) => handler(context, topic)); | ||
// Attach the handler | ||
if (!__classPrivateFieldGet(this, _PubSub_handlers, "f").has(topic)) { | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").set(topic, new Set()); | ||
// Get he handlers for the topic | ||
const current = __classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic); | ||
const handlers = current ?? new Set(); | ||
// Add the handler | ||
handlers.add(h); | ||
// Store it needed | ||
if (current == null) { | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").set(topic, handlers); | ||
} | ||
// Attach the handler | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic).add(h); | ||
// Unsubscribe | ||
return () => { | ||
// Load the handlers | ||
const handlers = __classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic); | ||
// Nothing to remove | ||
if (handlers == null) { | ||
return; | ||
} | ||
// Remove the handler | ||
@@ -59,0 +56,0 @@ handlers.delete(h); |
@@ -49,6 +49,6 @@ // A stack is a LIFO (last in, first out) data structure | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -55,0 +55,0 @@ *entries() { |
273
esm/trie.js
@@ -12,69 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
}; | ||
var _Trie_instances, _Trie_head, _Trie_size, _Trie_find; | ||
// Breadth first traversal for the node that is returning an iterator | ||
/* | ||
function* bfs<K, V>( | ||
node: Tree<K, V>, | ||
prefix: K[], | ||
): IterableIterator<[K[], V]> { | ||
const queue = new Queue< | ||
[Tree<K, V>, K[]] | ||
>([[node, prefix]]); | ||
while (queue.size > 0) { | ||
// A list of nodes, a value node maybe -> If value node, return the value | ||
// Else go to the next node and find the node | ||
const current = queue.pop(); | ||
// We are done, no more iterators | ||
if (current == null) { | ||
return; | ||
} | ||
// Get the current path/keys and node | ||
const [[subtree, value], keys] = current; | ||
for (const [k, v] of subtree) { | ||
queue.push([v as Tree<K, V>, [...keys, k]]); | ||
} | ||
if (value !== undefined) { | ||
yield [keys, value]; | ||
} | ||
} | ||
} | ||
*/ | ||
// Depth first traversal for the node that is returning an iterator | ||
function* dfs(node, prefix) { | ||
const [subtree, value] = node; | ||
if (value !== undefined) { | ||
yield [prefix, value]; | ||
} | ||
if (subtree.size === 0) { | ||
return; | ||
} | ||
for (const [k, v] of subtree) { | ||
yield* dfs(v, prefix.concat(k)); | ||
} | ||
} | ||
// Traverse node by path | ||
function* traverse(node, keys, index = 0) { | ||
// We are done once we reach the end of the keys | ||
if (index > keys.length) { | ||
return; | ||
} | ||
// Load the data from the node | ||
const [subtree, value] = node; | ||
if (value !== undefined) { | ||
yield [keys.slice(0, index), value]; | ||
} | ||
const k = keys[index]; | ||
if (subtree.has(k)) { | ||
yield* traverse(subtree.get(k), keys, index + 1); | ||
} | ||
} | ||
var _Trie_value, _Trie_nodes, _Trie_size; | ||
class Trie { | ||
constructor(entries) { | ||
_Trie_instances.add(this); | ||
_Trie_head.set(this, [new Map()]); | ||
_Trie_value.set(this, [false]); | ||
_Trie_nodes.set(this, new Map()); | ||
_Trie_size.set(this, 0); | ||
@@ -87,23 +25,47 @@ if (entries != null) { | ||
} | ||
ref(keys) { | ||
// Root value | ||
if (keys.length === 0) { | ||
return this; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Create the leaf if it doesn't exist | ||
if (!__classPrivateFieldGet(this, _Trie_nodes, "f").has(key)) { | ||
return; | ||
} | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Find the next | ||
return node.ref(rest); | ||
} | ||
set(keys, value) { | ||
var _a; | ||
// Start at the head | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Iterate over the keys to find the node where to insert | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree] = node; | ||
// Create a child node if we don't have one | ||
if (!subtree.has(key)) { | ||
subtree.set(key, [new Map()]); | ||
// Root value | ||
if (keys.length === 0) { | ||
// Check if the value was already set | ||
const [isSet] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Set the value | ||
__classPrivateFieldSet(this, _Trie_value, [true, value], "f"); | ||
// Increase the size if it wasn't set | ||
if (!isSet) { | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a++, _a), "f"); | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
// Chain | ||
return this; | ||
} | ||
// If this is new, we increase the size | ||
if (node[1] === undefined) { | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a++, _a), "f"); | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Create the leaf if it doesn't exist | ||
if (!__classPrivateFieldGet(this, _Trie_nodes, "f").has(key)) { | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").set(key, new Trie()); | ||
} | ||
// Set the value on the value | ||
node[1] = value; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Get the old size | ||
const oldSize = node.size; | ||
// Set the value on the node | ||
node.set(rest, value); | ||
// Update the size | ||
__classPrivateFieldSet(this, _Trie_size, __classPrivateFieldGet(this, _Trie_size, "f") + (node.size - oldSize), "f"); | ||
// Chain | ||
@@ -113,52 +75,69 @@ return this; | ||
get(keys) { | ||
// Start at the head | ||
const node = __classPrivateFieldGet(this, _Trie_instances, "m", _Trie_find).call(this, keys); | ||
// Get the value from the node | ||
return node?.[1]; | ||
// Get the node | ||
const node = this.ref(keys); | ||
// Node was not found | ||
if (node === undefined) { | ||
return; | ||
} | ||
// Found the value, get it | ||
if (node === this) { | ||
return __classPrivateFieldGet(this, _Trie_value, "f")[1]; | ||
} | ||
// Check the node recursively | ||
return node.get([]); | ||
} | ||
has(keys) { | ||
return this.get(keys) !== undefined; | ||
// Get the node | ||
const node = this.ref(keys); | ||
// Node was not found | ||
if (node === undefined) { | ||
return false; | ||
} | ||
// Found the value, get it | ||
if (node === this) { | ||
return __classPrivateFieldGet(this, _Trie_value, "f")[0]; | ||
} | ||
// Check the node recursively | ||
return node.has([]); | ||
} | ||
delete(keys) { | ||
var _a; | ||
// Start at the head | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Create a clean function | ||
// We start witht the assumption we need to remove everything | ||
let clean = () => { | ||
node[0].clear(); | ||
}; | ||
// Iterate over the keys | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree, value] = node; | ||
// Get the child node | ||
if (!subtree.has(key)) { | ||
return false; | ||
var _a, _b; | ||
// Root value | ||
if (keys.length === 0) { | ||
const [isSet] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Delete the value if set | ||
if (isSet) { | ||
__classPrivateFieldSet(this, _Trie_value, [false], "f"); | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a--, _a), "f"); | ||
return true; | ||
} | ||
// Check if the current subtree contains any other data | ||
const stale = subtree.size === 1 && value === undefined; | ||
// If we have other info, we update the assumption, only remove the key subtree | ||
if (!stale) { | ||
clean = () => { | ||
subtree.delete(key); | ||
}; | ||
// Nothing to do if not set | ||
return false; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Nothing to do if not found | ||
if (node === undefined) { | ||
return false; | ||
} | ||
// Attept to delete the node | ||
if (node.delete(rest)) { | ||
// Reduce the size | ||
__classPrivateFieldSet(this, _Trie_size, (_b = __classPrivateFieldGet(this, _Trie_size, "f"), _b--, _b), "f"); | ||
// Delete the node if it's empty | ||
if (node.size === 0) { | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").delete(key); | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
// We deleted it | ||
return true; | ||
} | ||
// Get the value from the node | ||
node[1] = undefined; | ||
// Reduce the size | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a--, _a), "f"); | ||
// Clean only if this is empty | ||
if (node[0].size === 0) { | ||
clean(); | ||
} | ||
// We deleted it | ||
return true; | ||
// We didn't delete it | ||
return false; | ||
} | ||
clear() { | ||
__classPrivateFieldSet(this, _Trie_head, [new Map()], "f"); | ||
__classPrivateFieldSet(this, _Trie_value, [false], "f"); | ||
__classPrivateFieldSet(this, _Trie_size, 0, "f"); | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").clear(); | ||
} | ||
@@ -171,3 +150,20 @@ get size() { | ||
*match(keys) { | ||
yield* traverse(__classPrivateFieldGet(this, _Trie_head, "f"), keys); | ||
// Root value | ||
const [isSet, value] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Get the value if set | ||
if (isSet) { | ||
yield [[], value]; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Nothing to do if no node | ||
if (node === undefined) { | ||
return; | ||
} | ||
// Match next layers | ||
for (const [k, v] of node.match(rest)) { | ||
yield [[key, ...k], v]; | ||
} | ||
} | ||
@@ -177,25 +173,22 @@ // This will get all the values starting with the path, | ||
*list(keys) { | ||
// Find the node | ||
const node = __classPrivateFieldGet(this, _Trie_instances, "m", _Trie_find).call(this, keys); | ||
// Find the root node based on the keys | ||
const node = this.ref(keys); | ||
// Nothing to do if no node | ||
if (node === undefined) { | ||
return; | ||
} | ||
yield* dfs(node, keys); | ||
// Traverse the node once found | ||
yield* node; | ||
} | ||
*[(_Trie_head = new WeakMap(), _Trie_size = new WeakMap(), _Trie_instances = new WeakSet(), _Trie_find = function _Trie_find(keys) { | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Iterate over the keys | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree] = node; | ||
// Get the child node | ||
if (!subtree.has(key)) { | ||
return undefined; | ||
*[(_Trie_value = new WeakMap(), _Trie_nodes = new WeakMap(), _Trie_size = new WeakMap(), Symbol.iterator)]() { | ||
// Get the value of the node if there is one | ||
if (this.has([])) { | ||
yield [[], this.get([])]; | ||
} | ||
// Traverse the other nodes | ||
for (const [key, node] of __classPrivateFieldGet(this, _Trie_nodes, "f")) { | ||
for (const [k, v] of node) { | ||
yield [[key, ...k], v]; | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
} | ||
return node; | ||
}, Symbol.iterator)]() { | ||
yield* this.list([]); | ||
} | ||
@@ -213,3 +206,3 @@ *keys() { | ||
*entries() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -216,0 +209,0 @@ forEach(callbackFn, thisArg) { |
@@ -51,3 +51,3 @@ { | ||
}, | ||
"version": "1.2.1" | ||
"version": "1.3.0" | ||
} |
@@ -93,6 +93,6 @@ "use strict"; | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -99,0 +99,0 @@ *entities() { |
@@ -98,6 +98,6 @@ "use strict"; | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -104,0 +104,0 @@ *entries() { |
@@ -75,3 +75,3 @@ "use strict"; | ||
*keys() { | ||
yield* __classPrivateFieldGet(this, _LFU_queue, "f")[Symbol.iterator](); | ||
yield* __classPrivateFieldGet(this, _LFU_queue, "f"); | ||
} | ||
@@ -84,3 +84,3 @@ *values() { | ||
*entries() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -87,0 +87,0 @@ forEach(callbackFn, thisArg) { |
@@ -138,6 +138,6 @@ "use strict"; | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -144,0 +144,0 @@ *entities() { |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Trie = exports.Stack = exports.Queue = exports.PubSub = exports.LRU = exports.LinkedList = exports.LFU = exports.Heap = exports.FQueue = exports.CircularList = exports.Channel = exports.Buffer = void 0; | ||
exports.Trie = exports.Stack = exports.Queue = exports.QuadTree = exports.PubSub = exports.LRU = exports.LinkedList = exports.LFU = exports.Heap = exports.FQueue = exports.CircularList = exports.Channel = exports.Buffer = exports.BloomFilter = void 0; | ||
const heap_js_1 = __importDefault(require("./heap.js")); | ||
@@ -32,1 +32,5 @@ exports.Heap = heap_js_1.default; | ||
exports.Channel = channel_js_1.default; | ||
const qtree_js_1 = __importDefault(require("./qtree.js")); | ||
exports.QuadTree = qtree_js_1.default; | ||
const bloom_js_1 = __importDefault(require("./bloom.js")); | ||
exports.BloomFilter = bloom_js_1.default; |
@@ -48,16 +48,13 @@ "use strict"; | ||
__classPrivateFieldGet(this, _PubSub_buffer, "f").forEach(([topic, context]) => handler(context, topic)); | ||
// Attach the handler | ||
if (!__classPrivateFieldGet(this, _PubSub_handlers, "f").has(topic)) { | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").set(topic, new Set()); | ||
// Get he handlers for the topic | ||
const current = __classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic); | ||
const handlers = current ?? new Set(); | ||
// Add the handler | ||
handlers.add(h); | ||
// Store it needed | ||
if (current == null) { | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").set(topic, handlers); | ||
} | ||
// Attach the handler | ||
__classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic).add(h); | ||
// Unsubscribe | ||
return () => { | ||
// Load the handlers | ||
const handlers = __classPrivateFieldGet(this, _PubSub_handlers, "f").get(topic); | ||
// Nothing to remove | ||
if (handlers == null) { | ||
return; | ||
} | ||
// Remove the handler | ||
@@ -64,0 +61,0 @@ handlers.delete(h); |
@@ -51,6 +51,6 @@ "use strict"; | ||
*keys() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
*values() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -57,0 +57,0 @@ *entries() { |
@@ -13,70 +13,8 @@ "use strict"; | ||
}; | ||
var _Trie_instances, _Trie_head, _Trie_size, _Trie_find; | ||
var _Trie_value, _Trie_nodes, _Trie_size; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Breadth first traversal for the node that is returning an iterator | ||
/* | ||
function* bfs<K, V>( | ||
node: Tree<K, V>, | ||
prefix: K[], | ||
): IterableIterator<[K[], V]> { | ||
const queue = new Queue< | ||
[Tree<K, V>, K[]] | ||
>([[node, prefix]]); | ||
while (queue.size > 0) { | ||
// A list of nodes, a value node maybe -> If value node, return the value | ||
// Else go to the next node and find the node | ||
const current = queue.pop(); | ||
// We are done, no more iterators | ||
if (current == null) { | ||
return; | ||
} | ||
// Get the current path/keys and node | ||
const [[subtree, value], keys] = current; | ||
for (const [k, v] of subtree) { | ||
queue.push([v as Tree<K, V>, [...keys, k]]); | ||
} | ||
if (value !== undefined) { | ||
yield [keys, value]; | ||
} | ||
} | ||
} | ||
*/ | ||
// Depth first traversal for the node that is returning an iterator | ||
function* dfs(node, prefix) { | ||
const [subtree, value] = node; | ||
if (value !== undefined) { | ||
yield [prefix, value]; | ||
} | ||
if (subtree.size === 0) { | ||
return; | ||
} | ||
for (const [k, v] of subtree) { | ||
yield* dfs(v, prefix.concat(k)); | ||
} | ||
} | ||
// Traverse node by path | ||
function* traverse(node, keys, index = 0) { | ||
// We are done once we reach the end of the keys | ||
if (index > keys.length) { | ||
return; | ||
} | ||
// Load the data from the node | ||
const [subtree, value] = node; | ||
if (value !== undefined) { | ||
yield [keys.slice(0, index), value]; | ||
} | ||
const k = keys[index]; | ||
if (subtree.has(k)) { | ||
yield* traverse(subtree.get(k), keys, index + 1); | ||
} | ||
} | ||
class Trie { | ||
constructor(entries) { | ||
_Trie_instances.add(this); | ||
_Trie_head.set(this, [new Map()]); | ||
_Trie_value.set(this, [false]); | ||
_Trie_nodes.set(this, new Map()); | ||
_Trie_size.set(this, 0); | ||
@@ -89,23 +27,47 @@ if (entries != null) { | ||
} | ||
ref(keys) { | ||
// Root value | ||
if (keys.length === 0) { | ||
return this; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Create the leaf if it doesn't exist | ||
if (!__classPrivateFieldGet(this, _Trie_nodes, "f").has(key)) { | ||
return; | ||
} | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Find the next | ||
return node.ref(rest); | ||
} | ||
set(keys, value) { | ||
var _a; | ||
// Start at the head | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Iterate over the keys to find the node where to insert | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree] = node; | ||
// Create a child node if we don't have one | ||
if (!subtree.has(key)) { | ||
subtree.set(key, [new Map()]); | ||
// Root value | ||
if (keys.length === 0) { | ||
// Check if the value was already set | ||
const [isSet] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Set the value | ||
__classPrivateFieldSet(this, _Trie_value, [true, value], "f"); | ||
// Increase the size if it wasn't set | ||
if (!isSet) { | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a++, _a), "f"); | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
// Chain | ||
return this; | ||
} | ||
// If this is new, we increase the size | ||
if (node[1] === undefined) { | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a++, _a), "f"); | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Create the leaf if it doesn't exist | ||
if (!__classPrivateFieldGet(this, _Trie_nodes, "f").has(key)) { | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").set(key, new Trie()); | ||
} | ||
// Set the value on the value | ||
node[1] = value; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Get the old size | ||
const oldSize = node.size; | ||
// Set the value on the node | ||
node.set(rest, value); | ||
// Update the size | ||
__classPrivateFieldSet(this, _Trie_size, __classPrivateFieldGet(this, _Trie_size, "f") + (node.size - oldSize), "f"); | ||
// Chain | ||
@@ -115,52 +77,69 @@ return this; | ||
get(keys) { | ||
// Start at the head | ||
const node = __classPrivateFieldGet(this, _Trie_instances, "m", _Trie_find).call(this, keys); | ||
// Get the value from the node | ||
return node?.[1]; | ||
// Get the node | ||
const node = this.ref(keys); | ||
// Node was not found | ||
if (node === undefined) { | ||
return; | ||
} | ||
// Found the value, get it | ||
if (node === this) { | ||
return __classPrivateFieldGet(this, _Trie_value, "f")[1]; | ||
} | ||
// Check the node recursively | ||
return node.get([]); | ||
} | ||
has(keys) { | ||
return this.get(keys) !== undefined; | ||
// Get the node | ||
const node = this.ref(keys); | ||
// Node was not found | ||
if (node === undefined) { | ||
return false; | ||
} | ||
// Found the value, get it | ||
if (node === this) { | ||
return __classPrivateFieldGet(this, _Trie_value, "f")[0]; | ||
} | ||
// Check the node recursively | ||
return node.has([]); | ||
} | ||
delete(keys) { | ||
var _a; | ||
// Start at the head | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Create a clean function | ||
// We start witht the assumption we need to remove everything | ||
let clean = () => { | ||
node[0].clear(); | ||
}; | ||
// Iterate over the keys | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree, value] = node; | ||
// Get the child node | ||
if (!subtree.has(key)) { | ||
return false; | ||
var _a, _b; | ||
// Root value | ||
if (keys.length === 0) { | ||
const [isSet] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Delete the value if set | ||
if (isSet) { | ||
__classPrivateFieldSet(this, _Trie_value, [false], "f"); | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a--, _a), "f"); | ||
return true; | ||
} | ||
// Check if the current subtree contains any other data | ||
const stale = subtree.size === 1 && value === undefined; | ||
// If we have other info, we update the assumption, only remove the key subtree | ||
if (!stale) { | ||
clean = () => { | ||
subtree.delete(key); | ||
}; | ||
// Nothing to do if not set | ||
return false; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Nothing to do if not found | ||
if (node === undefined) { | ||
return false; | ||
} | ||
// Attept to delete the node | ||
if (node.delete(rest)) { | ||
// Reduce the size | ||
__classPrivateFieldSet(this, _Trie_size, (_b = __classPrivateFieldGet(this, _Trie_size, "f"), _b--, _b), "f"); | ||
// Delete the node if it's empty | ||
if (node.size === 0) { | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").delete(key); | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
// We deleted it | ||
return true; | ||
} | ||
// Get the value from the node | ||
node[1] = undefined; | ||
// Reduce the size | ||
__classPrivateFieldSet(this, _Trie_size, (_a = __classPrivateFieldGet(this, _Trie_size, "f"), _a--, _a), "f"); | ||
// Clean only if this is empty | ||
if (node[0].size === 0) { | ||
clean(); | ||
} | ||
// We deleted it | ||
return true; | ||
// We didn't delete it | ||
return false; | ||
} | ||
clear() { | ||
__classPrivateFieldSet(this, _Trie_head, [new Map()], "f"); | ||
__classPrivateFieldSet(this, _Trie_value, [false], "f"); | ||
__classPrivateFieldSet(this, _Trie_size, 0, "f"); | ||
__classPrivateFieldGet(this, _Trie_nodes, "f").clear(); | ||
} | ||
@@ -173,3 +152,20 @@ get size() { | ||
*match(keys) { | ||
yield* traverse(__classPrivateFieldGet(this, _Trie_head, "f"), keys); | ||
// Root value | ||
const [isSet, value] = __classPrivateFieldGet(this, _Trie_value, "f"); | ||
// Get the value if set | ||
if (isSet) { | ||
yield [[], value]; | ||
} | ||
// Get the first key | ||
const [key, ...rest] = keys; | ||
// Get the node | ||
const node = __classPrivateFieldGet(this, _Trie_nodes, "f").get(key); | ||
// Nothing to do if no node | ||
if (node === undefined) { | ||
return; | ||
} | ||
// Match next layers | ||
for (const [k, v] of node.match(rest)) { | ||
yield [[key, ...k], v]; | ||
} | ||
} | ||
@@ -179,25 +175,22 @@ // This will get all the values starting with the path, | ||
*list(keys) { | ||
// Find the node | ||
const node = __classPrivateFieldGet(this, _Trie_instances, "m", _Trie_find).call(this, keys); | ||
// Find the root node based on the keys | ||
const node = this.ref(keys); | ||
// Nothing to do if no node | ||
if (node === undefined) { | ||
return; | ||
} | ||
yield* dfs(node, keys); | ||
// Traverse the node once found | ||
yield* node; | ||
} | ||
*[(_Trie_head = new WeakMap(), _Trie_size = new WeakMap(), _Trie_instances = new WeakSet(), _Trie_find = function _Trie_find(keys) { | ||
let node = __classPrivateFieldGet(this, _Trie_head, "f"); | ||
// Iterate over the keys | ||
for (const key of keys) { | ||
// Load the subtree | ||
const [subtree] = node; | ||
// Get the child node | ||
if (!subtree.has(key)) { | ||
return undefined; | ||
*[(_Trie_value = new WeakMap(), _Trie_nodes = new WeakMap(), _Trie_size = new WeakMap(), Symbol.iterator)]() { | ||
// Get the value of the node if there is one | ||
if (this.has([])) { | ||
yield [[], this.get([])]; | ||
} | ||
// Traverse the other nodes | ||
for (const [key, node] of __classPrivateFieldGet(this, _Trie_nodes, "f")) { | ||
for (const [k, v] of node) { | ||
yield [[key, ...k], v]; | ||
} | ||
// Go to the next level | ||
node = subtree.get(key); | ||
} | ||
return node; | ||
}, Symbol.iterator)]() { | ||
yield* this.list([]); | ||
} | ||
@@ -215,3 +208,3 @@ *keys() { | ||
*entries() { | ||
yield* this[Symbol.iterator](); | ||
yield* this; | ||
} | ||
@@ -218,0 +211,0 @@ forEach(callbackFn, thisArg) { |
@@ -13,2 +13,4 @@ import Heap from "./heap.js"; | ||
import Channel from "./channel.js"; | ||
export { Buffer, Channel, CircularList, FQueue, Heap, LFU, LinkedList, LRU, PubSub, Queue, Stack, Trie, }; | ||
import QuadTree from "./qtree.js"; | ||
import BloomFilter from "./bloom.js"; | ||
export { BloomFilter, Buffer, Channel, CircularList, FQueue, Heap, LFU, LinkedList, LRU, PubSub, QuadTree, Queue, Stack, Trie, }; |
declare class Trie<K, V> { | ||
#private; | ||
constructor(entries?: readonly [K[], V][]); | ||
ref(keys: K[]): Trie<K, V> | undefined; | ||
set(keys: K[], value: V): this; | ||
@@ -5,0 +6,0 @@ get(keys: K[]): V | undefined; |
178342
50
2945