aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/minsketch/count_min_sketch.h
blob: 4b8866d3d9218f12a315e275c21cb0955f959780 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once

#include <util/system/types.h>
#include <util/generic/strbuf.h>

namespace NKikimr {

class TCountMinSketch {
private:
    ui64 Width;
    ui64 Depth;
    ui64 ElementCount;

private:
    static ui64 Hash(const char* data, size_t size, size_t hashIndex);

    static size_t StaticSize(ui64 width, ui64 depth) {
        return sizeof(TCountMinSketch) + width * depth * sizeof(ui32);
    }

    const ui32* Buckets() const {
        return reinterpret_cast<const ui32*>(this + 1);
    }

    ui32* Buckets() {
        return reinterpret_cast<ui32*>(this + 1);
    }

public:
    static TCountMinSketch* Create(ui64 width = 256, ui64 depth = 8);
    static TCountMinSketch* FromString(const char* data, size_t size);

    void operator delete(void* data) noexcept;

    TCountMinSketch() = delete;
    TCountMinSketch(const TCountMinSketch&) = delete;

    size_t GetSize() const {
        return StaticSize(Width, Depth);
    }

    size_t GetWidth() const {
        return Width;
    }

    size_t GetDepth() const {
        return Depth;
    }

    size_t GetElementCount() const {
        return ElementCount;
    }

    TStringBuf AsStringBuf() const {
        return TStringBuf(reinterpret_cast<const char*>(this), GetSize());
    }

    void Count(const char* data, size_t size);

    ui32 Probe(const char* data, size_t size) const;

    TCountMinSketch& operator+=(const TCountMinSketch& rhs);
};

static_assert(sizeof(TCountMinSketch) == 24);

} // NKikimr