Skip to content

Commit acfb674

Browse files
committed
Move and deduplicate hash functions
1 parent 8ba5348 commit acfb674

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Oct 9th 2025
2+
3+
- Deduplicated and moved `when-resize` / `resizer` to `std/data/hash` from `hashset` / `hashmap`, and `pub import`ed `hash` from those modules.

std/data/hash.kk

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,16 @@ pub fun either/hash( e : either<l, r>, ?l/hasher : (l, int64) -> int, ?r/hasher
9393
hash-vector-int64( vec, seed )
9494

9595

96+
// Internal function that dictates when to resize the hash-set. This can be overridden with implicits.
97+
pub fun when-resize( table-size : int, item-count : int ) : bool
98+
if 2 * table-size <= item-count then
99+
True
100+
else
101+
False
102+
103+
// Internal function that determines the new size of a hash-set when inserting. This can be overridden with implicits.
104+
pub fun resizer( table-size : int, item-count : int ) : int
105+
item-count * 2
106+
96107
fun example()
97108
seed.println

std/data/hashmap.kk

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module std/data/hashmap
1010
import std/core-extras
1111
import std/num/int64
12-
import std/data/hash
12+
pub import std/data/hash
1313

1414

1515
pub struct hash-map<k, v>
@@ -100,17 +100,6 @@ fun helper/rehash( v : vector<list<(k, v)>>, buckets : list<(k, v)>, seed : int6
100100
val new-v = v.unsafe-set( position, Cons( (key, value), v.unsafe-idx( position.ssize_t ) ) )
101101
helper/rehash( new-v, xs, seed )
102102

103-
// Internal function that dictates when to resize the hash-map. This can be overridden with implicits.
104-
pub fun when-resize( table-size : int, item-count : int ) : bool
105-
if 2 * table-size <= item-count then
106-
True
107-
else
108-
False
109-
110-
// Internal function that determines the new size of a hash-map when inserting. This can be overridden with implicits.
111-
pub fun resizer( table-size : int, item-count : int ) : int
112-
item-count * 2
113-
114103
// Inserts a `value` into the hash-map with a given `key`.
115104
pub fun insert( hm : hash-map<k,v>, key : k, value : v, ?hash : (k, int64) -> int, ?(==) : (k, k) -> bool, ?when-resize : (int, int) -> bool, ?resizer : (int, int) -> int ) : hash-map<k, v>
116105
val the-hash = hash(key, hm.seed)

std/data/hashset.kk

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
module std/data/hashset
1010
import std/num/int64
1111
import std/core-extras
12-
import std/data/hash
12+
pub import std/data/hash
1313

1414
val int-hash : (int, int64) -> int = int/hash
1515
val char-hash : (char, int64) -> int = char/hash
@@ -100,18 +100,6 @@ fun helper/rehash( v : vector<list<v>>, buckets : list<v>, seed : int64, ?hash :
100100
val new-v = v.unsafe-set( position, Cons( value, v.unsafe-idx( position.ssize_t ) ) )
101101
helper/rehash( new-v, xs, seed )
102102

103-
104-
// Internal function that dictates when to resize the hash-set. This can be overridden with implicits.
105-
pub fun when-resize( table-size : int, item-count : int ) : bool
106-
if 2 * table-size <= item-count then
107-
True
108-
else
109-
False
110-
111-
// Internal function that determines the new size of a hash-set when inserting. This can be overridden with implicits.
112-
pub fun resizer( table-size : int, item-count : int ) : int
113-
item-count * 2
114-
115103
// Inserts a `value` into the hash-set.
116104
pub fun insert( hs : hash-set<v>, value : v, ?hash : (v, int64) -> int, ?(==) : (v, v) -> bool, ?when-resize : (int, int) -> bool, ?resizer : (int, int) -> int ) : hash-set<v>
117105
val the-hash = hash(value, hs.seed)

0 commit comments

Comments
 (0)