Coverage for src/gentrie/types.py: 93%
27 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-17 11:24 -0700
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-17 11:24 -0700
1# -*- coding: utf-8 -*-
2"""Core data types for the gentrie package."""
4from typing import Any, NamedTuple, Optional
6from .protocols import GeneralizedKey
9# Constants for TrieEntry fields (performance optimization)
10TRIE_IDENT: int = 0
11"""Alias for field number 0 (ident) in TrieEntry. It is faster to use this than accessing the field by name."""
12TRIE_KEY: int = 1
13"""Alias for field number 1 (key) in TrieEntry. It is faster to use this than accessing the field by name."""
14TRIE_VALUE: int = 2
15"""Alias for field number 2 (value) in TrieEntry. It is faster to use this than accessing the field by name."""
18class TrieId(int):
19 """Unique identifier for a key in a trie."""
20 __slots__ = ()
22 def __new__(cls, value: int):
23 return int.__new__(cls, value)
25 def __str__(self) -> str:
26 """Returns a string representation of the TrieId."""
27 return f'TrieId({int(self)})'
29 def __repr__(self) -> str:
30 """Returns a string representation of the TrieId for debugging."""
31 return f'TrieId({int(self)})'
34class TrieEntry(NamedTuple):
35 """A :class:`TrieEntry` is a :class:`NamedTuple` containing the unique identifer and key for an entry in the trie.
36 """
38 ident: TrieId
39 """:class:`TrieId` Unique identifier for a key in the trie. Alias for field number 0."""
40 key: GeneralizedKey
41 """:class:`GeneralizedKey` Key for an entry in the trie. Alias for field number 1."""
42 value: Optional[Any] = None
43 """Optional value for the entry in the trie. Alias for field number 2."""
45 def __eq__(self, other: object) -> bool:
46 if not isinstance(other, TrieEntry): 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true
47 return False
48 return self.ident == other.ident and tuple(self.key) == tuple(
49 other.key)
51 def __hash__(self) -> int:
52 return hash((self.ident, tuple(self.key)))