Repository navigation
Expand file tree
/
Copy pathPySet.java
More file actions
360 lines (301 loc) · 10.4 KB
/
Copy pathPySet.java
File metadata and controls
360 lines (301 loc) · 10.4 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package org.python.core;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
import org.python.util.Generic;
@ExposedType(name = "set", base = PyObject.class, doc = BuiltinDocs.set_doc)
public class PySet extends BaseSet {
public static final PyType TYPE = PyType.fromClass(PySet.class);
{
// Ensure set is not Hashable
TYPE.object___setattr__("__hash__", Py.None);
}
public PySet() {
this(TYPE);
}
public PySet(PyType type) {
super(type, Generic.<PyObject>concurrentSet());
}
public PySet(PyObject data) {
super(TYPE, _update(Generic.<PyObject>concurrentSet(), data));
}
public PySet(PyObject[] data) {
super(TYPE, _update(Generic.<PyObject>concurrentSet(), data));
}
public PySet(Set backing_set, PyObject data) {
super(TYPE, _update(backing_set, data));
}
public PySet(PyType type, Set backing_set, PyObject data) {
super(type, _update(backing_set, data));
}
@ExposedNew
@ExposedMethod(doc = BuiltinDocs.set___init___doc)
final void set___init__(PyObject[] args, String[] kwds) {
if (args.length > 1 || kwds.length != 0) {
throw PyBuiltinCallable.DefaultInfo.unexpectedCall(args.length, false, "Set", 0, 1);
}
if (args.length == 0) {
return;
}
_set.clear();
_update(args[0]);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___cmp___doc)
final PyObject set___cmp__(PyObject o) {
return new PyInteger(baseset___cmp__(o));
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___ne___doc)
final PyObject set___ne__(PyObject o) {
return baseset___ne__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___eq___doc)
final PyObject set___eq__(PyObject o) {
return baseset___eq__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___or___doc)
final PyObject set___or__(PyObject o) {
return baseset___or__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___xor___doc)
final PyObject set___xor__(PyObject o) {
return baseset___xor__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___sub___doc)
final PyObject set___sub__(PyObject o) {
return baseset___sub__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___and___doc)
final PyObject set___and__(PyObject o) {
return baseset___and__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___lt___doc)
final PyObject set___lt__(PyObject o) {
return baseset___lt__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___gt___doc)
final PyObject set___gt__(PyObject o) {
return baseset___gt__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___ge___doc)
final PyObject set___ge__(PyObject o) {
return baseset___ge__(o);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___le___doc)
final PyObject set___le__(PyObject o) {
return baseset___le__(o);
}
@ExposedMethod(doc = BuiltinDocs.set___iter___doc)
final PyObject set___iter__() {
return baseset___iter__();
}
@ExposedMethod(doc = BuiltinDocs.set___contains___doc)
final boolean set___contains__(PyObject item) {
return baseset___contains__(item);
}
@ExposedMethod(doc = BuiltinDocs.set_copy_doc)
final PyObject set_copy() {
return baseset_copy();
}
@ExposedMethod(doc = BuiltinDocs.set_union_doc)
final PyObject set_union(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("union() takes no keyword arguments");
}
return baseset_union(args);
}
@ExposedMethod(doc = BuiltinDocs.set_difference_doc)
final PyObject set_difference(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("difference() takes no keyword arguments");
}
return baseset_difference(args);
}
@ExposedMethod(doc = BuiltinDocs.set_symmetric_difference_doc)
final PyObject set_symmetric_difference(PyObject set) {
return baseset_symmetric_difference(set);
}
@ExposedMethod(doc = BuiltinDocs.set_intersection_doc)
final PyObject set_intersection(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("intersection() takes no keyword arguments");
}
return baseset_intersection(args);
}
@ExposedMethod(doc = BuiltinDocs.set_issubset_doc)
final PyObject set_issubset(PyObject set) {
return baseset_issubset(set);
}
@ExposedMethod(doc = BuiltinDocs.set_issuperset_doc)
final PyObject set_issuperset(PyObject set) {
return baseset_issuperset(set);
}
@ExposedMethod(doc = BuiltinDocs.set_isdisjoint_doc)
final PyObject set_isdisjoint(PyObject set) {
return baseset_isdisjoint(set);
}
@ExposedMethod(doc = BuiltinDocs.set___len___doc)
final int set___len__() {
return baseset___len__();
}
@ExposedMethod(doc = BuiltinDocs.set___reduce___doc)
final PyObject set___reduce__() {
return baseset___reduce__();
}
public PyObject __ior__(PyObject other) {
return set___ior__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___ior___doc)
final PyObject set___ior__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
_set.addAll(((BaseSet)other)._set);
return this;
}
public PyObject __ixor__(PyObject other) {
return set___ixor__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___ixor___doc)
final PyObject set___ixor__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
set_symmetric_difference_update(other);
return this;
}
public PyObject __iand__(PyObject other) {
return set___iand__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___iand___doc)
final PyObject set___iand__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
_set = ((BaseSet)__and__(other))._set;
return this;
}
public PyObject __isub__(PyObject other) {
return set___isub__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.set___isub___doc)
final PyObject set___isub__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
_set.removeAll(((BaseSet)other)._set);
return this;
}
public int hashCode() {
return set___hash__();
}
@ExposedMethod(doc = BuiltinDocs.set___hash___doc)
final int set___hash__() {
throw Py.TypeError("set objects are unhashable");
}
@ExposedMethod(doc = BuiltinDocs.set_add_doc)
final void set_add(PyObject o) {
_set.add(o);
}
@ExposedMethod(doc = BuiltinDocs.set_remove_doc)
final void set_remove(PyObject o) {
boolean b = false;
try {
b = _set.remove(o);
} catch (PyException e) {
PyObject frozen = asFrozen(e, o);
b = _set.remove(frozen);
}
if (!b) {
throw new PyException(Py.KeyError, o);
}
}
@ExposedMethod(doc = BuiltinDocs.set_discard_doc)
final void set_discard(PyObject o) {
try {
_set.remove(o);
} catch (PyException e) {
PyObject frozen = asFrozen(e, o);
_set.remove(frozen);
}
}
@ExposedMethod(doc = BuiltinDocs.set_pop_doc)
final synchronized PyObject set_pop() {
Iterator<PyObject> iterator = _set.iterator();
try {
Object first = iterator.next();
_set.remove(first);
return (PyObject)first;
} catch (NoSuchElementException e) {
throw new PyException(Py.KeyError, "pop from an empty set");
}
}
@ExposedMethod(doc = BuiltinDocs.set_clear_doc)
final void set_clear() {
_set.clear();
}
@ExposedMethod(doc = BuiltinDocs.set_update_doc)
final void set_update(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("update() takes no keyword arguments");
}
for (PyObject item: args) {
_update(item);
}
}
@ExposedMethod(doc = BuiltinDocs.set_intersection_update_doc)
final void set_intersection_update(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("intersection_update() takes no keyword arguments");
}
for (PyObject other: args) {
if (other instanceof BaseSet) {
set___iand__(other);
} else {
BaseSet set = (BaseSet)baseset_intersection(other);
_set = set._set;
}
}
}
@ExposedMethod(doc = BuiltinDocs.set_symmetric_difference_update_doc)
final void set_symmetric_difference_update(PyObject other) {
if (this == other) {
set_clear();
return;
}
BaseSet bs = (other instanceof BaseSet) ? (BaseSet)other : new PySet(other);
for (PyObject o : bs._set) {
if (_set.contains(o)) {
_set.remove(o);
} else {
_set.add(o);
}
}
}
@ExposedMethod(doc = BuiltinDocs.set_difference_update_doc)
final void set_difference_update(PyObject[] args, String [] kws) {
if (kws.length > 0) {
throw Py.TypeError("difference_update() takes no keyword arguments");
}
if (args.length == 0) {
return;
}
for (PyObject other: args) {
if (other instanceof BaseSet) {
set___isub__(other);
}
for (PyObject o : other.asIterable()) {
if (__contains__(o)) {
_set.remove(o);
}
}
}
}
@ExposedMethod(names = "__repr__", doc = BuiltinDocs.set___repr___doc)
final String set_toString() {
return baseset_toString();
}
}