carl  24.04
Computer ARithmetic Library
MultiCounter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/container/flat_map.hpp>
4 
5 namespace carl::statistics {
6 
7 template<typename T>
8 class MultiCounter {
9  boost::container::flat_map<T,std::size_t> m_data;
10  std::size_t m_total = 0;
11 
12 public:
13  void inc(const T& key, std::size_t inc) {
14  m_data.try_emplace(key).first->second += inc;
15  m_total += inc;
16  }
17 
18  void collect(std::map<std::string, std::string>& data, const std::string& key) const {
19  std::stringstream ss;
20  for (const auto& [k,v] : m_data) {
21  serialize(ss, k);
22  ss << "=" << v << ";";
23  }
24  data.emplace(key, ss.str());
25  data.emplace(key + ".total", std::to_string(m_total));
26  }
27 };
28 
29 }
void serialize(std::stringstream &ss, const std::pair< T, S > &pair)
Definition: Serialization.h:8
boost::container::flat_map< T, std::size_t > m_data
Definition: MultiCounter.h:9
void collect(std::map< std::string, std::string > &data, const std::string &key) const
Definition: MultiCounter.h:18
void inc(const T &key, std::size_t inc)
Definition: MultiCounter.h:13