Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Base
- open Ppx_compare
- let sep_space str =
- let g (alist, astr) c =
- if ([%compare: char] c ' ') = 0 then
- if String.is_empty astr then
- (alist, astr)
- else
- (astr::alist, "")
- else
- (alist, astr^(String.of_char c)) in
- let f str = List.fold ~init:([], "") ~f:g (String.to_list str) in
- let (l, last) = f str in
- let l = if String.is_empty last then l else last::l in
- List.rev l
- let read_mat_line str =
- let ss = str
- |> (String.tr ~target:'{' ~replacement:' ')
- |> (String.tr ~target:'}' ~replacement:' ')
- |> (String.tr ~target:',' ~replacement:' ') in
- let sl = sep_space ss in
- sl |> List.map ~f:Float.of_string
- let pop_ref_list xs =
- let x = List.hd !xs in
- match x with
- | None -> None
- | Some x -> (xs := List.tl_exn !xs);Some x
- let pop_ref_list_exn xs =
- let x = List.hd_exn !xs in
- xs := List.tl_exn !xs;
- x
- type read_state = SearchingVecY | FoundVecY | Reading of (int * int) | Quit [@@deriving compare]
- let () =
- let ic = Stdio.In_channel.create "out.txt" in
- let blockstr = ref [2; 3; 1; 1] in
- let reading = ref SearchingVecY in
- let blocktemp = ref [] in
- let mattemp = ref [] in
- while compare_read_state !reading Quit <> 0 do
- let line = Stdio.In_channel.input_line ic in
- match line with
- | None -> ()
- | Some line ->
- (* Stdio.Out_channel.print_endline line; *)
- match !reading with
- | SearchingVecY ->
- if String.is_prefix ~prefix:"yMat" line then
- reading := FoundVecY
- | FoundVecY ->
- reading := Reading (pop_ref_list_exn blockstr, 0);
- | Reading (size, row) ->(
- blocktemp := (read_mat_line line)::!blocktemp;
- reading := Reading (size, row + 1);
- if row + 1 = size then(
- mattemp := (List.rev !blocktemp)::!mattemp;
- blocktemp := [];
- match pop_ref_list blockstr with
- | None -> reading := Quit
- | Some sizee -> reading := (Reading (sizee, 0));)
- )
- | Quit -> ()
- done;
- let mat = List.rev !mattemp in
- let f1 = (fun x -> print_float x;print_string ",") in
- let f2 x = (List.iter ~f:f1 x);print_newline () in
- List.iter ~f:(fun x -> List.iter ~f:f2 x;print_newline ()) mat;
- (* build: ocamlbuild main.native -pkgs base,ppx_compare,stdio -use-ocamlfind *)
Add Comment
Please, Sign In to add comment