File: table_handler.cc

package info (click to toggle)
deal.ii 9.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,876 kB
  • sloc: cpp: 265,739; ansic: 52,054; python: 1,507; perl: 645; sh: 506; xml: 437; makefile: 73
file content (792 lines) | stat: -rw-r--r-- 22,610 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
145
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
256
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// ---------------------------------------------------------------------
//
// Copyright (C) 1999 - 2017 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, 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.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------

#include <deal.II/base/table_handler.h>
#include <deal.II/base/table.h>

#include <boost/io/ios_state.hpp>

#include <sstream>
#include <iostream>
#include <iomanip>


DEAL_II_NAMESPACE_OPEN


/*---------------------------------------------------------------------*/

// inline and template functions
namespace internal
{
  double TableEntry::get_numeric_value () const
  {
    // we don't quite know the data type in 'value', but
    // it must be one of the ones in the type list of the
    // boost::variant. Go through this list and return
    // the value if this happens to be a number
    //
    // first try with int
    try
      {
        return boost::get<int>(value);
      }
    catch (...)
      {}


    // ... then with unsigned int...
    try
      {
        return boost::get<unsigned int>(value);
      }
    catch (...)
      {}

    // ... then with unsigned long long int...
    try
      {
        return boost::get<unsigned long long int>(value);
      }
    catch (...)
      {}

    // ...and finally with double precision:
    try
      {
        return boost::get<double>(value);
      }
    catch (...)
      {
        Assert (false, ExcMessage ("The number stored by this element of the "
                                   "table is not a number."))
      }

    return 0;
  }

  void TableEntry::cache_string(bool scientific, unsigned int precision) const
  {
    std::ostringstream ss;

    ss << std::setprecision(precision);

    if (scientific)
      ss.setf(std::ios::scientific, std::ios::floatfield);
    else
      ss.setf(std::ios::fixed, std::ios::floatfield);

    ss << value;

    cached_value = ss.str();
    if (cached_value.size()==0)
      cached_value = "\"\"";
  }

  const std::string &TableEntry::get_cached_string() const
  {
    return cached_value;
  }


  namespace Local
  {
    // see which type we can cast to, then use this type to create
    // a default constructed object
    struct GetDefaultValue : public boost::static_visitor<>
    {
      template <typename T>
      void operator()( T &operand ) const
      {
        operand = T();
      }
    };
  }

  TableEntry TableEntry::get_default_constructed_copy () const
  {
    TableEntry new_entry = *this;
    boost::apply_visitor(Local::GetDefaultValue(), new_entry.value);

    return new_entry;
  }


}

/* ------------------------------------------------ */

TableHandler::Column::Column(const std::string &tex_caption)
  :
  tex_caption(tex_caption),
  tex_format("c"),
  precision(4),
  scientific(false),
  flag(0),
  max_length(0)
{}



TableHandler::Column::Column()
  :
  tex_caption(),
  tex_format("c"),
  precision(4),
  scientific(false),
  flag(0),
  max_length(0)
{}



void
TableHandler::Column::pad_column_below (const unsigned int size)
{
  // we should never have a column that is completely
  // empty and that needs to be padded
  Assert (entries.size() > 0, ExcInternalError());

  // add as many elements as necessary
  while (entries.size() < size)
    {
      entries.push_back (entries.back().get_default_constructed_copy());
      internal::TableEntry &entry = entries.back();
      entry.cache_string(scientific, precision);
      max_length = std::max(max_length, static_cast<unsigned int>(entry.get_cached_string().length()));
    }
}


void
TableHandler::Column::invalidate_cache()
{
  max_length = 0;

  for (std::vector<dealii::internal::TableEntry>::iterator it=entries.begin(); it!=entries.end(); ++it)
    {
      it->cache_string(this->scientific, this->precision);
      max_length = std::max(max_length, static_cast<unsigned int>(it->get_cached_string().length()));
    }
}


/*---------------------------------------------------------------------*/


TableHandler::TableHandler()
  :
  auto_fill_mode (false)
{}



void TableHandler::declare_column (const std::string &key)
{
  // see if the column already exists; insert it if not
  Assert (columns.find(key) == columns.end(),
          ExcMessage ("You are trying to declare a column with key <" + key +
                      "> but such a column already exists."));

  columns.insert(std::make_pair(key, Column(key)));
  column_order.push_back(key);
}



void TableHandler::start_new_row ()
{
  // figure out the longest current column
  unsigned int max_col_length = 0;
  for (std::map< std::string, Column >::iterator p = columns.begin(); p != columns.end(); ++p)
    max_col_length = std::max(max_col_length,
                              static_cast<unsigned int>(p->second.entries.size()));


  // then pad all columns to that length with empty strings
  for (std::map<std::string,Column>::iterator col=columns.begin(); col!=columns.end(); ++col)
    while (col->second.entries.size() < max_col_length)
      {
        col->second.entries.emplace_back("");
        internal::TableEntry &entry = col->second.entries.back();
        entry.cache_string(col->second.scientific, col->second.precision);
        col->second.max_length = std::max(col->second.max_length,
                                          static_cast<unsigned int>(entry.get_cached_string().length()));
      }
}



void
TableHandler::set_auto_fill_mode (const bool state)
{
  auto_fill_mode = state;
}


void TableHandler::add_column_to_supercolumn (const std::string &key,
                                              const std::string &superkey)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));

  if (!supercolumns.count(superkey))
    {
      std::pair<std::string, std::vector<std::string> >
      new_column(superkey, std::vector<std::string>());
      supercolumns.insert(new_column);
      // replace key in column_order
      // by superkey
      for (unsigned int j=0; j<column_order.size(); ++j)
        if (column_order[j]==key)
          {
            column_order[j]=superkey;
            break;
          }
    }
  else
    {
      // remove key from column_order
      // for erase we need an iterator
      for (std::vector<std::string>::iterator order_iter=column_order.begin();
           order_iter!=column_order.end(); ++order_iter)
        if (*order_iter==key)
          {
            column_order.erase(order_iter);
            break;
          }
    }

  if (supercolumns.count(superkey))
    {
      supercolumns[superkey].push_back(key);
      // By default set the
      // tex_supercaption to superkey
      std::pair<std::string, std::string> new_tex_supercaption(superkey, superkey);
      tex_supercaptions.insert(new_tex_supercaption);
    }
  else
    Assert(false, ExcInternalError());
}



void TableHandler::set_column_order (const std::vector<std::string> &new_order)
{
  for (unsigned int j=0; j<new_order.size(); ++j)
    Assert(supercolumns.count(new_order[j]) || columns.count(new_order[j]),
           ExcColumnOrSuperColumnNotExistent(new_order[j]));

  column_order=new_order;
}


void TableHandler::set_tex_caption (const std::string &key,
                                    const std::string &tex_caption)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));
  columns[key].tex_caption=tex_caption;
}



void TableHandler::set_tex_table_caption (const std::string &table_caption)
{
  tex_table_caption=table_caption;
}



void TableHandler::set_tex_table_label (const std::string &table_label)
{
  tex_table_label=table_label;
}



void TableHandler::set_tex_supercaption (const std::string &superkey,
                                         const std::string &tex_supercaption)
{
  Assert(supercolumns.count(superkey), ExcSuperColumnNotExistent(superkey));
  Assert(tex_supercaptions.count(superkey), ExcInternalError());
  tex_supercaptions[superkey]=tex_supercaption;
}



void TableHandler::set_tex_format (const std::string &key,
                                   const std::string &tex_format)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));
  Assert(tex_format=="l" || tex_format=="c" || tex_format=="r",
         ExcUndefinedTexFormat(tex_format));
  columns[key].tex_format=tex_format;
}



void TableHandler::set_precision (const std::string &key,
                                  const unsigned int precision)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));
  if (columns[key].precision!=precision)
    {
      columns[key].precision = precision;
      columns[key].invalidate_cache();
    }
}


void TableHandler::set_scientific (const std::string &key,
                                   const bool scientific)
{
  Assert(columns.count(key), ExcColumnNotExistent(key));
  if (columns[key].scientific!=scientific)
    {
      columns[key].scientific = scientific;
      columns[key].invalidate_cache();
    }
}


void TableHandler::write_text(std::ostream &out,
                              const TextOutputFormat format) const
{
  AssertThrow (out, ExcIO());
  boost::io::ios_flags_saver restore_flags(out);

  // first pad the table from below if necessary
  if (auto_fill_mode == true)
    {
      unsigned int max_rows = 0;
      for (std::map<std::string, Column>::const_iterator p = columns.begin();
           p != columns.end(); ++p)
        max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());

      for (std::map<std::string, Column>::iterator p = columns.begin();
           p != columns.end(); ++p)
        p->second.pad_column_below (max_rows);
    }

  std::vector<std::string> sel_columns;
  get_selected_columns(sel_columns);

  const unsigned int nrows  = n_rows();
  const unsigned int n_cols = sel_columns.size();

  // cache the columns and compute the widths of each column for alignment
  std::vector<const Column *> cols;
  std::vector<unsigned int> column_widths (n_cols, 0);
  for (unsigned int j=0; j<n_cols; ++j)
    {
      std::string key=sel_columns[j];
      const std::map<std::string, Column>::const_iterator
      col_iter=columns.find(key);
      Assert(col_iter!=columns.end(), ExcInternalError());
      cols.push_back(&(col_iter->second));

      column_widths[j] = col_iter->second.max_length;
    }

  switch (format)
    {
    case org_mode_table:
    {
      // write the captions
      out << "| " << std::left;
      for (unsigned int j=0; j<n_cols; ++j)
        {
          const std::string &key = sel_columns[j];
          column_widths[j] = std::max(column_widths[j],
                                      (unsigned int)key.length());
          out << std::setw(column_widths[j]);
          out << key << " | ";
        }
      out << std::endl;

      // write the body
      for (unsigned int i=0; i<nrows; ++i)
        {
          out << "| ";
          for (unsigned int j=0; j<n_cols; ++j)
            {
              const Column &column=*(cols[j]);

              out << std::setw(column_widths[j]);
              out << column.entries[i].get_cached_string();
              out << " | ";
            }
          out << '\n';
        }

      out << std::flush;
      return;
    }

    case simple_table_with_separate_column_description:
    {
      // write the captions
      for (unsigned int j=0; j<n_cols; ++j)
        {
          const std::string &key = sel_columns[j];
          out << "# " << j+1 << ": " << key << '\n';
        }

      // write the body
      for (unsigned int i=0; i<nrows; ++i)
        {
          for (unsigned int j=0; j<n_cols; ++j)
            {
              const Column &column=*(cols[j]);

              out << column.entries[i].get_cached_string();
              out << ' ';
            }
          out << '\n';
        }

      out << std::flush;
      return;
    }

    case table_with_separate_column_description:
    {
      // writing the captions for table_with_separate_column_description
      // means that we ignore supercolumns and output the column
      // header for each column. enumerate columns starting with 1
      for (unsigned int j=0; j<n_cols; ++j)
        {
          std::string key=sel_columns[j];
          out << "# " << j+1 << ": " << key << '\n';
        }
      break;
    }

    case table_with_headers:
    {
      // This format output supercolumn headers and aligns them centered
      // over all the columns that belong to it.
      for (unsigned int j=0; j<column_order.size(); ++j)
        {
          const std::string &key = column_order[j];
          unsigned int width=0;
          {
            // compute the width of this column or supercolumn
            const std::map<std::string, std::vector<std::string> >::const_iterator
            super_iter=supercolumns.find(key);
            if (super_iter!=supercolumns.end())
              {
                const unsigned int n_subcolumns=super_iter->second.size();
                for (unsigned int k=0; k<n_subcolumns; ++k)
                  {
                    const std::map<std::string, Column>::const_iterator
                    col_iter=columns.find(super_iter->second[k]);
                    Assert(col_iter!=columns.end(), ExcInternalError());

                    width += col_iter->second.max_length;
                  }
                width += n_subcolumns - 1; // separators between subcolumns
              }
            else
              {
                const std::map<std::string, Column>::const_iterator
                col_iter=columns.find(key);

                width = col_iter->second.max_length;
              }
          }

          // header is longer than the column(s) under it
          if (width<key.length())
            {
              // make the column or the last column in this
              // supercolumn wide enough
              std::string colname;

              const std::map<std::string, std::vector<std::string> >::const_iterator
              super_iter=supercolumns.find(key);
              if (super_iter!=supercolumns.end())
                colname = super_iter->second.back();
              else
                colname = key;

              // find column and change output width
              for (unsigned int i=0; i<n_cols; ++i)
                {
                  if (sel_columns[i]==colname)
                    {
                      column_widths[i] += key.length() - width;
                      break;
                    }
                }

              width=key.length();
            }

          // now write key. try to center it somehow
          const unsigned int front_padding = (width-key.length())/2,
                             rear_padding  = (width-key.length()) -
                                             front_padding;
          for (unsigned int i=0; i<front_padding; ++i)
            out << ' ';
          out << key;
          for (unsigned int i=0; i<rear_padding; ++i)
            out << ' ';

          out << ' ';
        }
      out << '\n';
      break;
    }

    default:
      Assert (false, ExcInternalError());
    }


  // finally output the data itself for
  // table_with_headers or table_with_separate_column_description:
  for (unsigned int i=0; i<nrows; ++i)
    {
      for (unsigned int j=0; j<n_cols; ++j)
        {
          const Column &column=*(cols[j]);
          out << std::setw(column_widths[j]);
          out << column.entries[i].get_cached_string();

          // pad after this column
          out << ' ';
        }
      out << '\n';
    }
  out << std::flush;
}


void TableHandler::write_tex (std::ostream &out, const bool with_header) const
{
  //TODO[TH]: update code similar to
  //write_text() to use the cache
  AssertThrow (out, ExcIO());
  if (with_header)
    out << "\\documentclass[10pt]{report}" << std::endl
        << "\\usepackage{float}" << std::endl << std::endl << std::endl
        << "\\begin{document}" << std::endl;

  out << "\\begin{table}[H]" << std::endl
      << "\\begin{center}" << std::endl
      << "\\begin{tabular}{|";

  // first pad the table from below if necessary
  if (auto_fill_mode == true)
    {
      unsigned int max_rows = 0;
      for (std::map<std::string, Column>::const_iterator p = columns.begin();
           p != columns.end(); ++p)
        max_rows = std::max<unsigned int>(max_rows, p->second.entries.size());

      for (std::map<std::string, Column>::iterator p = columns.begin();
           p != columns.end(); ++p)
        p->second.pad_column_below (max_rows);
    }

  std::vector<std::string> sel_columns;
  get_selected_columns(sel_columns);

  // write the column formats
  for (unsigned int j=0; j<column_order.size(); ++j)
    {
      std::string key=column_order[j];
      // avoid `supercolumns[key]'
      const std::map<std::string, std::vector<std::string> >::const_iterator
      super_iter=supercolumns.find(key);

      if (super_iter!=supercolumns.end())
        {
          const unsigned int n_subcolumns=super_iter->second.size();
          for (unsigned int k=0; k<n_subcolumns; ++k)
            {
              // avoid `columns[supercolumns[key]]'
              const std::map<std::string, Column>::const_iterator
              col_iter=columns.find(super_iter->second[k]);
              Assert(col_iter!=columns.end(), ExcInternalError());

              out << col_iter->second.tex_format << "|";
            }
        }
      else
        {
          // avoid `columns[key]';
          const std::map<std::string, Column>::const_iterator
          col_iter=columns.find(key);
          Assert(col_iter!=columns.end(), ExcInternalError());
          out << col_iter->second.tex_format << "|";
        }
    }
  out << "} \\hline" << std::endl;

  // write the caption line of the table

  for (unsigned int j=0; j<column_order.size(); ++j)
    {
      std::string key=column_order[j];
      const std::map<std::string, std::vector<std::string> >::const_iterator
      super_iter=supercolumns.find(key);

      if (super_iter!=supercolumns.end())
        {
          const unsigned int n_subcolumns=super_iter->second.size();
          // avoid use of `tex_supercaptions[key]'
          std::map<std::string,std::string>::const_iterator
          tex_super_cap_iter=tex_supercaptions.find(key);
          out << std::endl << "\\multicolumn{" << n_subcolumns << "}{|c|}{"
              << tex_super_cap_iter->second << "}";
        }
      else
        {
          // col_iter->second=columns[col];
          const std::map<std::string, Column>::const_iterator
          col_iter=columns.find(key);
          Assert(col_iter!=columns.end(), ExcInternalError());
          out << col_iter->second.tex_caption;
        }
      if (j<column_order.size()-1)
        out << " & ";
    }
  out << "\\\\ \\hline" << std::endl;

  // write the n rows
  const unsigned int nrows=n_rows();
  for (unsigned int i=0; i<nrows; ++i)
    {
      const unsigned int n_cols=sel_columns.size();

      for (unsigned int j=0; j<n_cols; ++j)
        {
          std::string key=sel_columns[j];
          // avoid `column[key]'
          const std::map<std::string, Column>::const_iterator
          col_iter=columns.find(key);
          Assert(col_iter!=columns.end(), ExcInternalError());

          const Column &column=col_iter->second;

          out << std::setprecision(column.precision);

          if (col_iter->second.scientific)
            out.setf(std::ios::scientific, std::ios::floatfield);
          else
            out.setf(std::ios::fixed, std::ios::floatfield);

          out << column.entries[i].value;

          if (j<n_cols-1)
            out << " & ";
        }
      out << "\\\\ \\hline" << std::endl;
    }

  out   << "\\end{tabular}" << std::endl
        << "\\end{center}" << std::endl;
  if (tex_table_caption!="")
    out << "\\caption{"  << tex_table_caption << "}" << std::endl;
  if (tex_table_label!="")
    out << "\\label{"   << tex_table_label << "}" << std::endl;
  out   << "\\end{table}" << std::endl;
  if (with_header)
    out << "\\end{document}" << std::endl;
}


void TableHandler::clear()
{

  columns.clear();
  supercolumns.clear();
  column_order.clear();
  tex_supercaptions.clear();

  tex_table_label.clear();
  tex_table_caption.clear();

}


unsigned int TableHandler::n_rows() const
{
  if (columns.size() == 0)
    return 0;

  std::map<std::string, Column>::const_iterator col_iter = columns.begin();
  unsigned int n = col_iter->second.entries.size();
  std::string first_name=col_iter->first;

  for (++col_iter; col_iter!=columns.end(); ++col_iter)
    Assert(col_iter->second.entries.size()==n,
           ExcWrongNumberOfDataEntries(col_iter->first,
                                       col_iter->second.entries.size(),
                                       first_name, n));

  return n;
}


void TableHandler::get_selected_columns(std::vector<std::string> &sel_columns) const
{
  sel_columns.clear();

  for (unsigned int j=0; j<column_order.size(); ++j)
    {
      std::string key=column_order[j];
      const std::map<std::string, std::vector<std::string> >::const_iterator
      super_iter=supercolumns.find(key);

      if (super_iter!=supercolumns.end())
        {
          // i.e. key is a supercolumn key
          const unsigned int n_subcolumns=super_iter->second.size();
          for (unsigned int k=0; k<n_subcolumns; ++k)
            {
              const std::string subkey=super_iter->second[k];
              Assert(columns.count(subkey), ExcInternalError());
              sel_columns.push_back(subkey);
            }
        }
      else
        {
          Assert(columns.count(key), ExcInternalError());
          // i.e. key is a column key
          sel_columns.push_back(key);
        }
    }
}


void TableHandler::clear_current_row ()
{
  // Figure out what is the currect (max) length of the columns
  // so that we "shave" one off.
  std::vector<internal::TableEntry>::size_type n = 0;
  for (std::map< std::string, Column >::iterator p = columns.begin(); p != columns.end(); ++p)
    n = std::max(n, p->second.entries.size());

  // shave the top most element
  if (n != 0)
    for (std::map< std::string, Column >::iterator p = columns.begin(); p != columns.end(); ++p)
      if (p->second.entries.size() == n)
        p->second.entries.pop_back();
}


DEAL_II_NAMESPACE_CLOSE