blob: c2717e72281406ee06993e567dc61a02df834893 [file] [log] [blame]
Raymond Hettinger93fa6082008-02-05 00:20:01 +00001# Access WeakSet through the weakref module.
2# This code is separated-out because it is needed
3# by abc.py to load everything else at startup.
4
5from _weakref import ref
6
7__all__ = ['WeakSet']
8
Antoine Pitrouc1baa602010-01-08 17:54:23 +00009
10class _IterationGuard:
11 # This context manager registers itself in the current iterators of the
12 # weak container, such as to delay all removals until the context manager
13 # exits.
14 # This technique should be relatively thread-safe (since sets are).
15
16 def __init__(self, weakcontainer):
17 # Don't create cycles
18 self.weakcontainer = ref(weakcontainer)
19
20 def __enter__(self):
21 w = self.weakcontainer()
22 if w is not None:
23 w._iterating.add(self)
24 return self
25
26 def __exit__(self, e, t, b):
27 w = self.weakcontainer()
28 if w is not None:
29 s = w._iterating
30 s.remove(self)
31 if not s:
32 w._commit_removals()
33
34
Raymond Hettinger93fa6082008-02-05 00:20:01 +000035class WeakSet:
36 def __init__(self, data=None):
37 self.data = set()
38 def _remove(item, selfref=ref(self)):
39 self = selfref()
40 if self is not None:
Antoine Pitrouc1baa602010-01-08 17:54:23 +000041 if self._iterating:
42 self._pending_removals.append(item)
43 else:
44 self.data.discard(item)
Raymond Hettinger93fa6082008-02-05 00:20:01 +000045 self._remove = _remove
Antoine Pitrouc1baa602010-01-08 17:54:23 +000046 # A list of keys to be removed
47 self._pending_removals = []
48 self._iterating = set()
Raymond Hettinger93fa6082008-02-05 00:20:01 +000049 if data is not None:
50 self.update(data)
51
Antoine Pitrouc1baa602010-01-08 17:54:23 +000052 def _commit_removals(self):
53 l = self._pending_removals
54 discard = self.data.discard
55 while l:
56 discard(l.pop())
57
Raymond Hettinger93fa6082008-02-05 00:20:01 +000058 def __iter__(self):
Antoine Pitrouc1baa602010-01-08 17:54:23 +000059 with _IterationGuard(self):
60 for itemref in self.data:
61 item = itemref()
62 if item is not None:
63 yield item
Raymond Hettinger93fa6082008-02-05 00:20:01 +000064
Georg Brandl9dba5d92008-05-18 16:27:29 +000065 def __len__(self):
Antoine Pitroubbe2f602012-03-01 16:26:35 +010066 return len(self.data) - len(self._pending_removals)
Georg Brandl9dba5d92008-05-18 16:27:29 +000067
Raymond Hettinger93fa6082008-02-05 00:20:01 +000068 def __contains__(self, item):
Georg Brandlf8de3fe2010-12-03 07:55:44 +000069 try:
70 wr = ref(item)
71 except TypeError:
72 return False
73 return wr in self.data
Raymond Hettinger93fa6082008-02-05 00:20:01 +000074
75 def __reduce__(self):
76 return (self.__class__, (list(self),),
77 getattr(self, '__dict__', None))
78
79 def add(self, item):
Antoine Pitrouc1baa602010-01-08 17:54:23 +000080 if self._pending_removals:
81 self._commit_removals()
Raymond Hettinger93fa6082008-02-05 00:20:01 +000082 self.data.add(ref(item, self._remove))
83
84 def clear(self):
Antoine Pitrouc1baa602010-01-08 17:54:23 +000085 if self._pending_removals:
86 self._commit_removals()
Raymond Hettinger93fa6082008-02-05 00:20:01 +000087 self.data.clear()
88
89 def copy(self):
90 return self.__class__(self)
91
92 def pop(self):
Antoine Pitrouc1baa602010-01-08 17:54:23 +000093 if self._pending_removals:
94 self._commit_removals()
Raymond Hettinger93fa6082008-02-05 00:20:01 +000095 while True:
Georg Brandlbf93b042008-05-18 07:46:13 +000096 try:
97 itemref = self.data.pop()
98 except KeyError:
99 raise KeyError('pop from empty WeakSet')
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000100 item = itemref()
101 if item is not None:
102 return item
103
104 def remove(self, item):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000105 if self._pending_removals:
106 self._commit_removals()
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000107 self.data.remove(ref(item))
108
109 def discard(self, item):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000110 if self._pending_removals:
111 self._commit_removals()
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000112 self.data.discard(ref(item))
113
114 def update(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000115 if self._pending_removals:
116 self._commit_removals()
Antoine Pitroude89d4b2012-03-04 20:20:34 +0100117 for element in other:
118 self.add(element)
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000119
Georg Brandl9dba5d92008-05-18 16:27:29 +0000120 def __ior__(self, other):
121 self.update(other)
122 return self
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000123
124 # Helper functions for simple delegating methods.
125 def _apply(self, other, method):
126 if not isinstance(other, self.__class__):
127 other = self.__class__(other)
128 newdata = method(other.data)
129 newset = self.__class__()
130 newset.data = newdata
131 return newset
132
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000133 def difference(self, other):
134 return self._apply(other, self.data.difference)
135 __sub__ = difference
136
137 def difference_update(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000138 if self._pending_removals:
139 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000140 if self is other:
141 self.data.clear()
142 else:
143 self.data.difference_update(ref(item) for item in other)
144 def __isub__(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000145 if self._pending_removals:
146 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000147 if self is other:
148 self.data.clear()
149 else:
150 self.data.difference_update(ref(item) for item in other)
151 return self
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000152
153 def intersection(self, other):
154 return self._apply(other, self.data.intersection)
155 __and__ = intersection
156
157 def intersection_update(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000158 if self._pending_removals:
159 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000160 self.data.intersection_update(ref(item) for item in other)
161 def __iand__(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000162 if self._pending_removals:
163 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000164 self.data.intersection_update(ref(item) for item in other)
165 return self
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000166
167 def issubset(self, other):
168 return self.data.issubset(ref(item) for item in other)
169 __lt__ = issubset
170
Georg Brandl9dba5d92008-05-18 16:27:29 +0000171 def __le__(self, other):
172 return self.data <= set(ref(item) for item in other)
173
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000174 def issuperset(self, other):
175 return self.data.issuperset(ref(item) for item in other)
176 __gt__ = issuperset
177
Georg Brandl9dba5d92008-05-18 16:27:29 +0000178 def __ge__(self, other):
179 return self.data >= set(ref(item) for item in other)
180
181 def __eq__(self, other):
Robert Schuppenies4ad1d6f2009-05-17 17:32:20 +0000182 if not isinstance(other, self.__class__):
183 return NotImplemented
Georg Brandl9dba5d92008-05-18 16:27:29 +0000184 return self.data == set(ref(item) for item in other)
185
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000186 def symmetric_difference(self, other):
187 return self._apply(other, self.data.symmetric_difference)
188 __xor__ = symmetric_difference
189
190 def symmetric_difference_update(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000191 if self._pending_removals:
192 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000193 if self is other:
194 self.data.clear()
195 else:
196 self.data.symmetric_difference_update(ref(item) for item in other)
197 def __ixor__(self, other):
Antoine Pitrouc1baa602010-01-08 17:54:23 +0000198 if self._pending_removals:
199 self._commit_removals()
Georg Brandl9dba5d92008-05-18 16:27:29 +0000200 if self is other:
201 self.data.clear()
202 else:
203 self.data.symmetric_difference_update(ref(item) for item in other)
204 return self
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000205
206 def union(self, other):
Georg Brandlbf93b042008-05-18 07:46:13 +0000207 return self._apply(other, self.data.union)
Raymond Hettinger93fa6082008-02-05 00:20:01 +0000208 __or__ = union
Georg Brandl9dba5d92008-05-18 16:27:29 +0000209
210 def isdisjoint(self, other):
211 return len(self.intersection(other)) == 0