Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cache hasheq() for seqs, sets, vectors, maps and queues

Signed-off-by: Rich Hickey <richhickey@gmail.com>
  • Loading branch information...
commit d77489d3ce912c177fe288a6f399a5c1da6683db 1 parent 372f03e
Christophe Grand cgrand authored richhickey committed
11 src/jvm/clojure/lang/APersistentMap.java
@@ -15,6 +15,7 @@
15 15
16 16 public abstract class APersistentMap extends AFn implements IPersistentMap, Map, Iterable, Serializable, MapEquivalence, IHashEq {
17 17 int _hash = -1;
  18 +int _hasheq = -1;
18 19
19 20 public String toString(){
20 21 return RT.printString(this);
@@ -111,8 +112,16 @@ static public int mapHash(IPersistentMap m){
111 112 }
112 113
113 114 public int hasheq(){
  115 + if(_hasheq == -1)
  116 + {
  117 + this._hasheq = mapHasheq(this);
  118 + }
  119 + return _hasheq;
  120 +}
  121 +
  122 +static public int mapHasheq(IPersistentMap m) {
114 123 int hash = 0;
115   - for(ISeq s = this.seq(); s != null; s = s.next())
  124 + for(ISeq s = m.seq(); s != null; s = s.next())
116 125 {
117 126 Map.Entry e = (Map.Entry) s.first();
118 127 hash += Util.hasheq(e.getKey()) ^
18 src/jvm/clojure/lang/APersistentSet.java
@@ -19,6 +19,7 @@
19 19
20 20 public abstract class APersistentSet extends AFn implements IPersistentSet, Collection, Set, Serializable, IHashEq {
21 21 int _hash = -1;
  22 +int _hasheq = -1;
22 23 final IPersistentMap impl;
23 24
24 25 protected APersistentSet(IPersistentMap impl){
@@ -93,13 +94,16 @@ public int hashCode(){
93 94 }
94 95
95 96 public int hasheq(){
96   - int hash = 0;
97   - for(ISeq s = seq(); s != null; s = s.next())
98   - {
99   - Object e = s.first();
100   - hash += Util.hasheq(e);
101   - }
102   - return hash;
  97 + if(_hasheq == -1){
  98 + int hash = 0;
  99 + for(ISeq s = seq(); s != null; s = s.next())
  100 + {
  101 + Object e = s.first();
  102 + hash += Util.hasheq(e);
  103 + }
  104 + this._hasheq = hash;
  105 + }
  106 + return _hasheq;
103 107 }
104 108
105 109 public Object[] toArray(){
6 src/jvm/clojure/lang/APersistentVector.java
@@ -20,6 +20,7 @@
20 20 RandomAccess, Comparable,
21 21 Serializable, IHashEq {
22 22 int _hash = -1;
  23 +int _hasheq = -1;
23 24
24 25 public String toString(){
25 26 return RT.printString(this);
@@ -153,6 +154,7 @@ public int hashCode(){
153 154 }
154 155
155 156 public int hasheq(){
  157 + if(_hasheq == -1) {
156 158 int hash = 1;
157 159 Iterator i = iterator();
158 160 while(i.hasNext())
@@ -160,7 +162,9 @@ public int hasheq(){
160 162 Object obj = i.next();
161 163 hash = 31 * hash + Util.hasheq(obj);
162 164 }
163   - return hash;
  165 + _hasheq = hash;
  166 + }
  167 + return _hasheq;
164 168 }
165 169
166 170 public Object get(int index){
13 src/jvm/clojure/lang/ASeq.java
@@ -15,6 +15,7 @@
15 15
16 16 public abstract class ASeq extends Obj implements ISeq, Sequential, List, Serializable, IHashEq {
17 17 transient int _hash = -1;
  18 +transient int _hasheq = -1;
18 19
19 20 public String toString(){
20 21 return RT.printString(this);
@@ -74,12 +75,16 @@ public int hashCode(){
74 75 }
75 76
76 77 public int hasheq(){
77   - int hash = 1;
78   - for(ISeq s = seq(); s != null; s = s.next())
  78 + if(_hasheq == -1)
79 79 {
80   - hash = 31 * hash + Util.hasheq(s.first());
  80 + int hash = 1;
  81 + for(ISeq s = seq(); s != null; s = s.next())
  82 + {
  83 + hash = 31 * hash + Util.hasheq(s.first());
  84 + }
  85 + this._hasheq = hash;
81 86 }
82   - return hash;
  87 + return _hasheq;
83 88 }
84 89
85 90
19 src/jvm/clojure/lang/PersistentQueue.java
@@ -31,6 +31,7 @@
31 31 final PersistentVector r;
32 32 //static final int INITIAL_REAR_SIZE = 4;
33 33 int _hash = -1;
  34 +int _hasheq = -1;
34 35
35 36 PersistentQueue(IPersistentMap meta, int cnt, ISeq f, PersistentVector r){
36 37 super(meta);
@@ -80,13 +81,17 @@ public int hashCode(){
80 81 return _hash;
81 82 }
82 83
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;
  84 +public int hasheq() {
  85 + if(_hasheq == -1)
  86 + {
  87 + int hash = 1;
  88 + for(ISeq s = seq(); s != null; s = s.next())
  89 + {
  90 + hash = 31 * hash + Util.hasheq(s.first());
  91 + }
  92 + this._hasheq = hash;
  93 + }
  94 + return _hasheq;
90 95 }
91 96
92 97 public Object peek(){

0 comments on commit d77489d

Please sign in to comment.
Something went wrong with that request. Please try again.