cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.95k stars 982 forks source link

hashtables with large number keys #692

Closed stannous closed 1 year ago

stannous commented 1 year ago

Is there a reason why I cannot use 2^63 as a key to my hashtable? (I need these for my bitboard based chess engine).

Thanks,

$ chez Chez Scheme Version 9.5.8 Copyright 1984-2022 Cisco Systems, Inc.

(define square (make-eq-hashtable)) (hashtable-set! square 44 "test1") (expt 2 63) 9223372036854775808 (hashtable-set! square (expt 2 63) "test2") (hashtable-entries square)

(44 9223372036854775808)

("test1" "test2")

(hashtable-ref square 44 #f) "test1" (hashtable-ref square (expt 2 63) #f)

f

(hashtable-keys square)

(44 9223372036854775808)

(hashtable-values square)

("test1" "test2")

burgerrg commented 1 year ago

You're using an eq-hashtable, so the keys must compare equal with eq?. 2^63 is too large to be a fixnum, so it's allocated on the heap and should be compared with eqv?.

In short, use make-eqv-hashtable instead.

dybvig commented 1 year ago

Alternatively, if all of your keys are exact nonnegative integers, using (make-hashtable values =) would be more efficient.

stannous commented 1 year ago

Thanks, Kent and Bob.