BiDict (Bidirectional Dictionary) provides a one-to-one mapping
between keys and values.
Supports both forward and reverse lookup.
Source code in pyrad2/bidict.py
| class BiDict:
"""BiDict (Bidirectional Dictionary) provides a one-to-one mapping
between keys and values.
Supports both forward and reverse lookup.
"""
def __init__(self) -> None:
"""Initialize empty forward and reverse dictionaries."""
self.forward: Dict[Hashable, Any] = {}
self.backward: Dict[Hashable, Any] = {}
def Add(self, one: Hashable, two: Hashable) -> None:
"""Add a bidirectional mapping between 'one' and 'two'."""
self.forward[one] = two
self.backward[two] = one
def __len__(self) -> int:
"""Return the number of entries in the dictionary."""
return len(self.forward)
def __getitem__(self, key: Hashable) -> Any:
"""Retrieve the value associated with the given key."""
return self.GetForward(key)
def __delitem__(self, key: Hashable) -> None:
"""Remove key and its associated value from the dictionary."""
if key in self.forward:
del self.backward[self.forward[key]]
del self.forward[key]
else:
del self.forward[self.backward[key]]
del self.backward[key]
def GetForward(self, key: Hashable) -> Any:
"""Return the value associated with 'key' from the forward mapping."""
return self.forward[key]
def HasForward(self, key: Hashable) -> bool:
"""Check if 'key' exists in the forward mapping."""
return key in self.forward
def GetBackward(self, key: Hashable) -> Any:
"""Return the key associated with 'value' from the reverse mapping."""
return self.backward[key]
def HasBackward(self, key: Hashable) -> bool:
"""Check if 'value' exists in the reverse mapping."""
return key in self.backward
|
__init__()
Initialize empty forward and reverse dictionaries.
Source code in pyrad2/bidict.py
| def __init__(self) -> None:
"""Initialize empty forward and reverse dictionaries."""
self.forward: Dict[Hashable, Any] = {}
self.backward: Dict[Hashable, Any] = {}
|
Add(one, two)
Add a bidirectional mapping between 'one' and 'two'.
Source code in pyrad2/bidict.py
| def Add(self, one: Hashable, two: Hashable) -> None:
"""Add a bidirectional mapping between 'one' and 'two'."""
self.forward[one] = two
self.backward[two] = one
|
__len__()
Return the number of entries in the dictionary.
Source code in pyrad2/bidict.py
| def __len__(self) -> int:
"""Return the number of entries in the dictionary."""
return len(self.forward)
|
__getitem__(key)
Retrieve the value associated with the given key.
Source code in pyrad2/bidict.py
| def __getitem__(self, key: Hashable) -> Any:
"""Retrieve the value associated with the given key."""
return self.GetForward(key)
|
__delitem__(key)
Remove key and its associated value from the dictionary.
Source code in pyrad2/bidict.py
| def __delitem__(self, key: Hashable) -> None:
"""Remove key and its associated value from the dictionary."""
if key in self.forward:
del self.backward[self.forward[key]]
del self.forward[key]
else:
del self.forward[self.backward[key]]
del self.backward[key]
|
GetForward(key)
Return the value associated with 'key' from the forward mapping.
Source code in pyrad2/bidict.py
| def GetForward(self, key: Hashable) -> Any:
"""Return the value associated with 'key' from the forward mapping."""
return self.forward[key]
|
HasForward(key)
Check if 'key' exists in the forward mapping.
Source code in pyrad2/bidict.py
| def HasForward(self, key: Hashable) -> bool:
"""Check if 'key' exists in the forward mapping."""
return key in self.forward
|
GetBackward(key)
Return the key associated with 'value' from the reverse mapping.
Source code in pyrad2/bidict.py
| def GetBackward(self, key: Hashable) -> Any:
"""Return the key associated with 'value' from the reverse mapping."""
return self.backward[key]
|
HasBackward(key)
Check if 'value' exists in the reverse mapping.
Source code in pyrad2/bidict.py
| def HasBackward(self, key: Hashable) -> bool:
"""Check if 'value' exists in the reverse mapping."""
return key in self.backward
|