6
// http://www2.research.att.com/~marioh/papers/vldb08-2.pdf
11
struct { intptr_t key; int count,zero; } pair[];
14
// size is the precision/return size: in sequence with n _add(), it will find at most >size elements with occurence > n/(size+1) times
15
FREQUENT *frequent_new(int size) // {{{ - just free() it
18
FREQUENT *ret=malloc(sizeof(ret[0])+sizeof(ret->pair[0])*size);
26
for (iA=0;iA<size;iA++) {
27
ret->pair[iA].key=INTPTR_MIN;
28
ret->pair[iA].count=0;
36
void frequent_add(FREQUENT *freq,intptr_t key) // {{{
40
for (iA=freq->size-1;iA>=0;iA--) {
41
if (freq->pair[iA].key==key) {
42
freq->pair[iA].count++;
45
} else if (freq->pair[iA].count==freq->czero) {
49
if (zero>=0) { // insert into set
50
freq->pair[zero].key=key;
51
freq->pair[zero].count++; // i.e. czero+1
52
freq->pair[zero].zero=freq->czero;
53
// if it was sorted, the free entries are at the end. zero points to the first free entry, because of the loop direction
54
} else { // out-of-set count
60
static int frequent_cmp(const void *a,const void *b) // {{{
62
const typeof(((FREQUENT *)0)->pair[0]) *aa=a;
63
const typeof(((FREQUENT *)0)->pair[0]) *bb=b;
64
return (bb->count-bb->zero)-(aa->count-aa->zero);
68
// true frequency is somewhere between (count-zero) and count
69
intptr_t frequent_get(FREQUENT *freq,int pos) // {{{
73
// sort by (count-zero)
74
qsort(freq->pair,freq->size,sizeof(freq->pair[0]),frequent_cmp);
77
if ( (pos<0)||(pos>=freq->size) ) {
80
return freq->pair[pos].key;