10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-12-22 12:23:31 +01:00

Improved reading basis

This commit is contained in:
Anthony Scemama 2022-11-07 11:24:20 +01:00 committed by Pierre-Francois Loos
parent bdece8ea60
commit a692242b51
2 changed files with 27 additions and 15 deletions

View File

@ -20,7 +20,7 @@ exception No_shell
exception Malformed_shell of string exception Malformed_shell of string
let read_shell line_stream = let read_shell ?element line_stream =
try try
let shell, n = let shell, n =
@ -40,9 +40,15 @@ let read_shell line_stream =
let line = Stream.next line_stream in let line = Stream.next line_stream in
try Scanf.sscanf line " %_d %f %f " try Scanf.sscanf line " %_d %f %f "
(fun exponent coefficient -> { exponent ; coefficient }) (fun exponent coefficient -> { exponent ; coefficient })
with _ -> raise (Malformed_shell (Printf.sprintf with _ -> raise (Malformed_shell
"Expected %d %c contractions, error at contraction %d:\n%s" (match element with
n shell (n-i+1) line)) | Some element -> Printf.sprintf
"In %s: Expected %d %c contractions.\nError at contraction %d:\n%s"
(Element.to_string element) n shell (n-i+1) line
| None -> Printf.sprintf
"Expected %d %c contractions.\nError at contraction %d:\n%s"
n shell (n-i+1) line
))
in in
contraction :: loop (pred i) contraction :: loop (pred i)
in in
@ -54,20 +60,26 @@ let read_shell line_stream =
let read_element line_stream = let rec read_element line_stream =
try try
let line = Stream.next line_stream in let line = Stream.next line_stream in
let element = if String.length line = 0 || line.[0] = '!' then
Scanf.sscanf line " %s " Element.of_string read_element line_stream
in else
let element =
try
Scanf.sscanf line " %s " Element.of_string
with
| Element.ElementError _ -> Element.X
in
let rec loop () = let rec loop () =
match read_shell line_stream with match read_shell line_stream ~element with
| Some shell -> shell :: loop () | Some shell -> shell :: loop ()
| None -> [] | None -> []
in in
Some (element, Array.of_list (loop ()) ) Some (element, Array.of_list (loop ()) )
with with
| Stream.Failure -> None | Stream.Failure -> None

View File

@ -100,7 +100,7 @@ Some
*) *)
val read_shell : string Stream.t -> general_contracted_shell option val read_shell : ?element:Element.t -> string Stream.t -> general_contracted_shell option
(** Reads a shell from the input [string Stream]. The [string Stream] is a (** Reads a shell from the input [string Stream]. The [string Stream] is a
stream of lines, like a text file split on the end-of-line character. stream of lines, like a text file split on the end-of-line character.
For example, For example,