1"""
2$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/durus/persistent_set.py $
3$Id: persistent_set.py 31433 2009-02-02 15:10:18Z dbinger $
4"""
5from durus.persistent import PersistentObject
6
7class PersistentSet (PersistentObject):
8
9    __slots__ = ['s']
10
11    s_is = set # for type checking using QP's spec module
12
13    def __init__(self, *args):
14        self.s = set(*args)
15
16    def __and__(self, other):
17        if isinstance(other, PersistentSet):
18            return self.__class__(self.s & other.s)
19        else:
20            return self.__class__(self.s & other)
21
22    def __contains__(self, item):
23        return item in self.s
24
25    def __eq__(self, other):
26        if not isinstance(other, PersistentSet):
27            return False
28        return self.s == other.s
29
30    def __ge__(self, other):
31        if not isinstance(other, PersistentSet):
32            raise TypeError("can only compare to a PersistentSet")
33        return self.s >= other.s
34
35    def __gt__(self, other):
36        if not isinstance(other, PersistentSet):
37            raise TypeError("can only compare to a PersistentSet")
38        return self.s > other.s
39
40    def __iand__(self, other):
41        self._p_note_change()
42        if isinstance(other, PersistentSet):
43            self.s &= other.s
44        else:
45            self.s &= other
46        return self
47
48    def __ior__(self, other):
49        self._p_note_change()
50        if isinstance(other, PersistentSet):
51            self.s |= other.s
52        else:
53            self.s |= other
54        return self
55
56    def __isub__(self, other):
57        self._p_note_change()
58        if isinstance(other, PersistentSet):
59            self.s -= other.s
60        else:
61            self.s -= other
62        return self
63
64    def __iter__(self):
65        for x in self.s:
66            yield x
67
68    def __ixor__(self, other):
69        self._p_note_change()
70        if isinstance(other, PersistentSet):
71            self.s ^= other.s
72        else:
73            self.s ^= other
74        return self
75
76    def __le__(self, other):
77        if not isinstance(other, PersistentSet):
78            raise TypeError("can only compare to a PersistentSet")
79        return self.s <= other.s
80
81    def __len__(self):
82        return len(self.s)
83
84    def __lt__(self, other):
85        if not isinstance(other, PersistentSet):
86            raise TypeError("can only compare to a PersistentSet")
87        return self.s < other.s
88
89    def __ne__(self, other):
90        if not isinstance(other, PersistentSet):
91            return True
92        return self.s != other.s
93
94    def __or__(self, other):
95        if isinstance(other, PersistentSet):
96            return self.__class__(self.s | other.s)
97        else:
98            return self.__class__(self.s | other)
99
100    def __rand__(self, other):
101        return self.__class__(other & self.s)
102
103    def __ror__(self, other):
104        return self.__class__(other | self.s)
105
106    def __rsub__(self, other):
107        return self.__class__(other - self.s)
108
109    def __rxor__(self, other):
110        return self.__class__(other ^ self.s)
111
112    def __sub__(self, other):
113        if isinstance(other, PersistentSet):
114            return self.__class__(self.s - other.s)
115        else:
116            return self.__class__(self.s - other)
117
118    def __xor__(self, other):
119        if isinstance(other, PersistentSet):
120            return self.__class__(self.s ^ other.s)
121        else:
122            return self.__class__(self.s ^other)
123
124    def add(self, item):
125        self._p_note_change()
126        self.s.add(item)
127
128    def clear(self):
129        self._p_note_change()
130        self.s.clear()
131
132    def copy(self):
133        return self.__class__(self.s)
134
135    def discard(self, item):
136        self._p_note_change()
137        self.s.discard(item)
138
139    def pop(self):
140        self._p_note_change()
141        return self.s.pop()
142
143    def remove(self, item):
144        self._p_note_change()
145        self.s.remove(item)
146
147    difference = __sub__
148    difference_update = __isub__
149    intersection = __and__
150    intersection_update = __iand__
151    issubset = __le__
152    issuperset = __ge__
153    symmetric_difference = __xor__
154    symmetric_difference_update = __ixor__
155    union = __or__
156    update = __ior__
157
158