print.h 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
#ifndef _SPELL_MODEL_PRINT_H_
#define _SPELL_MODEL_PRINT_H_

#include <iostream>
#include <iomanip>
#include "eigen.h"
#include "error.h"
#include "labelled_matrix.h"

10
11
12
13
14
15
16
17
18

inline
std::ostream& operator << (std::ostream& os, const std::vector<char>& v)
{
    for (char c: v) { os << c; }
    return os;
}


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
namespace model_print {
    extern int precision;
    extern std::string field_sep;
    extern std::string section_hsep;
    extern char section_vsep;
    extern std::string section_csep;

inline
    std::ostream&
    fill(std::ostream& os, char c, size_t width)
    {
        char f = os.fill();
        os.fill(c);
        os << std::setw(width) << "";
        os.fill(f);
        return os;
    }


template <typename T>
39
40
41
    std::string to_string(const T& x) { std::stringstream s; s << x; return s.str(); }
inline
    std::string to_string(const std::vector<char>& x) { return std::string(x.begin(), x.end()); }
42
43
44
45
46
47
48
49
50
51
inline
    std::string to_string(const std::string& s) { return s; }
inline
    std::string to_string(double x) { std::stringstream s; s << std::setprecision(precision) << x; return s.str(); }


template <typename _Printable>
    std::ostream&
    center(std::ostream& os, const _Printable& s, size_t width)
    {
52
53
        std::string str = to_string(s);
        size_t pad = width - str.size();
54
55
56
57
58
59
        size_t w1 = pad >> 1;
        size_t w2 = pad - w1;
        /*
        char f = os.fill();
        return os << std::setfill('_') << std::setw(w1) << "" << s << std::setw(w2) << "" << std::setfill(f);
        /*/
60
        return os << std::setw(w1) << "" << str << std::setw(w2) << "";
61
62
63
64
65
66
67
68
69
70
71
72
        //*/
    }


template <typename _Printable>
    std::ostream&
    right(std::ostream& os, const _Printable& s, size_t width)
    {
        /*
        char f = os.fill();
        return os << std::setfill('_') << std::setw(width) << std::right << s << std::setfill(f);
        /*/
73
        return os << std::setw(width) << std::right << to_string(s);
74
75
76
77
78
79
80
81
        //*/
    }


template <typename _Printable>
    std::ostream&
    left(std::ostream& os, const _Printable& s, size_t width)
    {
82
        return os << std::setw(width) << std::left << to_string(s);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    }


enum Align { Start, Center, End };
enum Orientation { Horizontal, Vertical };

template <typename T>
    size_t width(T x) { return to_string(x).size(); }


template <typename FieldLabel>
struct with_field_labels {
    static const bool has_field_labels = true;
    const std::vector<FieldLabel>& m_field_labels;

    with_field_labels(const std::vector<FieldLabel>& fl)
        : m_field_labels(fl)
    {}

    size_t n_fields() const { return m_field_labels.size(); }
    std::vector<size_t> field_label_widths() const
    {
        std::vector<size_t> ret;
        ret.reserve(m_field_labels.size());
        for (const FieldLabel& f: m_field_labels) {
            ret.push_back(to_string(f).size());
        }
        return ret;
    }

    const std::vector<FieldLabel>& field_labels() const { return m_field_labels; }
};

template <>
struct with_field_labels<void> {
    static const bool has_field_labels = false;
    size_t m_n_fields;

    with_field_labels(size_t n)
        : m_n_fields(n)
    {}

    size_t n_fields() const { return m_n_fields; }
    std::vector<size_t> field_label_widths() const
    {
        std::vector<size_t> ret;
        ret.resize(m_n_fields, 0);
        return ret;
    }

    std::vector<int> field_labels() const { return {}; }
};

template <typename SectionLabel>
struct with_section_label {
    static const bool has_section_label = true;
    const SectionLabel& m_label;

    with_section_label(const SectionLabel& sl) : m_label(sl) {}
    size_t section_label_width() const
    {
        size_t ret = to_string(m_label).size();
145
        /*MSG_DEBUG("label_width(" << to_string(m_label) << ") = " << ret);*/
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        return ret;
    }
};

template <>
struct with_section_label<void> {
    static const bool has_section_label = false;

    with_section_label() {}
    size_t section_label_width() const { return 0; }
};


template <typename SectionLabel, typename FieldLabel>
struct section
        : public with_section_label<SectionLabel>,
          public with_field_labels<FieldLabel> {
    section(const SectionLabel& sl, const std::vector<FieldLabel>& fl)
        : with_section_label<SectionLabel>(sl), with_field_labels<FieldLabel>(fl)
    {}
};

template <typename FieldLabel>
struct section<void, FieldLabel>
        : public with_section_label<void>,
          public with_field_labels<FieldLabel> {
    section(const std::vector<FieldLabel>& fl)
        : with_section_label<void>(), with_field_labels<FieldLabel>(fl)
    {}
};

template <typename SectionLabel>
struct section<SectionLabel, void>
        : public with_section_label<SectionLabel>,
          public with_field_labels<void> {
    section(const SectionLabel& sl, size_t nf)
        : with_section_label<SectionLabel>(sl), with_field_labels<void>(nf)
    {}
};


struct section_size {
    size_t label_length;
    size_t size;

    section_size() : label_length(0), size(0) {}
    size_t max_size() const { return size > label_length ? size : label_length; }
    size_t padding_before() const { return (max_size() - size) >> 1; }
    size_t padding_after() const { return max_size() - size - padding_before(); }
};

template <typename RS, typename RF, typename CS, typename CF>
struct matrix_with_sections {
    typedef section<RS, RF> row_section_type;
    typedef section<CS, CF> column_section_type;
    static const int has_column_field_labels = column_section_type::has_field_labels;
    static const int has_column_section_label = column_section_type::has_section_label;
    static const int has_row_field_labels = row_section_type::has_field_labels;
    static const int has_row_section_label = row_section_type::has_section_label;
    std::vector<row_section_type> m_row_sections;
    std::vector<column_section_type> m_column_sections;
    const Eigen::MatrixXd& m_matrix;

    matrix_with_sections(const Eigen::MatrixXd& m) : m_row_sections(), m_column_sections(), m_matrix(m) {}
    template <typename S, typename F>
        matrix_with_sections<RS, RF, CS, CF>&
        add_row_section(const S& s, const std::vector<F>& f) { m_row_sections.emplace_back(s, f); return *this; }
    template <typename SF>
        matrix_with_sections<RS, RF, CS, CF>&
        add_row_section(const SF& sf) { m_row_sections.emplace_back(sf); return *this; }
    template <typename SF>
        matrix_with_sections<RS, RF, CS, CF>&
        add_row_section(const SF& sf, size_t n) { m_row_sections.emplace_back(sf, n); return *this; }
    template <typename S, typename F>
        matrix_with_sections<RS, RF, CS, CF>&
        add_column_section(const S& s, const std::vector<F>& f) { m_column_sections.emplace_back(s, f); return *this; }
    template <typename SF>
        matrix_with_sections<RS, RF, CS, CF>&
        add_column_section(const SF& sf) { m_column_sections.emplace_back(sf); return *this; }
    template <typename SF>
        matrix_with_sections<RS, RF, CS, CF>&
        add_column_section(const SF& sf, size_t n) { m_column_sections.emplace_back(sf, n); return *this; }

/*private:*/

    std::vector<size_t> compute_field_widths() const
    {
        std::vector<size_t> ret;
        ret.resize(m_matrix.outerSize() + 1, 0);
        auto ri = ret.begin();
        if (has_row_field_labels) {
            for (const auto& s: m_row_sections) {
                for (const auto&f: s.field_labels()) {
                    size_t sz = to_string(f).size();
                    if (sz > *ri) {
                        *ri = sz;
                    }
                }
            }
        } else {
            *ri = 0;
        }
        ++ri;
        for (const auto& s: m_column_sections) {
            for (const auto&f: s.field_labels()) {
                *ri++ = to_string(f).size();
            }
        }
        for (int i = 0; i < m_matrix.outerSize(); ++i) {
            for (int j = 0; j < m_matrix.innerSize(); ++j) {
256
                size_t w = width(m_matrix(j, i));
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
                if (w > ret[i + 1]) {
                    ret[i + 1] = w;
                }
            }
        }
        return ret;
    }

    std::vector<section_size> compute_section_widths(const std::vector<size_t>& fwidths, size_t sep_size) const
    {
        std::vector<section_size> ret;
        ret.resize(m_column_sections.size());
        std::vector<size_t>::const_iterator wi = fwidths.begin();
        ++wi;
        for (size_t s = 0; s < m_column_sections.size(); ++s) {
            size_t nf = m_column_sections[s].n_fields();
            for (size_t i = 0; i < nf; ++i) {
                ret[s].size += *wi++;
            }
            ret[s].size += sep_size * (nf - 1);
            ret[s].label_length = m_column_sections[s].section_label_width();
        }
        return ret;
    }

    std::vector<section_size> compute_section_heights() const
    {
        std::vector<section_size> ret;
        ret.resize(m_row_sections.size());
        for (size_t s = 0; s < m_row_sections.size(); ++s) {
            ret[s].size = m_row_sections[s].n_fields();
            ret[s].label_length = m_row_sections[s].section_label_width();
        }
        return ret;
    }
};


template <typename RS, typename RF, typename CS, typename CF>
struct section_renderer {
    const matrix_with_sections<RS, RF, CS, CF>& mws;
    std::vector<size_t> fw;
    std::vector<section_size> sw, sh;
    size_t head_space;
    std::stringstream sep;

    section_renderer(const matrix_with_sections<RS, RF, CS, CF>& _)
        : mws(_)
        , fw(mws.compute_field_widths())
        , sw(mws.compute_section_widths(fw, field_sep.size()))
        , sh(mws.compute_section_heights())
        , head_space(1)
        , sep()
    {
        if (mws.has_row_field_labels) {
            head_space += field_sep.size() + fw[0];
        }

        fill(sep, section_vsep, head_space);
        auto swi = sw.begin();
        auto swj = sw.end();
        while(swi != swj) {
            sep << section_csep;
            fill(sep, section_vsep, swi->max_size());
            ++swi;
        }
        sep << std::endl;
    }

    void insert_pad_lines(std::ostream& os, const matrix_with_sections<RS, RF, CS, CF>& mws, size_t n_lines, std::string::iterator& li) const
    {
        for (size_t i = 0; i < n_lines; ++i) {
            left(os, *li++, head_space);
            auto swi = sw.begin();
            for (size_t s = 0; s < mws.m_column_sections.size(); ++s) {
                os << section_hsep;
                fill(os, ' ', swi->max_size());
                ++swi;
            }
            os << std::endl;
        }
    }

    void header(std::ostream& os) const
    {
        /* first row */
        fill(os, ' ', head_space);
        auto swi = sw.begin();
        for (const auto& s: mws.m_column_sections) {
            os << section_hsep;
            center(os, s.m_label, swi->max_size());
            ++swi;
        }
        os << std::endl;

        /* second row */
        if (mws.has_column_field_labels) {
            auto fwi = fw.begin();
            fill(os, ' ', head_space);
            swi = sw.begin();
            ++fwi;
            for (const auto& s: mws.m_column_sections) {
                os << section_hsep;
                fill(os, ' ', swi->padding_before());
                auto fi = s.field_labels().begin();
                auto fj = s.field_labels().end();
                if (fi != fj) {
                    right(os, *fi++, *fwi++);
                    while (fi != fj) {
                        os << field_sep;
                        right(os, *fi++, *fwi++);
                    }
                }
                fill(os, ' ', swi->padding_after());
371
                ++swi;
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
            }
            os << std::endl;
        }
    }

    void output_sections(std::ostream& os) const
    {
        auto shi = sh.begin();

        int row_start;
        int row_end = 0;
        int column;

        for (const auto& vs: mws.m_row_sections) {
            row_start = row_end;
            column = 0;
            int pad_before = shi->padding_before();
            int pad_after = shi->padding_after();
390
            /*MSG_DEBUG("max_size=" << shi->max_size() << " size=" << shi->size << " pad_before=" << pad_before << " pad_after=" << pad_after);*/
391
392
393
394
395
396
397
398
            row_end = row_start + shi->size;

            os << sep.str();

            /* prepare label */
            std::stringstream label;
            center(label, vs.m_label, shi->max_size());
            label << std::flush;
399
            /*MSG_DEBUG("LABEL[" << label.str() << ']');*/
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
            std::string labelstr = label.str();
            auto li = labelstr.begin();

            /* padding top */
            insert_pad_lines(os, mws, pad_before, li);

            for (int row = row_start, i = 0; row < row_end; ++row, ++i) {
                auto fwi = fw.begin();
                os << (*li++);
                if (mws.has_row_field_labels) {
                    os << field_sep;
                    right(os, vs.field_labels()[i], *fwi);
                }
                ++fwi;

                column = 0;
                auto swi = sw.begin();
                for (const auto& c: mws.m_column_sections) {
                    os << section_hsep;
                    fill(os, ' ', swi->padding_before());
                    right(os, to_string(mws.m_matrix(row, column)), *fwi++);
                    ++column;
                    for (size_t k = 1; k < c.n_fields(); ++k, ++column) {
                        os << field_sep;
                        right(os, to_string(mws.m_matrix(row, column)), *fwi++);
                    }
                    fill(os, ' ', swi->padding_after());
427
                    ++swi;
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
                }

                os << std::endl;
            }

            /* padding bottom */
            insert_pad_lines(os, mws, pad_after, li);

            ++shi;
        }
    }

    friend
        std::ostream& operator << (std::ostream& os, const section_renderer& sr)
        {
            sr.header(os);
            sr.output_sections(os);
            return os;
        }
};

449
450
} /* namespace model_print */

451
template <typename RS, typename RF, typename CS, typename CF>
452
std::ostream& operator << (std::ostream& os, const model_print::matrix_with_sections<RS, RF, CS, CF>& mws)
453
{
454
    model_print::section_renderer<RS, RF, CS, CF> sr(mws);
455
456
457
458
459
    return os << sr;
}

#endif