tinytrie 0.1.0__py3-none-any.whl
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.
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinytrie
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal type-safe trie (prefix tree) implementation in Python.
|
|
5
|
+
Author-email: Jifeng Wu <jifengwu2k@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/jifengwu2k/tinytrie
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/jifengwu2k/tinytrie/issues
|
|
9
|
+
Keywords: trie,prefix tree,data structure,python
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.6
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# TinyTrie
|
|
18
|
+
|
|
19
|
+
A minimal and type-safe trie (prefix tree) implementation for Python 3.6+.
|
|
20
|
+
|
|
21
|
+
TinyTrie provides basic operations on Tries:
|
|
22
|
+
- Insertion of sequences (`search_or_create`)
|
|
23
|
+
- Search for sequences (`search`)
|
|
24
|
+
- Find the longest common prefix (`longest_common_prefix`)
|
|
25
|
+
- Collect all stored sequences (`collect_sequences`)
|
|
26
|
+
|
|
27
|
+
It supports any type of keys and values using Python's type hints.
|
|
28
|
+
|
|
29
|
+
## Features
|
|
30
|
+
|
|
31
|
+
- **Typed**: Works with arbitrary key and value types (`Generic[K, V]`)
|
|
32
|
+
- **Minimal**: Only essential functionalities
|
|
33
|
+
- **Efficient**: Memory-efficient with `__slots__`
|
|
34
|
+
- **Iterable**: Easily traverse and list all stored sequences
|
|
35
|
+
- **No external dependencies**
|
|
36
|
+
|
|
37
|
+
## Example Usage
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from tinytrie import TrieNode, search, search_or_create, longest_common_prefix, collect_sequences
|
|
41
|
+
|
|
42
|
+
# Create the root node
|
|
43
|
+
root = TrieNode[str, int]()
|
|
44
|
+
|
|
45
|
+
# Insert sequences
|
|
46
|
+
search_or_create(root, ['a', 'b', 'c'], value=1)
|
|
47
|
+
search_or_create(root, ['a', 'b', 'd'], value=2)
|
|
48
|
+
|
|
49
|
+
# Search for a sequence
|
|
50
|
+
node = search(root, ['a', 'b', 'c'])
|
|
51
|
+
if node is not None:
|
|
52
|
+
print(f"Found with value: {node.value}")
|
|
53
|
+
|
|
54
|
+
# Find the longest common prefix
|
|
55
|
+
prefix, node = longest_common_prefix(root)
|
|
56
|
+
print(f"Longest common prefix: {prefix}")
|
|
57
|
+
|
|
58
|
+
# Collect all sequences
|
|
59
|
+
for seq, node in collect_sequences(root):
|
|
60
|
+
print(f"Sequence: {seq}, Value: {node.value}")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Output:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
Found with value: 1
|
|
67
|
+
Longest common prefix: ['a', 'b']
|
|
68
|
+
Sequence: ['a', 'b', 'c'], Value: 1
|
|
69
|
+
Sequence: ['a', 'b', 'd'], Value: 2
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Installation
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install tinytrie
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
MIT License
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
tinytrie.py,sha256=LLAtTVhOPH8d5qvoUHt7hIuucb9uHsAl7gC-5vBLH70,2289
|
|
2
|
+
tinytrie-0.1.0.dist-info/licenses/LICENSE,sha256=FZ9XWedK_wQ4wfqVanrQVQpArRHDkxwxic2rgii1pZg,1066
|
|
3
|
+
tinytrie-0.1.0.dist-info/METADATA,sha256=_Jr-3DZvHXuyyKEKl8O6kPUEjNsyqyoie8g2_6m2La0,2139
|
|
4
|
+
tinytrie-0.1.0.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
|
|
5
|
+
tinytrie-0.1.0.dist-info/top_level.txt,sha256=LmhFpKkudLkEcL3grI6tGPlXow6ouTLGawd4KyatyVw,9
|
|
6
|
+
tinytrie-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jifeng Wu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tinytrie
|
tinytrie.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# tinytrie: A minimal and type-safe trie (prefix tree) implementation in Python.
|
|
2
|
+
# Copyright (c) 2025 Jifeng Wu
|
|
3
|
+
# Licensed under the MIT License. See LICENSE file in the project root for full license information.
|
|
4
|
+
from typing import TypeVar, Generic, Dict, Optional, Sequence, List, Tuple, Iterator
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"TrieNode",
|
|
9
|
+
"search",
|
|
10
|
+
"search_or_create",
|
|
11
|
+
"longest_common_prefix",
|
|
12
|
+
"collect_sequences",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
K = TypeVar("K")
|
|
17
|
+
V = TypeVar("V")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class TrieNode(Generic[K, V]):
|
|
21
|
+
__slots__ = ("children", "is_end", "value")
|
|
22
|
+
|
|
23
|
+
children: Dict[K, "TrieNode[K, V]"]
|
|
24
|
+
is_end: bool
|
|
25
|
+
value: Optional[V]
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
self.children = {}
|
|
29
|
+
self.is_end = False
|
|
30
|
+
self.value = None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def search(root: TrieNode[K, V], sequence: Sequence[K]) -> Optional[TrieNode[K, V]]:
|
|
34
|
+
if not sequence:
|
|
35
|
+
return root if root.is_end else None
|
|
36
|
+
first, remaining = sequence[0], sequence[1:]
|
|
37
|
+
if first not in root.children:
|
|
38
|
+
return None
|
|
39
|
+
return search(root.children[first], remaining)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def search_or_create(
|
|
43
|
+
root: TrieNode[K, V], sequence: Sequence[K], value: Optional[V] = None
|
|
44
|
+
) -> TrieNode[K, V]:
|
|
45
|
+
if not sequence:
|
|
46
|
+
if not root.is_end:
|
|
47
|
+
root.is_end = True
|
|
48
|
+
root.value = value
|
|
49
|
+
return root
|
|
50
|
+
first, remaining = sequence[0], sequence[1:]
|
|
51
|
+
if first not in root.children:
|
|
52
|
+
root.children[first] = TrieNode()
|
|
53
|
+
return search_or_create(root.children[first], remaining, value)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def longest_common_prefix(root: TrieNode[K, V]) -> Tuple[Sequence[K], TrieNode[K, V]]:
|
|
57
|
+
prefix = []
|
|
58
|
+
node = root
|
|
59
|
+
|
|
60
|
+
while True:
|
|
61
|
+
# Stop if node is end of word or has multiple children
|
|
62
|
+
if node.is_end or len(node.children) != 1:
|
|
63
|
+
break
|
|
64
|
+
# Get the only child
|
|
65
|
+
key, next_node = next(iter(node.children.items()))
|
|
66
|
+
prefix.append(key)
|
|
67
|
+
node = next_node
|
|
68
|
+
|
|
69
|
+
return prefix, node
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def collect_sequences(
|
|
73
|
+
root: TrieNode[K, V], prefix: Optional[List[K]] = None
|
|
74
|
+
) -> Iterator[Tuple[List[K], TrieNode[K, V]]]:
|
|
75
|
+
if prefix is None:
|
|
76
|
+
prefix = []
|
|
77
|
+
|
|
78
|
+
if root.is_end:
|
|
79
|
+
yield prefix.copy(), root
|
|
80
|
+
|
|
81
|
+
for key, child in root.children.items():
|
|
82
|
+
prefix.append(key)
|
|
83
|
+
yield from collect_sequences(child, prefix)
|
|
84
|
+
prefix.pop()
|