4434e1e9d71857f7d8e0fb659416734f7d85edc9
[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
32 end
33
34 module Make (H : Hcons.SA) : S with type elt = H.t =
35 struct
36   type elt = H.t
37   type 'a node = Nil | Cons of elt * 'a
38   module rec Node : Hcons.S with type data = Data.t = Hcons.Make (Data)
39   and Data : Hashtbl.HashedType  with type t = Node.t node =
40   struct
41     type t = Node.t node
42     let equal x y =
43       match x,y with
44         | _,_ when x==y -> true
45         | Cons (a,aa), Cons(b,bb) -> (aa==bb) && (H.equal a b)
46         | _ -> false
47     let hash = function
48       | Nil -> 0
49       | Cons(a,aa) -> HASHINT3(PRIME3,Uid.to_int (H.uid a),Uid.to_int( aa.Node.id))
50   end
51   type data = Data.t
52   type t = Node.t
53   let make = Node.make
54   let node x = x.Node.node
55   let hash x = x.Node.key
56   let equal = Node.equal
57   let uid x= x.Node.id
58   let nil = Node.make Nil
59
60   (* doing sorted insertion allows to make better use of hash consing *)
61   let rec sorted_cons e l =
62     match l.Node.node with
63       | Nil -> Node.make (Cons(e, l))
64       | Cons (x, ll) ->
65           if H.uid e < H.uid x
66           then Node.make (Cons(e, l))
67           else Node.make (Cons(x, sorted_cons e ll))
68
69   let cons e l =
70     Node.make(Cons(e, l))
71
72   let cons ?(sorted=true) e l =
73     if sorted then sorted_cons e l else cons e l
74
75   let hd = function { Node.node = Cons(a,_) } -> a | _ -> failwith "hd"
76   let tl = function { Node.node = Cons(_,a) } -> a | _ -> failwith "tl"
77
78   let fold f l acc =
79     let rec loop acc l = match l.Node.node with
80       | Nil -> acc
81       | Cons (a, aa) -> loop (f a acc) aa
82     in
83       loop acc l
84
85   let map f l  =
86     let rec loop l = match l.Node.node with
87       | Nil -> nil
88       | Cons(a, aa) -> cons (f a) (loop aa)
89     in
90       loop l
91
92   let iter f l =
93     let rec loop l = match l.Node.node with
94       | Nil -> ()
95       | Cons(a,aa) ->  (f a);(loop aa)
96     in
97       loop l
98
99   let rev l = fold cons l nil
100   let rev_map f l = fold (fun x acc -> cons (f x) acc) l nil
101   let length l = fold (fun _ c -> c+1) l 0
102   let rec mem e l =
103     match l.Node.node with
104       | Nil -> false
105       | Cons (x, ll) -> x == e || mem e ll
106
107 end