-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
LongSet.java
98 lines (82 loc) · 2.07 KB
/
LongSet.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
package nallar.collections;
import net.minecraft.util.LongHashMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
public class LongSet extends AbstractSet<Long> implements Set<Long> {
private static final Object set = new Object();
private final LongHashMap m = new LongHashMap();
public boolean add(long l) {
return m.put(l, set) == null;
}
public boolean contains(long l) {
return m.containsItem(l);
}
@Override
public boolean contains(Object l) {
return m.containsItem((Long) l);
}
@Override
public boolean remove(Object l) {
return m.remove((Long) l) != null;
}
public boolean remove(long l) {
return m.remove(l) != null;
}
@Override
public LongIterator iterator() {
return new LongIterator(m.getKeys());
}
@Override
public int size() {
return 0;
}
public static final class LongIterator implements Iterator<Long> {
private static final long EMPTY_KEY = Long.MIN_VALUE;
private final long[][] keys;
private long nextKey = EMPTY_KEY;
private int outerIndex = 0;
private int innerIndex = 0;
public LongIterator(final long[][] keys) {
this.keys = keys;
nextLong();
}
@Override
public boolean hasNext() {
return nextKey != EMPTY_KEY;
}
@Deprecated
@Override
public Long next() {
return nextLong();
}
public long nextLong() {
long thisKey = this.nextKey;
if (thisKey == EMPTY_KEY) {
throw new NoSuchElementException();
}
long searchingKey = EMPTY_KEY;
for (; outerIndex < keys.length; outerIndex++) {
long[] keys = this.keys[outerIndex];
if (keys == null) {
innerIndex = 0;
continue;
}
for (; innerIndex < keys.length; innerIndex++) {
searchingKey = keys[innerIndex];
if (searchingKey == EMPTY_KEY) {
innerIndex = 0;
break;
}
}
}
this.nextKey = searchingKey;
return thisKey;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}