view src/clojure/lang/APersistentSet.java @ 10:ef7dbbd6452c

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