Previous: Hash Table Examples, Up: 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 satisfy hash-table?. The key and value arguments may be any Scheme object.

— Scheme Procedure: hash-table? obj

Return #t iff obj is a hash table.

— Scheme Procedure: make-hash-table [args...]

Create a new hash table, configured by keywords in args. The following keywords are recognized:

#:size N
Use n slots. Note that this does not limit the number of elements able to be hashed in the table. n should be similar to the expected number of elements which will be added to the table, but they need not match. For good performance, use a prime number for n. As a special case, if args begins with number, that is equivalent to #:size NUMBER. If #:size is not specified, the default is 61.
#:weakness WEAK
weak must be one of #f, #t, #:key, #:value, or #:key-and-value. If weak is not #f, the table returned is a weak table. Key/value pairs are removed from a weak hash table when there are no non-weak references pointing to their key, value, or both key and value, depending on weak. weak #t is equivalent to #:key-and-value. Default value of weak is #f.

— Scheme Procedure: hash-for-each proc table

Apply proc successively on all hash table items. The arguments to proc are (key value) where key and value are successive pairs from the hash table.

— Scheme Procedure: hashq-ref table key default
— C Function: scm_hashq_ref (table, key, default)

Look up key in the hash table table, and return the value (if any) associated with it. If key is not found, return otherwise (or #f if no otherwise argument is supplied). Use eq? for equality testing.

— Scheme Procedure: hashv-ref table key [otherwise]
— C Function: scm_hashv_rev (table, key, otherwise) |2 |1 |0

Look up key in the hash table table, and return the value (if any) associated with it. If key is not found, return otherwise (or #f if no otherwise argument is supplied). Use eqv? for equality testing.

— Scheme Procedure: hash-ref table key [otherwise]
— C Function: scm_hash_ref (table, key, otherwise) |2 |1 |0

Look up key in the hash table table, and return the value (if any) associated with it. If key is not found, return otherwise (or #f if no otherwise argument is supplied). Use equal? for equality testing.

— Scheme Procedure: hashq-set! table key value
— C Function: scm_hashq_set_x (table, key, value)

Find the entry in table associated with key, and store value there. Use eq? for equality testing.

— Scheme Procedure: hashv-set! table key value
— C Function: scm_hashv_set_x (table, key, value)

Find the entry in table associated with key, and store value there. Use eqv? for equality testing.

— Scheme Procedure: hash-set! table key value
— C Function: scm_hash_set_x (table, key, value)

Find the entry in table associated with key, and store value there. Use 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. Use 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. Use 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. Use 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 hashtable 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 hashtable 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 hashtable 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 hasher assoc table key [otherwise]
— C Function: scm_hashx_ref (hasher, assoc, table, key, otherwise) |4 |1 |0

Behave similar to the corresponding -ref functions, but use hasher as a hash function and assoc to compare keys. hasher 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! hasher assoc table key value
— C Function: scm_hashx_set_x (hasher, assoc, table, key, value)

Behave similar to the corresponding -set! functions, but use hasher as a hash function and assoc to compare keys. hasher 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)

Similar to hashq-ref, but return a handle from the hash table rather than the value associated with key. By convention, a handle in a hash table is the pair which associates a key with a value. Where (hashq-ref table key) returns only a value, (hashq-get-handle table key) returns the pair (key . value).

— Scheme Procedure: hashv-get-handle table key
— C Function: scm_hashv_get_handle (table, key)

Similar to hashv-ref, but return a handle from the hash table rather than the value associated with key. By convention, a handle in a hash table is the pair which associates a key with a value. Where (hashv-ref table key) returns only a value, (hashv-get-handle table key) returns the pair (key . value).

— Scheme Procedure: hash-get-handle table key
— C Function: scm_hashv_get_handle (table, key)

Similar to hash-ref, but return a handle from the hash table rather than the value associated with key. By convention, a handle in a hash table is the pair which associates a key with a value. Where (hash-ref table key) returns only a value, (hash-get-handle table key) returns the pair (key . value).

— Scheme Procedure: hashx-get-handle hasher assoc table key
— C Function: scm_hashx_get_handle (hasher, assoc, table, key)

Behave similar to the corresponding -get-handle functions, but use hasher as a hash function and assoc to compare keys. hasher 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)

Look up key in table and return its handle. If key is not already present, create a new handle which associates key with init.

— Scheme Procedure: hashv-create-handle! table key init
— C Function: scm_hashv_create_handle_x (table, key, init)

Look up key in table and return its handle. If key is not already present, create a new handle which associates key with init.

— Scheme Procedure: hash-create-handle! table key init
— C Function: scm_hash_create_handle_x (table, key, init)

Look up key in table and return its handle. If key is not already present, a new handle is created which associates key with init.

— Scheme Procedure: hashx-create-handle! hasher assoc table key init
— C Function: scm_hashx_create_handle_x (hasher, assoc, table, key, init)

Behave similar to the corresponding -create-handle functions, but use hasher as a hash function and assoc to compare keys. hasher 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. Accumulate and return 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 return an alist of key-value pairs.