mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-23 21:10:29 +02:00
(Hash Table Reference): Add hashx- case
insensitive string example, add cross references to symbol-hash, string-hash, string-hash-ci, and char-set-hash.
This commit is contained in:
parent
438a3ba10d
commit
3adbc48c24
1 changed files with 30 additions and 9 deletions
|
@ -2252,8 +2252,7 @@ Like the association list functions, the hash table functions come in
|
||||||
several varieties, according to the equality test used for the keys.
|
several varieties, according to the equality test used for the keys.
|
||||||
Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
|
Plain @code{hash-} functions use @code{equal?}, @code{hashq-}
|
||||||
functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
|
functions use @code{eq?}, @code{hashv-} functions use @code{eqv?}, and
|
||||||
the @code{hashx-} functions use an application supplied test (for
|
the @code{hashx-} functions use an application supplied test.
|
||||||
instance to implement case insensitive strings).
|
|
||||||
|
|
||||||
A single @code{make-hash-table} creates a hash table suitable for use
|
A single @code{make-hash-table} creates a hash table suitable for use
|
||||||
with any set of functions, but it's imperative that just one set is
|
with any set of functions, but it's imperative that just one set is
|
||||||
|
@ -2266,12 +2265,34 @@ bucket in case distinct keys hash together. Direct access to the
|
||||||
pairs in those lists is provided by the @code{-handle-} functions.
|
pairs in those lists is provided by the @code{-handle-} functions.
|
||||||
|
|
||||||
For the @code{hashx-} ``extended'' routines, an application supplies a
|
For the @code{hashx-} ``extended'' routines, an application supplies a
|
||||||
@var{hash} function producing an integer index (like @code{hashq} etc
|
@var{hash} function producing an integer index like @code{hashq} etc
|
||||||
below), and an @var{assoc} alist search function (like @code{assq}
|
below, and an @var{assoc} alist search function like @code{assq} etc
|
||||||
etc, @xref{Retrieving Alist Entries}.). The aim in the @var{hash}
|
(@pxref{Retrieving Alist Entries}). Here's an example of such
|
||||||
function is to have different keys spread out across the vector, so
|
functions implementing case-insensitive hashing of string keys,
|
||||||
the bucket lists don't become long, but the exact values generated are
|
|
||||||
otherwise arbitrary.
|
@example
|
||||||
|
(use-modules (srfi srfi-1)
|
||||||
|
(srfi srfi-13))
|
||||||
|
(define (my-hash str size)
|
||||||
|
(remainder (string-hash-ci str) size))
|
||||||
|
(define (my-assoc str alist)
|
||||||
|
(find (lambda (pair) (string-ci=? str (car pair))) alist))
|
||||||
|
|
||||||
|
(define my-table (make-hash-table))
|
||||||
|
(hashx-set! my-hash my-assoc my-table "foo" 123)
|
||||||
|
|
||||||
|
(hashx-ref my-hash my-assoc my-table "FOO")
|
||||||
|
@result{} 123
|
||||||
|
@end example
|
||||||
|
|
||||||
|
In a @code{hashx-} @var{hash} function the aim is to spread keys
|
||||||
|
across the vector, so bucket lists don't become long, but the actual
|
||||||
|
values are arbitrary (so long as they're in the range 0 to
|
||||||
|
@math{@var{size}-1}). Helpful functions for forming a hash value, in
|
||||||
|
addition to @code{hashq} etc below, include @code{symbol-hash}
|
||||||
|
(@pxref{Symbol Keys}), @code{string-hash} and @code{string-hash-ci}
|
||||||
|
(@pxref{SRFI-13 Comparison}), and @code{char-set-hash} (@pxref{SRFI-14
|
||||||
|
Predicates/Comparison}).
|
||||||
|
|
||||||
@sp 1
|
@sp 1
|
||||||
@deffn {Scheme Procedure} make-hash-table [size]
|
@deffn {Scheme Procedure} make-hash-table [size]
|
||||||
|
@ -2380,7 +2401,7 @@ pair.
|
||||||
Apply @var{proc} to the entries in the given hash @var{table}. Each
|
Apply @var{proc} to the entries in the given hash @var{table}. Each
|
||||||
call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map}
|
call is @code{(@var{proc} @var{key} @var{value})}. @code{hash-map}
|
||||||
returns a list of the results from these calls, @code{hash-for-each}
|
returns a list of the results from these calls, @code{hash-for-each}
|
||||||
discards the results and returns unspecified value.
|
discards the results and returns an unspecified value.
|
||||||
|
|
||||||
Calls are made over the table entries in an unspecified order, and for
|
Calls are made over the table entries in an unspecified order, and for
|
||||||
@code{hash-map} the order of the values in the returned list is
|
@code{hash-map} the order of the values in the returned list is
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue