-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistent_set.py
More file actions
157 lines (124 loc) · 4.08 KB
/
persistent_set.py
File metadata and controls
157 lines (124 loc) · 4.08 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/durus/persistent_set.py $
$Id: persistent_set.py 31433 2009-02-02 15:10:18Z dbinger $
"""
from durus.persistent import PersistentObject
class PersistentSet (PersistentObject):
__slots__ = ['s']
s_is = set # for type checking using QP's spec module
def __init__(self, *args):
self.s = set(*args)
def __and__(self, other):
if isinstance(other, PersistentSet):
return self.__class__(self.s & other.s)
else:
return self.__class__(self.s & other)
def __contains__(self, item):
return item in self.s
def __eq__(self, other):
if not isinstance(other, PersistentSet):
return False
return self.s == other.s
def __ge__(self, other):
if not isinstance(other, PersistentSet):
raise TypeError("can only compare to a PersistentSet")
return self.s >= other.s
def __gt__(self, other):
if not isinstance(other, PersistentSet):
raise TypeError("can only compare to a PersistentSet")
return self.s > other.s
def __iand__(self, other):
self._p_note_change()
if isinstance(other, PersistentSet):
self.s &= other.s
else:
self.s &= other
return self
def __ior__(self, other):
self._p_note_change()
if isinstance(other, PersistentSet):
self.s |= other.s
else:
self.s |= other
return self
def __isub__(self, other):
self._p_note_change()
if isinstance(other, PersistentSet):
self.s -= other.s
else:
self.s -= other
return self
def __iter__(self):
for x in self.s:
yield x
def __ixor__(self, other):
self._p_note_change()
if isinstance(other, PersistentSet):
self.s ^= other.s
else:
self.s ^= other
return self
def __le__(self, other):
if not isinstance(other, PersistentSet):
raise TypeError("can only compare to a PersistentSet")
return self.s <= other.s
def __len__(self):
return len(self.s)
def __lt__(self, other):
if not isinstance(other, PersistentSet):
raise TypeError("can only compare to a PersistentSet")
return self.s < other.s
def __ne__(self, other):
if not isinstance(other, PersistentSet):
return True
return self.s != other.s
def __or__(self, other):
if isinstance(other, PersistentSet):
return self.__class__(self.s | other.s)
else:
return self.__class__(self.s | other)
def __rand__(self, other):
return self.__class__(other & self.s)
def __ror__(self, other):
return self.__class__(other | self.s)
def __rsub__(self, other):
return self.__class__(other - self.s)
def __rxor__(self, other):
return self.__class__(other ^ self.s)
def __sub__(self, other):
if isinstance(other, PersistentSet):
return self.__class__(self.s - other.s)
else:
return self.__class__(self.s - other)
def __xor__(self, other):
if isinstance(other, PersistentSet):
return self.__class__(self.s ^ other.s)
else:
return self.__class__(self.s ^other)
def add(self, item):
self._p_note_change()
self.s.add(item)
def clear(self):
self._p_note_change()
self.s.clear()
def copy(self):
return self.__class__(self.s)
def discard(self, item):
self._p_note_change()
self.s.discard(item)
def pop(self):
self._p_note_change()
return self.s.pop()
def remove(self, item):
self._p_note_change()
self.s.remove(item)
difference = __sub__
difference_update = __isub__
intersection = __and__
intersection_update = __iand__
issubset = __le__
issuperset = __ge__
symmetric_difference = __xor__
symmetric_difference_update = __ixor__
union = __or__
update = __ior__