go(report): add table.go, table.tmpl
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-07-13 00:15:40 +02:00
parent 2d88ab9afc
commit 96a93788da
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 96 additions and 0 deletions

65
report/table.go Normal file

@ -0,0 +1,65 @@
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package report
import (
"log"
"os"
"text/template"
"time"
"git.dotya.ml/wanderer/math-optim/util"
)
type Table struct {
Algo string
Header []string
ColLayout []string
Rows []Row
}
type Row struct {
// Title of the row should contain the settings used to get the Values.
Title string
Values []float64
}
func NewTable() *Table {
return &Table{}
}
func NewRow() *Row {
return &Row{}
}
func SaveTableToFile(t Table) {
safeName := util.SanitiseFName(t.Algo)
texTableFile := GetTexDir() + "table-" + safeName + ".tex"
tmplTableFile := "report/table.tmpl"
tmplTable := template.Must(template.ParseFiles(tmplTableFile))
f, err := os.Create(texTableFile)
if err != nil {
log.Println(err)
}
defer f.Close()
err = tmplTable.Execute(f, struct {
Algo string
ColLayout []string
ColNames []string
Rs []Row
Timestamp time.Time
}{
Algo: t.Algo,
ColNames: t.Header,
ColLayout: t.ColLayout,
Rs: t.Rows,
Timestamp: time.Now(),
})
if err != nil {
log.Println(err)
}
}

31
report/table.tmpl Normal file

@ -0,0 +1,31 @@
% Code generated by math-optim; DO NOT EDIT.
%
% This file was generated by robots at
% {{ .Timestamp }}
% source: git.dotya.ml/wanderer/math-optim/report/table.tmpl
% This file is a part of the math-optim project.
% project homepage: https://git.dotya.ml/wanderer/math-optim/
\subsection{ {{- .Algo -}} }
\begin{table}[!htb]
\centering
% \begin{tabular}[t]{ |l|{{- range $i, $v := .ColLayout }}{{$v}}| {{- end}} }
\begin{tabular}[t]{ ||l||{{- range $i, $v := .ColLayout }}{{$v}}| {{- end -}}| }
\toprule
& {{ range $i, $v := .ColNames }}{{$v }} & {{ end}}\\
{{- range $j, $v := .Rs }}
{{ printf "%s & " $v.Title }}
{{- range $k, $val := $v.Values }}
{{- printf "%f & " $val }}
{{- end}}\\
{{- end }}
\bottomrule
\end{tabular}
\caption{\label{table: {{ .Algo }} algo statistics}}
\end{table}
% vim: ft=gotexttmpl ts=2 sts=2 sw=2 bs=2 expandtab