stl_treemap.policies
Node data access policies for sets, maps, and value iteration.
1"""Node data access policies for sets, maps, and value iteration.""" 2 3from __future__ import annotations 4 5from abc import ABC, abstractmethod 6 7from stl_treemap.tree_node import TreeNode 8 9 10class PolicyInterface[K, V](ABC): 11 """@private Interface for node data access policies.""" 12 13 @abstractmethod 14 def fetch(self, n: TreeNode[K, V]) -> K | V | tuple[K, V]: 15 """Return data from the specified node.""" 16 17 @abstractmethod 18 def copy(self, dst: TreeNode[K, V], src: TreeNode[K, V]) -> None: 19 """Copy data from one node to another.""" 20 21 @abstractmethod 22 def to_string(self, node: TreeNode[K, V]) -> str: 23 """Return string representation of the node.""" 24 25 26class KeyOnlyPolicy[K, V](PolicyInterface[K, V]): 27 """@private Used by sets.""" 28 29 def fetch(self, n: TreeNode[K, V]) -> K: 30 """ 31 Return key data from the specified node. 32 33 Args: 34 n: Node to inspect. 35 36 Returns: 37 Node's key. 38 39 """ 40 return n.key 41 42 def copy(self, dst: TreeNode[K, V], src: TreeNode[K, V]) -> None: 43 """ 44 Copy key data from one node to another. 45 46 Args: 47 dst: Destination node. 48 src: Source node. 49 50 """ 51 dst.key = src.key 52 53 def to_string(self, node: TreeNode[K, V]) -> str: 54 """ 55 Return string representation of the provided node. 56 57 Args: 58 node: Node to serialize. 59 60 Returns: 61 String representation of the key. 62 63 """ 64 return str(node.key) 65 66 67class KeyValuePolicy[K, V](PolicyInterface[K, V]): 68 """@private Used by maps.""" 69 70 def fetch(self, n: TreeNode[K, V]) -> tuple[K, V]: 71 """ 72 Return key-value data from the specified node. 73 74 Args: 75 n: Node to inspect. 76 77 Returns: 78 Tuple of key and value. 79 80 """ 81 return (n.key, n.value) 82 83 def copy(self, dst: TreeNode[K, V], src: TreeNode[K, V]) -> None: 84 """ 85 Copy key-value data from one node to another. 86 87 Args: 88 dst: Destination node. 89 src: Source node. 90 91 """ 92 dst.key = src.key 93 dst.value = src.value 94 95 def to_string(self, node: TreeNode[K, V]) -> str: 96 """ 97 Generate string representation of the node. 98 99 Args: 100 node: Node to serialize. 101 102 Returns: 103 String representation of key-value pair. 104 105 """ 106 return f"{node.key}:{node.value}" 107 108 109class ValueOnlyPolicy[K, V](PolicyInterface[K, V]): 110 """@private Used for iteration through values of a map.""" 111 112 def fetch(self, n: TreeNode[K, V]) -> V: 113 """ 114 Return value data from the specified node. 115 116 Args: 117 n: Node to inspect. 118 119 Returns: 120 Node's value. 121 122 """ 123 return n.value 124 125 def copy(self, dst: TreeNode[K, V], src: TreeNode[K, V]) -> None: 126 """ 127 Copy value data from one node to another. 128 129 Args: 130 dst: Destination node. 131 src: Source node. 132 133 """ 134 dst.value = src.value 135 136 def to_string(self, node: TreeNode[K, V]) -> str: 137 """ 138 Return string representation of the node. 139 140 Args: 141 node: Node to serialize. 142 143 Returns: 144 String representation of node's value. 145 146 """ 147 return str(node.value)