@thi.ng/trie 2.0.22 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,9 +7,11 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 215 standalone projects, maintained as part
11
- > of the [@thi.ng/umbrella](https://codeberg.org/thi.ng/umbrella/) ecosystem
12
- > and anti-framework.
10
+
11
+ > This is one of 215 standalone projects. LLM-free, human-made and
12
+ > cared for software, maintained as part of the
13
+ > [@thi.ng/umbrella](https://codeberg.org/thi.ng/umbrella/) ecosystem and
14
+ > anti-framework.
13
15
  >
14
16
  > 🚀 Please help me to work full-time on these projects by [sponsoring
15
17
  > me](https://codeberg.org/thi.ng/umbrella/src/branch/develop/CONTRIBUTING.md#donations).
@@ -144,7 +146,7 @@ For Node.js REPL:
144
146
  const trie = await import("@thi.ng/trie");
145
147
  ```
146
148
 
147
- Package sizes (brotli'd, pre-treeshake): ESM: 1.14 KB
149
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.21 KB
148
150
 
149
151
  ## Dependencies
150
152
 
package/multi-trie.d.ts CHANGED
@@ -16,6 +16,7 @@ export declare class MultiTrie<K, V> {
16
16
  next: Map<K, MultiTrie<K, V>>;
17
17
  vals?: Set<V>;
18
18
  constructor(pairs?: Nullable<Iterable<Pair<K[], V>>>, opts?: Partial<MultiTrieOpts<V>> | undefined);
19
+ get size(): number;
19
20
  [Symbol.iterator](): Generator<Pair<K[], V>, void, any>;
20
21
  keys(prefix?: K[], includePrefix?: boolean): Generator<K[], void, any>;
21
22
  values(prefix?: K[]): Generator<V, void, any>;
package/multi-trie.js CHANGED
@@ -6,6 +6,16 @@ class MultiTrie {
6
6
  opts;
7
7
  next = /* @__PURE__ */ new Map();
8
8
  vals;
9
+ get size() {
10
+ let size = 0;
11
+ const stack = [this];
12
+ while (stack.length) {
13
+ const node = stack.pop();
14
+ size += node.vals?.size ?? 0;
15
+ if (node.next) stack.push(...node.next.values());
16
+ }
17
+ return size;
18
+ }
9
19
  [Symbol.iterator]() {
10
20
  return this.iterate(
11
21
  (key, node) => [...node.vals].map((v) => [key, v])
@@ -131,17 +141,15 @@ class MultiTrie {
131
141
  *iterate(fn, prefix = [], includePrefix = true) {
132
142
  const root = this.find(prefix);
133
143
  if (!root) return;
134
- const queue = [
144
+ const stack = [
135
145
  [includePrefix ? prefix : [], root]
136
146
  ];
137
- while (queue.length) {
138
- const [key, node] = queue.pop();
147
+ while (stack.length) {
148
+ const [key, node] = stack.pop();
139
149
  if (node.vals) yield* fn(key, node);
140
- queue.push(
141
- ...[...node.next].map(
142
- ([k, v]) => [key.concat(k), v]
143
- )
144
- );
150
+ for (let [k, v] of node.next) {
151
+ stack.push([key.concat(k), v]);
152
+ }
145
153
  }
146
154
  }
147
155
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/trie",
3
- "version": "2.0.22",
3
+ "version": "2.1.2",
4
4
  "description": "Trie-based ES6-like Map data structures with prefix search/query support",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,7 +40,7 @@
40
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@thi.ng/api": "^8.12.22"
43
+ "@thi.ng/api": "^8.12.24"
44
44
  },
45
45
  "devDependencies": {
46
46
  "esbuild": "^0.28.0",
@@ -94,5 +94,5 @@
94
94
  ],
95
95
  "year": 2020
96
96
  },
97
- "gitHead": "ecb3c8877c7f6d086daec889bf618fe3558d87f2"
97
+ "gitHead": "b302822c51b2ab255f8f27f8dd010433d95f29df"
98
98
  }
package/trie-map.d.ts CHANGED
@@ -4,6 +4,7 @@ export declare class TrieMap<T> {
4
4
  val?: T;
5
5
  protected n: number;
6
6
  constructor(pairs?: Iterable<Pair<string, T>>);
7
+ get size(): number;
7
8
  [Symbol.iterator](): Generator<Pair<string, T>, void, unknown>;
8
9
  keys(prefix?: string, includePrefix?: boolean): Generator<string, void, unknown>;
9
10
  values(prefix?: string): Generator<NonNullable<T>, void, unknown>;
package/trie-map.js CHANGED
@@ -5,6 +5,16 @@ class TrieMap {
5
5
  constructor(pairs) {
6
6
  pairs && this.into(pairs);
7
7
  }
8
+ get size() {
9
+ let size = 0;
10
+ const stack = [this];
11
+ while (stack.length) {
12
+ const node = stack.pop();
13
+ if (node.val !== void 0) size++;
14
+ if (node.next) stack.push(...Object.values(node.next));
15
+ }
16
+ return size;
17
+ }
8
18
  [Symbol.iterator]() {
9
19
  return this.iterate((key, node) => [key, node.val]);
10
20
  }
@@ -102,17 +112,16 @@ class TrieMap {
102
112
  *iterate(fn, prefix = "", includePrefix = true) {
103
113
  const root = this.find(prefix);
104
114
  if (!root) return;
105
- const queue = [
115
+ const stack = [
106
116
  [includePrefix ? prefix : "", root]
107
117
  ];
108
- while (queue.length) {
109
- const [key, node] = queue.pop();
118
+ while (stack.length) {
119
+ const [key, node] = stack.pop();
110
120
  if (node.val !== void 0) yield fn(key, node);
111
- queue.push(
112
- ...Object.entries(node.next).map(
113
- ([k, v]) => [key + k, v]
114
- )
115
- );
121
+ const next = node.next;
122
+ for (let k in next) {
123
+ stack.push([key + k, next[k]]);
124
+ }
116
125
  }
117
126
  }
118
127
  }