[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.13 Hashtables

Builtin Class: <hash-table>

Hash table class. Inherits <collection> and <dictionary>.

Function: make-hash-table &optional type

Creates a hash table. A symbol type specifies the type of the table. The following types are currently supported. (If type is omitted, eq? is assumed.)

eq?

Keys are compared by eq?.

eqv?

Keys are compared by eqv?.

equal?

Keys are compared by equal?.

string=?

Keys are compared by string=?. Key must be a string.

Hash functions used for eq?, eqv? and string=?-type hash tables are built in the system; they can be called from Scheme as eq-hash, eqv-hash, and string-hash (SRFI-13). Those functions can't be extended for user-defined objects. On the other hand, equal?-type hash tables uses hash function below, with which you define hash functions for user-defined objects.

Function: hash obj

Returns a hash value of obj. This is the hash function used in equal?-type hash table. The hash value is an exact non-negative integer, and it has two properties:

If obj is either a number, a boolean, a character, a symbol, a keyword, a string, a list or a vector, internal hash function is used to calculate the hash value. If obj is other than that, hash calls a generic function object-hash to calculate the hash value.

Generic Function: object-hash obj

By defining a method for this generic function, objects of user-defined types can have a hash value and can be used in equal? hash table.

The method has to return an exact non-negative integer, and has to follow the same constraints as of hash.

If the method needs to get hash value of obj's elements, it has to call hash on them, not object-hash. For the hashing of primitive objects are done in hash.

 
(define-class <myclass> () (x y))

;; user-defined equality function
(define-method object-equal? ((a <myclass>) (b <myclass>))
  (and (equal? (ref a 'x) (ref b 'x))
       (eq?    (ref a 'y) (ref b 'y))))

;; user-defined hash function
(define-method object-hash ((a <myclass>))
  (hash (list (ref a 'x) (ref a 'y))))
Function: eq-hash obj
Function: eqv-hash obj

These are hash functions used for eq?-type and eqv?-type hash tables. Returns a non-negative integer up to 2^n-1, where n is system-dependent value no less than 32. The returned hash value is system- and process-dependent, and can't be carried over the boundary of the running process.

Note: don't hash numbers by eq-hash. Two numbers are not guaranteed to be eq? even if they are numerically equal, so they are not supposed to be used as a key in eq?-type hash table.

Function: hash-table? obj

Returns #t if obj is a hash table.

Function: hash-table-type ht

Returns one of symbols eq?, eqv?, equal? or string=?, indicating the type of the hash table ht.

Function: hash-table-num-entries ht

Returns the number of entries in the hash table ht.

Function: hash-table type key&value …

Constructs and returns a hash table of type type from given list of arguments. Type is the same as of make-hash-table. 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))
  ≡
  (let ((h (make-hash-table 'eq?)))
     (hash-table-put! h 'a 1)
     (hash-table-put! h 'b 2)
     h)
Function: hash-table-get ht key &optional default

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 signalled.

Function: hash-table-put! ht key value

Puts a key key with a value value to the hash table ht.

Method: ref (ht <hash-table>) key &optional default
Method: (setter ref) (ht <hash-table>) key value

Method versions of hash-table-get and hash-table-put!.

Function: hash-table-exists? ht key

Returns #t if a hash table ht has a key key.

Function: hash-table-delete! ht key

Deletes an entry that has a key key from the hash table ht. Returns #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.

Function: hash-table-clear! ht

Removes all entries in the hash table ht.

Function: hash-table-push! ht key value

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 (list 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 '())))
Function: hash-table-pop! ht key &optional default

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.

Function: hash-table-update! ht key proc &optional default

A more general version of hash-table-push! etc. 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))
Function: hash-table-for-each ht proc
Function: hash-table-map ht proc

A procedure proc is called with two arguments, a key and its associated value, over all the entries in the hash table ht.

Function: hash-table-fold ht kons knil

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 from hash-table-fold.

Function: hash-table-keys ht
Function: hash-table-values ht

Returns all the keys or values of hash table ht in a list, respectively.

See also util.list - Additional list library, where hash-table->alist and alist->hash-table are defined.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on October, 7 2008 using texi2html 1.78.