| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from _weakref import ref |
| 6 |
from types import GenericAlias |
| 7 |
|
| 8 |
__all__ = ['WeakSet'] |
| 9 |
|
| 10 |
|
| 11 |
class _IterationGuard: |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
def __init__(self, weakcontainer): |
| 18 |
|
| 19 |
self.weakcontainer = ref(weakcontainer) |
| 20 |
|
| 21 |
def __enter__(self): |
| 22 |
w = self.weakcontainer() |
| 23 |
if w is not None: |
| 24 |
w._iterating.add(self) |
| 25 |
return self |
| 26 |
|
| 27 |
def __exit__(self, e, t, b): |
| 28 |
w = self.weakcontainer() |
| 29 |
if w is not None: |
| 30 |
s = w._iterating |
| 31 |
s.remove(self) |
| 32 |
if not s: |
| 33 |
w._commit_removals() |
| 34 |
|
| 35 |
|
| 36 |
class WeakSet: |
| 37 |
def __init__(self, data=None): |
| 38 |
self.data = set() |
| 39 |
def _remove(item, selfref=ref(self)): |
| 40 |
self = selfref() |
| 41 |
if self is not None: |
| 42 |
if self._iterating: |
| 43 |
self._pending_removals.append(item) |
| 44 |
else: |
| 45 |
self.data.discard(item) |
| 46 |
self._remove = _remove |
| 47 |
|
| 48 |
self._pending_removals = [] |
| 49 |
self._iterating = set() |
| 50 |
if data is not None: |
| 51 |
self.update(data) |
| 52 |
|
| 53 |
def _commit_removals(self): |
| 54 |
pop = self._pending_removals.pop |
| 55 |
discard = self.data.discard |
| 56 |
while True: |
| 57 |
try: |
| 58 |
item = pop() |
| 59 |
except IndexError: |
| 60 |
return |
| 61 |
discard(item) |
| 62 |
|
| 63 |
def __iter__(self): |
| 64 |
with _IterationGuard(self): |
| 65 |
for itemref in self.data: |
| 66 |
item = itemref() |
| 67 |
if item is not None: |
| 68 |
|
| 69 |
|
| 70 |
yield item |
| 71 |
|
| 72 |
def __len__(self): |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
result = 0 |
| 78 |
for wr in list(self.data): |
| 79 |
result += (wr() is not None) |
| 80 |
return result |
| 81 |
|
| 82 |
def __contains__(self, item): |
| 83 |
try: |
| 84 |
wr = ref(item) |
| 85 |
except TypeError: |
| 86 |
return False |
| 87 |
return wr in self.data |
| 88 |
|
| 89 |
def __reduce__(self): |
| 90 |
return self.__class__, (list(self),), self.__getstate__() |
| 91 |
|
| 92 |
def add(self, item): |
| 93 |
if self._pending_removals: |
| 94 |
self._commit_removals() |
| 95 |
self.data.add(ref(item, self._remove)) |
| 96 |
|
| 97 |
def clear(self): |
| 98 |
if self._pending_removals: |
| 99 |
self._commit_removals() |
| 100 |
self.data.clear() |
| 101 |
|
| 102 |
def copy(self): |
| 103 |
return self.__class__(self) |
| 104 |
|
| 105 |
def pop(self): |
| 106 |
if self._pending_removals: |
| 107 |
self._commit_removals() |
| 108 |
while True: |
| 109 |
try: |
| 110 |
itemref = self.data.pop() |
| 111 |
except KeyError: |
| 112 |
raise KeyError('pop from empty WeakSet') from None |
| 113 |
item = itemref() |
| 114 |
if item is not None: |
| 115 |
return item |
| 116 |
|
| 117 |
def remove(self, item): |
| 118 |
if self._pending_removals: |
| 119 |
self._commit_removals() |
| 120 |
self.data.remove(ref(item)) |
| 121 |
|
| 122 |
def discard(self, item): |
| 123 |
if self._pending_removals: |
| 124 |
self._commit_removals() |
| 125 |
self.data.discard(ref(item)) |
| 126 |
|
| 127 |
def update(self, other): |
| 128 |
if self._pending_removals: |
| 129 |
self._commit_removals() |
| 130 |
for element in other: |
| 131 |
self.add(element) |
| 132 |
|
| 133 |
def __ior__(self, other): |
| 134 |
self.update(other) |
| 135 |
return self |
| 136 |
|
| 137 |
def difference(self, other): |
| 138 |
newset = self.copy() |
| 139 |
newset.difference_update(other) |
| 140 |
return newset |
| 141 |
__sub__ = difference |
| 142 |
|
| 143 |
def difference_update(self, other): |
| 144 |
self.__isub__(other) |
| 145 |
def __isub__(self, other): |
| 146 |
if self._pending_removals: |
| 147 |
self._commit_removals() |
| 148 |
if self is other: |
| 149 |
self.data.clear() |
| 150 |
else: |
| 151 |
self.data.difference_update(ref(item) for item in other) |
| 152 |
return self |
| 153 |
|
| 154 |
def intersection(self, other): |
| 155 |
return self.__class__(item for item in other if item in self) |
| 156 |
__and__ = intersection |
| 157 |
|
| 158 |
def intersection_update(self, other): |
| 159 |
self.__iand__(other) |
| 160 |
def __iand__(self, other): |
| 161 |
if self._pending_removals: |
| 162 |
self._commit_removals() |
| 163 |
self.data.intersection_update(ref(item) for item in other) |
| 164 |
return self |
| 165 |
|
| 166 |
def issubset(self, other): |
| 167 |
return self.data.issubset(ref(item) for item in other) |
| 168 |
__le__ = issubset |
| 169 |
|
| 170 |
def __lt__(self, other): |
| 171 |
return self.data < set(map(ref, other)) |
| 172 |
|
| 173 |
def issuperset(self, other): |
| 174 |
return self.data.issuperset(ref(item) for item in other) |
| 175 |
__ge__ = issuperset |
| 176 |
|
| 177 |
def __gt__(self, other): |
| 178 |
return self.data > set(map(ref, other)) |
| 179 |
|
| 180 |
def __eq__(self, other): |
| 181 |
if not isinstance(other, self.__class__): |
| 182 |
return NotImplemented |
| 183 |
return self.data == set(map(ref, other)) |
| 184 |
|
| 185 |
def symmetric_difference(self, other): |
| 186 |
newset = self.copy() |
| 187 |
newset.symmetric_difference_update(other) |
| 188 |
return newset |
| 189 |
__xor__ = symmetric_difference |
| 190 |
|
| 191 |
def symmetric_difference_update(self, other): |
| 192 |
self.__ixor__(other) |
| 193 |
def __ixor__(self, other): |
| 194 |
if self._pending_removals: |
| 195 |
self._commit_removals() |
| 196 |
if self is other: |
| 197 |
self.data.clear() |
| 198 |
else: |
| 199 |
self.data.symmetric_difference_update(ref(item, self._remove) for item in other) |
| 200 |
return self |
| 201 |
|
| 202 |
def union(self, other): |
| 203 |
return self.__class__(e for s in (self, other) for e in s) |
| 204 |
__or__ = union |
| 205 |
|
| 206 |
def isdisjoint(self, other): |
| 207 |
return len(self.intersection(other)) == 0 |
| 208 |
|
| 209 |
def __repr__(self): |
| 210 |
return repr(self.data) |
| 211 |
|
| 212 |
__class_getitem__ = classmethod(GenericAlias) |