Add sub to Debug Counter

This commit is contained in:
Zach Pomerantz 2016-04-15 11:10:36 -07:00
parent 7b49552066
commit cf9d160602

View file

@ -55,8 +55,11 @@ public:
} }
~Counter() { log(); } ~Counter() { log(); }
// Increase the count for key. // Increase the count for key (by inc).
void add(const K& key); void add(const K& key, size_t inc = 1);
// Decrease the count for key (by dec).
void sub(const K& key, size_t dec = 1);
// Log current counts (called on destruction). // Log current counts (called on destruction).
void log(); void log();
@ -123,20 +126,25 @@ private:
}; };
template<class K> template<class K>
void Counter<K>::add(const K& k) { void Counter<K>::add(const K& k, size_t inc) {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
auto& it = _map.find(k); auto& it = _map.find(k);
if (it == _map.end()) { if (it == _map.end()) {
// No entry for k; add it // No entry for k; add it
_map.insert(std::pair<K, size_t>(k, 1)); _map.insert(std::pair<K, size_t>(k, inc));
} else { } else {
// Entry for k; update it // Entry for k; update it
it->second++; it->second += inc;
} }
} }
template<class K>
void Counter<K>::sub(const K& k, size_t dec) {
add(k, -dec);
}
template <class K> template <class K>
void Counter<K>::log() { void Counter<K>::log() {
// Avoid logging nothing // Avoid logging nothing