Permalink
Browse files

Fixing set equality.

The following:

(= #{-1} #{(Integer. -1)})

was broken due to a bug in set equality. APersistentMap deals with
things the proper way this patch mirrors the implementation direction
of it by supplying different methods for equals and equiv.

Signed-off-by: Aaron Bedra <aaron@aaronbedra.com>
Signed-off-by: Rich Hickey <richhickey@gmail.com>
  • Loading branch information...
abedra authored and richhickey committed Feb 2, 2013
1 parent 8262a7e commit 21425a642c44bd1cdb8d3e537a7b6a23c1267bc7
Showing with 34 additions and 20 deletions.
  1. +32 −19 src/jvm/clojure/lang/APersistentSet.java
  2. +2 −1 test/clojure/test_clojure/data_structures.clj
@@ -51,30 +51,43 @@ public Object invoke(Object arg1) {
}
public boolean equals(Object obj){
- if(this == obj) return true;
- if(!(obj instanceof Set))
- return false;
- Set m = (Set) obj;
+ return setEquals(this, obj);
+}
- if(m.size() != count() || m.hashCode() != hashCode())
- return false;
+static public boolean setEquals(IPersistentSet s1, Object obj) {
+ if(s1 == obj) return true;
+ if(!(obj instanceof Set))
+ return false;
+ Set m = (Set) obj;
- for(Object aM : m)
- {
- if(!contains(aM))
- return false;
- }
-// for(ISeq s = seq(); s != null; s = s.rest())
-// {
-// if(!m.contains(s.first()))
-// return false;
-// }
+ if(m.size() != s1.count())
+ return false;
- return true;
+ for(Object aM : m)
+ {
+ if(!s1.contains(aM))
+ return false;
+ }
+
+ return true;
}
-public boolean equiv(Object o){
- return equals(o);
+public boolean equiv(Object obj){
+ if (!(obj instanceof Set))
+ return false;
+
+ Set m = (Set) obj;
+
+ if (m.size() != size())
+ return false;
+
+ for(Object aM : m)
+ {
+ if(!contains(aM))
+ return false;
+ }
+
+ return true;
}
public int hashCode(){
@@ -647,7 +647,8 @@
(hash-set nil 2) #{nil 2}
(hash-set #{}) #{#{}}
(hash-set 1 #{}) #{1 #{}}
- (hash-set #{} 2) #{#{} 2} ))
+ (hash-set #{} 2) #{#{} 2}
+ (hash-set (Integer. -1)) (hash-set (Long. -1))))
(deftest test-sorted-set

0 comments on commit 21425a6

Please sign in to comment.