1995-11-09 03:04:31 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* *)
|
|
|
|
(* Daniel de Rauglaudre, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1997-06-16 08:33:44 -07:00
|
|
|
(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-11-09 03:04:31 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-11-13 05:29:51 -08:00
|
|
|
(* The fields of type t are not mutable to preserve polymorphism of
|
|
|
|
the empty stream. This is type safe because the empty stream is never
|
|
|
|
patched. *)
|
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
type 'a t = { count : int; data : 'a data }
|
1995-11-09 03:04:31 -08:00
|
|
|
and 'a data =
|
|
|
|
Sempty
|
|
|
|
| Scons of 'a * 'a data
|
2012-07-10 02:58:38 -07:00
|
|
|
| Sapp of 'a data * 'a t
|
|
|
|
| Slazy of 'a t Lazy.t
|
1995-12-07 11:44:19 -08:00
|
|
|
| Sgen of 'a gen
|
1995-11-09 03:04:31 -08:00
|
|
|
| Sbuffio of buffio
|
1997-06-16 08:33:44 -07:00
|
|
|
and 'a gen = { mutable curr : 'a option option; func : int -> 'a option }
|
1995-11-09 03:04:31 -08:00
|
|
|
and buffio =
|
1997-06-16 08:33:44 -07:00
|
|
|
{ ic : in_channel; buff : string; mutable len : int; mutable ind : int }
|
|
|
|
;;
|
|
|
|
exception Failure;;
|
|
|
|
exception Error of string;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
external count : 'a t -> int = "%field0";;
|
|
|
|
external set_count : 'a t -> int -> unit = "%setfield0";;
|
|
|
|
let set_data (s : 'a t) (d : 'a data) =
|
|
|
|
Obj.set_field (Obj.repr s) 1 (Obj.repr d)
|
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let fill_buff b =
|
1997-06-16 08:33:44 -07:00
|
|
|
b.len <- input b.ic b.buff 0 (String.length b.buff); b.ind <- 0
|
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
2012-07-10 02:58:38 -07:00
|
|
|
let rec get_data s d = match d with
|
|
|
|
(* Only return a "forced stream", that is either Sempty or
|
|
|
|
Scons(a,_). If d is a generator or a buffer, the item a is seen as
|
|
|
|
extracted from the generator/buffer.
|
2012-07-30 11:04:46 -07:00
|
|
|
|
2012-07-10 02:58:38 -07:00
|
|
|
Forcing also updates the "count" field of the delayed stream,
|
|
|
|
in the Sapp and Slazy cases (see slazy/lapp implementation below). *)
|
2008-06-18 08:35:02 -07:00
|
|
|
Sempty | Scons (_, _) -> d
|
2012-07-10 02:58:38 -07:00
|
|
|
| Sapp (d1, s2) ->
|
|
|
|
begin match get_data s d1 with
|
|
|
|
Scons (a, d11) -> Scons (a, Sapp (d11, s2))
|
|
|
|
| Sempty ->
|
|
|
|
set_count s s2.count;
|
|
|
|
get_data s s2.data
|
2008-06-18 08:35:02 -07:00
|
|
|
| _ -> assert false
|
|
|
|
end
|
2012-07-10 02:58:38 -07:00
|
|
|
| Sgen {curr = Some None; _ } -> Sempty
|
|
|
|
| Sgen ({curr = Some(Some a); _ } as g) ->
|
2008-06-18 08:35:02 -07:00
|
|
|
g.curr <- None; Scons(a, d)
|
2012-07-10 02:58:38 -07:00
|
|
|
| Sgen ({curr = None; _} as g) ->
|
|
|
|
(* Warning: anyone using g thinks that an item has been read *)
|
|
|
|
begin match g.func s.count with
|
2008-06-18 08:35:02 -07:00
|
|
|
None -> g.curr <- Some(None); Sempty
|
2012-07-10 02:58:38 -07:00
|
|
|
| Some a ->
|
|
|
|
(* One must not update g.curr here, because there Scons(a,d)
|
|
|
|
result of get_data, if the outer stream s was a Sapp, will
|
|
|
|
be used to update the outer stream to Scons(a,s): there is
|
|
|
|
already a memoization process at the outer layer. If g.curr
|
|
|
|
was updated here, the saved element would be produced twice,
|
|
|
|
once by the outer layer, once by Sgen/g.curr. *)
|
|
|
|
Scons(a, d)
|
2008-06-18 08:35:02 -07:00
|
|
|
end
|
|
|
|
| Sbuffio b ->
|
|
|
|
if b.ind >= b.len then fill_buff b;
|
|
|
|
if b.len == 0 then Sempty else
|
|
|
|
let r = Obj.magic (String.unsafe_get b.buff b.ind) in
|
|
|
|
(* Warning: anyone using g thinks that an item has been read *)
|
|
|
|
b.ind <- succ b.ind; Scons(r, d)
|
2012-07-10 02:58:38 -07:00
|
|
|
| Slazy f ->
|
|
|
|
let s2 = Lazy.force f in
|
|
|
|
set_count s s2.count;
|
|
|
|
get_data s s2.data
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let rec peek s =
|
2008-06-18 08:35:02 -07:00
|
|
|
(* consult the first item of s *)
|
|
|
|
match s.data with
|
|
|
|
Sempty -> None
|
|
|
|
| Scons (a, _) -> Some a
|
|
|
|
| Sapp (_, _) ->
|
2012-07-10 02:58:38 -07:00
|
|
|
begin match get_data s s.data with
|
|
|
|
| Scons(a, _) as d -> set_data s d; Some a
|
2008-06-18 08:35:02 -07:00
|
|
|
| Sempty -> None
|
|
|
|
| _ -> assert false
|
|
|
|
end
|
2012-07-10 02:58:38 -07:00
|
|
|
| Slazy f ->
|
|
|
|
let s2 = Lazy.force f in
|
|
|
|
set_count s s2.count;
|
|
|
|
set_data s s2.data;
|
|
|
|
peek s
|
|
|
|
| Sgen {curr = Some a; _ } -> a
|
|
|
|
| Sgen ({curr = None; _ } as g) ->
|
|
|
|
let x = g.func s.count in
|
|
|
|
g.curr <- Some x; x
|
2008-06-18 08:35:02 -07:00
|
|
|
| Sbuffio b ->
|
|
|
|
if b.ind >= b.len then fill_buff b;
|
|
|
|
if b.len == 0 then begin set_data s Sempty; None end
|
|
|
|
else Some (Obj.magic (String.unsafe_get b.buff b.ind))
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let rec junk s =
|
|
|
|
match s.data with
|
1997-06-16 08:33:44 -07:00
|
|
|
Scons (_, d) -> set_count s (succ s.count); set_data s d
|
|
|
|
| Sgen ({curr = Some _} as g) -> set_count s (succ s.count); g.curr <- None
|
|
|
|
| Sbuffio b -> set_count s (succ s.count); b.ind <- succ b.ind
|
|
|
|
| _ ->
|
|
|
|
match peek s with
|
|
|
|
None -> ()
|
|
|
|
| Some _ -> junk s
|
|
|
|
;;
|
|
|
|
|
|
|
|
let rec nget n s =
|
|
|
|
if n <= 0 then [], s.data, 0
|
|
|
|
else
|
|
|
|
match peek s with
|
|
|
|
Some a ->
|
|
|
|
junk s;
|
|
|
|
let (al, d, k) = nget (pred n) s in a :: al, Scons (a, d), succ k
|
|
|
|
| None -> [], s.data, 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
let npeek n s =
|
|
|
|
let (al, d, len) = nget n s in set_count s (s.count - len); set_data s d; al
|
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let next s =
|
|
|
|
match peek s with
|
|
|
|
Some a -> junk s; a
|
1997-06-16 08:33:44 -07:00
|
|
|
| None -> raise Failure
|
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let empty s =
|
|
|
|
match peek s with
|
1997-06-16 08:33:44 -07:00
|
|
|
Some _ -> raise Failure
|
1995-11-09 03:04:31 -08:00
|
|
|
| None -> ()
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
let iter f strm =
|
|
|
|
let rec do_rec () =
|
|
|
|
match peek strm with
|
1999-02-24 07:21:50 -08:00
|
|
|
Some a -> junk strm; ignore(f a); do_rec ()
|
1995-11-10 07:15:12 -08:00
|
|
|
| None -> ()
|
|
|
|
in
|
|
|
|
do_rec ()
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-10 07:15:12 -08:00
|
|
|
|
|
|
|
(* Stream building functions *)
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1997-06-16 08:33:44 -07:00
|
|
|
let from f = {count = 0; data = Sgen {curr = None; func = f}};;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let of_list l =
|
|
|
|
{count = 0; data = List.fold_right (fun x l -> Scons (x, l)) l Sempty}
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let of_string s =
|
|
|
|
from (fun c -> if c < String.length s then Some s.[c] else None)
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
|
|
|
let of_channel ic =
|
|
|
|
{count = 0;
|
|
|
|
data = Sbuffio {ic = ic; buff = String.create 4096; len = 0; ind = 0}}
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(* Stream expressions builders *)
|
|
|
|
|
2012-07-10 02:58:38 -07:00
|
|
|
(* In the slazy and lapp case, we can't statically predict the value
|
|
|
|
of the "count" field. We put a dummy 0 value, which will be updated
|
|
|
|
when the parameter stream is forced (see update code in [get_data]
|
|
|
|
and [peek]). *)
|
1997-06-16 08:33:44 -07:00
|
|
|
|
2012-07-10 02:58:38 -07:00
|
|
|
let ising i = {count = 0; data = Scons (i, Sempty)};;
|
|
|
|
let icons i s = {count = s.count - 1; data = Scons (i, s.data)};;
|
|
|
|
let iapp i s = {count = i.count; data = Sapp (i.data, s)};;
|
1997-06-16 08:33:44 -07:00
|
|
|
|
|
|
|
let sempty = {count = 0; data = Sempty};;
|
2012-07-10 02:58:38 -07:00
|
|
|
let slazy f = {count = 0; data = Slazy (lazy (f()))};;
|
|
|
|
|
|
|
|
let lsing f = {count = 0; data = Slazy (lazy (ising (f())))};;
|
|
|
|
let lcons f s = {count = 0; data = Slazy (lazy (icons (f()) s))};;
|
|
|
|
let lapp f s = {count = 0; data = Slazy (lazy(iapp (f()) s))};;
|
1995-11-09 03:04:31 -08:00
|
|
|
|
1995-11-10 07:15:12 -08:00
|
|
|
(* For debugging use *)
|
|
|
|
|
1995-11-09 03:04:31 -08:00
|
|
|
let rec dump f s =
|
1997-06-16 08:33:44 -07:00
|
|
|
print_string "{count = ";
|
|
|
|
print_int s.count;
|
|
|
|
print_string "; data = ";
|
|
|
|
dump_data f s.data;
|
|
|
|
print_string "}";
|
|
|
|
print_newline ()
|
1995-11-09 03:04:31 -08:00
|
|
|
and dump_data f =
|
|
|
|
function
|
|
|
|
Sempty -> print_string "Sempty"
|
|
|
|
| Scons (a, d) ->
|
1997-06-16 08:33:44 -07:00
|
|
|
print_string "Scons (";
|
|
|
|
f a;
|
|
|
|
print_string ", ";
|
|
|
|
dump_data f d;
|
1995-11-09 03:04:31 -08:00
|
|
|
print_string ")"
|
2012-07-10 02:58:38 -07:00
|
|
|
| Sapp (d1, s2) ->
|
1997-06-16 08:33:44 -07:00
|
|
|
print_string "Sapp (";
|
|
|
|
dump_data f d1;
|
|
|
|
print_string ", ";
|
2012-07-10 02:58:38 -07:00
|
|
|
dump f s2;
|
1997-06-16 08:33:44 -07:00
|
|
|
print_string ")"
|
2008-06-18 08:35:02 -07:00
|
|
|
| Slazy _ -> print_string "Slazy"
|
1995-12-07 11:44:19 -08:00
|
|
|
| Sgen _ -> print_string "Sgen"
|
1995-11-09 03:04:31 -08:00
|
|
|
| Sbuffio b -> print_string "Sbuffio"
|
1997-06-16 08:33:44 -07:00
|
|
|
;;
|