carl  24.04
Computer ARithmetic Library
Statistics.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <sstream>
5 #include <algorithm>
6 #include <cassert>
7 
8 #include "StatisticsCollector.h"
9 #include "Serialization.h"
10 #include "Timing.h"
11 #include "Series.h"
12 #include "MultiCounter.h"
13 
14 namespace carl {
15 namespace statistics {
16 
17 class Statistics {
18 private:
19  std::string mName;
20  std::map<std::string, std::string> mCollected;
21  bool has_illegal_chars(const std::string& val) const {
22  return std::find_if(val.begin(), val.end(), [](char c) {
23  return c == ':' || c == '(' || c == ')' || std::isspace(static_cast<unsigned char>(c));
24  }) != val.end();
25  }
26 protected:
27  void addKeyValuePair(const std::string& key, const std::string& value) {
28  assert(!has_illegal_chars(key) && "spaces, (, ), : are not allowed here");
29  if (has_illegal_chars(key)) return;
30  assert(!has_illegal_chars(static_cast<std::string>(value)) && "spaces, (, ), : are not allowed here");
31  if (has_illegal_chars(value)) return;
32  mCollected.emplace(key, value);
33  }
34 
35  void addKeyValuePair(const std::string& key, Timer& value) {
36  value.collect(mCollected, key);
37  }
38 
39  void addKeyValuePair(const std::string& key, const Series& value) {
40  value.collect(mCollected, key);
41  }
42 
43  template<typename T>
44  void addKeyValuePair(const std::string& key, const MultiCounter<T>& value) {
45  value.collect(mCollected, key);
46  }
47 
48  template<typename T>
49  void addKeyValuePair(const std::string& key, const T& value) {
50  std::stringstream ss;
51  serialize(ss, value);
52  mCollected.emplace(key, ss.str());
53  }
54 
55 public:
56  Statistics() = default;
57  virtual ~Statistics() = default;
58  Statistics(const Statistics&) = delete;
59  Statistics(Statistics&&) = delete;
60  Statistics& operator=(const Statistics&) = delete;
62 
63  void set_name(const std::string& name) {
64  mName = name;
65  }
66 
67  virtual bool enabled() const {
68  return true;
69  }
70  virtual void collect() {}
71 
72  const auto& name() const {
73  return mName;
74  }
75  const auto& collected() const {
76  return mCollected;
77  }
78 };
79 
80 }
81 }
carl is the main namespace for the library.
void serialize(std::stringstream &ss, const std::pair< T, S > &pair)
Definition: Serialization.h:8
void collect(std::map< std::string, std::string > &data, const std::string &key) const
Definition: MultiCounter.h:18
void collect(std::map< std::string, std::string > &data, const std::string &key) const
Definition: Series.h:19
Statistics(const Statistics &)=delete
void addKeyValuePair(const std::string &key, const T &value)
Definition: Statistics.h:49
Statistics & operator=(const Statistics &)=delete
virtual ~Statistics()=default
void addKeyValuePair(const std::string &key, const MultiCounter< T > &value)
Definition: Statistics.h:44
void addKeyValuePair(const std::string &key, const std::string &value)
Definition: Statistics.h:27
void set_name(const std::string &name)
Definition: Statistics.h:63
void addKeyValuePair(const std::string &key, Timer &value)
Definition: Statistics.h:35
std::map< std::string, std::string > mCollected
Definition: Statistics.h:20
void addKeyValuePair(const std::string &key, const Series &value)
Definition: Statistics.h:39
Statistics & operator=(Statistics &&)=delete
virtual bool enabled() const
Definition: Statistics.h:67
bool has_illegal_chars(const std::string &val) const
Definition: Statistics.h:21
const auto & name() const
Definition: Statistics.h:72
Statistics(Statistics &&)=delete
const auto & collected() const
Definition: Statistics.h:75
void collect(std::map< std::string, std::string > &data, const std::string &key)
Definition: Timing.h:64