Previous: Hash Table Examples, Up: Hash Tables
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.
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#:sizeis 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.
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.
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
#fif no otherwise argument is supplied). Useeq?for equality testing.
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
#fif no otherwise argument is supplied). Useeqv?for equality testing.
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
#fif no otherwise argument is supplied). Useequal?for equality testing.
Find the entry in table associated with key, and store value there. Use
eq?for equality testing.
Find the entry in table associated with key, and store value there. Use
eqv?for equality testing.
Find the entry in table associated with key, and store value there. Use
equal?for equality testing.
Remove key (and any value associated with it) from table. Use
eq?for equality tests.
Remove key (and any value associated with it) from table. Use
eqv?for equality tests.
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.
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 thathashqmay use internal addresses. Thus two calls tohashqwhere the keys areeq?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, sincefoowill be garbage collected.
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 tohashvwhere the keys areeqv?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, sincefoowill be garbage collected.
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.
Behave similar to the corresponding
-reffunctions, but use hasher as a hash function and assoc to compare keys.hashermust be a function that takes two arguments, a key to be hashed and a table size.assocmust be an associator function, likeassoc,assqorassv.By way of illustration,
(hashq-ref table key)is equivalent to(hashx-ref hashq assq table key).
Behave similar to the corresponding
-set!functions, but use hasher as a hash function and assoc to compare keys.hashermust be a function that takes two arguments, a key to be hashed and a table size.assocmust be an associator function, likeassoc,assqorassv.By way of illustration,
(hashq-set! table key)is equivalent to(hashx-set! hashq assq 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 avalue,(hashq-get-handle table key)returns the pair(key . value).
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 avalue,(hashv-get-handle table key)returns the pair(key . value).
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 avalue,(hash-get-handle table key)returns the pair(key . value).
Behave similar to the corresponding
-get-handlefunctions, but use hasher as a hash function and assoc to compare keys.hashermust be a function that takes two arguments, a key to be hashed and a table size.assocmust be an associator function, likeassoc,assqorassv.
Look up key in table and return its handle. If key is not already present, create a new handle which associates key with 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.
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.
Behave similar to the corresponding
-create-handlefunctions, but use hasher as a hash function and assoc to compare keys.hashermust be a function that takes two arguments, a key to be hashed and a table size.assocmust be an associator function, likeassoc,assqorassv.
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.