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