Guest User

Untitled

a guest
Oct 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. open Base
  2. open Ppx_compare
  3.  
  4.  
  5. let sep_space str =
  6. let g (alist, astr) c =
  7. if ([%compare: char] c ' ') = 0 then
  8. if String.is_empty astr then
  9. (alist, astr)
  10. else
  11. (astr::alist, "")
  12. else
  13. (alist, astr^(String.of_char c)) in
  14. let f str = List.fold ~init:([], "") ~f:g (String.to_list str) in
  15. let (l, last) = f str in
  16. let l = if String.is_empty last then l else last::l in
  17. List.rev l
  18.  
  19. let read_mat_line str =
  20. let ss = str
  21. |> (String.tr ~target:'{' ~replacement:' ')
  22. |> (String.tr ~target:'}' ~replacement:' ')
  23. |> (String.tr ~target:',' ~replacement:' ') in
  24. let sl = sep_space ss in
  25. sl |> List.map ~f:Float.of_string
  26.  
  27. let pop_ref_list xs =
  28. let x = List.hd !xs in
  29. match x with
  30. | None -> None
  31. | Some x -> (xs := List.tl_exn !xs);Some x
  32.  
  33. let pop_ref_list_exn xs =
  34. let x = List.hd_exn !xs in
  35. xs := List.tl_exn !xs;
  36. x
  37.  
  38.  
  39. type read_state = SearchingVecY | FoundVecY | Reading of (int * int) | Quit [@@deriving compare]
  40.  
  41. let () =
  42. let ic = Stdio.In_channel.create "out.txt" in
  43. let blockstr = ref [2; 3; 1; 1] in
  44. let reading = ref SearchingVecY in
  45. let blocktemp = ref [] in
  46. let mattemp = ref [] in
  47. while compare_read_state !reading Quit <> 0 do
  48. let line = Stdio.In_channel.input_line ic in
  49. match line with
  50. | None -> ()
  51. | Some line ->
  52. (* Stdio.Out_channel.print_endline line; *)
  53. match !reading with
  54. | SearchingVecY ->
  55. if String.is_prefix ~prefix:"yMat" line then
  56. reading := FoundVecY
  57. | FoundVecY ->
  58. reading := Reading (pop_ref_list_exn blockstr, 0);
  59. | Reading (size, row) ->(
  60. blocktemp := (read_mat_line line)::!blocktemp;
  61. reading := Reading (size, row + 1);
  62. if row + 1 = size then(
  63. mattemp := (List.rev !blocktemp)::!mattemp;
  64. blocktemp := [];
  65. match pop_ref_list blockstr with
  66. | None -> reading := Quit
  67. | Some sizee -> reading := (Reading (sizee, 0));)
  68. )
  69. | Quit -> ()
  70. done;
  71. let mat = List.rev !mattemp in
  72. let f1 = (fun x -> print_float x;print_string ",") in
  73. let f2 x = (List.iter ~f:f1 x);print_newline () in
  74. List.iter ~f:(fun x -> List.iter ~f:f2 x;print_newline ()) mat;
  75.  
  76. (* build: ocamlbuild main.native -pkgs base,ppx_compare,stdio -use-ocamlfind *)
Add Comment
Please, Sign In to add comment