summaryrefslogtreecommitdiff
path: root/src/cli/runtime/APersistentMap.cs
diff options
context:
space:
mode:
authorRich Hickey <richhickey@gmail.com>2006-08-05 22:39:38 +0000
committerRich Hickey <richhickey@gmail.com>2006-08-05 22:39:38 +0000
commitc318f7c60e4cff91b0591d1e9dc8bee63c716404 (patch)
tree09afb1b876bc2efdc4e943a7fcd963809d29a2c2 /src/cli/runtime/APersistentMap.cs
parent397f768ca30a4615c753ec544dedc5c39be7743b (diff)
added equals and hashcode to maps
Diffstat (limited to 'src/cli/runtime/APersistentMap.cs')
-rw-r--r--src/cli/runtime/APersistentMap.cs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/cli/runtime/APersistentMap.cs b/src/cli/runtime/APersistentMap.cs
index 99ca0ea4..8c082d23 100644
--- a/src/cli/runtime/APersistentMap.cs
+++ b/src/cli/runtime/APersistentMap.cs
@@ -15,6 +15,7 @@ using System.Collections;
namespace clojure.lang
{
public abstract class APersistentMap : Obj, IPersistentMap{
+ int _hash = -1;
public override Obj withMeta(IPersistentMap meta)
{
@@ -25,7 +26,39 @@ public abstract class APersistentMap : Obj, IPersistentMap{
return ret;
}
+override public bool Equals(Object obj) {
+ IPersistentMap m = obj as IPersistentMap;
+ if(obj == null)
+ return false;
+
+ if(m.count() != count())
+ return false;
+ for(ISeq s = seq();s!=null;s = s.rest())
+ {
+ IMapEntry e = (IMapEntry) s.first();
+ IMapEntry me = m.find(e.key());
+
+ if(me == null || !RT.equal(e.val(),me.val()))
+ return false;
+ }
+
+ return true;
+}
+
+override public int GetHashCode() {
+ if(_hash == -1)
+ {
+ int hash = count();
+ for(ISeq s = seq();s!=null;s = s.rest())
+ {
+ IMapEntry e = (IMapEntry) s.first();
+ hash ^= RT.hashCombine(RT.hash(e.key()), RT.hash(e.val()));
+ }
+ this._hash = hash;
+ }
+ return _hash;
+}
#region IPersistentMap Members
abstract public IPersistentMap assocEx(object key, object val);