tinytrie 0.1.0__tar.gz

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.
tinytrie-0.1.0/LICENSE ADDED
@@ -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,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,65 @@
1
+ # TinyTrie
2
+
3
+ A minimal and type-safe trie (prefix tree) implementation for Python 3.6+.
4
+
5
+ TinyTrie provides basic operations on Tries:
6
+ - Insertion of sequences (`search_or_create`)
7
+ - Search for sequences (`search`)
8
+ - Find the longest common prefix (`longest_common_prefix`)
9
+ - Collect all stored sequences (`collect_sequences`)
10
+
11
+ It supports any type of keys and values using Python's type hints.
12
+
13
+ ## Features
14
+
15
+ - **Typed**: Works with arbitrary key and value types (`Generic[K, V]`)
16
+ - **Minimal**: Only essential functionalities
17
+ - **Efficient**: Memory-efficient with `__slots__`
18
+ - **Iterable**: Easily traverse and list all stored sequences
19
+ - **No external dependencies**
20
+
21
+ ## Example Usage
22
+
23
+ ```python
24
+ from tinytrie import TrieNode, search, search_or_create, longest_common_prefix, collect_sequences
25
+
26
+ # Create the root node
27
+ root = TrieNode[str, int]()
28
+
29
+ # Insert sequences
30
+ search_or_create(root, ['a', 'b', 'c'], value=1)
31
+ search_or_create(root, ['a', 'b', 'd'], value=2)
32
+
33
+ # Search for a sequence
34
+ node = search(root, ['a', 'b', 'c'])
35
+ if node is not None:
36
+ print(f"Found with value: {node.value}")
37
+
38
+ # Find the longest common prefix
39
+ prefix, node = longest_common_prefix(root)
40
+ print(f"Longest common prefix: {prefix}")
41
+
42
+ # Collect all sequences
43
+ for seq, node in collect_sequences(root):
44
+ print(f"Sequence: {seq}, Value: {node.value}")
45
+ ```
46
+
47
+ Output:
48
+
49
+ ```
50
+ Found with value: 1
51
+ Longest common prefix: ['a', 'b']
52
+ Sequence: ['a', 'b', 'c'], Value: 1
53
+ Sequence: ['a', 'b', 'd'], Value: 2
54
+ ```
55
+
56
+ ## Installation
57
+
58
+
59
+ ```bash
60
+ pip install tinytrie
61
+ ```
62
+
63
+ ## License
64
+
65
+ MIT License
@@ -0,0 +1,23 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tinytrie"
7
+ version = "0.1.0"
8
+ description = "A minimal type-safe trie (prefix tree) implementation in Python."
9
+ readme = "README.md"
10
+ requires-python = ">=3.6"
11
+ license = "MIT"
12
+ authors = [
13
+ { name="Jifeng Wu", email="jifengwu2k@gmail.com" }
14
+ ]
15
+ keywords = ["trie", "prefix tree", "data structure", "python"]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+
21
+ [project.urls]
22
+ "Homepage" = "https://github.com/jifengwu2k/tinytrie"
23
+ "Bug Tracker" = "https://github.com/jifengwu2k/tinytrie/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -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,8 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ tinytrie.py
5
+ tinytrie.egg-info/PKG-INFO
6
+ tinytrie.egg-info/SOURCES.txt
7
+ tinytrie.egg-info/dependency_links.txt
8
+ tinytrie.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ tinytrie
@@ -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()