1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """A simple Set class."""
17
19 """A simple set class.
20
21 This class implements a mutable set using a list as the container.
22 We don't use Python's set class because it's not indexable.
23
24 @ivar items: A list of the items which are in the set
25 @type items: list"""
26
27 __slots__ = ['items']
28
30 """Initialize the set.
31
32 @param items: the initial set of items
33 @type items: any iterable or None
34 """
35
36 self.items = []
37 if not items is None:
38 for item in items:
39 self.add(item)
40
42 return "dns.simpleset.Set(%s)" % repr(self.items)
43
44 - def add(self, item):
45 """Add an item to the set."""
46 if not item in self.items:
47 self.items.append(item)
48
50 """Remove an item from the set."""
51 self.items.remove(item)
52
54 """Remove an item from the set if present."""
55 try:
56 self.items.remove(item)
57 except ValueError:
58 pass
59
61 """Make a (shallow) copy of the set.
62
63 There is a 'clone protocol' that subclasses of this class
64 should use. To make a copy, first call your super's _clone()
65 method, and use the object returned as the new instance. Then
66 make shallow copies of the attributes defined in the subclass.
67
68 This protocol allows us to write the set algorithms that
69 return new instances (e.g. union) once, and keep using them in
70 subclasses.
71 """
72
73 cls = self.__class__
74 obj = cls.__new__(cls)
75 obj.items = list(self.items)
76 return obj
77
79 """Make a (shallow) copy of the set."""
80 return self._clone()
81
83 """Make a (shallow) copy of the set."""
84 return self._clone()
85
87 """Update the set, adding any elements from other which are not
88 already in the set.
89 @param other: the collection of items with which to update the set
90 @type other: Set object
91 """
92 if not isinstance(other, Set):
93 raise ValueError('other must be a Set instance')
94 if self is other:
95 return
96 for item in other.items:
97 self.add(item)
98
100 """Update the set, removing any elements from other which are not
101 in both sets.
102 @param other: the collection of items with which to update the set
103 @type other: Set object
104 """
105 if not isinstance(other, Set):
106 raise ValueError('other must be a Set instance')
107 if self is other:
108 return
109
110
111 for item in list(self.items):
112 if item not in other.items:
113 self.items.remove(item)
114
116 """Update the set, removing any elements from other which are in
117 the set.
118 @param other: the collection of items with which to update the set
119 @type other: Set object
120 """
121 if not isinstance(other, Set):
122 raise ValueError('other must be a Set instance')
123 if self is other:
124 self.items = []
125 else:
126 for item in other.items:
127 self.discard(item)
128
130 """Return a new set which is the union of I{self} and I{other}.
131
132 @param other: the other set
133 @type other: Set object
134 @rtype: the same type as I{self}
135 """
136
137 obj = self._clone()
138 obj.union_update(other)
139 return obj
140
142 """Return a new set which is the intersection of I{self} and I{other}.
143
144 @param other: the other set
145 @type other: Set object
146 @rtype: the same type as I{self}
147 """
148
149 obj = self._clone()
150 obj.intersection_update(other)
151 return obj
152
154 """Return a new set which I{self} - I{other}, i.e. the items
155 in I{self} which are not also in I{other}.
156
157 @param other: the other set
158 @type other: Set object
159 @rtype: the same type as I{self}
160 """
161
162 obj = self._clone()
163 obj.difference_update(other)
164 return obj
165
167 return self.union(other)
168
171
173 return self.union(other)
174
177
181
185
189
193
195 """Update the set, adding any elements from other which are not
196 already in the set.
197 @param other: the collection of items with which to update the set
198 @type other: any iterable type"""
199 for item in other:
200 self.add(item)
201
203 """Make the set empty."""
204 self.items = []
205
207
208
209 for item in self.items:
210 if not item in other.items:
211 return False
212 for item in other.items:
213 if not item in self.items:
214 return False
215 return True
216
218 return not self.__eq__(other)
219
221 return len(self.items)
222
224 return iter(self.items)
225
228
231
233 return self.items[i:j]
234
237
239 """Is I{self} a subset of I{other}?
240
241 @rtype: bool
242 """
243
244 if not isinstance(other, Set):
245 raise ValueError('other must be a Set instance')
246 for item in self.items:
247 if not item in other.items:
248 return False
249 return True
250
252 """Is I{self} a superset of I{other}?
253
254 @rtype: bool
255 """
256
257 if not isinstance(other, Set):
258 raise ValueError('other must be a Set instance')
259 for item in other.items:
260 if not item in self.items:
261 return False
262 return True
263