CVC3  2.4.1
statistics.h
Go to the documentation of this file.
1 /*****************************************************************************/
2 /*!
3  * \file statistics.h
4  * \brief Description: Counters and flags for collecting run-time statistics.
5  *
6  * Author: Sergey Berezin
7  *
8  * Created: Thu Jun 5 17:38:13 2003
9  *
10  * <hr>
11  *
12  * License to use, copy, modify, sell and/or distribute this software
13  * and its documentation for any purpose is hereby granted without
14  * royalty, subject to the terms and conditions defined in the \ref
15  * LICENSE file provided with this distribution.
16  *
17  * <hr>
18  *
19  */
20 /*****************************************************************************/
21 
22 #ifndef _cvc3__statistics_h
23 #define _cvc3__statistics_h
24 
25 #include <string>
26 #include <iostream>
27 #include <sstream>
28 #include <map>
29 
30 namespace CVC3 {
31 
32  class Statistics; // The main class, defined below
33 
34  // First, wrapper classes for flags and counters. Later, we
35  // overload some operators like '=', '++', etc. for those classes.
36 
37  // Boolean flag (can only be true or false)
38  class StatFlag {
39  private:
40  bool* d_flag; // We don't own the pointer
41  public:
42  // Constructor: takes the pointer to the actual flag, normally
43  // stored in class Statistics below.
44  StatFlag(bool& flag) : d_flag(&flag) { }
45  // Destructor
46  ~StatFlag() { }
47  // Auto-cast to boolean
48  operator bool() { return *d_flag; }
49 
50  // Setting and resetting by ++ and --
51  // Prefix versions:
52  bool operator--() { *d_flag = false; return false; }
53  bool operator++() { *d_flag = true; return true; }
54  // Postfix versions:
55  bool operator--(int) { bool x=*d_flag; *d_flag=false; return x; }
56  bool operator++(int) { bool x=*d_flag; *d_flag=true; return x; }
57  // Can be assigned only a boolean value
58  StatFlag& operator=(bool x) { *d_flag=(x!=false); return *this; }
59  // Comparisons
60  friend bool operator==(const StatFlag& f1, const StatFlag& f2);
61  friend bool operator!=(const StatFlag& f1, const StatFlag& f2);
62  // Printing
63  friend std::ostream& operator<<(std::ostream& os, const StatFlag& f);
64  }; // end of class StatFlag
65 
66  inline bool operator==(const StatFlag& f1, const StatFlag& f2) {
67  return (*f1.d_flag) == (*f2.d_flag);
68  }
69  inline bool operator!=(const StatFlag& f1, const StatFlag& f2) {
70  return (*f1.d_flag) != (*f2.d_flag);
71  }
72  inline std::ostream& operator<<(std::ostream& os, const StatFlag& f) {
73  if(*f.d_flag) return(os << "true");
74  else return(os << "false");
75  }
76 
77  // Integer counter. Intended use is to count events (e.g. number of
78  // function calls), but can be used to store any integer value
79  // (e.g. size of some data structure)
80  class StatCounter {
81  private:
82  int* d_counter; // We don't own the pointer
83  public:
84  // Constructor: takes the pointer to the actual counter, normally
85  // stored in class Statistics below.
86  StatCounter(int& c) : d_counter(&c) { }
87  // Destructor
89  // Auto-cast to int. In particular, arithmetic comparisons like
90  // <, >, <=, >= will work because of this.
91 
92  operator int() { return *d_counter; }
93 
94  // Auto-increment operators
95  // Prefix versions:
96  int operator--() { return --(*d_counter); }
97  int operator++() { return ++(*d_counter); }
98  // Postfix versions:
99  int operator--(int) { return (*d_counter)--; }
100  int operator++(int) { return (*d_counter)++; }
101  // Can be assigned an integer or the value of another StatCounter
102  StatCounter& operator=(int x) { *d_counter=x; return *this; }
103  StatCounter& operator+=(int x) { *d_counter+=x; return *this; }
104  StatCounter& operator-=(int x) { *d_counter-=x; return *this; }
106  { *d_counter=*x.d_counter; return *this; }
108  { *d_counter-=*x.d_counter; return *this; }
110  { *d_counter+=*x.d_counter; return *this; }
111  // Comparisons to integers and other StatCounters
112  friend bool operator==(const StatCounter& c1, const StatCounter& c2);
113  friend bool operator!=(const StatCounter& c1, const StatCounter& c2);
114  friend bool operator==(int c1, const StatCounter& c2);
115  friend bool operator!=(int c1, const StatCounter& c2);
116  friend bool operator==(const StatCounter& c1, int c2);
117  friend bool operator!=(const StatCounter& c1, int c2);
118  // Printing
119  friend std::ostream& operator<<(std::ostream& os, const StatCounter& f);
120  }; // end of class StatCounter
121 
122  inline bool operator==(const StatCounter& c1, const StatCounter& c2) {
123  return (*c1.d_counter) == (*c2.d_counter);
124  }
125  inline bool operator!=(const StatCounter& c1, const StatCounter& c2) {
126  return (*c1.d_counter) != (*c2.d_counter);
127  }
128  inline bool operator==(int c1, const StatCounter& c2) {
129  return c1 == (*c2.d_counter);
130  }
131  inline bool operator!=(int c1, const StatCounter& c2) {
132  return c1 != (*c2.d_counter);
133  }
134  inline bool operator==(const StatCounter& c1, int c2) {
135  return (*c1.d_counter) == c2;
136  }
137  inline bool operator!=(const StatCounter& c1, int c2) {
138  return (*c1.d_counter) != c2;
139  }
140  inline std::ostream& operator<<(std::ostream& os, const StatCounter& c) {
141  return (os << *c.d_counter);
142  }
143 
144  // class Statistics: the storage for all flags and counters
145 
146  class Statistics {
147  private:
148  // Output control
149  std::ostream* d_os;
150  typedef std::map<std::string, bool> StatFlagMap;
151  typedef std::map<std::string, int> StatCounterMap;
152  StatFlagMap d_flags;
153  StatCounterMap d_counters;
154  public:
155  // Constructor
157  // Destructor (must destroy objects it d_timers)
159  // Accessing flags, counters, and timers by name. If an object
160  // doesn't exist, it is created and initialized to false or 0.
161  StatFlag flag(const std::string& name)
162  { return StatFlag(d_flags[name]); }
163  StatCounter counter(const std::string& name)
164  { return StatCounter(d_counters[name]); }
165 
166  // Print all the collected data
167  std::ostream& printAll(std::ostream& os) const;
168  friend std::ostream& operator<<(std::ostream& os,
169  const Statistics& stats) {
170  return stats.printAll(os);
171  }
172  }; // end of class Statistics
173 
174 } // end of namespace CVC3
175 
176 #endif
friend bool operator==(const StatCounter &c1, const StatCounter &c2)
Definition: statistics.h:122
bool * d_flag
Definition: statistics.h:40
int operator++(int)
Definition: statistics.h:100
ostream & operator<<(ostream &os, const Expr &e)
Definition: expr.cpp:621
bool operator--()
Definition: statistics.h:52
friend bool operator!=(const StatCounter &c1, const StatCounter &c2)
Definition: statistics.h:125
StatCounter(int &c)
Definition: statistics.h:86
StatFlagMap d_flags
Definition: statistics.h:152
StatCounter & operator=(int x)
Definition: statistics.h:102
bool operator==(const Expr &e1, const Expr &e2)
Definition: expr.h:1600
StatCounterMap d_counters
Definition: statistics.h:153
StatCounter & operator-=(int x)
Definition: statistics.h:104
int operator--(int)
Definition: statistics.h:99
StatFlag flag(const std::string &name)
Definition: statistics.h:161
StatCounter & operator+=(const StatCounter &x)
Definition: statistics.h:109
friend bool operator!=(const StatFlag &f1, const StatFlag &f2)
Definition: statistics.h:69
StatCounter & operator-=(const StatCounter &x)
Definition: statistics.h:107
friend std::ostream & operator<<(std::ostream &os, const StatFlag &f)
Definition: statistics.h:72
StatCounter counter(const std::string &name)
Definition: statistics.h:163
std::map< std::string, int > StatCounterMap
Definition: statistics.h:151
std::ostream * d_os
Definition: statistics.h:149
StatCounter & operator+=(int x)
Definition: statistics.h:103
friend bool operator==(const StatFlag &f1, const StatFlag &f2)
Definition: statistics.h:66
StatFlag(bool &flag)
Definition: statistics.h:44
StatFlag & operator=(bool x)
Definition: statistics.h:58
bool operator--(int)
Definition: statistics.h:55
std::ostream & printAll(std::ostream &os) const
Definition: statistics.cpp:32
StatCounter & operator=(const StatCounter &x)
Definition: statistics.h:105
bool operator!=(const Expr &e1, const Expr &e2)
Definition: expr.h:1605
std::map< std::string, bool > StatFlagMap
Definition: statistics.h:150
Definition: expr.cpp:35
bool operator++(int)
Definition: statistics.h:56
friend std::ostream & operator<<(std::ostream &os, const StatCounter &f)
Definition: statistics.h:140
friend std::ostream & operator<<(std::ostream &os, const Statistics &stats)
Definition: statistics.h:168
bool operator++()
Definition: statistics.h:53