aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/buffered/string_pool.h
blob: 2d67fd37a35a37bed594f16d1955db3ee9a9dd9e (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once 
 
#include <util/generic/hash.h> 
#include <util/generic/vector.h> 
 
namespace NMonitoring { 
    //////////////////////////////////////////////////////////////////////////////// 
    // TStringPoolBuilder 
    //////////////////////////////////////////////////////////////////////////////// 
    class TStringPoolBuilder { 
    public: 
        struct TValue: TNonCopyable {
            TValue(ui32 idx, ui32 freq)
                : Index{idx}
                , Frequency{freq}
            {
            }

            ui32 Index; 
            ui32 Frequency; 
        }; 
 
    public: 
        const TValue* PutIfAbsent(TStringBuf str); 
        const TValue* GetByIndex(ui32 index) const;
 
        /// Determines whether pool must be sorted by value frequencies
        TStringPoolBuilder& SetSorted(bool sorted) {
            RequiresSorting_ = sorted;
            return *this;
        }
 
        TStringPoolBuilder& Build();

        TStringBuf Get(ui32 index) const { 
            Y_ENSURE(IsBuilt_, "Pool must be sorted first");
            return StrVector_.at(index).first; 
        } 
 
        TStringBuf Get(const TValue* value) const {
            return StrVector_.at(value->Index).first;
        }

        template <typename TConsumer> 
        void ForEach(TConsumer&& c) { 
            Y_ENSURE(IsBuilt_, "Pool must be sorted first");
            for (const auto& value : StrVector_) { 
                c(value.first, value.second->Index, value.second->Frequency); 
            } 
        } 
 
        size_t BytesSize() const noexcept { 
            return BytesSize_; 
        } 
 
        size_t Count() const noexcept { 
            return StrMap_.size(); 
        } 
 
    private: 
        THashMap<TString, TValue> StrMap_; 
        TVector<std::pair<TStringBuf, TValue*>> StrVector_; 
        bool RequiresSorting_ = false;
        bool IsBuilt_ = false;
        size_t BytesSize_ = 0; 
    }; 
 
    //////////////////////////////////////////////////////////////////////////////// 
    // TStringPool 
    //////////////////////////////////////////////////////////////////////////////// 
    class TStringPool { 
    public: 
        TStringPool(const char* data, ui32 size) { 
            InitIndex(data, size); 
        } 
 
        TStringBuf Get(ui32 i) const { 
            return Index_.at(i); 
        } 
 
        size_t Size() const {
            return Index_.size();
        }

    private: 
        void InitIndex(const char* data, ui32 size); 
 
    private: 
        TVector<TStringBuf> Index_; 
    }; 
 
}