|
5.6.1 Hash tables
Bigloo offers hash tables. Here are described functions which define
and use them.
make-hashtable [bucket-len] [max-bucket-len] | bigloo procedure |
Defines an hash table for which the number of buckets is bucket-len .
The variable max-bucket-len specify when the table should be
resized. If provided, these two values have to be exact integers greater or
equal to 1. Normally you could ignore bucket-len and max-bucket-len
arguments and call make-hashtable with no argument at all.
|
hashtable? obj | bigloo procedure |
Returns #t if obj is an hash table, constructed by
make-hashtable .
|
hashtable-size table | bigloo procedure |
Returns the number of entries contained in table .
|
hashtable-get table key | bigloo procedure |
Returns the entry whose key is key in table . If not entry
is found #f is returned.
|
hashtable-put! table key obj | bigloo procedure |
Puts obj in table under the key key . This function
returns the object bound in the table. If there was an object
obj-old already in the table with the same key as obj ,
this function returns obj-old ; otherwise it returns obj .
|
hashtable-remove! table key | bigloo procedure |
Removes the object associated to key from table ,
returning #t if such object
was bound in table and #f otherwise.
|
hashtable-update! table key update-fun init-value | bigloo procedure |
If key is already in table, the new value is calculated by
(update-fun current-value) . Otherwise the table is extended
by an entry linking key and init-value .
|
hashtable->vector table | bigloo procedure |
hashtable->list table | bigloo procedure |
Convert a hash table table to a vector or to a list.
|
hashtable-for-each table fun | bigloo procedure |
Applies fun to each of the keys and elements of table
(no order is specified). In consequence, fun must be a procedure
of two arguments. The first one is a key and the second one, an
associated object.
|
Here is an example of hash table.
(define *table* (make-hashtable))
(hashtable-put! *table* "toto" "tutu")
(hashtable-put! *table* "tata" "titi")
(hashtable-put! *table* "titi" 5)
(hashtable-put! *table* "tutu" 'tutu)
(hashtable-put! *table* 'foo 'foo)
(print (hashtable-get *table* "toto"))
-| "tutu"
(print (hashtable-get *table* 'foo))
-| 'foo
(print (hashtable-get *table* 'bar))
-| #f
(hashtable-for-each *table* (lambda (key obj) (print (cons key obj))))
-| ("toto" . "tutu")
("tata" . "titi")
("titi" . 5)
("tutu" . TUTU)
(foo . foo)
|
object-hashnumber object | bigloo generic |
This generic function computes a hash number of the instance object .
Example:
(define-method (object-hashnumber pt::point)
(with-access::point pt (x y)
(+fx (*fx x 10) y)))
|
|
5.6.2 Deprecated Hash tables
Bigloo offers hash tables. Here are described functions which define
and use them.
make-hash-table ms nb gk eq [is] | bigloo procedure |
Defines an hash table of maximum length ms . If
is is provided, it sets the initial table size. The
formal nb is a function which, if applied to a
key , has to return an integer bound in interval
[0 ..ms ]. The formal gk
is a function which, if applied to an object, returns its
key. For example, the identity is a good candidate for integers,
strings, symbols, etc. The last formal eq is a
equivalence predicate which is used when searching for an entry in
the table.
|
hash-table? obj | bigloo procedure |
Returns #t if obj is an hash table.
|
hash-table-nb-entry table | bigloo procedure |
Returns the number of entries contained in table .
|
get-hash key table | bigloo procedure |
Returns the entry whose key is key in table . If not entry
is found #f is returned.
|
put-hash! obj table | bigloo procedure |
Puts obj in table . This function returns the object bound
in the table. If there was an object obj-old already in the
table with the same key as obj , this function returns obj-old ; o
therwise it returns obj .
|
rem-obj-hash! obj table | bigloo procedure |
Removes obj from table , returning #t if such object
was bound in table and #f otherwise.
|
rem-key-hash! key table | bigloo procedure |
Removes an object associated with key in
table , returning #t if such an object was bound
in table and #f otherwise.
|
for-each-hash fun table | bigloo procedure |
Applies fun to each of the elements of table (no order
is specified).
|
Some functions compute hash numbers from Scheme objects:
string->0..255 string | bigloo procedure |
string->0..2^x-1 string power | bigloo procedure |
The formal power has to be a power of 2 between 1 and 16.
|
int->0..255 int | bigloo procedure |
int->0..2^x-1 int power | bigloo procedure |
obj->0..255 obj | bigloo procedure |
obj->0..2^x-1 obj power | bigloo procedure |
|
Here is an example of hash table that contains pairs:
(define *table*
(make-hash-table 1024
(lambda (o) (string->0..2^x-1 o 10))
(lambda (x) (car x))
string=?
64))
(let ((cell1 (cons "toto" "tutu"))
(cell2 (cons "tata" "titi"))
(cell3 (cons "titi" 5))
(cell4 (cons "tutu" 'tutu)))
(put-hash! cell1 *table*)
(put-hash! cell2 *table*)
(put-hash! cell3 *table*)
(put-hash! cell4 *table*))
(print (cdr (get-hash "toto" *table*)))
-| "tutu"
(for-each-hash print *table*)
-| ("toto" . "tutu")
("tata" . "titi")
("titi" . 5)
("tutu" . TUTU)
|
|