Gauche’s native hashtable API is Gauche-specific. For portable
programs, you can use
srfi-69 (see Basic hash tables)
for the time being.
There will be a revised hashtable API as a part of R7RS-large, we plan to adapt Gauche’s native API once it is published.
Hash table class. Inherits
Creates a hash table. The optional comparator argument
specifies key equality and hash function. It can be either a
comparator (see Basic comparators), or one of the symbols
If it is one of those symbols,
string-comparator will be used, respectively.
The comparator must have hash function, of course. See Hashing, for the built-in hash functions. In general, comparators derived from other comparators having hash functions also have appropriate hash functions.
If comparator is omitted,
eq-comparator is assumed.
#t if obj is a hash table.
Returns a comparator used in the hashtable ht.
This is an old API, superseded by
Returns one of symbols
general, indicating the type of the hash table ht.
Returns the number of entries in the hash table ht.
Constructs and returns a hash table from given
list of arguments.
The comparator argument is the same as of
Each key&value must be a pair, and its car is used as a key
and its cdr is used as a value.
(hash-table 'eq? '(a . 1) '(b . 2)) ≡ (rlet1 h (make-hash-table 'eq?) (hash-table-put! h 'a 1) (hash-table-put! h 'b 2))
Returns a new copy of a hash table ht.
Search key from a hash table ht, and returns its value if found. If the key is not found in the table and default is given, it is returned. Otherwise an error is signaled.
Puts a key key with a value value to the hash table ht.
Method versions of
#t if a hash table ht has a key key.
Deletes an entry that has a key key from the hash table ht.
#t if the entry has exist, or
#f if the entry
hasn’t exist. The same function is called
hash-table-remove! in STk
(except that it returns an undefined value); I use ‘delete’ for consistency
to SRFI-1, SRFI-13 and other parts of the libraries.
Removes all entries in the hash table ht.
Conses value to the existing value for the key key in the
hash table ht and makes it the new value for key.
If there’s no entry for key, an entry
is created with the value
Works the same as the following code, except that this function only looks up the key once, thus it’s more efficient.
(hash-table-put! ht key (cons value (hash-table-get ht key '())))
Looks for the value for the key key in the hash table ht. If found and it is a pair, replaces the value for its cdr and returns car of the original value. If no entry for key is in the table, or the value is not a pair, the table is not modified and the procedure returns default if given, or signals an error otherwise.
During the operation the key is looked for only once, thus runs efficiently.
A more general version of
It works basically as the following code piece,
except that the lookup of key is only done once.
(let ((tmp (proc (hash-table-get ht key default)))) (hash-table-put! ht key tmp) tmp)
For example, when you use a hash table to count the occurrences of items, the following line is suffice to increment the counter of the item, regardless of whether item has already appeared or not.
(hash-table-update! ht item (cut + 1 <>) 0))
A procedure proc is called with two arguments, a key and its associated value, over all the entries in the hash table ht.
For all entries in the hash table ht,
a procedure kons is called with three arguments;
a key, its associated value, and the previous return value of kons.
The first call of kons receives knil as the third argument.
The return value of the last call of kons is returned
Returns all the keys or values of hash table ht in a list, respectively.
Creates and returns a hash table that has entries of
each element in alist, using its car as the key and
its cdr as the value. The comparator argument
is the same as in
make-hash-table. The default value
of comparator is
(hash-table-map h cons)