[mercury-users] Hash_table initialization

Ralph Becket rafe at cs.mu.OZ.AU
Wed Apr 30 11:55:00 AEST 2003


Fergus Henderson, Wednesday, 30 April 2003:
> 
> If the keys for your hash table are one of the builtin types
> such as string, int, etc. then you can use one of the
> predefined hashing predicates, e.g. string_double_hash.
> Otherwise you will need to write your own.

The hash_table module provides generic_double_hash/3 which 
works for all types.  If you have a quality hash function
in mind for your keys then you may obtain better results
by writing your own.

> 	main(!IO) :-
> 		% create an empty hash table
> 		HashTable0 = new_default(string_double_hash),
> 
> 		% insert a key and value into the hash table
> 		HashTable1 = det_insert(HashTable0, "some key", 42),

Unless you really want an exception if your program attempts to
overwrite a key, I'd suggest 

		HashTable1 = ( HashTable0 ^ elem("some key") := 42),

as a perhaps more perspicuous way of saying the same thing.

> 		% lookup a key in the hash table
> 		N = HashTable1^elem("some key"),
> 
> 		% print the result
> 		print("N = ", !IO),
> 		print(N, !IO),
> 		nl(!IO).

Cheers,
- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list