Commits

Yaron Minsky committed 3a9dcb8

simplified sexp format

Comments (0)

Files changed (3)

   in
 *)
   let iet =
-    Iet.create_simple
+    Iet.create
       { top = [0;1;2]; bot = [1;0;2] }
       ~widths:[1;2;1]
   in
 
 type strand_info =
   { branch : Branch.t
-  ; this   :  attachment
+  ; this   : attachment
   ; other  : attachment
   }
 with sexp
   && side = att_side
 
 type annotated_branch =
-  { start: Strand.t
-  ; branch: Branch.t
-  ; width: int
-  ; side: Side.t
+  { start  : Strand.t
+  ; branch : Branch.t
+  ; width  : int
+  ; side   : Side.t
   }
 with sexp
 
+module Format = struct
+  type t = { branches : Branch.t list Side_pair.t
+           ; widths : int array
+           }
+  with sexp
+end
+
 type t = { branch_by_strand : Branch.t array Side_pair.t
          ; attachments_by_branch : (attachment * attachment) array
+         ; format : Format.t
          }
 with sexp
 
         | Some x -> x)
 
 
-let create branches ~widths =
+let of_format (format:Format.t) =
+  let branches = format.branches in
+  let widths =
+    Array.foldi format.widths
+      ~init:Branch.Map.empty
+      ~f:(fun idx acc width ->
+          Map.add acc ~key:(Branch.of_int idx) ~data:width)
+  in
   let annotated_branches =
     let of_side side = annotate_branches branches ~widths side in
     of_side Top @ of_side Bot
         map_to_array map (module Strand) "strand")
   in
   let t = { branch_by_strand
-          ; attachments_by_branch }
+          ; attachments_by_branch
+          ; format }
   in
   let top_strands = Array.length branch_by_strand.top in
   let bot_strands = Array.length branch_by_strand.bot in
   t
     
 
-let create_simple branches ~widths =
+let diagnostic_sexp = sexp_of_t
+
+let sexp_of_t t = Format.sexp_of_t t.format
+let t_of_sexp sexp = 
+  let format = Format.t_of_sexp sexp in
+  try of_format format
+  with exn -> of_sexp_error_exn exn sexp
+
+let create branches ~widths =
   let branches = Side_pair.map branches ~f:(List.map ~f:Branch.of_int) in
-  let widths =
-    List.foldi widths ~init:Branch.Map.empty ~f:(fun idx map width ->
-        let branch = Branch.of_int idx in
-        Map.add map ~key:branch ~data:width)
-  in
-  create branches ~widths
+  let widths = Array.of_list widths in
+  of_format { branches; widths }
 
 (* Given a strand and a side in an IET, find the strand/side pair
    that it is connected to by the branch in question *)
 (** {2 Creation functions} *)
 
 val create
-  :  Branch.t list Side_pair.t
-  -> widths : int Branch.Map.t
-  -> t
-
-val create_simple
   :  int list Side_pair.t
   -> widths:int list
   -> t
 
 (** {2 Accessors} *)
 
+val diagnostic_sexp : t -> Sexp.t
+
 val num_strands : t -> int
 
 type attachment =