about summary refs log tree commit diff stats
path: root/histogram.cpp
blob: 68961461cde9d0d8065aa7214c6633de986145dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "histogram.h"
#include <cstdlib>

template <class T>
void histogram<T>::add(const T& inst)
{
  freqtable[inst]++;
}

template <class T>
void histogram<T>::compile()
{
  distribution.clear();
  
  int max = 0;
  for (auto& it : freqtable)
  {
    max += it.second;
    distribution.emplace(max, it.first);
  }
  
  freqtable.clear();
}

template <class T>
const T& histogram<T>::next() const
{
  int max = distribution.rbegin()->first;
  int r = rand() % max;
  
  return distribution.upper_bound(r)->second;
}

template class histogram <std::string>;