trie.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Suffix Trie based on Mnemonist Trie
  3. */
  4. // import { Trie } from 'mnemonist';
  5. export const SENTINEL = Symbol('SENTINEL');
  6. type TrieNode = {
  7. [SENTINEL]: boolean,
  8. [Bun.inspect.custom]: () => string
  9. } & Map<string, TrieNode>;
  10. const deepTrieNodeToJSON = (node: TrieNode) => {
  11. const obj: Record<string, any> = {};
  12. if (node[SENTINEL]) {
  13. obj['[start]'] = node[SENTINEL];
  14. }
  15. node.forEach((value, key) => {
  16. obj[key] = deepTrieNodeToJSON(value);
  17. });
  18. return obj;
  19. };
  20. function trieNodeInspectCustom(this: TrieNode) {
  21. return JSON.stringify(deepTrieNodeToJSON(this), null, 2);
  22. }
  23. const createNode = (): TrieNode => {
  24. const node = new Map<string, TrieNode>() as TrieNode;
  25. node[SENTINEL] = false;
  26. node[Bun.inspect.custom] = trieNodeInspectCustom;
  27. return node;
  28. };
  29. export const createTrie = (from?: string[] | Set<string> | null) => {
  30. let size = 0;
  31. const root: TrieNode = createNode();
  32. /**
  33. * Method used to add the given prefix to the trie.
  34. */
  35. const add = (suffix: string): void => {
  36. let node: TrieNode = root;
  37. let token: string;
  38. for (let i = suffix.length - 1; i >= 0; i--) {
  39. token = suffix[i];
  40. if (node.has(token)) {
  41. node = node.get(token)!;
  42. } else {
  43. const newNode = createNode();
  44. node.set(token, newNode);
  45. node = newNode;
  46. }
  47. }
  48. // Do we need to increase size?
  49. if (!node[SENTINEL]) {
  50. size++;
  51. }
  52. node[SENTINEL] = true;
  53. };
  54. /**
  55. * @param {string} suffix
  56. */
  57. const contains = (suffix: string): boolean => {
  58. let node: TrieNode | undefined = root;
  59. let token: string;
  60. for (let i = suffix.length - 1; i >= 0; i--) {
  61. token = suffix[i];
  62. node = node.get(token);
  63. if (!node) return false;
  64. }
  65. return true;
  66. };
  67. /**
  68. * Method used to retrieve every item in the trie with the given prefix.
  69. */
  70. const find = (inputSuffix: string, /** @default true */ includeEqualWithSuffix = true): string[] => {
  71. let node: TrieNode | undefined = root;
  72. let token: string;
  73. for (let i = inputSuffix.length - 1; i >= 0; i--) {
  74. token = inputSuffix[i];
  75. node = node.get(token);
  76. if (!node) return [];
  77. }
  78. const matches: string[] = [];
  79. // Performing DFS from prefix
  80. const nodeStack: TrieNode[] = [node];
  81. const suffixStack: string[] = [inputSuffix];
  82. do {
  83. const suffix: string = suffixStack.pop()!;
  84. node = nodeStack.pop()!;
  85. if (node[SENTINEL]) {
  86. if (includeEqualWithSuffix || suffix !== inputSuffix) {
  87. matches.push(suffix);
  88. }
  89. }
  90. node.forEach((childNode, k) => {
  91. nodeStack.push(childNode);
  92. suffixStack.push(k + suffix);
  93. });
  94. } while (nodeStack.length);
  95. return matches;
  96. };
  97. /**
  98. * Works like trie.find, but instead of returning the matches as an array, it removes them from the given set in-place.
  99. */
  100. const substractSetInPlaceFromFound = (inputSuffix: string, set: Set<string>) => {
  101. let node: TrieNode | undefined = root;
  102. let token: string;
  103. // Find the leaf-est node, and early return if not any
  104. for (let i = inputSuffix.length - 1; i >= 0; i--) {
  105. token = inputSuffix[i];
  106. node = node.get(token);
  107. if (!node) return;
  108. }
  109. // Performing DFS from prefix
  110. const nodeStack: TrieNode[] = [node];
  111. const suffixStack: string[] = [inputSuffix];
  112. do {
  113. const suffix = suffixStack.pop()!;
  114. node = nodeStack.pop()!;
  115. if (node[SENTINEL]) {
  116. if (suffix !== inputSuffix) {
  117. // found match, delete it from set
  118. set.delete(suffix);
  119. }
  120. }
  121. node.forEach((childNode, k) => {
  122. nodeStack.push(childNode);
  123. suffixStack.push(k + suffix);
  124. });
  125. } while (nodeStack.length);
  126. };
  127. /**
  128. * Method used to delete a prefix from the trie.
  129. */
  130. const remove = (suffix: string): boolean => {
  131. let node: TrieNode | undefined = root;
  132. let toPrune: TrieNode | null = null;
  133. let tokenToPrune: string | null = null;
  134. let parent: TrieNode = node;
  135. let token: string;
  136. for (let i = suffix.length - 1; i >= 0; i--) {
  137. token = suffix[i];
  138. parent = node;
  139. node = node.get(token);
  140. if (!node) {
  141. return false;
  142. }
  143. // Keeping track of a potential branch to prune
  144. // If the node is to be pruned, but they are more than one token child in it, we can't prune it
  145. // If there is only one token child, or no child at all, we can prune it safely
  146. const onlyChild = node.size === 1 && node.has(token);
  147. if (onlyChild) {
  148. toPrune = parent;
  149. tokenToPrune = token;
  150. } else if (toPrune !== null) { // not only child, retain the branch
  151. toPrune = null;
  152. tokenToPrune = null;
  153. }
  154. }
  155. if (!node[SENTINEL]) return false;
  156. size--;
  157. if (tokenToPrune && toPrune) {
  158. toPrune.delete(tokenToPrune);
  159. } else {
  160. node[SENTINEL] = false;
  161. }
  162. return true;
  163. };
  164. /**
  165. * Method used to assert whether the given prefix exists in the Trie.
  166. */
  167. const has = (suffix: string): boolean => {
  168. let node: TrieNode = root;
  169. for (let i = suffix.length - 1; i >= 0; i--) {
  170. const token = suffix[i];
  171. if (node.has(token)) {
  172. node = node.get(token)!;
  173. } else {
  174. return false;
  175. }
  176. }
  177. return node[SENTINEL];
  178. };
  179. if (Array.isArray(from)) {
  180. for (let i = 0, l = from.length; i < l; i++) {
  181. add(from[i]);
  182. }
  183. } else if (from) {
  184. from.forEach(add);
  185. }
  186. const dump = () => {
  187. const node = root;
  188. const nodeStack: TrieNode[] = [];
  189. const suffixStack: string[] = [];
  190. // Resolving initial string
  191. const suffix = '';
  192. nodeStack.push(node);
  193. suffixStack.push(suffix);
  194. const results: string[] = [];
  195. let currentNode: TrieNode;
  196. let currentPrefix: string;
  197. let hasValue = false;
  198. do {
  199. currentNode = nodeStack.pop()!;
  200. currentPrefix = suffixStack.pop()!;
  201. if (currentNode[SENTINEL]) {
  202. hasValue = true;
  203. }
  204. node.forEach((childNode, k) => {
  205. nodeStack.push(childNode);
  206. suffixStack.push(k + suffix);
  207. });
  208. if (hasValue) results.push(currentPrefix);
  209. } while (nodeStack.length);
  210. return results;
  211. };
  212. return {
  213. add,
  214. contains,
  215. find,
  216. substractSetInPlaceFromFound,
  217. remove,
  218. delete: remove,
  219. has,
  220. dump,
  221. get size() {
  222. return size;
  223. },
  224. get root() {
  225. return root;
  226. },
  227. [Bun.inspect.custom]: () => JSON.stringify(deepTrieNodeToJSON(root), null, 2)
  228. };
  229. };
  230. export default createTrie;