2254 |
then used consistently, or results will be unpredictable. |
then used consistently, or results will be unpredictable. |
2255 |
|
|
2256 |
@sp 1 |
@sp 1 |
2257 |
Hash tables are implemented as a vector indexed by an integer formed |
Hash tables are implemented as a vector indexed by a hash value formed |
2258 |
from the key, with an association list of key/value pairs for each |
from the key, with an association list of key/value pairs for each |
2259 |
bucket in case distinct keys hash together. Direct access to the |
bucket in case distinct keys hash together. Direct access to the |
2260 |
pairs in those lists is provided by the @code{-handle-} functions. |
pairs in those lists is provided by the @code{-handle-} functions. |
2261 |
|
|
2262 |
|
When the number of table entries goes above a threshold the vector is |
2263 |
|
increased and the entries rehashed, to prevent the bucket lists |
2264 |
|
becoming too long and slowing down accesses. When the number of |
2265 |
|
entries goes below a threshold the vector is decreased to save space. |
2266 |
|
|
2267 |
|
@sp 1 |
2268 |
For the @code{hashx-} ``extended'' routines, an application supplies a |
For the @code{hashx-} ``extended'' routines, an application supplies a |
2269 |
@var{hash} function producing an integer index like @code{hashq} etc |
@var{hash} function producing an integer index like @code{hashq} etc |
2270 |
below, and an @var{assoc} alist search function like @code{assq} etc |
below, and an @var{assoc} alist search function like @code{assq} etc |
2274 |
@example |
@example |
2275 |
(use-modules (srfi srfi-1) |
(use-modules (srfi srfi-1) |
2276 |
(srfi srfi-13)) |
(srfi srfi-13)) |
2277 |
|
|
2278 |
(define (my-hash str size) |
(define (my-hash str size) |
2279 |
(remainder (string-hash-ci str) size)) |
(remainder (string-hash-ci str) size)) |
2280 |
(define (my-assoc str alist) |
(define (my-assoc str alist) |
2288 |
@end example |
@end example |
2289 |
|
|
2290 |
In a @code{hashx-} @var{hash} function the aim is to spread keys |
In a @code{hashx-} @var{hash} function the aim is to spread keys |
2291 |
across the vector, so bucket lists don't become long, but the actual |
across the vector, so bucket lists don't become long. But the actual |
2292 |
values are arbitrary (so long as they're in the range 0 to |
values are arbitrary as long as they're in the range 0 to |
2293 |
@math{@var{size}-1}). Helpful functions for forming a hash value, in |
@math{@var{size}-1}. Helpful functions for forming a hash value, in |
2294 |
addition to @code{hashq} etc below, include @code{symbol-hash} |
addition to @code{hashq} etc below, include @code{symbol-hash} |
2295 |
(@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci} |
(@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci} |
2296 |
(@pxref{SRFI-13 Comparison}), and @code{char-set-hash} (@pxref{SRFI-14 |
(@pxref{SRFI-13 Comparison}), and @code{char-set-hash} (@pxref{SRFI-14 |
2297 |
Predicates/Comparison}). |
Predicates/Comparison}). |
2298 |
|
|
2299 |
|
Note that currently, unfortunately, there's no @code{hashx-remove!} |
2300 |
|
function, which rather limits the usefulness of the @code{hashx-} |
2301 |
|
routines. |
2302 |
|
|
2303 |
@sp 1 |
@sp 1 |
2304 |
@deffn {Scheme Procedure} make-hash-table [size] |
@deffn {Scheme Procedure} make-hash-table [size] |
2305 |
Create a new hash table, with an optional initial vector @var{size}. |
Create a new hash table, with an optional minimum vector @var{size}. |
2306 |
|
|
2307 |
@var{size} doesn't limit the entries in the table, merely gives a |
When @var{size} is given, the table vector will still grow and shrink |
2308 |
starting size for the internal vector. A prime number bigger than the |
automatically, as described above, but with @var{size} as a minimum. |
2309 |
expected number of entries would be a good choice. |
If an application knows roughly how many entries the table will hold |
2310 |
|
then it can use @var{size} to avoid rehashing when initial entries are |
2311 |
|
added. |
2312 |
@end deffn |
@end deffn |
2313 |
|
|
2314 |
@deffn {Scheme Procedure} hash-ref table key [dflt] |
@deffn {Scheme Procedure} hash-ref table key [dflt] |