1997-02-24 11:24:39 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
1997-02-25 06:39:02 -08:00
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
1997-02-24 11:24:39 -08:00
|
|
|
(* *)
|
1997-02-25 06:39:02 -08:00
|
|
|
(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1997-02-24 11:24:39 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2002-01-23 09:54:11 -08:00
|
|
|
|
|
|
|
(** Weak array operations *)
|
1997-02-24 11:24:39 -08:00
|
|
|
|
1997-03-13 08:24:05 -08:00
|
|
|
type 'a t;;
|
1997-02-24 11:24:39 -08:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
external create: int -> 'a t = "caml_weak_create";;
|
1997-02-24 11:24:39 -08:00
|
|
|
|
1997-03-05 06:38:24 -08:00
|
|
|
let length x = Obj.size(Obj.repr x) - 1;;
|
1997-02-24 11:24:39 -08:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
external set : 'a t -> int -> 'a option -> unit = "caml_weak_set";;
|
|
|
|
external get: 'a t -> int -> 'a option = "caml_weak_get";;
|
|
|
|
external get_copy: 'a t -> int -> 'a option = "caml_weak_get_copy";;
|
|
|
|
external check: 'a t -> int -> bool = "caml_weak_check";;
|
2008-01-11 03:55:36 -08:00
|
|
|
external blit: 'a t -> int -> 'a t -> int -> int -> unit = "caml_weak_blit";;
|
1998-11-13 15:42:11 -08:00
|
|
|
|
1997-02-24 11:24:39 -08:00
|
|
|
let fill ar ofs len x =
|
2000-05-08 10:53:58 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs + len > length ar
|
1997-02-24 11:24:39 -08:00
|
|
|
then raise (Invalid_argument "Weak.fill")
|
|
|
|
else begin
|
|
|
|
for i = ofs to (ofs + len - 1) do
|
1997-03-08 04:14:57 -08:00
|
|
|
set ar i x
|
1997-02-24 11:24:39 -08:00
|
|
|
done
|
|
|
|
end
|
|
|
|
;;
|
|
|
|
|
2002-01-23 09:54:11 -08:00
|
|
|
(** Weak hash tables *)
|
|
|
|
|
|
|
|
module type S = sig
|
|
|
|
type data
|
|
|
|
type t
|
|
|
|
val create : int -> t
|
|
|
|
val clear : t -> unit
|
|
|
|
val merge : t -> data -> data
|
|
|
|
val add : t -> data -> unit
|
|
|
|
val remove : t -> data -> unit
|
|
|
|
val find : t -> data -> data
|
|
|
|
val find_all : t -> data -> data list
|
|
|
|
val mem : t -> data -> bool
|
|
|
|
val iter : (data -> unit) -> t -> unit
|
|
|
|
val fold : (data -> 'a -> 'a) -> t -> 'a -> 'a
|
|
|
|
val count : t -> int
|
|
|
|
val stats : t -> int * int * int * int * int * int
|
|
|
|
end;;
|
|
|
|
|
|
|
|
module Make (H : Hashtbl.HashedType) : (S with type data = H.t) = struct
|
|
|
|
|
|
|
|
type 'a weak_t = 'a t;;
|
|
|
|
let weak_create = create;;
|
|
|
|
let emptybucket = weak_create 0;;
|
|
|
|
|
|
|
|
type data = H.t;;
|
|
|
|
|
|
|
|
type t = {
|
|
|
|
mutable table : data weak_t array;
|
2008-01-11 03:55:36 -08:00
|
|
|
mutable hashes : int array array;
|
|
|
|
mutable limit : int; (* bucket size limit *)
|
|
|
|
mutable oversize : int; (* number of oversize buckets *)
|
|
|
|
mutable rover : int; (* for internal bookkeeping *)
|
2002-01-23 09:54:11 -08:00
|
|
|
};;
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
let get_index t h = (h land max_int) mod (Array.length t.table);;
|
|
|
|
|
|
|
|
let limit = 7;;
|
|
|
|
let over_limit = 2;;
|
2002-01-23 09:54:11 -08:00
|
|
|
|
|
|
|
let create sz =
|
|
|
|
let sz = if sz < 7 then 7 else sz in
|
|
|
|
let sz = if sz > Sys.max_array_length then Sys.max_array_length else sz in
|
|
|
|
{
|
|
|
|
table = Array.create sz emptybucket;
|
2008-01-11 03:55:36 -08:00
|
|
|
hashes = Array.create sz [| |];
|
|
|
|
limit = limit;
|
|
|
|
oversize = 0;
|
|
|
|
rover = 0;
|
2002-01-23 09:54:11 -08:00
|
|
|
};;
|
|
|
|
|
|
|
|
let clear t =
|
|
|
|
for i = 0 to Array.length t.table - 1 do
|
|
|
|
t.table.(i) <- emptybucket;
|
2008-01-11 03:55:36 -08:00
|
|
|
t.hashes.(i) <- [| |];
|
2002-01-23 09:54:11 -08:00
|
|
|
done;
|
2008-01-11 03:55:36 -08:00
|
|
|
t.limit <- limit;
|
|
|
|
t.oversize <- 0;
|
2002-01-23 09:54:11 -08:00
|
|
|
;;
|
|
|
|
|
|
|
|
let fold f t init =
|
|
|
|
let rec fold_bucket i b accu =
|
|
|
|
if i >= length b then accu else
|
|
|
|
match get b i with
|
|
|
|
| Some v -> fold_bucket (i+1) b (f v accu)
|
|
|
|
| None -> fold_bucket (i+1) b accu
|
|
|
|
in
|
|
|
|
Array.fold_right (fold_bucket 0) t.table init
|
|
|
|
;;
|
|
|
|
|
2002-07-23 09:10:00 -07:00
|
|
|
let iter f t =
|
|
|
|
let rec iter_bucket i b =
|
|
|
|
if i >= length b then () else
|
|
|
|
match get b i with
|
|
|
|
| Some v -> f v; iter_bucket (i+1) b
|
|
|
|
| None -> iter_bucket (i+1) b
|
|
|
|
in
|
|
|
|
Array.iter (iter_bucket 0) t.table
|
|
|
|
;;
|
2002-01-23 09:54:11 -08:00
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
let rec count_bucket i b accu =
|
|
|
|
if i >= length b then accu else
|
|
|
|
count_bucket (i+1) b (accu + (if check b i then 1 else 0))
|
|
|
|
;;
|
|
|
|
|
2002-01-23 09:54:11 -08:00
|
|
|
let count t =
|
|
|
|
Array.fold_right (count_bucket 0) t.table 0
|
|
|
|
;;
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
let next_sz n = min (3 * n / 2 + 3) Sys.max_array_length;;
|
|
|
|
let prev_sz n = ((n - 3) * 2 + 2) / 3;;
|
|
|
|
|
|
|
|
let test_shrink_bucket t =
|
|
|
|
let bucket = t.table.(t.rover) in
|
|
|
|
let hbucket = t.hashes.(t.rover) in
|
|
|
|
let len = length bucket in
|
|
|
|
let prev_len = prev_sz len in
|
|
|
|
let live = count_bucket 0 bucket 0 in
|
|
|
|
if live <= prev_len then begin
|
|
|
|
let rec loop i j =
|
|
|
|
if j > prev_len then begin
|
|
|
|
if check bucket i then loop (i + 1) j
|
|
|
|
else if check bucket j then begin
|
|
|
|
blit bucket j bucket i 1;
|
|
|
|
hbucket.(i) <- hbucket.(j);
|
|
|
|
loop (i + 1) (j - 1);
|
|
|
|
end else loop i (j - 1);
|
|
|
|
end;
|
|
|
|
in
|
|
|
|
loop 0 (length bucket - 1);
|
|
|
|
if prev_len = 0 then begin
|
|
|
|
t.table.(t.rover) <- emptybucket;
|
|
|
|
t.hashes.(t.rover) <- [| |];
|
|
|
|
end else begin
|
|
|
|
Obj.truncate (Obj.repr bucket) (prev_len + 1);
|
|
|
|
Obj.truncate (Obj.repr hbucket) prev_len;
|
|
|
|
end;
|
|
|
|
if len > t.limit && prev_len <= t.limit then t.oversize <- t.oversize - 1;
|
|
|
|
end;
|
|
|
|
t.rover <- (t.rover + 1) mod (Array.length t.table);
|
|
|
|
;;
|
2002-01-23 09:54:11 -08:00
|
|
|
|
|
|
|
let rec resize t =
|
|
|
|
let oldlen = Array.length t.table in
|
|
|
|
let newlen = next_sz oldlen in
|
|
|
|
if newlen > oldlen then begin
|
|
|
|
let newt = create newlen in
|
|
|
|
fold (fun d () -> add newt d) t ();
|
|
|
|
t.table <- newt.table;
|
2008-01-11 03:55:36 -08:00
|
|
|
t.hashes <- newt.hashes;
|
|
|
|
t.limit <- newt.limit;
|
|
|
|
t.oversize <- newt.oversize;
|
|
|
|
t.rover <- t.rover mod Array.length newt.table;
|
|
|
|
end else begin
|
|
|
|
t.limit <- max_int; (* maximum size already reached *)
|
|
|
|
t.oversize <- 0;
|
2002-01-23 09:54:11 -08:00
|
|
|
end
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
and add_aux t d h index =
|
2002-01-23 09:54:11 -08:00
|
|
|
let bucket = t.table.(index) in
|
2008-01-11 03:55:36 -08:00
|
|
|
let hashes = t.hashes.(index) in
|
2002-01-23 09:54:11 -08:00
|
|
|
let sz = length bucket in
|
|
|
|
let rec loop i =
|
|
|
|
if i >= sz then begin
|
2008-01-11 03:55:36 -08:00
|
|
|
let newsz = min (3 * sz / 2 + 3) (Sys.max_array_length - 1) in
|
|
|
|
if newsz <= sz then failwith "Weak.Make: hash bucket cannot grow more";
|
2002-01-23 09:54:11 -08:00
|
|
|
let newbucket = weak_create newsz in
|
2008-01-11 03:55:36 -08:00
|
|
|
let newhashes = Array.make newsz 0 in
|
2002-01-23 09:54:11 -08:00
|
|
|
blit bucket 0 newbucket 0 sz;
|
2008-01-11 03:55:36 -08:00
|
|
|
Array.blit hashes 0 newhashes 0 sz;
|
|
|
|
set newbucket sz (Some d);
|
|
|
|
newhashes.(sz) <- h;
|
2002-01-23 09:54:11 -08:00
|
|
|
t.table.(index) <- newbucket;
|
2008-01-11 03:55:36 -08:00
|
|
|
t.hashes.(index) <- newhashes;
|
|
|
|
if sz <= t.limit && newsz > t.limit then begin
|
|
|
|
t.oversize <- t.oversize + 1;
|
|
|
|
for i = 0 to over_limit do test_shrink_bucket t done;
|
|
|
|
end;
|
|
|
|
if t.oversize > Array.length t.table / over_limit then resize t;
|
2002-01-23 09:54:11 -08:00
|
|
|
end else begin
|
|
|
|
if check bucket i
|
2008-01-11 03:55:36 -08:00
|
|
|
then loop (i + 1)
|
|
|
|
else begin
|
|
|
|
set bucket i (Some d);
|
|
|
|
hashes.(i) <- h;
|
|
|
|
end;
|
|
|
|
end;
|
2002-01-23 09:54:11 -08:00
|
|
|
in
|
|
|
|
loop 0;
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
and add t d =
|
|
|
|
let h = H.hash d in
|
|
|
|
add_aux t d h (get_index t h);
|
2002-01-23 09:54:11 -08:00
|
|
|
;;
|
|
|
|
|
|
|
|
let find_or t d ifnotfound =
|
2008-01-11 03:55:36 -08:00
|
|
|
let h = H.hash d in
|
|
|
|
let index = get_index t h in
|
2002-01-23 09:54:11 -08:00
|
|
|
let bucket = t.table.(index) in
|
2008-01-11 03:55:36 -08:00
|
|
|
let hashes = t.hashes.(index) in
|
2002-01-23 09:54:11 -08:00
|
|
|
let sz = length bucket in
|
|
|
|
let rec loop i =
|
2008-01-11 03:55:36 -08:00
|
|
|
if i >= sz then ifnotfound h index
|
|
|
|
else if h = hashes.(i) then begin
|
2002-01-23 09:54:11 -08:00
|
|
|
match get_copy bucket i with
|
|
|
|
| Some v when H.equal v d
|
|
|
|
-> begin match get bucket i with
|
|
|
|
| Some v -> v
|
2008-01-11 03:55:36 -08:00
|
|
|
| None -> loop (i + 1)
|
2002-01-23 09:54:11 -08:00
|
|
|
end
|
2008-01-11 03:55:36 -08:00
|
|
|
| _ -> loop (i + 1)
|
|
|
|
end else loop (i + 1)
|
2002-01-23 09:54:11 -08:00
|
|
|
in
|
|
|
|
loop 0
|
|
|
|
;;
|
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
let merge t d = find_or t d (fun h index -> add_aux t d h index; d);;
|
2002-01-23 09:54:11 -08:00
|
|
|
|
2008-01-11 03:55:36 -08:00
|
|
|
let find t d = find_or t d (fun h index -> raise Not_found);;
|
2002-01-23 09:54:11 -08:00
|
|
|
|
|
|
|
let find_shadow t d iffound ifnotfound =
|
2008-01-11 03:55:36 -08:00
|
|
|
let h = H.hash d in
|
|
|
|
let index = get_index t h in
|
2002-01-23 09:54:11 -08:00
|
|
|
let bucket = t.table.(index) in
|
2008-01-11 03:55:36 -08:00
|
|
|
let hashes = t.hashes.(index) in
|
2002-01-23 09:54:11 -08:00
|
|
|
let sz = length bucket in
|
|
|
|
let rec loop i =
|
2008-01-11 03:55:36 -08:00
|
|
|
if i >= sz then ifnotfound
|
|
|
|
else if h = hashes.(i) then begin
|
2002-01-23 09:54:11 -08:00
|
|
|
match get_copy bucket i with
|
|
|
|
| Some v when H.equal v d -> iffound bucket i
|
2008-01-11 03:55:36 -08:00
|
|
|
| _ -> loop (i + 1)
|
|
|
|
end else loop (i + 1)
|
2002-01-23 09:54:11 -08:00
|
|
|
in
|
|
|
|
loop 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
let remove t d = find_shadow t d (fun w i -> set w i None) ();;
|
|
|
|
|
|
|
|
let mem t d = find_shadow t d (fun w i -> true) false;;
|
|
|
|
|
|
|
|
let find_all t d =
|
2008-01-11 03:55:36 -08:00
|
|
|
let h = H.hash d in
|
|
|
|
let index = get_index t h in
|
2002-01-23 09:54:11 -08:00
|
|
|
let bucket = t.table.(index) in
|
2008-01-11 03:55:36 -08:00
|
|
|
let hashes = t.hashes.(index) in
|
2002-01-23 09:54:11 -08:00
|
|
|
let sz = length bucket in
|
|
|
|
let rec loop i accu =
|
|
|
|
if i >= sz then accu
|
2008-01-11 03:55:36 -08:00
|
|
|
else if h = hashes.(i) then begin
|
2002-01-23 09:54:11 -08:00
|
|
|
match get_copy bucket i with
|
|
|
|
| Some v when H.equal v d
|
|
|
|
-> begin match get bucket i with
|
2008-01-11 03:55:36 -08:00
|
|
|
| Some v -> loop (i + 1) (v :: accu)
|
|
|
|
| None -> loop (i + 1) accu
|
2002-01-23 09:54:11 -08:00
|
|
|
end
|
2008-01-11 03:55:36 -08:00
|
|
|
| _ -> loop (i + 1) accu
|
|
|
|
end else loop (i + 1) accu
|
2002-01-23 09:54:11 -08:00
|
|
|
in
|
|
|
|
loop 0 []
|
|
|
|
;;
|
|
|
|
|
|
|
|
let stats t =
|
|
|
|
let len = Array.length t.table in
|
|
|
|
let lens = Array.map length t.table in
|
|
|
|
Array.sort compare lens;
|
|
|
|
let totlen = Array.fold_left ( + ) 0 lens in
|
|
|
|
(len, count t, totlen, lens.(0), lens.(len/2), lens.(len-1))
|
|
|
|
;;
|
|
|
|
|
|
|
|
end;;
|