aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/robots_txt/prefix_tree.h
blob: 5feafcb74d1a91fecc7fe8d91a19b8c2baa1d5ea (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
#pragma once

#include <util/generic/ptr.h>
#include <util/generic/vector.h>
#include <cstdio>
#include <util/generic/noncopyable.h>

struct TPrefixTreeNodeElement {
    const char* Key;
    i32 KeyLen;
    i32 Val;
    i32 Index;

    TPrefixTreeNodeElement();
    TPrefixTreeNodeElement(const char*, i32, i32, i32);
};

class TPrefixTreeNode {
public:
    TVector<TPrefixTreeNodeElement> Elements;
    TPrefixTreeNode();

    int Find(char) const;
    void Set(const char*, i32, i32, i32);
    void Dump(FILE*) const;
};

class TPrefixTree : TNonCopyable {
private:
    static const i32 INDEX_BOUND = 1 << 30;

    TVector<TPrefixTreeNode> Nodes;

public:
    void Init(int);
    TPrefixTree(int);

    void Add(const char*, i32);
    i32 MinPrefixIndex(const char*) const;
    void Clear();
    void Dump(FILE*) const;
    int GetMemorySize() const;
    void Compress();

private:
    void AddInternal(const char*, TPrefixTreeNode&, i32);
};