File: Table.h

package info (click to toggle)
fenics-dolfinx 1%3A0.10.0.post4-1exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,028 kB
  • sloc: cpp: 36,535; python: 25,391; makefile: 226; sh: 171; xml: 55
file content (97 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2008-2011 Anders Logg
//
// This file is part of DOLFINx (https://www.fenicsproject.org)
//
// SPDX-License-Identifier:    LGPL-3.0-or-later

#pragma once

#include <cstdint>
#include <map>
#include <mpi.h>
#include <string>
#include <variant>
#include <vector>

namespace dolfinx
{

/// @brief This class provides storage and pretty-printing for tables.
///
/// Example usage:
///
///   Table table("Timings");
///   table.set("Foo", "Assemble", 0.010);
///   table.set("Foo", "Solve", 0.020);
///   table.set("Bar", "Assemble", 0.011);
///   table.set("Bar", "Solve", 0.019);
class Table
{
public:
  /// Types of MPI reduction available for Table, to get the max, min or
  /// average values over an MPI_Comm
  enum class Reduction : std::uint8_t
  {
    average,
    max,
    min
  };

  /// Create empty table
  Table(std::string title = "", bool right_justify = true);

  /// Copy constructor
  Table(const Table& table) = default;

  /// Move constructor
  Table(Table&& table) = default;

  /// Destructor
  ~Table() = default;

  /// Assignment operator
  Table& operator=(const Table& table) = default;

  /// Move assignment
  Table& operator=(Table&& table) = default;

  /// Set table entry
  /// @param[in] row Row name
  /// @param[in] col Column name
  /// @param[in] value The value to set
  void set(const std::string& row, const std::string& col,
           std::variant<std::string, int, double> value);

  /// Get value of table entry
  /// @param[in] row Row name
  /// @param[in] col Column name
  /// @returns Returns the entry for requested row and columns
  std::variant<std::string, int, double> get(const std::string& row,
                                             const std::string& col) const;

  /// Do MPI reduction on Table
  /// @param[in] comm MPI communicator
  /// @param[in] reduction Type of reduction to perform
  /// @return Reduced Table
  Table reduce(MPI_Comm comm, Reduction reduction) const;

  /// Table name
  std::string name;

  /// Return string representation of the table
  std::string str() const;

private:
  // Row and column names
  std::vector<std::string> _rows, _cols;

  // Table entry values
  std::map<std::pair<std::string, std::string>,
           std::variant<std::string, int, double>>
      _values;

  // True if we should right-justify the table entries
  bool _right_justify;
};

} // namespace dolfinx