about summary refs log tree commit diff stats
path: root/histogram.cpp
blob: 77c5c3e17ae9b9c124a8ab77eb8b401e0baa65ff (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
45
46
#include "histogram.h"
#include "kgramstats.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 <std::string>;
template class histogram <rawr::terminator>;