Skip to content

Commit d3eb3b4

Browse files
committed
Minimize calls to hash function in map.rs
1 parent 117aff8 commit d3eb3b4

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/lib/map.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,16 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
4646
// is always a power of 2), so that all buckets are probed for a
4747
// fixed key.
4848

49-
fn hashl[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
50-
ret (hasher(key) >>> (sys.rustrt.size_of[uint]() * 8u / 2u));
49+
fn hashl(uint n, uint nbkts) -> uint {
50+
ret (n >>> 16u);
5151
}
5252

53-
fn hashr[K](&hashfn[K] hasher, uint nbkts, &K key) -> uint {
54-
ret ((((~ 0u) >>> (sys.rustrt.size_of[uint]() * 8u / 2u))
55-
& hasher(key)) * 2u + 1u);
53+
fn hashr(uint n, uint nbkts) -> uint {
54+
ret ((((~ 0u) >>> 16u) & n) * 2u + 1u);
5655
}
5756

58-
fn hash[K](&hashfn[K] hasher, uint nbkts, &K key, uint i) -> uint {
59-
ret (hashl[K](hasher, nbkts, key)
60-
+ i * hashr[K](hasher, nbkts, key)) % nbkts;
57+
fn hash(uint h, uint nbkts, uint i) -> uint {
58+
ret (hashl(h, nbkts) + i * hashr(h, nbkts)) % nbkts;
6159
}
6260

6361
/**
@@ -73,8 +71,9 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
7371
-> bool
7472
{
7573
let uint i = 0u;
74+
let uint h = hasher(key);
7675
while (i < nbkts) {
77-
let uint j = hash[K](hasher, nbkts, key, i);
76+
let uint j = hash(h, nbkts, i);
7877
alt (bkts.(j)) {
7978
case (some[K, V](?k, _)) {
8079
if (eqer(key, k)) {
@@ -100,8 +99,9 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
10099
-> option.t[V]
101100
{
102101
let uint i = 0u;
102+
let uint h = hasher(key);
103103
while (i < nbkts) {
104-
let uint j = (hash[K](hasher, nbkts, key, i));
104+
let uint j = (hash(h, nbkts, i));
105105
alt (bkts.(j)) {
106106
case (some[K, V](?k, ?v)) {
107107
if (eqer(key, k)) {
@@ -189,8 +189,9 @@ fn mk_hashmap[K, V](&hashfn[K] hasher, &eqfn[K] eqer) -> hashmap[K, V] {
189189

190190
fn remove(&K key) -> option.t[V] {
191191
let uint i = 0u;
192+
let uint h = hasher(key);
192193
while (i < nbkts) {
193-
let uint j = (hash[K](hasher, nbkts, key, i));
194+
let uint j = (hash(h, nbkts, i));
194195
alt (bkts.(j)) {
195196
case (some[K, V](?k, ?v)) {
196197
if (eqer(key, k)) {

0 commit comments

Comments
 (0)