-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathBaseSet.java
More file actions
687 lines (601 loc) · 18.4 KB
/
BaseSet.java
File metadata and controls
687 lines (601 loc) · 18.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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/* Copyright (c) Jython Developers */
package org.python.core;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
public abstract class BaseSet extends PyObject implements Set, Traverseproc {
/** The underlying Set. */
protected Set<PyObject> _set;
/**
* Create a new Python set of type from the specified Set object.
*/
protected BaseSet(PyType type, Set<PyObject> set) {
super(type);
_set = set;
}
public Set<PyObject> getSet() {
return _set;
}
protected void _update(PyObject data) {
_update(_set, data);
}
protected void _update(PyObject [] args) {
_update(_set, args);
}
/**
* Update the underlying set with the contents of the iterable.
*/
protected static Set<PyObject> _update(Set<PyObject> set, PyObject data) {
if (data == null) {
return set;
}
if (data instanceof BaseSet) {
// Skip the iteration if both are sets
set.addAll(((BaseSet)data)._set);
} else {
for (PyObject item : data.asIterable()) {
set.add(item);
}
}
return set;
}
/**
* Update the underlying set with the contents of the array.
*/
protected static Set<PyObject> _update(Set<PyObject> set, PyObject[] data) {
if (data == null) {
return set;
}
for (PyObject item : data) {
set.add(item);
}
return set;
}
/**
* The union of <code>this</code> with <code>other</code>. <p> (I.e. all elements
* that are in either set)
*
* @param other A <code>BaseSet</code> instance.
* @return The union of the two sets as a new set.
*/
@Override
public PyObject __or__(PyObject other) {
return baseset___or__(other);
}
final PyObject baseset___or__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
return baseset_union(other);
}
/**
* The intersection of the <code>this</code> with <code>other</code>.
* <p>
*
* (I.e. all elements that are in both sets)
*
* @param other A <code>BaseSet</code> instance.
* @return The intersection of the two sets as a new set.
*/
@Override
public PyObject __and__(PyObject other) {
return baseset___and__(other);
}
final PyObject baseset___and__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
return baseset_intersection(other);
}
/**
* The difference of the <code>this</code> with <code>other</code>.
* <p>
*
* (I.e. all elements that are in this set and not in the other)
*
* @param other A <code>BaseSet</code> instance.
* @return The difference of the two sets as a new set.
*/
@Override
public PyObject __sub__(PyObject other) {
return baseset___sub__(other);
}
final PyObject baseset___sub__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
return baseset_difference(other);
}
public PyObject difference(PyObject other) {
return baseset_difference(other);
}
final PyObject baseset_difference(PyObject other) {
return baseset_difference(new PyObject[] {other});
}
final PyObject baseset_difference(PyObject [] args) {
if (args.length == 0) {
return BaseSet.makeNewSet(getType(), this);
}
BaseSet o = BaseSet.makeNewSet(getType(), this);
for (PyObject item: args) {
BaseSet bs = args[0] instanceof BaseSet ? (BaseSet)item : new PySet(item);
Set<PyObject> set = bs._set;
for (PyObject p : set) {
if (_set.contains(p)) {
o._set.remove(p);
}
}
}
return o;
}
/**
* The symmetric difference of the <code>this</code> with <code>other</code>.
* <p>
*
* (I.e. all elements that are in exactly one of the sets)
*
* @param other A <code>BaseSet</code> instance.
* @return The symmetric difference of the two sets as a new set.
*/
@Override
public PyObject __xor__(PyObject other) {
return baseset___xor__(other);
}
final PyObject baseset___xor__(PyObject other) {
if (!(other instanceof BaseSet)) {
return null;
}
return baseset_symmetric_difference(other);
}
public PyObject symmetric_difference(PyObject other) {
return baseset_symmetric_difference(other);
}
final PyObject baseset_symmetric_difference(PyObject other) {
BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other);
BaseSet o = BaseSet.makeNewSet(getType());
for (PyObject p : _set) {
if (!bs._set.contains(p)) {
o._set.add(p);
}
}
for (PyObject p : bs._set) {
if (!_set.contains(p)) {
o._set.add(p);
}
}
return o;
}
/**
* The hashCode of the set. Only immutable instances can be hashed.
*
* @return The hashCode of the set.
*/
@Override
public abstract int hashCode();
/**
* The length of the set.
*
* @return The length of the set.
*/
@Override
public int __len__() {
return baseset___len__();
}
final int baseset___len__() {
return _set.size();
}
/**
* Determines if the instance is considered <code>true</code> by Python.
* This implementation returns true if the set is not empty.
*
* @return <code>true</code> if the set is not empty, <code>false</code> otherwise
*/
@Override
public boolean __nonzero__() {
return !_set.isEmpty();
}
/**
* Produce an iterable object.
*
* @return An iteration of the set.
*/
@Override
public PyObject __iter__() {
return baseset___iter__();
}
final PyObject baseset___iter__() {
return new PyIterator() {
private int size = size();
private Iterator<PyObject> iterator = _set.iterator();
@Override
public PyObject __iternext__() {
if (iterator.hasNext()) {
return iterator.next();
}
return null;
}
};
}
@Override
public boolean __contains__(PyObject other) {
return baseset___contains__(other);
}
final boolean baseset___contains__(PyObject other) {
try {
return _set.contains(other);
} catch (PyException pye) {
PyFrozenSet frozen = asFrozen(pye, other);
return _set.contains(frozen);
}
}
@Override
public int __cmp__(PyObject other) {
return baseset___cmp__(other);
}
final int baseset___cmp__(PyObject other) {
throw Py.TypeError("cannot compare sets using cmp()");
}
@Override
public PyObject __eq__(PyObject other) {
return baseset___eq__(other);
}
final PyObject baseset___eq__(PyObject other) {
// jobj might be Py.NoConversion if other is not a Set
Object jobj = other.__tojava__(Set.class);
if (jobj instanceof Set) {
final Set jSet = (Set) jobj;
// If the sizes differ must be not equal
if (jSet.size() != size()) {
return Py.False;
}
// Now need to perform element comparison
for (Object otherItem : jSet) {
if (!contains(otherItem)) {
return Py.False; // If any item is not contained then they are not equal
}
}
// All items are contained and the lentgh is the same so we are equal
return Py.True;
}
// other wasn't a set so not equal
return Py.False;
}
@Override
public PyObject __ne__(PyObject other) {
return baseset___ne__(other);
}
final PyObject baseset___ne__(PyObject other) {
// jobj might be Py.NoConversion if other is not a Set
Object jobj = other.__tojava__(Set.class);
if (jobj instanceof Set) {
final Set jSet = (Set) jobj;
// If the sizes differ must be not equal
if (jSet.size() != size()) {
return Py.True;
}
// Now need to perform element comparison
for (Object otherItem : jSet) {
if (!contains(otherItem)) {
return Py.True; // If any item is not contained then they are not equal
}
}
// All items are contained and the lentgh is the same so we are equal
return Py.False;
}
// other wasn't a set so not equal
return Py.True;
}
@Override
public PyObject __le__(PyObject other) {
return baseset___le__(other);
}
final PyObject baseset___le__(PyObject other) {
return baseset_issubset(asBaseSet(other));
}
@Override
public PyObject __ge__(PyObject other) {
return baseset___ge__(other);
}
final PyObject baseset___ge__(PyObject other) {
return baseset_issuperset(asBaseSet(other));
}
@Override
public PyObject __lt__(PyObject other) {
return baseset___lt__(other);
}
final PyObject baseset___lt__(PyObject other) {
BaseSet bs = asBaseSet(other);
return Py.newBoolean(size() < bs.size() && baseset_issubset(other).__nonzero__());
}
@Override
public PyObject __gt__(PyObject other) {
return baseset___gt__(other);
}
final PyObject baseset___gt__(PyObject other) {
BaseSet bs = asBaseSet(other);
return Py.newBoolean(size() > bs.size() && baseset_issuperset(other).__nonzero__());
}
/**
* Used for pickling. Uses the module <code>setsfactory</code> to
* export safe constructors.
*
* @return a tuple of (constructor, (elements))
*/
@Override
public PyObject __reduce__() {
return baseset___reduce__();
}
final PyObject baseset___reduce__(){
PyObject args = new PyTuple(new PyList((PyObject)this));
PyObject dict = __findattr__("__dict__");
if (dict == null) {
dict = Py.None;
}
return new PyTuple(getType(), args, dict);
}
final PyObject baseset_union(PyObject other) {
BaseSet result = BaseSet.makeNewSet(getType(), this);
result._update(other);
return result;
}
final PyObject baseset_union(PyObject [] args) {
BaseSet result = BaseSet.makeNewSet(getType(), this);
for (PyObject item: args) {
result._update(item);
}
return result;
}
final PyObject baseset_intersection(PyObject other) {
PyObject little, big;
if (!(other instanceof BaseSet)) {
other = new PySet(other);
}
if (size() <= __builtin__.len(other)) {
little = this;
big = other;
} else {
little = other;
big = this;
}
PyObject common = __builtin__.filter(big.__getattr__("__contains__"), little);
return BaseSet.makeNewSet(getType(), common);
}
final PyObject baseset_intersection(PyObject [] args) {
BaseSet result = BaseSet.makeNewSet(getType(), this);
if (args.length == 0) {
return result;
}
for (PyObject other: args) {
result = (BaseSet)result.baseset_intersection(other);
}
return result;
}
final PyObject baseset_copy() {
BaseSet copy = BaseSet.makeNewSet(getType(), this);
return copy;
}
final PyObject baseset_issubset(PyObject other) {
BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other);
if (size() > bs.size()) {
return Py.False;
}
for (Object p : _set) {
if (!bs._set.contains(p)) {
return Py.False;
}
}
return Py.True;
}
final PyObject baseset_issuperset(PyObject other) {
BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other);
return bs.baseset_issubset(this);
}
final PyObject baseset_isdisjoint(PyObject other) {
BaseSet bs = other instanceof BaseSet ? (BaseSet)other : new PySet(other);
return Collections.disjoint(_set, bs._set) ? Py.True : Py.False;
}
@Override
public String toString() {
return baseset_toString();
}
final String baseset_toString() {
String name = getType().fastGetName();
ThreadState ts = Py.getThreadState();
if (!ts.enterRepr(this)) {
return name + "(...)";
}
StringBuilder buf = new StringBuilder(name).append("([");
for (Iterator<PyObject> i = _set.iterator(); i.hasNext();) {
buf.append((i.next()).__repr__().toString());
if (i.hasNext()) {
buf.append(", ");
}
}
buf.append("])");
ts.exitRepr(this);
return buf.toString();
}
/**
* Casts other as BaseSet, throwing a TypeError tailored for the rich comparison
* methods when not applicable.
*
* @param other a PyObject
* @return a BaseSet
*/
protected final BaseSet asBaseSet(PyObject other) {
if (other instanceof BaseSet) {
return (BaseSet)other;
}
throw Py.TypeError("can only compare to a set");
}
/**
* Return a PyFrozenSet whose contents are shared with value when value is a BaseSet and pye is
* a TypeError.
*
* WARNING: The PyFrozenSet returned is only intended to be used temporarily (and internally);
* since its contents are shared with value, it could be mutated!
*
* This is better than special-casing behavior based on isinstance, because a Python subclass
* can override, say, __hash__ and all of a sudden you can't assume that a non-PyFrozenSet is
* unhashable anymore.
*
* @param pye The exception thrown from a hashable operation.
* @param value The object which was unhashable.
* @return A PyFrozenSet if appropriate, otherwise the pye is rethrown
*/
protected final PyFrozenSet asFrozen(PyException pye, PyObject value) {
if (!(value instanceof BaseSet) || !pye.match(Py.TypeError)) {
throw pye;
}
PyFrozenSet tmp = new PyFrozenSet();
tmp._set = ((BaseSet)value)._set;
return tmp;
}
/**
* Create a new set of type.
*
* @param type a set type
* @return a new set
*/
protected static BaseSet makeNewSet(PyType type) {
return makeNewSet(type, null);
}
/**
* Create a new set of type from iterable.
*
* @param type a set type
* @param iterable an iterable or null
* @return a new set
*/
protected static BaseSet makeNewSet(PyType type, PyObject iterable) {
BaseSet so;
if (type == PySet.TYPE) {
so = new PySet(iterable);
} else if (type == PyFrozenSet.TYPE) {
so = new PyFrozenSet(iterable);
} else if (Py.isSubClass(type, PySet.TYPE)) {
so = (BaseSet)(type.__call__(iterable == null ? Py.EmptyTuple : iterable));
} else {
so = new PyFrozenSetDerived(type, iterable);
}
return so;
}
@Override
public int size() {
return _set.size();
}
@Override
public void clear() {
_set.clear();
}
@Override
public boolean isEmpty() {
return _set.isEmpty();
}
@Override
public boolean add(Object o) {
return _set.add(Py.java2py(o));
}
@Override
public boolean contains(Object o) {
return _set.contains(Py.java2py(o));
}
@Override
public boolean remove(Object o) {
return _set.remove(Py.java2py(o));
}
@Override
public boolean addAll(Collection c) {
boolean added = false;
for (Object object : c) {
added |= add(object);
}
return added;
}
@Override
public boolean containsAll(Collection c) {
for (Object object : c) {
if (!_set.contains(Py.java2py(object))) {
return false;
}
}
return true;
}
@Override
public boolean removeAll(Collection c) {
boolean removed = false;
for (Object object : c) {
removed |= _set.remove(Py.java2py(object));
}
return removed;
}
@Override
public boolean retainAll(Collection c) {
boolean modified = false;
Iterator e = iterator();
while (e.hasNext()) {
if (!c.contains(e.next())) {
e.remove();
modified = true;
}
}
return modified;
}
@Override
public Iterator iterator() {
return new Iterator() {
Iterator<PyObject> real = _set.iterator();
@Override
public boolean hasNext() {
return real.hasNext();
}
@Override
public Object next() {
return Py.tojava(real.next(), Object.class);
}
@Override
public void remove() {
real.remove();
}
};
}
@Override
public Object[] toArray() {
return toArray(new Object[size()]);
}
@Override
public Object[] toArray(Object a[]) {
int size = size();
if (a.length < size) {
a = (Object[])Array.newInstance(a.getClass().getComponentType(), size);
}
Iterator<PyObject> it = iterator();
for (int i = 0; i < size; i++) {
a[i] = it.next();
}
if (a.length > size) {
a[size] = null;
}
return a;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retValue;
for (PyObject ob: _set) {
if (ob != null) {
retValue = visit.visit(ob, arg);
if (retValue != 0) {
return retValue;
}
}
}
return 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && _set.contains(ob);
}
}