rlm@10
|
1 /**
|
rlm@10
|
2 * Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
3 * The use and distribution terms for this software are covered by the
|
rlm@10
|
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
5 * which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
6 * By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
7 * the terms of this license.
|
rlm@10
|
8 * You must not remove this notice, or any other, from this software.
|
rlm@10
|
9 **/
|
rlm@10
|
10
|
rlm@10
|
11 /* rich Mar 3, 2008 */
|
rlm@10
|
12
|
rlm@10
|
13 package clojure.lang;
|
rlm@10
|
14
|
rlm@10
|
15 import java.io.Serializable;
|
rlm@10
|
16 import java.util.Collection;
|
rlm@10
|
17 import java.util.Iterator;
|
rlm@10
|
18 import java.util.Set;
|
rlm@10
|
19
|
rlm@10
|
20 public abstract class APersistentSet extends AFn implements IPersistentSet, Collection, Set, Serializable {
|
rlm@10
|
21 int _hash = -1;
|
rlm@10
|
22 final IPersistentMap impl;
|
rlm@10
|
23
|
rlm@10
|
24 protected APersistentSet(IPersistentMap impl){
|
rlm@10
|
25 this.impl = impl;
|
rlm@10
|
26 }
|
rlm@10
|
27
|
rlm@10
|
28 public String toString(){
|
rlm@10
|
29 return RT.printString(this);
|
rlm@10
|
30 }
|
rlm@10
|
31
|
rlm@10
|
32 public boolean contains(Object key){
|
rlm@10
|
33 return impl.containsKey(key);
|
rlm@10
|
34 }
|
rlm@10
|
35
|
rlm@10
|
36 public Object get(Object key){
|
rlm@10
|
37 return impl.valAt(key);
|
rlm@10
|
38 }
|
rlm@10
|
39
|
rlm@10
|
40 public int count(){
|
rlm@10
|
41 return impl.count();
|
rlm@10
|
42 }
|
rlm@10
|
43
|
rlm@10
|
44 public ISeq seq(){
|
rlm@10
|
45 return RT.keys(impl);
|
rlm@10
|
46 }
|
rlm@10
|
47
|
rlm@10
|
48 public Object invoke(Object arg1) throws Exception{
|
rlm@10
|
49 return get(arg1);
|
rlm@10
|
50 }
|
rlm@10
|
51
|
rlm@10
|
52 public boolean equals(Object obj){
|
rlm@10
|
53 if(this == obj) return true;
|
rlm@10
|
54 if(!(obj instanceof Set))
|
rlm@10
|
55 return false;
|
rlm@10
|
56 Set m = (Set) obj;
|
rlm@10
|
57
|
rlm@10
|
58 if(m.size() != count() || m.hashCode() != hashCode())
|
rlm@10
|
59 return false;
|
rlm@10
|
60
|
rlm@10
|
61 for(Object aM : m)
|
rlm@10
|
62 {
|
rlm@10
|
63 if(!contains(aM))
|
rlm@10
|
64 return false;
|
rlm@10
|
65 }
|
rlm@10
|
66 // for(ISeq s = seq(); s != null; s = s.rest())
|
rlm@10
|
67 // {
|
rlm@10
|
68 // if(!m.contains(s.first()))
|
rlm@10
|
69 // return false;
|
rlm@10
|
70 // }
|
rlm@10
|
71
|
rlm@10
|
72 return true;
|
rlm@10
|
73 }
|
rlm@10
|
74
|
rlm@10
|
75 public boolean equiv(Object o){
|
rlm@10
|
76 return equals(o);
|
rlm@10
|
77 }
|
rlm@10
|
78
|
rlm@10
|
79 public int hashCode(){
|
rlm@10
|
80 if(_hash == -1)
|
rlm@10
|
81 {
|
rlm@10
|
82 //int hash = count();
|
rlm@10
|
83 int hash = 0;
|
rlm@10
|
84 for(ISeq s = seq(); s != null; s = s.next())
|
rlm@10
|
85 {
|
rlm@10
|
86 Object e = s.first();
|
rlm@10
|
87 // hash = Util.hashCombine(hash, Util.hash(e));
|
rlm@10
|
88 hash += Util.hash(e);
|
rlm@10
|
89 }
|
rlm@10
|
90 this._hash = hash;
|
rlm@10
|
91 }
|
rlm@10
|
92 return _hash;
|
rlm@10
|
93 }
|
rlm@10
|
94
|
rlm@10
|
95 public Object[] toArray(){
|
rlm@10
|
96 return RT.seqToArray(seq());
|
rlm@10
|
97 }
|
rlm@10
|
98
|
rlm@10
|
99 public boolean add(Object o){
|
rlm@10
|
100 throw new UnsupportedOperationException();
|
rlm@10
|
101 }
|
rlm@10
|
102
|
rlm@10
|
103 public boolean remove(Object o){
|
rlm@10
|
104 throw new UnsupportedOperationException();
|
rlm@10
|
105 }
|
rlm@10
|
106
|
rlm@10
|
107 public boolean addAll(Collection c){
|
rlm@10
|
108 throw new UnsupportedOperationException();
|
rlm@10
|
109 }
|
rlm@10
|
110
|
rlm@10
|
111 public void clear(){
|
rlm@10
|
112 throw new UnsupportedOperationException();
|
rlm@10
|
113 }
|
rlm@10
|
114
|
rlm@10
|
115 public boolean retainAll(Collection c){
|
rlm@10
|
116 throw new UnsupportedOperationException();
|
rlm@10
|
117 }
|
rlm@10
|
118
|
rlm@10
|
119 public boolean removeAll(Collection c){
|
rlm@10
|
120 throw new UnsupportedOperationException();
|
rlm@10
|
121 }
|
rlm@10
|
122
|
rlm@10
|
123 public boolean containsAll(Collection c){
|
rlm@10
|
124 for(Object o : c)
|
rlm@10
|
125 {
|
rlm@10
|
126 if(!contains(o))
|
rlm@10
|
127 return false;
|
rlm@10
|
128 }
|
rlm@10
|
129 return true;
|
rlm@10
|
130 }
|
rlm@10
|
131
|
rlm@10
|
132 public Object[] toArray(Object[] a){
|
rlm@10
|
133 if(a.length >= count())
|
rlm@10
|
134 {
|
rlm@10
|
135 ISeq s = seq();
|
rlm@10
|
136 for(int i = 0; s != null; ++i, s = s.next())
|
rlm@10
|
137 {
|
rlm@10
|
138 a[i] = s.first();
|
rlm@10
|
139 }
|
rlm@10
|
140 if(a.length > count())
|
rlm@10
|
141 a[count()] = null;
|
rlm@10
|
142 return a;
|
rlm@10
|
143 }
|
rlm@10
|
144 else
|
rlm@10
|
145 return toArray();
|
rlm@10
|
146 }
|
rlm@10
|
147
|
rlm@10
|
148 public int size(){
|
rlm@10
|
149 return count();
|
rlm@10
|
150 }
|
rlm@10
|
151
|
rlm@10
|
152 public boolean isEmpty(){
|
rlm@10
|
153 return count() == 0;
|
rlm@10
|
154 }
|
rlm@10
|
155
|
rlm@10
|
156 public Iterator iterator(){
|
rlm@10
|
157 return new SeqIterator(seq());
|
rlm@10
|
158 }
|
rlm@10
|
159
|
rlm@10
|
160 }
|