summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-08-05 23:01:07 +0000
committerRich Hickey <richhickey@gmail.com>2006-08-05 23:01:07 +0000
commit3d28909a40372830824c98434fab45198248e75e (patch)
tree3c6dc22138922fad3db9163f82131af7a3c9ce56 /src
parentc318f7c60e4cff91b0591d1e9dc8bee63c716404 (diff)
added equals and hashcode to arrays and seqs
Diffstat (limited to 'src')
-rw-r--r--src/cli/runtime/APersistentArray.cs39
-rw-r--r--src/cli/runtime/ASeq.cs26
-rw-r--r--src/jvm/clojure/lang/APersistentArray.java41
-rw-r--r--src/jvm/clojure/lang/ASeq.java27
4 files changed, 133 insertions, 0 deletions
diff --git a/src/cli/runtime/APersistentArray.cs b/src/cli/runtime/APersistentArray.cs
index fe6a42f5..de9b548c 100644
--- a/src/cli/runtime/APersistentArray.cs
+++ b/src/cli/runtime/APersistentArray.cs
@@ -14,6 +14,7 @@ using System.Collections;
namespace clojure.lang
{
public abstract class APersistentArray : Obj, IPersistentArray {
+int _hash = -1;
public virtual IPersistentCollection cons(Object o) {
PersistentArrayList ret = new PersistentArrayList(this, this.count() + 10);
@@ -31,6 +32,44 @@ public virtual IPersistentCollection cons(Object o) {
return ret;
}
+override public bool Equals(Object obj) {
+ if(obj is IPersistentArray)
+ {
+ IPersistentArray ma = (IPersistentArray) obj;
+ if(ma.count() != count())
+ return false;
+ for(int i=0;i<count();i++)
+ {
+ if(!RT.equal(nth(i),ma.nth(i)))
+ return false;
+ }
+ }
+ else
+ {
+ if(!(obj is Sequential))
+ return false;
+ for(ISeq s = seq(), ms = ((IPersistentCollection)obj).seq();s!=null;s = s.rest(), ms = ms.rest())
+ {
+ if(ms == null || !RT.equal(s.first(),ms.first()))
+ return false;
+ }
+ }
+
+ return true;
+}
+
+override public int GetHashCode() {
+ if(_hash == -1)
+ {
+ int hash = 0;
+ for(int i=0;i<count();i++)
+ {
+ hash = RT.hashCombine(hash, RT.hash(nth(i)));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
#region IArray Members
diff --git a/src/cli/runtime/ASeq.cs b/src/cli/runtime/ASeq.cs
index 18c307d6..252a69f6 100644
--- a/src/cli/runtime/ASeq.cs
+++ b/src/cli/runtime/ASeq.cs
@@ -14,6 +14,7 @@ namespace clojure.lang
{
public abstract class ASeq : Obj, ISeq{
+int _hash = -1;
public override Obj withMeta(IPersistentMap meta)
{
@@ -23,6 +24,31 @@ public abstract class ASeq : Obj, ISeq{
ret._meta = meta;
return ret;
}
+
+override public bool Equals(Object obj) {
+ if(!(obj is Sequential))
+ return false;
+ for(ISeq s = seq(), ms = ((IPersistentCollection)obj).seq();s!=null;s = s.rest(), ms = ms.rest())
+ {
+ if(ms == null || !RT.equal(s.first(),ms.first()))
+ return false;
+ }
+
+ return true;
+}
+
+override public int GetHashCode() {
+ if(_hash == -1)
+ {
+ int hash = 0;
+ for(ISeq s = seq();s!=null;s = s.rest())
+ {
+ hash = RT.hashCombine(hash, RT.hash(s.first()));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
public virtual Object peek() {
return first();
diff --git a/src/jvm/clojure/lang/APersistentArray.java b/src/jvm/clojure/lang/APersistentArray.java
index bd5300d4..f1fb426a 100644
--- a/src/jvm/clojure/lang/APersistentArray.java
+++ b/src/jvm/clojure/lang/APersistentArray.java
@@ -11,6 +11,7 @@
package clojure.lang;
public abstract class APersistentArray extends Obj implements IPersistentArray, Cloneable {
+int _hash = -1;
public IPersistentCollection cons(Object o) {
PersistentArrayList ret = new PersistentArrayList(this, this.count() + 10);
@@ -33,6 +34,46 @@ public Obj withMeta(IPersistentMap meta) {
}
}
+public boolean equals(Object obj) {
+ if(obj instanceof IPersistentArray)
+ {
+ IPersistentArray ma = (IPersistentArray) obj;
+ if(ma.count() != count())
+ return false;
+ for(int i=0;i<count();i++)
+ {
+ if(!RT.equal(nth(i),ma.nth(i)))
+ return false;
+ }
+ }
+ else
+ {
+ if(!(obj instanceof Sequential))
+ return false;
+ for(ISeq s = seq(), ms = ((IPersistentCollection)obj).seq();s!=null;s = s.rest(), ms = ms.rest())
+ {
+ if(ms == null || !RT.equal(s.first(),ms.first()))
+ return false;
+ }
+ }
+
+ return true;
+}
+
+public int hashCode() {
+ if(_hash == -1)
+ {
+ int hash = 0;
+ for(int i=0;i<count();i++)
+ {
+ hash = RT.hashCombine(hash, RT.hash(nth(i)));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
+
+
public boolean contains(Object key) {
if(!(key instanceof Number))
return false;
diff --git a/src/jvm/clojure/lang/ASeq.java b/src/jvm/clojure/lang/ASeq.java
index 3d2168d4..663d5b00 100644
--- a/src/jvm/clojure/lang/ASeq.java
+++ b/src/jvm/clojure/lang/ASeq.java
@@ -11,6 +11,33 @@
package clojure.lang;
public abstract class ASeq extends Obj implements ISeq{
+int _hash = -1;
+
+public boolean equals(Object obj) {
+
+ if(!(obj instanceof Sequential))
+ return false;
+ for(ISeq s = seq(), ms = ((IPersistentCollection)obj).seq();s!=null;s = s.rest(), ms = ms.rest())
+ {
+ if(ms == null || !RT.equal(s.first(),ms.first()))
+ return false;
+ }
+
+ return true;
+}
+
+public int hashCode() {
+ if(_hash == -1)
+ {
+ int hash = 0;
+ for(ISeq s = seq();s!=null;s = s.rest())
+ {
+ hash = RT.hashCombine(hash, RT.hash(s.first()));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
public Object peek() {
return first();