<commit_before>(* Sized Linear Algebra Package (SLAP)

   Copyright (C) 2013- <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

open Format

let default_ellipsis = ref "..."

let default_max_rows, default_max_cols =
  let v = if !Sys.interactive then Some 6 else None in
  ref v, ref v

let set_max n =
  if n < 3 then invalid_arg "Slap.Io.set_max: too small max size";
  default_max_rows := Some n;
  default_max_cols := Some n

(** {2 Pretty printers} *)

type index =
  | Head
  | Foot
  | Ellipsis
  | Cell of int

let default_pp_open ppf = pp_open_box ppf 0
let default_pp_close ppf = pp_close_box ppf ()
let default_pp_end_row ppf _ = pp_force_newline ppf ()
let default_pp_end_col ppf ~row:_ ~col:_ = pp_print_string ppf " "
let default_pad = ' '

let make_index_array ?pp_head ?pp_foot max_n n =
  let idx = match max_n with
    | Some max_n when n > max_n && max_n > 1 -> (* omit columns or rows *)
       let side_n = max_n / 2 in
       let offset = n - 2 * side_n in
       Array.init (2 * side_n + 1)
                  (fun i -> if i < side_n then Cell (i + 1)
                            else if i = side_n then Ellipsis
                            else Cell (i + offset))
    | _ -> Array.init n (fun i -> Cell (i + 1)) (* show all columns or rows *)
  in
  let add_if_some pp_print f idx =
    match pp_print with Some pp_print -> f pp_print idx | None -> idx in
  idx
  |> add_if_some pp_head (fun pp_head idx -> Array.append [|Head|] idx)
  |> add_if_some pp_foot (fun pp_foot idx -> Array.append idx [|Foot|])

let pp_dummy ppf = assert(false)
(* This is a dummy pretty printer. This is never called. *)

let stringize_table ?(pp_head = pp_dummy)
                    ?(pp_foot = pp_dummy)
                    ?(pp_left = pp_dummy)
                    ?(pp_right = pp_dummy)
                    ~ellipsis ~idx_m ~idx_n m n pp_el =
  let buf = Buffer.create 32 in
  let ppf_buf = formatter_of_buffer buf in
  let stringize_pp pp =
    Buffer.clear buf;
    pp ppf_buf;
    pp_print_flush ppf_buf ();
    Buffer.contents buf
  in
  let stringize_cell i = function
    | Head     -> stringize_pp (fun ppf -> pp_left ppf i)
    | Foot     -> stringize_pp (fun ppf -> pp_right ppf i)
    | Cell j   -> stringize_pp (fun ppf -> pp_el ppf i j)
    | Ellipsis -> ellipsis
  in
  let stringize_head_foot pp = function
    | Head     -> ""
    | Foot     -> ""
    | Cell j   -> stringize_pp (fun ppf -> pp ppf j)
    | Ellipsis -> ellipsis
  in
  let stringize_row = function
    | Head     -> Array.map (stringize_head_foot pp_head) idx_n
    | Foot     -> Array.map (stringize_head_foot pp_foot) idx_n
    | Cell i   -> Array.map (stringize_cell i) idx_n
    | Ellipsis -> Array.make (Array.length idx_n) ellipsis
  in
  Array.map stringize_row idx_m

let calc_column_width str_tbl =
  let w = Array.make (Array.length str_tbl.(0)) 0 in
  let update = Array.iteri (fun j el -> w.(j)<- max w.(j) (String.length el)) in
  Array.iter update str_tbl;
  w

let pp_print_table ppf ~pp_end_row ~pp_end_col ~pad
                   ~idx_m ~idx_n width str_tbl =
  let pp_print_row i row =
    Array.iteri (fun j col ->
                 let s = str_tbl.(i).(j) in
                 let d = width.(j) - String.length s in
                 pp_print_string ppf (String.make d pad);
                 pp_print_string ppf s;
                 pp_end_col ppf ~row ~col)
                idx_n;
    pp_end_row ppf row
  in
  Array.iteri pp_print_row idx_m

let pp_table ?(pp_open = default_pp_open)
             ?(pp_close = default_pp_close)
             ?pp_head
             ?pp_foot
             ?(pp_end_row = default_pp_end_row)
             ?(pp_end_col = default_pp_end_col)
             ?pp_left
             ?pp_right
             ?(pad = default_pad)
             ?(ellipsis = !default_ellipsis)
             ?(max_rows = !default_max_rows)
             ?(max_cols = !default_max_cols)
             ppf pp_el m n get_el =
  if m > 0 && n > 0 then
    begin
      let pp_el ppf i j = pp_el ppf (get_el i j) in
      let idx_m = make_index_array ?pp_head:pp_head ?pp_foot:pp_foot<commit_msg>Fix `Slap.Io.pp_print_table' for compatibility with Lacaml<commit_after>(* Sized Linear Algebra Package (SLAP)

   Copyright (C) 2013- <NAME> <<EMAIL>>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)

open Format

let default_ellipsis = ref "..."

let default_max_rows, default_max_cols =
  let v = if !Sys.interactive then Some 6 else None in
  ref v, ref v

let set_max n =
  if n < 3 then invalid_arg "Slap.Io.set_max: too small max size";
  default_max_rows := Some n;
  default_max_cols := Some n

(** {2 Pretty printers} *)

type index =
  | Head
  | Foot
  | Ellipsis
  | Cell of int

let default_pp_open ppf = pp_open_box ppf 0
let default_pp_close ppf = pp_close_box ppf ()
let default_pp_end_row ppf _ = pp_force_newline ppf ()
let default_pp_end_col ppf ~row:_ ~col:_ = pp_print_string ppf " "
let default_pad = ' '

let make_index_array ?pp_head ?pp_foot max_n n =
  let idx = match max_n with
    | Some max_n when n > max_n && max_n > 1 -> (* omit columns or rows *)
       let side_n = max_n / 2 in
       let offset = n - 2 * side_n in
       Array.init (2 * side_n + 1)
                  (fun i -> if i < side_n then Cell (i + 1)
                            else if i = side_n then Ellipsis
                            else Cell (i + offset))
    | _ -> Array.init n (fun i -> Cell (i + 1)) (* show all columns or rows *)
  in
  let add_if_some pp_print f idx =
    match pp_print with Some pp_print -> f pp_print idx | None -> idx in
  idx
  |> add_if_some pp_head (fun pp_head idx -> Array.append [|Head|] idx)
  |> add_if_some pp_foot (fun pp_foot idx -> Array.append idx [|Foot|])

let pp_dummy ppf = assert(false)
(* This is a dummy pretty printer. This is never called. *)

let stringize_table ?(pp_head = pp_dummy)
                    ?(pp_foot = pp_dummy)
                    ?(pp_left = pp_dummy)
                    ?(pp_right = pp_dummy)
                    ~ellipsis ~idx_m ~idx_n m n pp_el =
  let buf = Buffer.create 32 in
  let ppf_buf = formatter_of_buffer buf in
  let stringize_pp pp =
    Buffer.clear buf;
    pp ppf_buf;
    pp_print_flush ppf_buf ();
    Buffer.contents buf
  in
  let stringize_cell i = function
    | Head     -> stringize_pp (fun ppf -> pp_left ppf i)
    | Foot     -> stringize_pp (fun ppf -> pp_right ppf i)
    | Cell j   -> stringize_pp (fun ppf -> pp_el ppf i j)
    | Ellipsis -> ellipsis
  in
  let stringize_head_foot pp = function
    | Head     -> ""
    | Foot     -> ""
    | Cell j   -> stringize_pp (fun ppf -> pp ppf j)
    | Ellipsis -> ellipsis
  in
  let stringize_row = function
    | Head     -> Array.map (stringize_head_foot pp_head) idx_n
    | Foot     -> Array.map (stringize_head_foot pp_foot) idx_n
    | Cell i   -> Array.map (stringize_cell i) idx_n
    | Ellipsis -> Array.make (Array.length idx_n) ellipsis
  in
  Array.map stringize_row idx_m

let calc_column_width str_tbl =
  let w = Array.make (Array.length str_tbl.(0)) 0 in
  let update = Array.iteri (fun j el -> w.(j)<- max w.(j) (String.length el)) in
  Array.iter update str_tbl;
  w

let pp_print_table ppf ~pp_end_row ~pp_end_col ~pad
                   ~idx_m ~idx_n width str_tbl =
  let last_row = idx_m.(Array.length idx_m - 1) in
  let pp_print_row i row =
    Array.iteri (fun j col ->
                 let s = str_tbl.(i).(j) in
                 let d = width.(j) - String.length s in
                 pp_print_string ppf (String.make d pad);
                 pp_print_string ppf s;
                 pp_end_col ppf ~row ~col)
                idx_n;
    if last_row <> row then pp_end_row ppf row
  in
  Array.iteri pp_print_row idx_m

let pp_table ?(pp_open = default_pp_open)
             ?(pp_close = default_pp_close)
             ?pp_head
             ?pp_foot
             ?(pp_end_row = default_pp_end_row)
             ?(pp_end_col = default_pp_end_col)
             ?pp_left
             ?pp_right
             ?(pad = default_pad)
             ?(ellipsis = !default_ellipsis)
             ?(max_rows = !default_max_rows)
             ?(max_cols = !default_max_cols)
             ppf pp_el m n get_el =
  if m > 0 && n > 0 then
    begin
      let pp_el ppf i j = pp_el ppf (get_el i j) in
      let idx_m = make_index_array ?pp_head:pp_head ?pp_foot:pp_foot
{'fraction_non_alphanumeric': 0.08352237574757682, 'fraction_numerical': 0.007527325221695195, 'mean_word_length': 2.28334461746784, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3460705', 'n_tokens_mistral': 3340, 'n_tokens_neox': 3245, 'n_words': 1128}