-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAPersistentSet.java
More file actions
196 lines (155 loc) · 3.79 KB
/
APersistentSet.java
File metadata and controls
196 lines (155 loc) · 3.79 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
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
/* rich Mar 3, 2008 */
package clojure.lang;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
public abstract class APersistentSet extends AFn implements IPersistentSet, Collection, Set, Serializable, IHashEq {
private static final long serialVersionUID = 889908853183699706L;
int _hash;
int _hasheq;
final IPersistentMap impl;
protected APersistentSet(IPersistentMap impl){
this.impl = impl;
}
public String toString(){
return RT.printString(this);
}
public boolean contains(Object key){
return impl.containsKey(key);
}
public Object get(Object key){
return impl.valAt(key);
}
public int count(){
return impl.count();
}
public ISeq seq(){
return RT.keys(impl);
}
public Object invoke(Object arg1) {
return get(arg1);
}
public boolean equals(Object obj){
return setEquals(this, obj);
}
static public boolean setEquals(IPersistentSet s1, Object obj) {
if(s1 == obj) return true;
if(!(obj instanceof Set))
return false;
Set m = (Set) obj;
if(m.size() != s1.count())
return false;
for(Object aM : m)
{
if(!s1.contains(aM))
return false;
}
return true;
}
public boolean equiv(Object obj){
if (!(obj instanceof Set))
return false;
Set m = (Set) obj;
if (m.size() != size())
return false;
for(Object aM : m)
{
if(!contains(aM))
return false;
}
return true;
}
public int hashCode(){
int hash = this._hash;
if(hash == 0)
{
//int hash = count();
for(ISeq s = seq(); s != null; s = s.next())
{
Object e = s.first();
// hash = Util.hashCombine(hash, Util.hash(e));
hash += Util.hash(e);
}
this._hash = hash;
}
return hash;
}
public int hasheq(){
int cached = this._hasheq;
if(cached == 0){
// int hash = 0;
// for(ISeq s = seq(); s != null; s = s.next())
// {
// Object e = s.first();
// hash += Util.hasheq(e);
// }
// this._hasheq = hash;
this._hasheq = cached = Murmur3.hashUnordered(this);
}
return cached;
}
public Object[] toArray(){
return RT.seqToArray(seq());
}
public boolean add(Object o){
throw new UnsupportedOperationException();
}
public boolean remove(Object o){
throw new UnsupportedOperationException();
}
public boolean addAll(Collection c){
throw new UnsupportedOperationException();
}
public void clear(){
throw new UnsupportedOperationException();
}
public boolean retainAll(Collection c){
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection c){
throw new UnsupportedOperationException();
}
public boolean containsAll(Collection c){
for(Object o : c)
{
if(!contains(o))
return false;
}
return true;
}
public Object[] toArray(Object[] a){
return RT.seqToPassedArray(seq(), a);
}
public int size(){
return count();
}
public boolean isEmpty(){
return count() == 0;
}
public Iterator iterator(){
if(impl instanceof IMapIterable)
return ((IMapIterable)impl).keyIterator();
else return new Iterator() {
private final Iterator iter = impl.iterator();
public boolean hasNext() {
return iter.hasNext();
}
public Object next() {
return ((IMapEntry)iter.next()).key();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}