Debian Codegraph @

dev/bench/table.ml

raw symbols SCIP
1 type sized_string = { str : string; size : int }
2
3 let size s = s.size
4
5 type header = sized_string
6
7 type row = sized_string list list
8
9 module Align = struct
10 type t =
11 | Left
12 | MidLeft
13 | Middle
14 | MidRight
15 | Right
16 end
17
18 let rec map3 f l1 l2 l3 =
19 match (l1, l2, l3) with
20 | ([], [], []) -> []
21 | (a1::l1, a2::l2, a3::l3) -> let r = f a1 a2 a3 in r :: map3 f l1 l2 l3
22 | (_, _, _) -> invalid_arg "Table.map3"
23
24 let val_padding = 2
25 (* Padding between data in the same row *)
26 let row_padding = 1
27 (* Padding between rows *)
28
29 let homogeneous b = if b then () else failwith "Heterogeneous data"
30
31 let vert_split (ls : 'a list list) =
32 let split l = match l with
33 | [] -> failwith "vert_split"
34 | x :: l -> (x, l)
35 in
36 let ls = CList.map split ls in
37 CList.split ls
38
39 let justify align n s =
40 let len = s.size in
41 let s = s.str in
42 let () = assert (len <= n) in
43 let pad = n - len in
44 match align with
45 | Align.Left -> s ^ String.make pad ' '
46 | Align.Right -> String.make pad ' ' ^ s
47 | Align.Middle ->
48 let pad = pad / 2 in
49 String.make pad ' ' ^ s ^ String.make (n - pad - len) ' '
50 | Align.MidLeft ->
51 let pad = pad / 5 in
52 String.make pad ' ' ^ s ^ String.make (n - pad - len) ' '
53 | Align.MidRight ->
54 let pad = pad / 5 in
55 String.make (n - pad - len) ' ' ^ s ^ String.make pad ' '
56
57 let justify_row align_row layout data =
58 let data = map3 justify align_row layout data in
59 { str = String.concat (String.make val_padding ' ') data;
60 size = List.fold_left (+) (val_padding * (List.length data - 1)) layout; }
61
62 let angle hkind vkind = match hkind, vkind with
63 | `Lft, `Top -> "┌"
64 | `Rgt, `Top -> "┐"
65 | `Mid, `Top -> "┬"
66 | `Lft, `Mid -> "├"
67 | `Rgt, `Mid -> "┤"
68 | `Mid, `Mid -> "┼"
69 | `Lft, `Bot -> "└"
70 | `Rgt, `Bot -> "┘"
71 | `Mid, `Bot -> "┴"
72
73 let print_separator vkind col_size =
74 let rec dashes n = if n = 0 then "" else "─" ^ dashes (n - 1) in
75 let len = CList.length col_size in
76 let pad = dashes row_padding in
77 let () = assert (0 < len) in
78 let map n = dashes n in
79 angle `Lft vkind ^ pad ^
80 String.concat (pad ^ angle `Mid vkind ^ pad) (CList.map map col_size) ^
81 pad ^ angle `Rgt vkind
82
83 let print_blank col_size =
84 let len = CList.length col_size in
85 let () = assert (0 < len) in
86 let pad = String.make row_padding ' ' in
87 let map n = String.make n ' ' in
88 "│" ^ pad ^ String.concat (pad ^ "│" ^ pad) (CList.map map col_size) ^ pad ^ "│"
89
90 let print_row row =
91 let len = CList.length row in
92 let () = assert (0 < len) in
93 let pad = String.make row_padding ' ' in
94 "│" ^ pad ^ String.concat (pad ^ "│" ^ pad) row ^ pad ^ "│"
95
96 let default_align_headers = CList.map (fun _ -> Align.Middle)
97 let default_align_top = CList.map @@ CList.map (fun _ -> Align.Middle)
98 let default_align_rows rows =
99 CList.hd rows
100 |> CList.map @@ CList.map (fun _ -> Align.Right)
101
102 (* Invariant : all rows must have the same shape *)
103
104 let print (headers : header list) (top : row) (rows : row list)
105 ?(align_headers = default_align_headers headers)
106 ?(align_top = default_align_top top)
107 ?(align_rows = default_align_rows rows)
108 () =
109 (* Sanitize input *)
110 let ncolums = CList.length headers in
111 let shape = ref None in
112 let check row =
113 let () = homogeneous (CList.length row = ncolums) in
114 let rshape : int list = CList.map (fun data -> CList.length data) row in
115 match !shape with
116 | None -> shape := Some rshape
117 | Some s -> homogeneous (rshape = s)
118 in
119 let () = CList.iter check rows in
120 (* TODO: check is broken please fix *)
121 (* let () = CList.iter check (CList.map (CList.map (fun _ -> [])) align_rows) in *)
122 let () = homogeneous (CList.length align_headers = ncolums) in
123 (* Compute layout *)
124 let rec layout n (rows : row list) =
125 if n = 0 then []
126 else
127 let (col, rows) = vert_split rows in
128 let ans = layout (n - 1) rows in
129 let data = ref None in
130 let iter args =
131 let size = CList.map size args in
132 match !data with
133 | None -> data := Some size
134 | Some s ->
135 data := Some (CList.map2 (fun len1 len2 -> max len1 len2) s size)
136 in
137 let () = CList.iter iter col in
138 let data = match !data with None -> [] | Some s -> s in
139 data :: ans
140 in
141 let layout = layout ncolums (top::rows) in
142 let map hd shape =
143 let data_size = match shape with
144 | [] -> 0
145 | n :: shape -> CList.fold_left (fun accu n -> accu + n + val_padding) n shape
146 in
147 max (size hd) data_size
148 in
149 let col_size = CList.map2 map headers layout in
150 (* Justify the data *)
151 let headers = map3 justify align_headers col_size headers in
152 let top = CList.map2 (justify Align.Middle) col_size (map3 justify_row align_top layout top) in
153
154 let rows = CList.map (fun row -> CList.map2 (justify Align.Right) col_size (map3 justify_row align_rows layout row)) rows in
155 (* Print the table *)
156 let lines =
157 print_separator `Top col_size ::
158 print_row headers ::
159 print_blank col_size ::
160 print_row top ::
161 print_separator `Mid col_size ::
162 CList.map print_row rows @
163 print_separator `Bot col_size ::
164 []
165 in
166 String.concat "\n" lines
167
168 type raw_header = string
169 type raw_row = string list list
170
171 let raw_str s = { str = s; size = String.length s }
172
173 let raw_row r : row = List.map (List.map raw_str) r
174
175 let raw_print (headers : raw_header list) (top : raw_row) (rows : raw_row list)
176 ?align_headers
177 ?align_top
178 ?align_rows
179 () =
180 let headers = List.map raw_str headers in
181 let top = raw_row top in
182 let rows = List.map raw_row rows in
183 print headers top rows ?align_headers ?align_top ?align_rows ()