aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/encode/buffered/string_pool.cpp
blob: 35b0b6d11414e38712163f5802e5fb1ceb3fecde (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
#include "string_pool.h"

namespace NMonitoring {
    ////////////////////////////////////////////////////////////////////////////////
    // TStringPoolBuilder
    ////////////////////////////////////////////////////////////////////////////////
    const TStringPoolBuilder::TValue* TStringPoolBuilder::PutIfAbsent(TStringBuf str) {
        Y_ENSURE(!IsBuilt_, "Cannot add more values after string has been built");

        auto [it, isInserted] = StrMap_.try_emplace(str, Max<ui32>(), 0);
        if (isInserted) {
            BytesSize_ += str.size(); 
            it->second.Index = StrVector_.size();
            StrVector_.emplace_back(it->first, &it->second);
        }

        TValue* value = &it->second;
        ++value->Frequency;
        return value;
    }

    const TStringPoolBuilder::TValue* TStringPoolBuilder::GetByIndex(ui32 index) const {
        return StrVector_.at(index).second;
    }

    TStringPoolBuilder& TStringPoolBuilder::Build() {
        if (RequiresSorting_) {
            // sort in reversed order
            std::sort(StrVector_.begin(), StrVector_.end(), [](auto& a, auto& b) {
                return a.second->Frequency > b.second->Frequency;
            });

            ui32 i = 0;
            for (auto& value : StrVector_) {
                value.second->Index = i++;
            }
        }

        IsBuilt_ = true;

        return *this;
    }

    ////////////////////////////////////////////////////////////////////////////////
    // TStringPool
    ////////////////////////////////////////////////////////////////////////////////
    void TStringPool::InitIndex(const char* data, ui32 size) {
        const char* begin = data;
        const char* end = begin + size;
        for (const char* p = begin; p != end; ++p) {
            if (*p == '\0') {
                Index_.push_back(TStringBuf(begin, p));
                begin = p + 1;
            }
        }
    }

}