Commit cc07e0a0 authored by POTTIER Francois's avatar POTTIER Francois

Moved the string hash-consing service to [Misc.new_intern].

parent 8d528697
open Grammar
module Q = LowIntegerPriorityQueue
(* Throughout, we ignore the [error] pseudo-token completely. We consider that
it never appears on the input stream. Hence, any state whose incoming
......@@ -112,8 +113,6 @@ module W : sig
val elements: word -> Terminal.t list
val print: word -> string
val stats: unit -> unit
end = struct
let () =
......@@ -124,22 +123,8 @@ end = struct
let c2t (t : char) : Terminal.t =
Obj.magic (Char.code t)
module H = Hashtbl.Make(struct
type t = string
let equal = (=)
let hash = Hashtbl.hash
end)
let table = H.create 1023
let c = ref 0
let n = ref 0
let intern s =
incr c;
try
H.find table s
with Not_found ->
incr n;
H.add table s s;
s
let intern =
Misc.new_intern 1023
type word = string
let epsilon = ""
......@@ -158,16 +143,8 @@ end = struct
string_of_int (length w) ^ " " ^
String.concat " " (List.map Terminal.print (elements w))
let stats () =
Printf.fprintf stderr
"Number of calls to intern: %d\n\
Number of elements in intern table: %d\n%!"
!c !n
end
module Q = LowIntegerPriorityQueue
module Trie = struct
let c = ref 0
......@@ -513,7 +490,6 @@ let level = ref 0
let done_with_level () =
Printf.fprintf stderr "Done with level %d.\n" !level;
W.stats();
T.stats();
E.stats();
Printf.fprintf stderr "Q stores %d facts.\n%!" (Q.cardinal q)
......
......@@ -244,3 +244,17 @@ let map_opt f l =
| None -> ys
| Some y -> y :: ys
) [] l))
let new_intern capacity : string -> string =
let module H = Hashtbl.Make(struct
type t = string
let equal = (=)
let hash = Hashtbl.hash
end) in
let table = H.create capacity in
fun s ->
try
H.find table s
with Not_found ->
H.add table s s;
s
......@@ -148,3 +148,8 @@ val postincrement: int ref -> int
(* [map_opt f l] returns the list of [y]s such that [f x = Some y] where [x]
is in [l], preserving the order of elements of [l]. *)
val map_opt : ('a -> 'b option) -> 'a list -> 'b list
(* [new_intern capacity] creates a new service for interning (hash-consing)
strings. [capacity] is the initial capacity of the internal hash table. *)
val new_intern: int -> (string -> string)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment