cmake: Disable C++ GNU extensions. (#3446)
[cvc5.git] / src / util / statistics.cpp
1 /********************* */
2 /*! \file statistics.cpp
3 ** \verbatim
4 ** Top contributors (to current version):
5 ** Morgan Deters, Andres Noetzli, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
11 **
12 ** \brief [[ Add one-line brief description here ]]
13 **
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
16 **/
17
18 #include "util/statistics.h"
19
20 #include <typeinfo>
21
22 #include "util/safe_print.h"
23 #include "util/statistics_registry.h" // for details about class Stat
24
25
26 namespace CVC4 {
27
28 std::string StatisticsBase::s_regDelim("::");
29
30 bool StatisticsBase::StatCmp::operator()(const Stat* s1, const Stat* s2) const {
31 return s1->getName() < s2->getName();
32 }
33
34 StatisticsBase::iterator::value_type StatisticsBase::iterator::operator*() const {
35 return std::make_pair((*d_it)->getName(), (*d_it)->getValue());
36 }
37
38 StatisticsBase::StatisticsBase() :
39 d_prefix(),
40 d_stats() {
41 }
42
43 StatisticsBase::StatisticsBase(const StatisticsBase& stats) :
44 d_prefix(stats.d_prefix),
45 d_stats() {
46 }
47
48 StatisticsBase& StatisticsBase::operator=(const StatisticsBase& stats) {
49 d_prefix = stats.d_prefix;
50 return *this;
51 }
52
53 void Statistics::copyFrom(const StatisticsBase& stats) {
54 // This is ugly, but otherwise we have to introduce a "friend" relation for
55 // Base to its derived class (really obnoxious).
56 const StatisticsBase::const_iterator i_begin = stats.begin();
57 const StatisticsBase::const_iterator i_end = stats.end();
58 for(StatisticsBase::const_iterator i = i_begin; i != i_end; ++i) {
59 SExprStat* p = new SExprStat((*i).first, (*i).second);
60 d_stats.insert(p);
61 }
62 }
63
64 void Statistics::clear() {
65 for(StatSet::iterator i = d_stats.begin(); i != d_stats.end(); ++i) {
66 delete *i;
67 }
68 d_stats.clear();
69 }
70
71 Statistics::Statistics(const StatisticsBase& stats) :
72 StatisticsBase(stats) {
73 copyFrom(stats);
74 }
75
76 Statistics::Statistics(const Statistics& stats) :
77 StatisticsBase(stats) {
78 copyFrom(stats);
79 }
80
81 Statistics::~Statistics() {
82 clear();
83 }
84
85 Statistics& Statistics::operator=(const StatisticsBase& stats) {
86 clear();
87 this->StatisticsBase::operator=(stats);
88 copyFrom(stats);
89
90 return *this;
91 }
92
93 Statistics& Statistics::operator=(const Statistics& stats) {
94 return this->operator=((const StatisticsBase&)stats);
95 }
96
97 StatisticsBase::const_iterator StatisticsBase::begin() const {
98 return iterator(d_stats.begin());
99 }
100
101 StatisticsBase::const_iterator StatisticsBase::end() const {
102 return iterator(d_stats.end());
103 }
104
105 void StatisticsBase::flushInformation(std::ostream &out) const {
106 #ifdef CVC4_STATISTICS_ON
107 for(StatSet::iterator i = d_stats.begin();
108 i != d_stats.end();
109 ++i) {
110 Stat* s = *i;
111 if(d_prefix != "") {
112 out << d_prefix << s_regDelim;
113 }
114 s->flushStat(out);
115 out << std::endl;
116 }
117 #endif /* CVC4_STATISTICS_ON */
118 }
119
120 void StatisticsBase::safeFlushInformation(int fd) const {
121 #ifdef CVC4_STATISTICS_ON
122 for (StatSet::iterator i = d_stats.begin(); i != d_stats.end(); ++i) {
123 Stat* s = *i;
124 if (d_prefix.size() != 0) {
125 safe_print(fd, d_prefix);
126 safe_print(fd, s_regDelim);
127 }
128 s->safeFlushStat(fd);
129 safe_print(fd, "\n");
130 }
131 #endif /* CVC4_STATISTICS_ON */
132 }
133
134 SExpr StatisticsBase::getStatistic(std::string name) const {
135 SExpr value;
136 IntStat s(name, 0);
137 StatSet::iterator i = d_stats.find(&s);
138 if(i != d_stats.end()) {
139 return (*i)->getValue();
140 } else {
141 return SExpr();
142 }
143 }
144
145 void StatisticsBase::setPrefix(const std::string& prefix) {
146 d_prefix = prefix;
147 }
148
149 }/* CVC4 namespace */