This repository has been archived by the owner on May 31, 2020. It is now read-only.
/
Set.java
481 lines (418 loc) · 15.6 KB
/
Set.java
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package org.python.types;
public class Set extends org.python.types.Object {
public java.util.Set<org.python.Object> value;
/**
* A utility method to update the internal value of this object.
*
* Used by __i*__ operations to do an in-place operation.
* obj must be of type org.python.types.Set
*/
void setValue(org.python.Object obj) {
this.value = ((org.python.types.Set) obj).value;
}
public java.lang.Object toJava() {
return this.value;
}
public int hashCode() {
return this.value.hashCode();
}
@Override
public org.python.Object __hash__() {
throw new org.python.exceptions.AttributeError(this, "__hash__");
}
public Set() {
super();
this.value = new java.util.HashSet<org.python.Object>();
}
public Set(java.util.Set<org.python.Object> set) {
super();
this.value = set;
}
// public org.python.Object __new__() {
// throw new org.python.exceptions.NotImplementedError("__new__() has not been implemented");
// }
// public org.python.Object __init__() {
// throw new org.python.exceptions.NotImplementedError("__init__() has not been implemented");
// }
@org.python.Method(
__doc__ = ""
)
public org.python.types.Str __repr__() {
// Representation of an empty set is different
if (this.value.size() == 0) {
return new org.python.types.Str("set()");
}
java.lang.StringBuilder buffer = new java.lang.StringBuilder("{");
boolean first = true;
for (org.python.Object obj: this.value) {
if (first) {
first = false;
} else {
buffer.append(", ");
}
buffer.append(obj.__repr__());
}
buffer.append("}");
return new org.python.types.Str(buffer.toString());
}
@org.python.Method(
__doc__ = ""
)
public org.python.types.Str __format__(org.python.Object format_string) {
throw new org.python.exceptions.NotImplementedError("__format__() has not been implemented");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __getitem__(org.python.Object index) {
if (index instanceof org.python.types.Int || index instanceof org.python.types.Bool) {
throw new org.python.exceptions.TypeError("'set' object does not support indexing");
} else {
throw new org.python.exceptions.TypeError("'set' object is not subscriptable");
}
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __lt__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value) && !this.value.equals(otherSet.value));
}
throw new org.python.exceptions.TypeError(
String.format("unorderable types: set() < %s()",
org.Python.typeName(other.getClass())));
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __le__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(otherSet.value.containsAll(this.value));
}
throw new org.python.exceptions.TypeError(
String.format("unorderable types: set() <= %s()",
org.Python.typeName(other.getClass())));
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __eq__(org.python.Object other) {
boolean eq = false;
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
eq = this.value.equals(otherSet.value);
}
return new org.python.types.Bool(eq);
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __ne__(org.python.Object other) {
return new org.python.types.Bool(!((org.python.types.Bool) this.__eq__(other)).value);
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __gt__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value) && !this.value.equals(otherSet.value));
}
throw new org.python.exceptions.TypeError(
String.format("unorderable types: set() > %s()",
org.Python.typeName(other.getClass())));
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __ge__(org.python.Object other) {
if (other instanceof org.python.types.Set) {
org.python.types.Set otherSet = (org.python.types.Set) other;
return new org.python.types.Bool(this.value.containsAll(otherSet.value));
}
throw new org.python.exceptions.TypeError(
String.format("unorderable types: set() >= %s()",
org.Python.typeName(other.getClass())));
}
public boolean __setattr_null(java.lang.String name, org.python.Object value) {
// Builtin types can't have attributes set on them.
return false;
}
@org.python.Method(
__doc__ = ""
)
public org.python.types.Bool __bool__() {
return new org.python.types.Bool(this.value.size() > 0);
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __invert__() {
throw new org.python.exceptions.TypeError("bad operand type for unary ~: 'set'");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __pos__() {
throw new org.python.exceptions.TypeError("bad operand type for unary +: 'set'");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __neg__() {
throw new org.python.exceptions.TypeError("bad operand type for unary -: 'set'");
}
@org.python.Method(
__doc__ = ""
)
public org.python.types.List __dir__() {
throw new org.python.exceptions.NotImplementedError("__dir__() has not been implemented");
}
@org.python.Method(
__doc__ = ""
)
public org.python.types.Int __len__() {
return new org.python.types.Int(this.value.size());
}
@org.python.Method(
__doc__ = ""
)
public org.python.Iterable __iter__() {
throw new org.python.exceptions.NotImplementedError("__iter__() has not been implemented");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __contains__(org.python.Object other) {
return new org.python.types.Bool(this.value.contains(other));
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __not_contains__(org.python.Object other) {
return new org.python.types.Bool(!this.value.contains(other));
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __mul__(org.python.Object other) {
if (other instanceof org.python.types.List) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + this.typeName() + "'");
} else if (other instanceof org.python.types.Tuple) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + this.typeName() + "'");
} else if (other instanceof org.python.types.Str) {
throw new org.python.exceptions.TypeError("can't multiply sequence by non-int of type '" + this.typeName() + "'");
}
return super.__mul__(other);
}
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __sub__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__sub__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __and__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__and__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __xor__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__xor__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __or__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__or__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __rsub__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__rsub__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __rand__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__rand__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __rxor__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__rxor__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public org.python.Object __ror__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__ror__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public void __isub__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__isub__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public void __iand__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__iand__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public void __ixor__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__ixor__() has not been implemented");
// }
// @org.python.Method(
// __doc__ = ""
// )
// public void __ior__(org.python.Object other) {
// throw new org.python.exceptions.NotImplementedError("__ior__() has not been implemented");
// }
@org.python.Method(
__doc__ = ""
)
public org.python.Object add(org.python.Object other) {
this.value.add(other);
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object clear() {
this.value.clear();
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = "Return a shallow copy of a set."
)
public org.python.Object copy() {
return new Set(new java.util.HashSet<org.python.Object>(this.value));
}
@org.python.Method(
__doc__ = "Return the difference of two or more sets as a new set.\n\n(i.e. all elements that are in this set but not the others.)",
args = {"other"}
)
public org.python.Object difference(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.removeAll(((Set) other).value);
return new Set(set);
}
@org.python.Method(
__doc__ = "Remove all elements of another set from this set.",
args = {"other"}
)
public org.python.Object difference_update(org.python.Object other) {
this.value.removeAll(((Set) other).value);
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = "Remove an element from a set if it is a member.\n\nIf the element is not a member, do nothing.",
args = {"item"}
)
public org.python.Object discard(org.python.Object item) {
this.value.remove(item);
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object __iadd__(org.python.Object other) {
throw new org.python.exceptions.TypeError("unsupported operand type(s) for +=: 'set' and '" + other.typeName() + "'");
}
@org.python.Method(
__doc__ = "Return the intersection of two sets as a new set.\n\n(i.e. all elements that are in both sets.)",
args = {"other"}
)
public org.python.Object intersection(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.retainAll(((Set) other).value);
return new Set(set);
}
@org.python.Method(
__doc__ = "Update a set with the intersection of itself and another.",
args = {"other"}
)
public org.python.Object intersection_update(org.python.Object other) {
this.value.retainAll(((Set) other).value);
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object isdisjoint(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("isdisjoint() has not been implemented.");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object issubset(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("issubset() has not been implemented.");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object issuperset(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("issuperset() has not been implemented.");
}
@org.python.Method(
__doc__ = "",
args = {}
)
public org.python.Object pop() {
if (this.value.size() == 0) {
throw new org.python.exceptions.KeyError(new org.python.types.Str("pop from an empty set"));
}
java.util.Iterator<org.python.Object> iterator = this.value.iterator();
org.python.Object popped = iterator.next();
iterator.remove();
return popped;
}
@org.python.Method(
__doc__ = "Remove an element from a set; it must be a member.\n\nIf the element is not a member, raise a KeyError.",
args = {"item"}
)
public org.python.Object remove(org.python.Object item) {
if (!this.value.remove(item)) {
throw new org.python.exceptions.KeyError(item);
}
return org.python.types.NoneType.NONE;
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object symmetric_difference(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("symmetric_difference() has not been implemented.");
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object symmetric_difference_update(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("symmetric_difference_update() has not been implemented.");
}
@org.python.Method(
__doc__ = "Return the union of sets as a new set.\n\n(i.e. all elements that are in either set.)",
args = {"other"}
)
public org.python.Object union(org.python.Object other) {
java.util.Set set = ((Set) this.copy()).value;
set.addAll(((Set) other).value);
return new Set(set);
}
@org.python.Method(
__doc__ = ""
)
public org.python.Object update(org.python.Object other) {
throw new org.python.exceptions.NotImplementedError("update() has not been implemented.");
}
}