(guile.info.gz) Hash Table Reference
Info Catalog
(guile.info.gz) Hash Table Examples
(guile.info.gz) Hash Tables
22.7.3.2 Hash Table Reference
.............................
Like the association list functions, the hash table functions come in
several varieties: `hashq', `hashv', and `hash'. The `hashq' functions
use `eq?' to determine whether two keys match. The `hashv' functions
use `eqv?', and the `hash' functions use `equal?'.
In each of the functions that follow, the TABLE argument must be a
vector. The KEY and VALUE arguments may be any Scheme object.
-- Scheme Procedure: make-hash-table size
Create a new hash table of SIZE slots. Note that the number of
slots does not limit the size of the table, it just tells how large
the underlying vector will be. The SIZE should be similar to the
expected number of elements which will be added to the table, but
they need not match. For good performance, it might be a good
idea to use a prime number as the SIZE.
-- Scheme Procedure: hashq-ref table key [dflt]
-- C Function: scm_hashq_ref (table, key, dflt)
Look up KEY in the hash table TABLE, and return the value (if any)
associated with it. If KEY is not found, return DEFAULT (or `#f'
if no DEFAULT argument is supplied). Uses `eq?' for equality
testing.
-- Scheme Procedure: hashv-ref table key [dflt]
-- C Function: scm_hashv_ref (table, key, dflt)
Look up KEY in the hash table TABLE, and return the value (if any)
associated with it. If KEY is not found, return DEFAULT (or `#f'
if no DEFAULT argument is supplied). Uses `eqv?' for equality
testing.
-- Scheme Procedure: hash-ref table key [dflt]
-- C Function: scm_hash_ref (table, key, dflt)
Look up KEY in the hash table TABLE, and return the value (if any)
associated with it. If KEY is not found, return DEFAULT (or `#f'
if no DEFAULT argument is supplied). Uses `equal?' for equality
testing.
-- Scheme Procedure: hashq-set! table key val
-- C Function: scm_hashq_set_x (table, key, val)
Find the entry in TABLE associated with KEY, and store VALUE
there. Uses `eq?' for equality testing.
-- Scheme Procedure: hashv-set! table key val
-- C Function: scm_hashv_set_x (table, key, val)
Find the entry in TABLE associated with KEY, and store VALUE
there. Uses `eqv?' for equality testing.
-- Scheme Procedure: hash-set! table key val
-- C Function: scm_hash_set_x (table, key, val)
Find the entry in TABLE associated with KEY, and store VALUE
there. Uses `equal?' for equality testing.
-- Scheme Procedure: hashq-remove! table key
-- C Function: scm_hashq_remove_x (table, key)
Remove KEY (and any value associated with it) from TABLE. Uses
`eq?' for equality tests.
-- Scheme Procedure: hashv-remove! table key
-- C Function: scm_hashv_remove_x (table, key)
Remove KEY (and any value associated with it) from TABLE. Uses
`eqv?' for equality tests.
-- Scheme Procedure: hash-remove! table key
-- C Function: scm_hash_remove_x (table, key)
Remove KEY (and any value associated with it) from TABLE. Uses
`equal?' for equality tests.
The standard hash table functions may be too limited for some
applications. For example, you may want a hash table to store strings
in a case-insensitive manner, so that references to keys named
"foobar", "FOOBAR" and "FooBaR" will all yield the same item. Guile
provides you with "extended" hash tables that permit you to specify a
hash function and associator function of your choosing. The functions
described in the rest of this section can be used to implement such
custom hash table structures.
If you are unfamiliar with the inner workings of hash tables, then
this facility will probably be a little too abstract for you to use
comfortably. If you are interested in learning more, see an
introductory textbook on data structures or algorithms for an
explanation of how hash tables are implemented.
-- Scheme Procedure: hashq key size
-- C Function: scm_hashq (key, size)
Determine a hash value for KEY that is suitable for lookups in a
hash table of size SIZE, where `eq?' is used as the equality
predicate. The function returns an integer in the range 0 to SIZE
- 1. Note that `hashq' may use internal addresses. Thus two
calls to hashq where the keys are `eq?' are not guaranteed to
deliver the same value if the key object gets garbage collected in
between. This can happen, for example with symbols: `(hashq 'foo
n) (gc) (hashq 'foo n)' may produce two different values, since
`foo' will be garbage collected.
-- Scheme Procedure: hashv key size
-- C Function: scm_hashv (key, size)
Determine a hash value for KEY that is suitable for lookups in a
hash table of size SIZE, where `eqv?' is used as the equality
predicate. The function returns an integer in the range 0 to SIZE
- 1. Note that `(hashv key)' may use internal addresses. Thus
two calls to hashv where the keys are `eqv?' are not guaranteed to
deliver the same value if the key object gets garbage collected in
between. This can happen, for example with symbols: `(hashv 'foo
n) (gc) (hashv 'foo n)' may produce two different values, since
`foo' will be garbage collected.
-- Scheme Procedure: hash key size
-- C Function: scm_hash (key, size)
Determine a hash value for KEY that is suitable for lookups in a
hash table of size SIZE, where `equal?' is used as the equality
predicate. The function returns an integer in the range 0 to SIZE
- 1.
-- Scheme Procedure: hashx-ref hash assoc table key [dflt]
-- C Function: scm_hashx_ref (hash, assoc, table, key, dflt)
This behaves the same way as the corresponding `ref' function, but
uses HASH as a hash function and ASSOC to compare keys. `hash'
must be a function that takes two arguments, a key to be hashed
and a table size. `assoc' must be an associator function, like
`assoc', `assq' or `assv'.
By way of illustration, `hashq-ref table key' is equivalent to
`hashx-ref hashq assq table key'.
-- Scheme Procedure: hashx-set! hash assoc table key val
-- C Function: scm_hashx_set_x (hash, assoc, table, key, val)
This behaves the same way as the corresponding `set!' function,
but uses HASH as a hash function and ASSOC to compare keys.
`hash' must be a function that takes two arguments, a key to be
hashed and a table size. `assoc' must be an associator function,
like `assoc', `assq' or `assv'.
By way of illustration, `hashq-set! table key' is equivalent to
`hashx-set! hashq assq table key'.
-- Scheme Procedure: hashq-get-handle table key
-- C Function: scm_hashq_get_handle (table, key)
This procedure returns the `(key . value)' pair from the hash
table TABLE. If TABLE does not hold an associated value for KEY,
`#f' is returned. Uses `eq?' for equality testing.
-- Scheme Procedure: hashv-get-handle table key
-- C Function: scm_hashv_get_handle (table, key)
This procedure returns the `(key . value)' pair from the hash
table TABLE. If TABLE does not hold an associated value for KEY,
`#f' is returned. Uses `eqv?' for equality testing.
-- Scheme Procedure: hash-get-handle table key
-- C Function: scm_hash_get_handle (table, key)
This procedure returns the `(key . value)' pair from the hash
table TABLE. If TABLE does not hold an associated value for KEY,
`#f' is returned. Uses `equal?' for equality testing.
-- Scheme Procedure: hashx-get-handle hash assoc table key
-- C Function: scm_hashx_get_handle (hash, assoc, table, key)
This behaves the same way as the corresponding `-get-handle'
function, but uses HASH as a hash function and ASSOC to compare
keys. `hash' must be a function that takes two arguments, a key
to be hashed and a table size. `assoc' must be an associator
function, like `assoc', `assq' or `assv'.
-- Scheme Procedure: hashq-create-handle! table key init
-- C Function: scm_hashq_create_handle_x (table, key, init)
This function looks up KEY in TABLE and returns its handle. If
KEY is not already present, a new handle is created which
associates KEY with INIT.
-- Scheme Procedure: hashv-create-handle! table key init
-- C Function: scm_hashv_create_handle_x (table, key, init)
This function looks up KEY in TABLE and returns its handle. If
KEY is not already present, a new handle is created which
associates KEY with INIT.
-- Scheme Procedure: hash-create-handle! table key init
-- C Function: scm_hash_create_handle_x (table, key, init)
This function looks up KEY in TABLE and returns its handle. If
KEY is not already present, a new handle is created which
associates KEY with INIT.
-- Scheme Procedure: hashx-create-handle! hash assoc table key init
-- C Function: scm_hashx_create_handle_x (hash, assoc, table, key,
init)
This behaves the same way as the corresponding `-create-handle'
function, but uses HASH as a hash function and ASSOC to compare
keys. `hash' must be a function that takes two arguments, a key
to be hashed and a table size. `assoc' must be an associator
function, like `assoc', `assq' or `assv'.
-- Scheme Procedure: hash-fold proc init table
-- C Function: scm_hash_fold (proc, init, table)
An iterator over hash-table elements. Accumulates and returns a
result by applying PROC successively. The arguments to PROC are
"(key value prior-result)" where key and value are successive
pairs from the hash table TABLE, and prior-result is either INIT
(for the first application of PROC) or the return value of the
previous application of PROC. For example, `(hash-fold acons '()
tab)' will convert a hash table into an a-list of key-value pairs.
Info Catalog
(guile.info.gz) Hash Table Examples
(guile.info.gz) Hash Tables
automatically generated byinfo2html