From b86d963704e7ffc8be80add50c23294599ad3856 Mon Sep 17 00:00:00 2001 From: Youngteac Hong Date: Thu, 21 Sep 2023 10:35:28 +0900 Subject: [PATCH] Remove unused trie (#651) Trie was initially introduced to reduce the number of events from a common ancestor(#351). However, it was later removed during the implementation of the topic subscribe feature(#487, #566). For now, I'll remove unused Trie and reopen the event-reducing issue again(#266). --- src/util/trie.ts | 126 ------------------------------------ test/unit/util/trie_test.ts | 51 --------------- 2 files changed, 177 deletions(-) delete mode 100644 src/util/trie.ts delete mode 100644 test/unit/util/trie_test.ts diff --git a/src/util/trie.ts b/src/util/trie.ts deleted file mode 100644 index 78e33eea6..000000000 --- a/src/util/trie.ts +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2022 The Yorkie Authors. 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License 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. - */ - -/** - * `TrieNode` is node of Trie. - */ -export class TrieNode { - public value: V; - public children: Record>; - public parent?: TrieNode; - public isTerminal: boolean; - - constructor(value: V, parent?: TrieNode) { - this.value = value; - this.children = {} as Record>; - this.isTerminal = false; - this.parent = parent; - } - - /** - * `getPath` returns the path from the Trie root to this node. - * @returns array of path from Trie root to this node - */ - public getPath(): Array { - const path: Array = []; - let node: TrieNode | undefined = this; - while (node) { - path.unshift(node.value); - node = node.parent; - } - return path; - } -} - -/** - * `Trie` is a type of k-ary search tree for locating specific values or common prefixes. - */ -export class Trie { - private root: TrieNode; - - constructor(value: V) { - this.root = new TrieNode(value); - } - - /** - * `insert` inserts the value to the Trie - * @param values - values array - * @returns array of find result - */ - public insert(values: Array): void { - let node = this.root; - for (const value of values) { - if (!node.children[value]) { - node.children[value] = new TrieNode(value, node); - } - node = node.children[value]; - } - node.isTerminal = true; - } - - /** - * `find` finds all words that have the prefix in the Trie - * @param prefix - prefix array - */ - public find(prefix: Array): Array> { - let node = this.root; - const output: Array> = []; - for (const value of prefix) { - if (node.children[value]) { - node = node.children[value]; - } else { - return output; - } - } - this.traverse(node, true, output); - return output; - } - - /** - * `traverse` does a depth first to push necessary elements to the output - * @param node - node to start the depth first search - * @param isTerminalIncluded - whether to traverse till the terminal or not - * @param output - the output array - */ - public traverse( - node: TrieNode, - isTerminalIncluded: boolean, - output: Array>, - ): void { - if (node.isTerminal) { - output.push(node.getPath()); - if (!isTerminalIncluded) { - return; - } - } - for (const [, value] of Object.entries(node.children)) { - this.traverse(value as TrieNode, isTerminalIncluded, output); - } - } - - /** - * `findPrefixes` finds the prefixes added to the Trie. - * @returns array of prefixes - */ - public findPrefixes(): Array> { - const prefixes: Array> = []; - for (const [, value] of Object.entries(this.root.children)) { - const child = value as TrieNode; - this.traverse(child, false, prefixes); - } - return prefixes; - } -} diff --git a/test/unit/util/trie_test.ts b/test/unit/util/trie_test.ts deleted file mode 100644 index 234be4375..000000000 --- a/test/unit/util/trie_test.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2020 The Yorkie Authors. 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License 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. - */ - -import { Trie } from '@yorkie-js-sdk/src/util/trie'; -import { assert } from 'chai'; - -describe('Trie', function () { - const philWords = ['phil', 'philosophy', 'philanthropy', 'philadelphia']; - const unWords = ['un', 'undo', 'unpack', 'unhappy']; - const otherWords = ['english', 'hello']; - const words = [...philWords, ...unWords, ...otherWords]; - it('can find words with specific prefix', function () { - const trie = new Trie(''); - for (const word of words) { - trie.insert(word.split('')); - } - const philResult = trie - .find('phil'.split('')) - .map((element) => element.join('')); - const unResult = trie - .find('un'.split('')) - .map((element) => element.join('')); - assert.deepEqual(philWords.sort(), philResult.sort()); - assert.deepEqual(unWords.sort(), unResult.sort()); - }); - - it('can find prefixes', function () { - const trie = new Trie(''); - for (const word of words) { - trie.insert(word.split('')); - } - const commonPrefixes = ['phil', 'un', ...otherWords]; - const prefixesResult = trie - .findPrefixes() - .map((element) => element.join('')); - assert.deepEqual(commonPrefixes.sort(), prefixesResult.sort()); - }); -});