summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2008-03-03 21:14:24 +0000
committerRich Hickey <richhickey@gmail.com>2008-03-03 21:14:24 +0000
commit50eed41cee8a274ac37a5052ae1f3cd232750176 (patch)
treebbc18d991c17ef6400b3451ce4ae98409388ac33
parent1eb86f5103d7aa46b40d1cdfca343a0e30f53237 (diff)
starting set support
-rw-r--r--src/jvm/clojure/lang/APersistentMap.java6
-rw-r--r--src/jvm/clojure/lang/APersistentSet.java139
-rw-r--r--src/jvm/clojure/lang/IPersistentSet.java18
-rw-r--r--src/jvm/clojure/lang/PersistentHashSet.java65
4 files changed, 225 insertions, 3 deletions
diff --git a/src/jvm/clojure/lang/APersistentMap.java b/src/jvm/clojure/lang/APersistentMap.java
index 9e7cc289..d131cbce 100644
--- a/src/jvm/clojure/lang/APersistentMap.java
+++ b/src/jvm/clojure/lang/APersistentMap.java
@@ -180,10 +180,10 @@ public boolean removeAll(Collection c){
public boolean containsAll(Collection c){
for(Object o : c)
{
- if(contains(o))
- return true;
+ if(!contains(o))
+ return false;
}
- return false;
+ return true;
}
public Object[] toArray(Object[] a){
diff --git a/src/jvm/clojure/lang/APersistentSet.java b/src/jvm/clojure/lang/APersistentSet.java
new file mode 100644
index 00000000..b55631bc
--- /dev/null
+++ b/src/jvm/clojure/lang/APersistentSet.java
@@ -0,0 +1,139 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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.util.Collection;
+import java.util.Iterator;
+
+public abstract class APersistentSet extends AFn implements IPersistentSet, Collection{
+int _hash = -1;
+final IPersistentMap impl;
+
+protected APersistentSet(IPersistentMap meta, IPersistentMap impl){
+ super(meta);
+ this.impl = impl;
+}
+
+public boolean contains(Object key){
+ return impl.containsKey(key);
+}
+
+public int count(){
+ return impl.count();
+}
+
+public ISeq seq(){
+ return RT.keys(impl);
+}
+
+public Object invoke(Object arg1) throws Exception{
+ return contains(arg1)?arg1:null;
+}
+
+public boolean equals(Object obj){
+ if(!(obj instanceof IPersistentSet))
+ return false;
+ IPersistentSet m = (IPersistentSet) obj;
+
+ if(m.count() != count() || m.hashCode() != hashCode())
+ return false;
+
+ for(ISeq s = seq(); s != null; s = s.rest())
+ {
+ if(!m.contains(s.first()))
+ return false;
+ }
+
+ return true;
+}
+
+public int hashCode(){
+ if(_hash == -1)
+ {
+ int hash = count();
+ for(ISeq s = seq(); s != null; s = s.rest())
+ {
+ Object e = s.first();
+ hash = RT.hashCombine(hash,RT.hash(e));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
+
+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){
+ if(a.length >= count())
+ {
+ ISeq s = seq();
+ for(int i = 0; s != null; ++i, s = s.rest())
+ {
+ a[i] = s.first();
+ }
+ if(a.length >= count())
+ a[count()] = null;
+ return a;
+ }
+ else
+ return toArray();
+}
+
+public int size(){
+ return count();
+}
+
+public boolean isEmpty(){
+ return count() == 0;
+}
+
+public Iterator iterator(){
+ return new SeqIterator(seq());
+}
+
+}
diff --git a/src/jvm/clojure/lang/IPersistentSet.java b/src/jvm/clojure/lang/IPersistentSet.java
new file mode 100644
index 00000000..0aeebf06
--- /dev/null
+++ b/src/jvm/clojure/lang/IPersistentSet.java
@@ -0,0 +1,18 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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;
+
+public interface IPersistentSet extends IPersistentCollection{
+ public IPersistentSet disjoin(Object key) throws Exception;
+ public boolean contains(Object key);
+}
diff --git a/src/jvm/clojure/lang/PersistentHashSet.java b/src/jvm/clojure/lang/PersistentHashSet.java
new file mode 100644
index 00000000..c28ff2cb
--- /dev/null
+++ b/src/jvm/clojure/lang/PersistentHashSet.java
@@ -0,0 +1,65 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
+ * which can be found in the file CPL.TXT 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.util.List;
+import java.util.Iterator;
+
+public class PersistentHashSet extends APersistentSet{
+
+static public final PersistentHashSet EMPTY = new PersistentHashSet(null, PersistentHashMap.EMPTY);
+
+public static PersistentHashSet create(Object... init){
+ PersistentHashSet ret = EMPTY;
+ for(int i = 0; i < init.length; i++)
+ {
+ ret = (PersistentHashSet) ret.cons(init[i]);
+ }
+ return ret;
+}
+
+public static PersistentHashSet create(List init){
+ PersistentHashSet ret = EMPTY;
+ for(Iterator i = init.iterator(); i.hasNext();)
+ {
+ Object key = i.next();
+ ret = (PersistentHashSet) ret.cons(key);
+ }
+ return ret;
+}
+
+static public PersistentHashSet create(ISeq items){
+ PersistentHashSet ret = EMPTY;
+ for(; items != null; items = items.rest())
+ {
+ ret = (PersistentHashSet) ret.cons(items.first());
+ }
+ return ret;
+}
+
+PersistentHashSet(IPersistentMap meta, IPersistentMap impl){
+ super(meta, impl);
+}
+
+public IPersistentSet disjoin(Object key) throws Exception{
+ if(contains(key))
+ return new PersistentHashSet(meta(),impl.without(key));
+ return this;
+}
+
+public IPersistentCollection cons(Object o){
+ if(contains(o))
+ return this;
+ return new PersistentHashSet(meta(),impl.assoc(o,o));
+}
+}