A hash table is a data structure that:
provides a mapping from some set of keys to some set of values associated to those keys;
has no intrinsic order for the (key, value) associations it contains;
supports in-place modification as the primary means of setting the contents of a hash table; and
provides key lookup and destructive update in amortised constant time, provided that a good hash function is used.
The Kawa hash table functions follow the
SRFI-69 specifiation.
The Kawa implementation has been optimized for performance and better
Java integration.  Specifically, the default hash function uses
the standard Java hashCode method.
To use the hash table functions in your Kawa program you must first:
(require 'srfi-69)
or
(require 'hash-table)
Function: make-hash-table [ equal? [ hash [ size-hint]]] → hash-table
Create a new hash table with no associations. The
equal?parameter is a predicate that should accept two keys and return a boolean telling whether they denote the same key value; it defaults to theequal?function.The
hashparameter is a hash function, and defaults to an appropriate hash function for the givenequal?predicate (see the Hashing section). However, an acceptable default is not guaranteed to be given for any equivalence predicate coarser thanequal?, except forstring-ci=?. (The functionhashis acceptable forequal?, so if you use coarser equivalence thanequal?other thanstring-ci=?, you must always provide the function hash yourself.) (An equivalence predicatec1is coarser than a equivalence predicatec2iff there exist valuesxandysuch that(and (.)c1xy) (not (c2xy)))The
size-hintparameter can be used to suggested an approriate initial size. This option is not part of the SRFI-69 specification (though it is handled by the reference implementation), so specifying that option might be unportable.
Function: alist->hash-table alist [ equal? [ hash [ size-hint]]] → hash-table
Takes an association list
alistand creates a hash tablehash-tablewhich maps thecarof every element inalistto thecdrof corresponding elements inalist. Theequal?,hash, andsize-hintparameters are interpreted as inmake-hash-table. If some key occurs multiple times inalist, the value in the first association will take precedence over later ones. (Note: the choice of usingcdr(instead ofcadr) for values tries to strike balance between the two approaches: usingcadrwould render this procedure unusable forcdralists, but not vice versa.)
Function: hash-table-ref hash-table key [ thunk ] → value
This procedure returns the value associated to
keyinhash-table. If no value is associated tokeyandthunkis given, it is called with no arguments and its value is returned; ifthunkis not given, an error is signalled. Given a good hash function, this operation should have an (amortised) complexity of O(1) with respect to the number of associations inhash-table.
Function: hash-table-ref/default hash-table key default → value
Evaluates to the same value as
(hash-table-ref. Given a good hash function, this operation should have an (amortised) complexity of O(1) with respect to the number of associations in hash-table.hash-tablekey(lambda ()default))
Function: hash-table-set! hash-table key value → void
This procedure sets the value associated to
keyinhash-table. The previous association (if any) is removed. Given a good hash function, this operation should have an (amortised) complexity of O(1) with respect to the number of associations in hash-table.
Function: hash-table-delete! hash-table key → void
This procedure removes any association to
keyinhash-table. It is not an error if no association for thekeyexists; in this case, nothing is done. Given a good hash function, this operation should have an (amortised) complexity of O(1) with respect to the number of associations in hash-table.
Function: hash-table-exists? hash-table key → boolean
This predicate tells whether there is any association to
keyinhash-table. Given a good hash function, this operation should have an (amortised) complexity of O(1) with respect to the number of associations in hash-table.
Function: hash-table-size hash-table → integer
Returns the number of associations in
hash-table. This operation takes constant time.
Function: hash-table-keys hash-table → list
Returns a list of keys in
hash-table. The order of the keys is unspecified.
Function: hash-table-values hash-table → list
Returns a list of values in
hash-table. The order of the values is unspecified, and is not guaranteed to match the order of keys in the result ofhash-table-keys.
Function: hash-table-walk hash-table proc → void
procshould be a function taking two arguments, a key and a value. This procedure callsprocfor each association inhash-table, giving the key of the association as key and the value of the association as value. The results ofprocare discarded. The order in whichprocis called for the different associations is unspecified.
Function: hash-table-fold hash-table f init-value → final-value
This procedure calls
ffor every association inhash-tablewith three arguments: the key of the association key, the value of the association value, and an accumulated value,val. Thevalisinit-valuefor the first invocation off, and for subsequent invocations off, the return value of the previous invocation off. The valuefinal-valuereturned byhash-table-foldis the return value of the last invocation off. The order in whichfis called for different associations is unspecified.
Function: hash-table->alist hash-table → alist
Returns an association list such that the
carof each element inalistis a key inhash-tableand the correspondingcdrof each element inalistis the value associated to the key inhash-table. The order of the elements is unspecified.The following should always produce a hash table with the same mappings as a hash table
h:(alist->hash-table (hash-table->alisth) (hash-table-equivalence-functionh) (hash-table-hash-functionh))
Hashing means the act of taking some value and producing a number from
the value. A hash function is a function that does this. Every
equivalence predicate e has a set of acceptable hash functions for
that predicate; a hash funtion hash is acceptable iff
(e  implies
obj1 obj2)(= (hash .obj1) (hash obj2))
A hash function h is good for a equivalence predicate e if
it distributes the result numbers (hash values) for non-equal objects
(by e) as uniformly as possible over the numeric range of hash
values, especially in the case when some (non-equal) objects resemble
each other by e.g. having common subsequences. This definition is
vague but should be enough to assert that e.g. a constant function is
not a good hash function.
When the definition of make-hash-table above talks about an
appropriate hashing function for e, it means a hashing function
that gives decent performance (for the hashing operation) while being
both acceptable and good for e. This definition, too, is
intentionally vague.
The Kawa implementation always calls the hash functions with a single
parameter, and expects the result to be within the entire
(32-bit signed) <int> range, for compatibility with
standard hashCode methods.
Function: hash object [ bound ] → integer
Produces a hash value for object in the range from 0 (inclusive) tp to
bound(exclusive).If
boundis not given, the Kawa implementation returns a value within the range(- (expt 2 32))(inclusive) to(- (expt 2 32) 1)(inclusive). It does this by calling the standardhashCodemethod, and returning the result as is. (If theobjectis the Javanullvalue, 0 is returned.) This hash function is acceptable forequal?.
Function: string-hash string [ bound ] → integer
The same as
hash, except that the argument string must be a string. (The Kawa implementation returns the same as thehashfunction.)