5. Bigloo -- Standard Library<br>5.6 Hash Tables

5. Bigloo -- Standard Library
5.6 Hash Tables

Browsing

Home: Bigloo
A ``practical Scheme compiler''
User manual for version 2.6c
March 2004

Previous chapter: Core Language
Next chapter: Pattern Matching

Standard Library

5.1 Scheme Library
  5.1.1 Booleans
  5.1.2 Equivalence predicates
  5.1.3 Pairs and lists
  5.1.4 Symbols
  5.1.5 Keywords
  5.1.6 Numbers
  5.1.7 Characters
  5.1.8 UCS-2 Characters
  5.1.9 Strings
  5.1.10 Unicode (UCS-2) Strings
  5.1.11 Vectors
  5.1.12 Control features
5.2 Input and output
5.3 Structures and Records
  5.3.1 Structures
  5.3.2 Records (SRFI-9)
5.4 Serialization
5.5 Bit manipulation
5.6 Hash Tables
  5.6.1 Hash tables
  5.6.2 Deprecated Hash tables
5.7 System programming
  5.7.1 Operating System interface
  5.7.2 Files
5.8 Process support
5.9 Socket support
5.10 Date
5.11 Posix Regular Expressions
  5.11.1 Regular Expressions Procedures
  5.11.2 Regular Expressions Pattern Language
  5.11.3 An Extended Example

Chapters

  Acknowledgements
1. Table of contents
2. Overview of Bigloo
3. Modules
4. Core Language
5. Standard Library
6. Pattern Matching
7. Object System
8. Threads
9. Regular parsing
10. Lalr(1) parsing
11. Errors and Assertions
12. Eval and code interpretation
13. Macro expansion
14. Command Line Parsing
15. Explicit typing
16. The C interface
17. The Java interface
18. Bigloo Libraries
19. Extending the Runtime System
20. SRFIs
21. DSSSL support
22. Compiler description
23. User Extensions
24. Bigloo Development Environment
25. Global Index
26. Library Index
  Bibliography

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? objbigloo procedure
Returns #t if obj is an hash table, constructed by make-hashtable.

hashtable-size tablebigloo procedure
Returns the number of entries contained in table.

hashtable-get table keybigloo procedure
Returns the entry whose key is key in table. If not entry is found #f is returned.

hashtable-put! table key objbigloo 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 keybigloo 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-valuebigloo 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 tablebigloo procedure
hashtable->list tablebigloo procedure
Convert a hash table table to a vector or to a list.

hashtable-for-each table funbigloo 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 objectbigloo 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? objbigloo procedure
Returns #t if obj is an hash table.

hash-table-nb-entry tablebigloo procedure
Returns the number of entries contained in table.

get-hash key tablebigloo procedure
Returns the entry whose key is key in table. If not entry is found #f is returned.

put-hash! obj tablebigloo 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 tablebigloo procedure
Removes obj from table, returning #t if such object was bound in table and #f otherwise.

rem-key-hash! key tablebigloo 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 tablebigloo 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 stringbigloo procedure
string->0..2^x-1 string powerbigloo procedure
The formal power has to be a power of 2 between 1 and 16.

int->0..255 intbigloo procedure
int->0..2^x-1 int powerbigloo procedure
obj->0..255 objbigloo procedure
obj->0..2^x-1 obj powerbigloo 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)

This Scribe page has been generated by scribeinfo.
Last update Tue Mar 16 17:05:46 2004