ad3768b1a841ea704073af0776e6ef9e0812e91b
[SXSI/xpathcomp.git] / src / nodeSet.ml
1 INCLUDE "debug.ml"
2 INCLUDE "utils.ml"
3
4
5 module type S =
6   sig
7     type t
8     type elt = Tree.node
9     val empty : t
10     val var : (int*State.t) -> t
11     val close : ((int*State.t), t) Hashtbl.t -> t -> t
12     val is_open : t -> bool
13     val singleton : elt -> t
14     val cons : elt -> t -> t
15     val snoc : t -> elt -> t
16     val concat : t -> t -> t
17     val concat3 : t -> t -> t -> t
18     val concat4 : t -> t -> t -> t -> t
19     val conscat : elt -> t -> t -> t
20     val conscat3 : elt -> t -> t -> t -> t
21     val conscat4 : elt -> t -> t -> t -> t -> t
22     val subtree_tags : Tree.t -> elt -> Tag.t -> t
23     val subtree_elements : Tree.t -> elt -> t
24     val iter : ( elt -> unit) -> t -> unit
25     val fold : ( elt -> 'a -> 'a) -> t -> 'a -> 'a
26     val length : t -> int
27     val serialize : string -> Tree.t -> t -> unit
28
29   end
30
31 module Count : S with type t = int =
32   struct
33     type t = int
34     type elt = Tree.node
35
36     let empty = 0
37     let var _ = empty
38     let is_open _ = false
39     let close _ x = x
40     let singleton _ = 1
41     let cons _ x = x+1
42     let snoc x _ = x+1
43     let concat x y = x + y
44     let concat3 x y z = x + y + z
45     let concat4 x y z t = x + y + z + t
46     let conscat _ x y = 1 + x + y
47     let conscat3 _ x y z = 1 + x + y + z
48     let conscat4 _ x y z t = 1 + x + y + z + t
49     let subtree_tags tree node tag = Tree.subtree_tags tree node tag
50     let subtree_elements tree node = Tree.subtree_elements tree node
51     let iter _ _ = failwith "iter not implemented"
52     let fold _ _ _ = failwith "fold not implemented"
53     let map _ _ = failwith "map not implemented"
54     let length x = x
55     let serialize _ _ _ = ()
56   end
57
58 type  clist =
59   | Nil
60   | Cons of Tree.node * clist
61   | Concat of clist * clist
62   | ConsCat of Tree.node * clist * clist
63   | SubtreeTags of Tree.t * Tree.node * Tag.t
64   | SubtreeElts of Tree.t * Tree.node
65
66
67 type 'a mat = { mutable clist : clist;
68                 mutable length : int }
69
70 module Mat : S with type t = Tree.node mat =
71   struct
72     type t = Tree.node mat
73     type elt = Tree.node
74     let is_open _ = false
75     let empty = { clist = Nil; length = 0 }
76     let singleton e = { clist = Cons(e, Nil) ; length = 1 }
77     let cons e l = { clist = Cons(e, l.clist); length = l.length + 1 }
78     let concat l1 l2 =
79       let ll1 = l1.length in
80       if ll1 == 0 then l2 else
81         let ll2 = l2.length in if ll2 == 0 then l1 else
82             { clist = Concat(l1.clist, l2.clist); length = ll1 + ll2 }
83
84     let snoc l e = concat l (singleton e)
85     let concat3 l1 l2 l3 = concat l1 (concat l2 l3)
86     let concat4 l1 l2 l3 l4 = concat (concat l1 l2) (concat l3 l4)
87     let var _ = empty
88     let close _ x = x
89
90
91     let conscat e l1 l2 =
92       let ll1 = l1.length in
93       if ll1 == 0 then cons e l2 else
94         let ll2 = l2.length in if ll2 == 0 then cons e l1 else
95             { clist = ConsCat(e, l1.clist, l2.clist); length = 1 + ll1 + ll2 }
96
97 (*    let conscat e l1 l2 = cons e (concat l1 l2) *)
98
99     let conscat3 e l1 l2 l3 = conscat e l1 (concat l2 l3)
100     let conscat4 e l1 l2 l3 l4 = conscat e l1 (concat l2 (concat l3 l4))
101
102     let subtree_tags tree node tag =
103       { clist = SubtreeTags(tree, node, tag);
104         length = Tree.subtree_tags tree node tag }
105     let subtree_elements tree node =
106       { clist = SubtreeElts(tree, node);
107         length = Tree.subtree_elements tree node }
108
109     let fst_tagged tree t tag =
110       if Tree.tag tree t == tag then t
111       else Tree.tagged_descendant tree t tag
112
113     let fst_element tree t =
114       let tag = Tree.tag tree t in
115       let t = if Ptset.Int.mem tag
116           (Ptset.Int.remove Tag.document_node (Tree.element_tags tree))
117         then t
118         else Tree.first_element tree t
119       in Tree.first_element tree t
120
121     let element_fold f tree t acc =
122       let rec loop node acc =
123         if node == Tree.nil then acc
124         else
125           let acc = f node acc in
126           let acc' = loop (Tree.first_element tree node) acc in
127             loop (Tree.next_element tree node) acc'
128       in
129         loop (fst_element tree t) acc
130
131     let element_iter f tree t =
132       let rec loop node =
133         if node != Tree.nil then begin
134           f node;
135           loop (Tree.first_element tree node);
136           loop (Tree.next_element tree node)
137         end
138       in
139       let t' = fst_element tree t in loop t'
140
141     let tag_fold f tree t tag acc =
142       let rec loop close node acc =
143         if node > Tree.nil && node < close then acc
144         else
145           let acc = f node acc in
146           loop close (Tree.tagged_next tree node tag) acc
147       in
148       let t' = fst_tagged tree t tag in
149         loop (Tree.closing tree t) t' acc
150
151     let tag_iter f tree t tag =
152       let rec loop close node =
153         if node > Tree.nil && node < close then begin
154           f node;
155           loop close (Tree.tagged_next tree node tag);
156         end
157       in
158       let t' = fst_tagged tree t tag in
159         loop (Tree.closing tree t) t'
160
161     let fold f l acc =
162       let rec loop l acc =
163         match l with
164           | Nil -> acc
165           | Cons(e, ll) -> loop ll (f e acc)
166           | Concat(l1, l2) -> loop l2 (loop l1 acc)
167           | ConsCat(e, l1, l2) -> loop l2 (loop l1 (f e acc))
168           | SubtreeTags(tree, t, tag) -> tag_fold f tree t tag acc
169           | SubtreeElts(tree, t) -> element_fold f tree t acc
170       in
171         loop l.clist acc
172
173     let iter f l =
174       let rec loop l =
175         match l with
176           | Nil -> ()
177           | Cons(e, l) -> f e; loop l
178           | Concat(l1, l2) -> loop l1; loop l2
179           | ConsCat(e, l1, l2) -> f e; loop l1; loop l2
180           | SubtreeTags(tree, t, tag) -> tag_iter f tree t tag
181           | SubtreeElts(tree, t) ->
182               element_iter f tree t
183       in
184         loop l.clist
185
186     let length l = l.length
187
188     let serialize name v l =
189       let fd, finish =
190         if name = "-" then Unix.stdout, ignore
191         else
192           Unix.openfile name [ Unix.O_WRONLY; Unix.O_TRUNC; Unix.O_CREAT ] 0o666,
193           Unix.close
194       in
195       iter (fun node -> Tree.print_xml v node fd) l;
196       Tree.flush v fd;
197       finish fd
198
199   end
200 let rec debug_clist =
201   function
202       Nil -> Printf.eprintf "Nil"
203     | Cons(e, clist) ->
204         Printf.eprintf "Cons(%i," (Obj.magic e);
205         debug_clist clist;
206         Printf.eprintf ")";
207     | Concat(clist1, clist2) ->
208             Printf.eprintf "Concat(";
209         debug_clist clist1;
210         Printf.eprintf ",";
211             debug_clist clist2;
212             Printf.eprintf ")";
213     | ConsCat(_, clist1, clist2) ->
214             Printf.eprintf "Concat(";
215         debug_clist clist1;
216         Printf.eprintf ",";
217             debug_clist clist2;
218             Printf.eprintf ")";
219
220     | SubtreeTags(tree, node, tag) ->
221         Printf.eprintf "SubtreeTags(tree, %i, %s)"
222           (Obj.magic node)
223           (Tag.to_string tag);
224     | SubtreeElts(tree, node) ->
225         Printf.eprintf "SubtreeElts(tree, %i)"
226           (Obj.magic node)
227
228 let debug l = debug_clist l.clist
229
230
231
232 module Partial(N : S) : S =
233 struct
234
235   type elt = Tree.node
236   type t = { env : ((int*State.t), t) Hashtbl.t;
237              elem : list;
238              opened : bool;
239            }
240   and list =
241     | Var of (int * State.t)
242     | Nil
243     | Cons of elt * list
244     | Concat of list * list
245     | Lambda of t
246
247   let dummy = Hashtbl.create 0
248   let empty = { env = dummy;
249                 elem = Nil;
250                 opened = false }
251   let is_open t = t.opened
252
253
254   let close h t =
255     {empty with elem =
256         Lambda { t with env = h; opened = false } }
257
258   let singleton i = { empty with elem = Cons(i, Nil) }
259   let cons e t = { t with elem = Cons(e, t.elem) }
260   let concat t1 t2 =
261     { t1 with elem = Concat (t1.elem, t2.elem) }
262
263   let snoc t e = concat t (singleton e)
264   let concat3 t1 t2 t3 = concat t1 (concat t2 t3)
265   let concat4 t1 t2 t3 t4 = concat (concat t1 t2) (concat t3 t4)
266   let conscat e t1 t2 = cons e (concat t1 t2)
267   let conscat3 e t1 t2 t3 = cons e (concat3 t1 t2 t3)
268   let conscat4 e t1 t2 t3 t4 = cons e (concat4 t1 t2 t3 t4)
269   let subtree_tags _ = failwith "not implemented"
270   let subtree_elements _ = failwith "not_implemented"
271
272   let iter f t =
273     let rec loop t =
274       loop_list t.env t.elem
275     and loop_list h = function
276       | Nil -> ()
277       | Var i -> loop (Hashtbl.find h i)
278       | Cons (e, l) -> f e; loop_list h l
279       | Concat (l1, l2) -> loop_list h l1; loop_list h l2
280       | Lambda t -> loop t
281     in
282     loop t
283
284   let fold f t acc =
285     let rec loop t acc =
286       loop_list t.env acc t.elem
287     and loop_list h acc = function
288       | Nil -> acc
289       | Var i -> loop (try Hashtbl.find h i with Not_found -> let a,b = i in Printf.eprintf "%i,%i not found\n%!" a b; empty) acc
290       | Cons (e, l) ->   loop_list h (f e acc) l
291       | Concat (l1, l2) -> loop_list h (loop_list h acc l1) l2
292       | Lambda t -> loop t acc
293     in
294     loop t acc
295
296
297   let rec dump t =
298     Hashtbl.iter (fun (i,j) t ->
299       Format.eprintf "%i, %a ->" i State.print j;
300       dump t;
301       Format.eprintf "----------------\n%!";
302     ) t.env;
303     dump_list t.elem
304   and dump_list  = function
305     | Nil -> ()
306     | Var (i,j) -> Format.eprintf "Var(%i, %a) " i State.print j;
307     | Cons (e, l) -> Format.eprintf "%i " (Node.to_int e); dump_list l
308     | Concat (l1, l2) -> dump_list l1 ; dump_list l2
309     | Lambda t -> dump t
310
311
312   let length t = fold (fun _ acc -> 1 + acc) t 0
313
314
315   let var i =
316     { empty with elem = Var i; opened = true }
317
318   let serialize _ = failwith "not implemented"
319 end