Add hooks to re-initialize hconsed modules.
[SXSI/xpathcomp.git] / src / hlist.ml
1 INCLUDE "utils.ml"
2 module type S = sig
3   type elt
4   type 'a node = Nil | Cons of elt * 'a
5
6   module rec Node :
7   sig
8     include Hcons.S with type data = Data.t
9   end
10   and Data : sig
11     include Hashtbl.HashedType with type t = Node.t node
12   end
13   type data = Data.t
14   type t = Node.t
15   val hash : t -> int
16   val uid : t -> Uid.t
17   val make : data -> t
18   val equal : t -> t -> bool
19   val nil : t
20   val node : t -> t node
21   val cons : ?sorted:bool -> elt -> t -> t
22   val hd : t -> elt
23   val tl : t -> t
24   val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a
25   val map : (elt -> elt) -> t -> t
26   val iter : (elt -> 'a) -> t -> unit
27   val rev : t -> t
28   val rev_map : (elt -> elt) -> t -> t
29   val length : t -> int
30   val mem : elt -> t -> bool
31   val stats : unit -> unit
32   val init : unit -> unit
33 end
34
35 module Make (H : Hcons.SA) : S with type elt = H.t =
36 struct
37   type elt = H.t
38   type 'a node = Nil | Cons of elt * 'a
39   module rec Node : Hcons.S with type data = Data.t = Hcons.Make (Data)
40   and Data : Hashtbl.HashedType  with type t = Node.t node =
41   struct
42     type t = Node.t node
43     let equal x y =
44       match x,y with
45         | _,_ when x==y -> true
46         | Cons (a,aa), Cons(b,bb) -> (aa==bb) && (H.equal a b)
47         | _ -> false
48     let hash = function
49       | Nil -> 0
50       | Cons(a,aa) -> HASHINT3(PRIME3,Uid.to_int (H.uid a),Uid.to_int( aa.Node.id))
51   end
52   type data = Data.t
53   type t = Node.t
54   let make = Node.make
55   let node x = x.Node.node
56   let hash x = x.Node.key
57   let equal = Node.equal
58   let uid x= x.Node.id
59   let nil = Node.make Nil
60   let stats = Node.stats
61   let init = Node.init
62   (* doing sorted insertion allows to make better use of hash consing *)
63   let rec sorted_cons e l =
64     match l.Node.node with
65       | Nil -> Node.make (Cons(e, l))
66       | Cons (x, ll) ->
67           if H.uid e < H.uid x
68           then Node.make (Cons(e, l))
69           else Node.make (Cons(x, sorted_cons e ll))
70
71   let cons e l =
72     Node.make(Cons(e, l))
73
74   let cons ?(sorted=true) e l =
75     if sorted then sorted_cons e l else cons e l
76
77   let hd = function { Node.node = Cons(a,_) } -> a | _ -> failwith "hd"
78   let tl = function { Node.node = Cons(_,a) } -> a | _ -> failwith "tl"
79
80   let fold f l acc =
81     let rec loop acc l = match l.Node.node with
82       | Nil -> acc
83       | Cons (a, aa) -> loop (f a acc) aa
84     in
85       loop acc l
86
87   let map f l  =
88     let rec loop l = match l.Node.node with
89       | Nil -> nil
90       | Cons(a, aa) -> cons (f a) (loop aa)
91     in
92       loop l
93
94   let iter f l =
95     let rec loop l = match l.Node.node with
96       | Nil -> ()
97       | Cons(a,aa) ->  (f a);(loop aa)
98     in
99       loop l
100
101   let rev l = fold cons l nil
102   let rev_map f l = fold (fun x acc -> cons (f x) acc) l nil
103   let length l = fold (fun _ c -> c+1) l 0
104   let rec mem e l =
105     match l.Node.node with
106       | Nil -> false
107       | Cons (x, ll) -> x == e || mem e ll
108
109 end