File: ApproximateCounter.h

package info (click to toggle)
snap-aligner 2.0.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,652 kB
  • sloc: cpp: 41,051; ansic: 5,239; python: 227; makefile: 85; sh: 28
file content (30 lines) | stat: -rw-r--r-- 689 bytes parent folder | download | duplicates (5)
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
#pragma once

#include "Compat.h"

// Counts the number of distinct items in a stream approximately using Flajolet-Martin.
class ApproximateCounter
{
public:
    ApproximateCounter();

    void add(_uint64 value);

    unsigned getCount();

private:
    static const int SHIFT = 9;
    static const int BUCKETS = 1 << SHIFT;

    std::vector<_uint64> buckets;

    // MurmurHash3 finalization step from http://sites.google.com/site/murmurhash
    inline _uint64 hash(_uint64 value) {
        value ^= (value >> 33);
        value *= 0xff51afd7ed558ccdLL;
        value ^= (value >> 33);
        value *= 0xc4ceb9fe1a85ec53LL;
        value ^= (value >> 33);
        return value;
    }
};