Next: , Previous: , Up: Scheme reference   [Contents][Index]


4.9 Hashtables

There are many mutable hashtable APIs amongst all the various Scheme implementations, standards, and SRFIs. From our point of view, there is no clear “best” hashtable API that has emerged, but we think the R6RS interface is OK. Guile’s own hashtable API has design issues that are best left in the past. So, the (hoot hashtables) module is R6RS-like but with some notable differences.

Procedure: make-hashtable [hash hash] [equiv equal?]

Return a new, empty hashtable that uses the hash procedure hash and equivalence procedure equiv.

Procedure: make-eq-hashtable

Return a new, empty hashtable that uses eq? as the equivalence function and hashes keys accordingly.

Procedure: make-eqv-hashtable

Return a new, empty hashtable that uses eqv? as the equivalence function and hashes keys accordingly.

Procedure: hashtable? obj

Return #t if obj

Procedure: hashtable-hash table

Return the hash function for table.

Procedure: hashtable-equiv table

Return the equivalence function for table.

Procedure: hashtable-size table

Return the current number of key/value pairs in table.

Procedure: hashtable-ref table key [default #f]

Return the value associated with key in table, or default if there is no such association.

Procedure: hashtable-set! table key value

Associate val with key in table, potentially overwriting any previous association with key.

Procedure: hashtable-delete! table key

Remove the association with key in table, if one exists.

Procedure: hashtable-clear! table

Remove all of the key/value associations in table.

Procedure: hashtable-contains? table key

Return #t if key has an associated value in table.

Procedure: hashtable-copy table

Return a copy of table.

Procedure: hashtable-keys table

Return a list of keys in table.

Procedure: hashtable-values table

Return a list of values in table.

Procedure: hashtable-for-each proc table

Apply proc to each key/value association in table. Each call is of the form (proc key value).

Procedure: hashtable-fold proc init table

Accumulate a result by applying proc with each key/value association in table and the result of the previous proc call. Each call is of the form (proc key value prev). For the first call, prev is the initial value init.

Hoot also includes weak key hash tables that wrap those of the Wasm host platform, such as the WeakMap JavaScript class on the web.

Procedure: make-weak-key-hashtable

Return a new weak key hashtable.

Procedure: weak-key-hashtable? obj

Return #t if obj is a weak key hashtable.

Procedure: weak-key-hashtable-ref hashtable key [default #f]

Return the value associated with key in hashtable or default if there is no such association.

Procedure: weak-key-hashtable-set! hashtable key value

Modify hashtable to associate key with value, overwriting any previous association that may have existed.

Procedure: weak-key-hashtable-delete! hashtable key

Remove the association with key in hashtable, if one exists.

The following hash functions are available:

Procedure: hash key size
Procedure: hashq key size
Procedure: hashv key size

Return a hash value for key suitable for using in a table containing size buckets. The returned hash value will be in the range [0, size).

hashq is the hash function used by make-eq-hashtable, and hashv is used by make-eqv-hashtable.


Next: Records, Previous: Parameters, Up: Scheme reference   [Contents][Index]