Permalink
Browse files

CLJ-1070 make PersistentQueue's hash & equiv match

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent ed9670b commit a34e8f478220ea3394e672ea77b017feffec5b99 @philandstuff philandstuff committed with stuarthalloway Sep 3, 2012
Showing with 15 additions and 3 deletions.
  1. +12 −3 src/jvm/clojure/lang/PersistentQueue.java
  2. +3 −0 test/clojure/test_clojure/data_structures.clj
View
15 src/jvm/clojure/lang/PersistentQueue.java
@@ -21,7 +21,7 @@
* so no reversing or suspensions required for persistent use
*/
-public class PersistentQueue extends Obj implements IPersistentList, Collection, Counted{
+public class PersistentQueue extends Obj implements IPersistentList, Collection, Counted, IHashEq{
final public static PersistentQueue EMPTY = new PersistentQueue(null, 0, null, null);
@@ -70,16 +70,25 @@ public boolean equals(Object obj){
public int hashCode(){
if(_hash == -1)
{
- int hash = 0;
+ int hash = 1;
for(ISeq s = seq(); s != null; s = s.next())
{
- hash = Util.hashCombine(hash, Util.hash(s.first()));
+ hash = 31 * hash + (s.first() == null ? 0 : s.first().hashCode());
}
this._hash = hash;
}
return _hash;
}
+public int hasheq() {
+ int hash = 1;
+ for(ISeq s = seq(); s != null; s = s.next())
+ {
+ hash = 31 * hash + Util.hasheq(s.first());
+ }
+ return hash;
+}
+
public Object peek(){
return RT.first(f);
}
View
3 test/clojure/test_clojure/data_structures.clj
@@ -865,6 +865,9 @@
(are [x y] (= x y)
EMPTY EMPTY
(into EMPTY (range 50)) (into EMPTY (range 50))
+ (conj EMPTY (Long. -1)) (conj EMPTY (Integer. -1))
+ (hash (conj EMPTY (Long. -1))) (hash (conj EMPTY (Integer. -1)))
+ (hash [1 2 3]) (hash (conj EMPTY 1 2 3))
(range 5) (into EMPTY (range 5))
(range 1 6) (-> EMPTY
(into (range 6))

0 comments on commit a34e8f4

Please sign in to comment.