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
hash
parameter 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 functionhash
is acceptable forequal?
, so if you use coarser equivalence thanequal?
other thanstring-ci=?
, you must always provide the function hash yourself.) (An equivalence predicatec1
is coarser than a equivalence predicatec2
iff there exist valuesx
andy
such that(and (
.)c1
x
y
) (not (c2
x
y
)))The
size-hint
parameter 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
alist
and creates a hash tablehash-table
which maps thecar
of every element inalist
to thecdr
of corresponding elements inalist
. Theequal?
,hash
, andsize-hint
parameters 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: usingcadr
would render this procedure unusable forcdr
alists, but not vice versa.)
Function: hash-table-ref
hash-table
key
[ thunk
] →
value
This procedure returns the value associated to
key
inhash-table
. If no value is associated tokey
andthunk
is given, it is called with no arguments and its value is returned; ifthunk
is 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-table
key
(lambda ()default
))
Function: hash-table-set!
hash-table
key
value
→
void
This procedure sets the value associated to
key
inhash-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
key
inhash-table
. It is not an error if no association for thekey
exists; 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
key
inhash-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
proc
should be a function taking two arguments, a key and a value. This procedure callsproc
for each association inhash-table
, giving the key of the association as key and the value of the association as value. The results ofproc
are discarded. The order in whichproc
is called for the different associations is unspecified.
Function: hash-table-fold
hash-table
f
init-value
→
final-value
This procedure calls
f
for every association inhash-table
with three arguments: the key of the association key, the value of the association value, and an accumulated value,val
. Theval
isinit-value
for the first invocation off
, and for subsequent invocations off
, the return value of the previous invocation off
. The valuefinal-value
returned byhash-table-fold
is the return value of the last invocation off
. The order in whichf
is called for different associations is unspecified.
Function: hash-table->alist
hash-table
→
alist
Returns an association list such that the
car
of each element inalist
is a key inhash-table
and the correspondingcdr
of each element inalist
is 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
bound
is 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 standardhashCode
method, and returning the result as is. (If theobject
is the Javanull
value, 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 thehash
function.)