Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a34e8f4

Browse files
philandstuffstuarthalloway
authored andcommitted
CLJ-1070 make PersistentQueue's hash & equiv match
Signed-off-by: Stuart Halloway <[email protected]>
1 parent ed9670b commit a34e8f4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/jvm/clojure/lang/PersistentQueue.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* so no reversing or suspensions required for persistent use
2222
*/
2323

24-
public class PersistentQueue extends Obj implements IPersistentList, Collection, Counted{
24+
public class PersistentQueue extends Obj implements IPersistentList, Collection, Counted, IHashEq{
2525

2626
final public static PersistentQueue EMPTY = new PersistentQueue(null, 0, null, null);
2727

@@ -70,16 +70,25 @@ public boolean equals(Object obj){
7070
public int hashCode(){
7171
if(_hash == -1)
7272
{
73-
int hash = 0;
73+
int hash = 1;
7474
for(ISeq s = seq(); s != null; s = s.next())
7575
{
76-
hash = Util.hashCombine(hash, Util.hash(s.first()));
76+
hash = 31 * hash + (s.first() == null ? 0 : s.first().hashCode());
7777
}
7878
this._hash = hash;
7979
}
8080
return _hash;
8181
}
8282

83+
public int hasheq() {
84+
int hash = 1;
85+
for(ISeq s = seq(); s != null; s = s.next())
86+
{
87+
hash = 31 * hash + Util.hasheq(s.first());
88+
}
89+
return hash;
90+
}
91+
8392
public Object peek(){
8493
return RT.first(f);
8594
}

test/clojure/test_clojure/data_structures.clj

+3
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,9 @@
865865
(are [x y] (= x y)
866866
EMPTY EMPTY
867867
(into EMPTY (range 50)) (into EMPTY (range 50))
868+
(conj EMPTY (Long. -1)) (conj EMPTY (Integer. -1))
869+
(hash (conj EMPTY (Long. -1))) (hash (conj EMPTY (Integer. -1)))
870+
(hash [1 2 3]) (hash (conj EMPTY 1 2 3))
868871
(range 5) (into EMPTY (range 5))
869872
(range 1 6) (-> EMPTY
870873
(into (range 6))

0 commit comments

Comments
 (0)