forked from modular/modular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_identity.py
More file actions
71 lines (53 loc) · 2.24 KB
/
_identity.py
File metadata and controls
71 lines (53 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2025, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
"""Utility classes for using objects as keys in data structures."""
from collections.abc import MutableMapping, MutableSet
# From https://stackoverflow.com/questions/16994307/identityset-in-python
class IdentitySet(MutableSet):
"""Set that uses object `id` as keys to support unhashable types."""
def __init__(self, iterable=()):
self.map = {} # id -> object
self |= iterable # add elements from iterable to the set (union)
def __len__(self):
return len(self.map)
def __iter__(self):
return iter(self.map.values())
def __contains__(self, x):
return id(x) in self.map
def add(self, value):
"""Add an element."""
self.map[id(value)] = value
def discard(self, value):
"""Remove an element. Do not raise an exception if absent."""
self.map.pop(id(value), None)
def __repr__(self):
if not self:
return f"{self.__class__.__name__}()"
return f"{self.__class__.__name__}({list(self)!r})"
class IdentityMap(MutableMapping):
"""Map that uses object `id` as keys to support unhashable types."""
def __init__(self):
self.key_map = {} # id -> object
self.value_map = {} # id -> Value
def __getitem__(self, key):
return self.value_map[id(key)]
def __setitem__(self, key, value):
self.key_map[id(key)] = key
self.value_map[id(key)] = value
def __delitem__(self, key):
del self.key_map[id(key)]
del self.value_map[id(key)]
def __iter__(self):
return iter(self.key_map.values())
def __len__(self):
return len(self.key_map)