1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Hash tables *)
|
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
external hash_param : int -> int -> 'a -> int = "hash_univ_param" "noalloc"
|
|
|
|
|
|
|
|
let hash x = hash_param 10 100 x
|
|
|
|
|
|
|
|
(* We do dynamic hashing, and resize the table and rehash the elements
|
|
|
|
when buckets become too long. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type ('a, 'b) t =
|
1996-10-31 08:03:04 -08:00
|
|
|
{ mutable max_len: int; (* max length of a bucket *)
|
1995-05-04 03:15:53 -07:00
|
|
|
mutable data: ('a, 'b) bucketlist array } (* the buckets *)
|
|
|
|
|
|
|
|
and ('a, 'b) bucketlist =
|
|
|
|
Empty
|
|
|
|
| Cons of 'a * 'b * ('a, 'b) bucketlist
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let create initial_size =
|
1999-10-02 05:09:43 -07:00
|
|
|
let s = if initial_size < 1 then 1 else initial_size in
|
|
|
|
let s = if s > Sys.max_array_length then Sys.max_array_length else s in
|
|
|
|
{ max_len = 3; data = Array.make s Empty }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let clear h =
|
|
|
|
for i = 0 to Array.length h.data - 1 do
|
|
|
|
h.data.(i) <- Empty
|
|
|
|
done
|
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
let resize hashfun tbl =
|
|
|
|
let odata = tbl.data in
|
|
|
|
let osize = Array.length odata in
|
2000-02-29 05:06:40 -08:00
|
|
|
let nsize = min (2 * osize + 1) Sys.max_array_length in
|
|
|
|
if nsize <> osize then begin
|
|
|
|
let ndata = Array.create nsize Empty in
|
|
|
|
let rec insert_bucket = function
|
|
|
|
Empty -> ()
|
|
|
|
| Cons(key, data, rest) ->
|
|
|
|
insert_bucket rest; (* preserve original order of elements *)
|
|
|
|
let nidx = (hashfun key) mod nsize in
|
|
|
|
ndata.(nidx) <- Cons(key, data, ndata.(nidx)) in
|
|
|
|
for i = 0 to osize - 1 do
|
|
|
|
insert_bucket odata.(i)
|
|
|
|
done;
|
|
|
|
tbl.data <- ndata;
|
|
|
|
end;
|
1996-10-31 08:03:04 -08:00
|
|
|
tbl.max_len <- 2 * tbl.max_len
|
2000-02-29 05:06:40 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec bucket_too_long n bucket =
|
|
|
|
if n < 0 then true else
|
|
|
|
match bucket with
|
|
|
|
Empty -> false
|
1996-10-31 08:03:04 -08:00
|
|
|
| Cons(_,_,rest) -> bucket_too_long (n - 1) rest
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let add h key info =
|
1999-11-29 11:04:12 -08:00
|
|
|
let i = (hash key) mod (Array.length h.data) in
|
1995-05-04 03:15:53 -07:00
|
|
|
let bucket = Cons(key, info, h.data.(i)) in
|
1996-10-31 08:03:04 -08:00
|
|
|
h.data.(i) <- bucket;
|
|
|
|
if bucket_too_long h.max_len bucket then resize hash h
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let remove h key =
|
|
|
|
let rec remove_bucket = function
|
|
|
|
Empty ->
|
|
|
|
Empty
|
|
|
|
| Cons(k, i, next) ->
|
|
|
|
if k = key then next else Cons(k, i, remove_bucket next) in
|
1999-11-29 11:04:12 -08:00
|
|
|
let i = (hash key) mod (Array.length h.data) in
|
1996-10-31 08:03:04 -08:00
|
|
|
h.data.(i) <- remove_bucket h.data.(i)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-07-28 05:24:25 -07:00
|
|
|
let rec find_rec key = function
|
|
|
|
Empty ->
|
|
|
|
raise Not_found
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
if key = k then d else find_rec key rest
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let find h key =
|
1999-11-29 11:04:12 -08:00
|
|
|
match h.data.((hash key) mod (Array.length h.data)) with
|
1995-05-04 03:15:53 -07:00
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k1, d1, rest1) ->
|
|
|
|
if key = k1 then d1 else
|
|
|
|
match rest1 with
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k2, d2, rest2) ->
|
|
|
|
if key = k2 then d2 else
|
|
|
|
match rest2 with
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k3, d3, rest3) ->
|
2000-07-28 05:24:25 -07:00
|
|
|
if key = k3 then d3 else find_rec key rest3
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let find_all h key =
|
|
|
|
let rec find_in_bucket = function
|
|
|
|
Empty ->
|
|
|
|
[]
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
if k = key then d :: find_in_bucket rest else find_in_bucket rest in
|
1999-11-29 11:04:12 -08:00
|
|
|
find_in_bucket h.data.((hash key) mod (Array.length h.data))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2000-07-28 05:24:25 -07:00
|
|
|
let replace h key info =
|
|
|
|
let rec replace_bucket = function
|
|
|
|
Empty ->
|
|
|
|
raise Not_found
|
|
|
|
| Cons(k, i, next) ->
|
|
|
|
if k = key
|
|
|
|
then Cons(k, info, next)
|
|
|
|
else Cons(k, i, replace_bucket next) in
|
|
|
|
let i = (hash key) mod (Array.length h.data) in
|
|
|
|
let l = h.data.(i) in
|
|
|
|
try
|
|
|
|
h.data.(i) <- replace_bucket l
|
|
|
|
with Not_found ->
|
|
|
|
h.data.(i) <- Cons(key, info, l)
|
|
|
|
|
1999-02-11 01:46:14 -08:00
|
|
|
let mem h key =
|
|
|
|
let rec mem_in_bucket = function
|
|
|
|
| Empty ->
|
|
|
|
false
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
k = key || mem_in_bucket rest in
|
1999-11-29 11:04:12 -08:00
|
|
|
mem_in_bucket h.data.((hash key) mod (Array.length h.data))
|
1999-02-11 01:46:14 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let iter f h =
|
1996-10-31 08:03:04 -08:00
|
|
|
let rec do_bucket = function
|
|
|
|
Empty ->
|
|
|
|
()
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
f k d; do_bucket rest in
|
1996-04-30 07:53:58 -07:00
|
|
|
let d = h.data in
|
1996-10-31 08:03:04 -08:00
|
|
|
for i = 0 to Array.length d - 1 do
|
1996-04-30 07:53:58 -07:00
|
|
|
do_bucket d.(i)
|
1995-05-04 03:15:53 -07:00
|
|
|
done
|
|
|
|
|
2001-06-25 01:33:25 -07:00
|
|
|
let fold f h init =
|
|
|
|
let rec do_bucket b accu =
|
|
|
|
match b with
|
|
|
|
Empty ->
|
|
|
|
accu
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
do_bucket rest (f k d accu) in
|
|
|
|
let d = h.data in
|
|
|
|
let accu = ref init in
|
|
|
|
for i = 0 to Array.length d - 1 do
|
|
|
|
accu := do_bucket d.(i) !accu
|
|
|
|
done;
|
|
|
|
!accu
|
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
(* Functorial interface *)
|
|
|
|
|
|
|
|
module type HashedType =
|
|
|
|
sig
|
|
|
|
type t
|
|
|
|
val equal: t -> t -> bool
|
|
|
|
val hash: t -> int
|
|
|
|
end
|
|
|
|
|
|
|
|
module type S =
|
|
|
|
sig
|
|
|
|
type key
|
|
|
|
type 'a t
|
|
|
|
val create: int -> 'a t
|
|
|
|
val clear: 'a t -> unit
|
|
|
|
val add: 'a t -> key -> 'a -> unit
|
|
|
|
val remove: 'a t -> key -> unit
|
|
|
|
val find: 'a t -> key -> 'a
|
|
|
|
val find_all: 'a t -> key -> 'a list
|
2000-07-28 05:24:25 -07:00
|
|
|
val replace : 'a t -> key -> 'a -> unit
|
1999-02-11 01:46:14 -08:00
|
|
|
val mem : 'a t -> key -> bool
|
1997-10-31 04:59:29 -08:00
|
|
|
val iter: (key -> 'a -> unit) -> 'a t -> unit
|
2001-09-06 01:52:32 -07:00
|
|
|
val fold: (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
|
1996-10-31 08:03:04 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
module Make(H: HashedType): (S with type key = H.t) =
|
|
|
|
struct
|
|
|
|
type key = H.t
|
|
|
|
type 'a hashtbl = (key, 'a) t
|
|
|
|
type 'a t = 'a hashtbl
|
|
|
|
let create = create
|
|
|
|
let clear = clear
|
|
|
|
|
|
|
|
let add h key info =
|
|
|
|
let i = (H.hash key) mod (Array.length h.data) in
|
|
|
|
let bucket = Cons(key, info, h.data.(i)) in
|
|
|
|
h.data.(i) <- bucket;
|
|
|
|
if bucket_too_long h.max_len bucket then resize H.hash h
|
|
|
|
|
|
|
|
let remove h key =
|
|
|
|
let rec remove_bucket = function
|
|
|
|
Empty ->
|
|
|
|
Empty
|
|
|
|
| Cons(k, i, next) ->
|
|
|
|
if H.equal k key
|
|
|
|
then next
|
|
|
|
else Cons(k, i, remove_bucket next) in
|
|
|
|
let i = (H.hash key) mod (Array.length h.data) in
|
|
|
|
h.data.(i) <- remove_bucket h.data.(i)
|
|
|
|
|
2000-07-28 05:24:25 -07:00
|
|
|
let rec find_rec key = function
|
|
|
|
Empty ->
|
|
|
|
raise Not_found
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
if H.equal key k then d else find_rec key rest
|
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
let find h key =
|
|
|
|
match h.data.((H.hash key) mod (Array.length h.data)) with
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k1, d1, rest1) ->
|
|
|
|
if H.equal key k1 then d1 else
|
|
|
|
match rest1 with
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k2, d2, rest2) ->
|
|
|
|
if H.equal key k2 then d2 else
|
|
|
|
match rest2 with
|
|
|
|
Empty -> raise Not_found
|
|
|
|
| Cons(k3, d3, rest3) ->
|
2000-07-28 05:24:25 -07:00
|
|
|
if H.equal key k3 then d3 else find_rec key rest3
|
1996-10-31 08:03:04 -08:00
|
|
|
|
|
|
|
let find_all h key =
|
|
|
|
let rec find_in_bucket = function
|
|
|
|
Empty ->
|
|
|
|
[]
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
if H.equal k key
|
|
|
|
then d :: find_in_bucket rest
|
|
|
|
else find_in_bucket rest in
|
|
|
|
find_in_bucket h.data.((H.hash key) mod (Array.length h.data))
|
|
|
|
|
2000-07-28 05:24:25 -07:00
|
|
|
let replace h key info =
|
|
|
|
let rec replace_bucket = function
|
|
|
|
Empty ->
|
|
|
|
raise Not_found
|
|
|
|
| Cons(k, i, next) ->
|
|
|
|
if H.equal k key
|
|
|
|
then Cons(k, info, next)
|
|
|
|
else Cons(k, i, replace_bucket next) in
|
|
|
|
let i = (H.hash key) mod (Array.length h.data) in
|
|
|
|
let l = h.data.(i) in
|
|
|
|
try
|
|
|
|
h.data.(i) <- replace_bucket l
|
|
|
|
with Not_found ->
|
|
|
|
h.data.(i) <- Cons(key, info, l)
|
|
|
|
|
1999-02-11 01:46:14 -08:00
|
|
|
let mem h key =
|
|
|
|
let rec mem_in_bucket = function
|
|
|
|
| Empty ->
|
|
|
|
false
|
|
|
|
| Cons(k, d, rest) ->
|
|
|
|
H.equal k key || mem_in_bucket rest in
|
1999-11-25 14:39:23 -08:00
|
|
|
mem_in_bucket h.data.((H.hash key) mod (Array.length h.data))
|
1999-02-11 01:46:14 -08:00
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
let iter = iter
|
2001-06-25 01:33:25 -07:00
|
|
|
let fold = fold
|
1996-10-31 08:03:04 -08:00
|
|
|
end
|