@mangos/radix 0.0.9
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 +14 -0
- package/dist/RadixNodeImpl.d.ts +15 -0
- package/dist/RadixNodeImpl.js +118 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/dist/types/RadixNode.d.ts +12 -0
- package/dist/types/Token.d.ts +3 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
<h1>Radix</h1>
|
|
3
|
+
|
|
4
|
+
This is an implementation radix index based on token objects instead of string characters.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2019-2025 Jacob Bogers `jkfbogers@gmail.com`.
|
|
8
|
+
|
|
9
|
+
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
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
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.
|
|
14
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { RadixNode } from './types/RadixNode.js';
|
|
2
|
+
import type { Token } from './types/Token.js';
|
|
3
|
+
export declare class RadixNodeImpl<T extends Token> implements RadixNode<T> {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(id: T, parent?: RadixNodeImpl<T>);
|
|
6
|
+
insert(path: readonly T[]): number;
|
|
7
|
+
select(path: readonly T[]): null | RadixNode<T>;
|
|
8
|
+
isPathTerminal(path: readonly T[]): boolean;
|
|
9
|
+
children(): RadixNodeImpl<T>[];
|
|
10
|
+
parent(): RadixNode<T>;
|
|
11
|
+
pathFromRoot(): T[];
|
|
12
|
+
id(): T;
|
|
13
|
+
nrTerminals(): number;
|
|
14
|
+
delete(path: readonly T[]): number;
|
|
15
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
class RadixNodeImpl {
|
|
2
|
+
#parent;
|
|
3
|
+
#id;
|
|
4
|
+
#children;
|
|
5
|
+
constructor(id, parent) {
|
|
6
|
+
this.#parent = parent ?? this;
|
|
7
|
+
this.#children = [];
|
|
8
|
+
this.#id = id;
|
|
9
|
+
}
|
|
10
|
+
// the path[0] must match one of its children
|
|
11
|
+
insert(path) {
|
|
12
|
+
if (path.length === 0) {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
const find = this.#children.find((child) => child.id().equals(path[0]));
|
|
16
|
+
if (find) {
|
|
17
|
+
return find.insert(path.slice(1));
|
|
18
|
+
}
|
|
19
|
+
const radixNode = new RadixNodeImpl(path[0], this);
|
|
20
|
+
const nrNodes = 1 + radixNode.insert(path.slice(1));
|
|
21
|
+
this.#children.push(radixNode);
|
|
22
|
+
return nrNodes;
|
|
23
|
+
}
|
|
24
|
+
select(path) {
|
|
25
|
+
if (path.length === 0) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (!path[0].equals(this.#id)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
if (path.length === 1) {
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
if (this.children().length === 0) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
for (const child of this.children()) {
|
|
38
|
+
const result = child.select(path.slice(1));
|
|
39
|
+
if (result !== null) {
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
// the path is not an empty array or it
|
|
46
|
+
isPathTerminal(path) {
|
|
47
|
+
const radixNode = this.select(path);
|
|
48
|
+
if (radixNode?.children().length === 0) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
children() {
|
|
54
|
+
return this.#children;
|
|
55
|
+
}
|
|
56
|
+
parent() {
|
|
57
|
+
return this.#parent;
|
|
58
|
+
}
|
|
59
|
+
pathFromRoot() {
|
|
60
|
+
if (this.#parent === this) {
|
|
61
|
+
return [this.#id];
|
|
62
|
+
}
|
|
63
|
+
return [...this.parent().pathFromRoot(), this.id()];
|
|
64
|
+
}
|
|
65
|
+
id() {
|
|
66
|
+
return this.#id;
|
|
67
|
+
}
|
|
68
|
+
nrTerminals() {
|
|
69
|
+
if (this.children().length === 0) {
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
return this.children().reduce((sum, child) => {
|
|
73
|
+
return sum + child.nrTerminals();
|
|
74
|
+
}, 0);
|
|
75
|
+
}
|
|
76
|
+
// you can only delete terminals
|
|
77
|
+
delete(path) {
|
|
78
|
+
if (path.length === 0) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
if (!path[0].equals(this.#id)) {
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
if (path.length === 1) {
|
|
85
|
+
if (this.#children.length > 0) {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
if (this.#parent === this) {
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
const index2 = this.#parent.#children.indexOf(this);
|
|
92
|
+
this.#parent.#children.splice(index2, 1);
|
|
93
|
+
this.#parent = this;
|
|
94
|
+
return 1;
|
|
95
|
+
}
|
|
96
|
+
if (this.#children.length === 0) {
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
const child = this.#children.find((child2) => child2.#id.equals(path[1]));
|
|
100
|
+
if (child === void 0) {
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
const nrNodesChanged = child.delete(path.slice(1));
|
|
104
|
+
if (child.#parent !== child) {
|
|
105
|
+
return nrNodesChanged;
|
|
106
|
+
}
|
|
107
|
+
if (this.#children.length > 0) {
|
|
108
|
+
return nrNodesChanged;
|
|
109
|
+
}
|
|
110
|
+
const index = this.#parent.#children.indexOf(this);
|
|
111
|
+
this.#parent.#children.splice(index, 1);
|
|
112
|
+
this.#parent = this;
|
|
113
|
+
return nrNodesChanged + 1;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
RadixNodeImpl
|
|
118
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Token } from './Token';
|
|
2
|
+
export interface RadixNode<T extends Token> {
|
|
3
|
+
nrTerminals(): number;
|
|
4
|
+
select(path: readonly T[]): null | RadixNode<T>;
|
|
5
|
+
delete(this: RadixNode<T>, path: readonly T[]): number;
|
|
6
|
+
insert(path: readonly T[]): number;
|
|
7
|
+
isPathTerminal(path: readonly T[]): boolean;
|
|
8
|
+
parent(): RadixNode<T> | undefined;
|
|
9
|
+
pathFromRoot(): T[];
|
|
10
|
+
children(): RadixNode<T>[];
|
|
11
|
+
id(): T;
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mangos/radix",
|
|
3
|
+
"version": "0.0.9",
|
|
4
|
+
"author": "jacob bogers <jkfbogers@gmail.com>",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/R-js/mangos.git",
|
|
8
|
+
"directory": "packages/radix"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"main": "./dist/index.js",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@biomejs/biome": "^2.3.8",
|
|
20
|
+
"@mangos/filepath": "^1.0.3",
|
|
21
|
+
"@types/node": "^25.0.0",
|
|
22
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
23
|
+
"typescript": "^5.9.3",
|
|
24
|
+
"vite": "^7.2.7",
|
|
25
|
+
"vitest": "^4.0.15"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/R-js/mangos.git",
|
|
29
|
+
"email": "jkfbogers@gmail.com"
|
|
30
|
+
},
|
|
31
|
+
"description": "Radix implements a radix index based on tokens instead of strings",
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=v20.8.0"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"package.json"
|
|
39
|
+
],
|
|
40
|
+
"homepage": "https://github.com/R-js/mangos/blob/master/packages/radix/README.md",
|
|
41
|
+
"keywords": [
|
|
42
|
+
"radix",
|
|
43
|
+
"index",
|
|
44
|
+
"trie",
|
|
45
|
+
"try",
|
|
46
|
+
"ART"
|
|
47
|
+
],
|
|
48
|
+
"license": "Apache License, Version 2.0",
|
|
49
|
+
"scripts": {
|
|
50
|
+
"prebuild": "npm run lint",
|
|
51
|
+
"build": "vite build && tsc",
|
|
52
|
+
"prepublishOnly": "npm run build",
|
|
53
|
+
"lint": "biome check",
|
|
54
|
+
"test": "vitest run --coverage"
|
|
55
|
+
},
|
|
56
|
+
"sideEffects": false,
|
|
57
|
+
"type": "module"
|
|
58
|
+
}
|