summary refs log tree commit diff stats
path: root/histogram.cpp
blob: 38fca4502e7917d6fdba1cceb6fbebe4f2e9ec14 (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
35
36
37
38
39
40
41
42
43
44
#include "histogram.h"
#include <cstdlib>
#include <iostream>

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 T>
void histogram<T>::print() const
{
  for (auto& freqpair : freqtable)
  {
    std::cout << freqpair.first << ": " << freqpair.second << std::endl;
  }
}

template class histogram <unsigned long>;