tudocomp
– The TU Dortmund Compression Framework
Counter.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cstdint>
5 #include <unordered_map>
6 #include <utility>
7 #include <vector>
8 
9 #include <glog/logging.h>
10 
11 namespace tdc {
12 
14 template<class T>
15 class Counter {
16 
17  std::unordered_map<T, size_t> map;
18 
19 public:
20  typedef std::unordered_map<T, size_t> ranking_t;
21 
24  void increase(const T& item) {
25  // Indexing is defined as inserting and returning 0
26  // in case of missing entry in the map.
27  size_t prev_value = map[item];
28  map[item] = prev_value + 1;
29  }
30 
32  void setCount(const T& item, size_t count) {
33  map[item] = count;
34  }
35 
37  size_t getCount(const T& item) {
38  return map[item];
39  }
40 
42  size_t getNumItems() {
43  return map.size();
44  }
45 
47  std::vector<std::pair<T, size_t>> getSorted() {
48  std::vector<std::pair<T, size_t>> v(map.begin(), map.end());
49  std::sort(v.begin(), v.end(), [] (
50  std::pair<T, size_t> a, std::pair<T, size_t> b
51  ) {
52  // TODO: Only compare first in debug mode to get deterministic output
53  if (a.second != b.second) {
54  return a.second > b.second;
55  } else {
56  return a.first < b.first;
57  }
58  });
59 
60  /*
61  DLOG(INFO) << "getSorted() [";
62  for(auto e : v) {
63  DLOG(INFO) << " " << e.first << " " << e.second;
64  }
65  DLOG(INFO) << "]";
66  */
67 
68  return v;
69  }
70 
75  ranking_t createRanking(size_t num = SIZE_MAX) {
76  ranking_t r(num + 1);
77  size_t i = 0;
78  for (auto pair : getSorted()) {
79  r[pair.first] = i++;
80  if (i >= num) {
81  break;
82  }
83  }
84  return r;
85  }
86 };
87 
88 }
89 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::unordered_map< T, size_t > ranking_t
Definition: Counter.hpp:20
std::vector< std::pair< T, size_t > > getSorted()
Return a list of value-count pairs, sorted with the most-seen first.
Definition: Counter.hpp:47
A data structure for counting occurences of values of a given type T.
Definition: Counter.hpp:15
size_t getCount(const T &item)
Get the count associated with a value.
Definition: Counter.hpp:37
size_t getNumItems()
Return how many differnt values have been seen so far.
Definition: Counter.hpp:42
void increase(const T &item)
Increase the counter for the passed value by one, setting it to 1 if it was not yet seen...
Definition: Counter.hpp:24
ranking_t createRanking(size_t num=SIZE_MAX)
Return a map of the num-most common values, keyed by their common-ness, with map[0] being hte most co...
Definition: Counter.hpp:75
void setCount(const T &item, size_t count)
Set the count of an item to a specific value.
Definition: Counter.hpp:32