Skip to content

Commit

Permalink
hashCode calculation & caching for LazyTransformer
Browse files Browse the repository at this point in the history
This uses the hashCode impl from ASeq.
LazyTransformer is its own ISeq, unlike LazySeq which delegates
to the collections realized from its thunks.

This caches hash and hasheq as well.

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information
ghadishayban authored and stuarthalloway committed Jan 9, 2015
1 parent bf61027 commit 4e7c9cb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/jvm/clojure/lang/LazyTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.*;

public final class LazyTransformer extends Obj implements ISeq, Sequential, List, IPending, IHashEq{
transient int _hash = -1;
transient int _hasheq = -1;

IStepper stepper;
Object first = null;
Expand Down Expand Up @@ -210,14 +212,24 @@ public boolean equiv(Object o){
return ms == null;}

public int hashCode(){
ISeq s = seq();
if(s == null)
return 1;
return Util.hash(seq());
if(_hash == -1)
{
int hash = 1;
for(ISeq s = seq(); s != null; s = s.next())
{
hash = 31 * hash + (s.first() == null ? 0 : s.first().hashCode());
}
this._hash = hash;
}
return _hash;
}

public int hasheq(){
return Murmur3.hashOrdered(this);
if(_hasheq == -1)
{
_hasheq = Murmur3.hashOrdered(this);
}
return _hasheq;
}

public boolean equals(Object o){
Expand Down
3 changes: 2 additions & 1 deletion test/clojure/test_clojure/data_structures.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@
(lazy-seq (cons :a
(lazy-seq (cons "7th" nil))))))
(into clojure.lang.PersistentQueue/EMPTY
[-3 :a "7th"]) ]]
[-3 :a "7th"])
(sequence (map identity) [-3 :a "7th"]) ]]
(doseq [c1 colls1, c2 colls1]
(is-same-collection c1 c2)))
(is-same-collection [-3 1 7] (vector-of :long -3 1 7)))
Expand Down

0 comments on commit 4e7c9cb

Please sign in to comment.