module Hashtree:This module implements a normal hashtbl, with one tweak. Instead of using linked lists for buckets it uses (mutable) AVL trees. This property means that the worst case time complexity of find, add, and remove is O(log(N)), instead of O(N) as with the list version. As with the standard Hashtbl, the average case time complexity is O(1) with a good hash function, and the constants appear to be comparable.sig
..end
You must pay for this guarentee by specifying a comparison
function instead of an equality function (the polymorphic version
uses Pervasives.compare). This means that this version of hashtbl
does not work with certian classes of keys (E.G. keys where
physical equality is the only method of comparison).
You must pay for this guarentee by specifying a comparison
function instead of an equality function (the polymorphic version
uses Pervasives.compare). This means that this version of hashtbl
does not work with certian classes of keys (E.G. keys where
physical equality is the only method of comparison).
type ('a, 'b)
t
include Sexpable.S2
include Binable.S2
val create : int -> ('a, 'b) t
create i
create a new table with initial size i
.
Note that this implementation DOES implement table growth when the
buckets get too crowded, however, the cutoff is much higher than
in hashtbl. When there are 10 times more elements than buckets it
will grow by a factor of 10. Using this larger factor seems to
improve performance. Probably because searching through buckets is
much faster than in a standard table (3 comparisons instead of
10).
val add : ('a, 'b) t -> key:'a -> data:'b -> unit
add t ~key ~data
add a new binding of key
to data
to the table.
Note that the semantics of add
are the same as those of
Hashtbl.replace
. This implementation does NOT support multiple
bindings.
worst case: O(log(N)), average case: amortized O(1)
val remove : ('a, 'b) t -> 'a -> unit
remove t key
remove the binding of key
from the table, if no
binding exists do nothing.
worst case: O(log(N)), average case: amortized O(1)
val find : ('a, 'b) t -> 'a -> 'b option
find t key
look up the binding of key
, if no binding exists
return None
worst case: O(log(N)), average case: amortized O(1)
val length : ('a, 'b) t -> int
length t
return the number of bindings in the table.
O(1)
val fold : ('a, 'b) t -> init:'c -> f:(key:'a -> data:'b -> 'c -> 'c) -> 'c
fold t ~init:z ~f
fold through the table. NO guarentee is made
about the order in which you will see elements.
O(N)
module type Key =sig
..end
module type S =sig
..end
module Make: