You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.7 KiB
51 lines
1.7 KiB
9 months ago
|
# Copyright 2009-2024 Joshua Bronson. All rights reserved.
|
||
4 years ago
|
#
|
||
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
||
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
||
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||
|
|
||
|
|
||
|
# * Code review nav *
|
||
2 years ago
|
# (see comments in __init__.py)
|
||
9 months ago
|
# ============================================================================
|
||
|
# ← Prev: _base.py Current: _frozen.py Next: _bidict.py →
|
||
|
# ============================================================================
|
||
4 years ago
|
|
||
3 years ago
|
"""Provide :class:`frozenbidict`, an immutable, hashable bidirectional mapping type."""
|
||
4 years ago
|
|
||
9 months ago
|
from __future__ import annotations
|
||
|
|
||
2 years ago
|
import typing as t
|
||
4 years ago
|
|
||
2 years ago
|
from ._base import BidictBase
|
||
9 months ago
|
from ._typing import KT
|
||
|
from ._typing import VT
|
||
4 years ago
|
|
||
3 years ago
|
|
||
2 years ago
|
class frozenbidict(BidictBase[KT, VT]):
|
||
4 years ago
|
"""Immutable, hashable bidict type."""
|
||
|
|
||
3 years ago
|
_hash: int
|
||
|
|
||
2 years ago
|
if t.TYPE_CHECKING:
|
||
9 months ago
|
|
||
|
@property
|
||
|
def inverse(self) -> frozenbidict[VT, KT]: ...
|
||
|
|
||
3 years ago
|
@property
|
||
9 months ago
|
def inv(self) -> frozenbidict[VT, KT]: ...
|
||
4 years ago
|
|
||
3 years ago
|
def __hash__(self) -> int:
|
||
4 years ago
|
"""The hash of this bidict as determined by its items."""
|
||
|
if getattr(self, '_hash', None) is None:
|
||
2 years ago
|
# The following is like hash(frozenset(self.items()))
|
||
|
# but more memory efficient. See also: https://bugs.python.org/issue46684
|
||
9 months ago
|
self._hash = t.ItemsView(self)._hash()
|
||
4 years ago
|
return self._hash
|
||
|
|
||
|
|
||
|
# * Code review nav *
|
||
9 months ago
|
# ============================================================================
|
||
|
# ← Prev: _base.py Current: _frozen.py Next: _bidict.py →
|
||
|
# ============================================================================
|