aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/comptrie/leaf_skipper.h
blob: 0bdfa11d63e6ab4d2c0c90291e6035f9c72208a6 (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
#pragma once

#include <cstddef>

namespace NCompactTrie {
    class ILeafSkipper {
    public:
        virtual size_t SkipLeaf(const char* p) const = 0;
        virtual ~ILeafSkipper() = default;
    };

    template <class TPacker> 
    class TPackerLeafSkipper: public ILeafSkipper { 
    private:
        const TPacker* Packer;
 
    public:
        TPackerLeafSkipper(const TPacker* packer)
            : Packer(packer)
        {
        }

        size_t SkipLeaf(const char* p) const override {
            return Packer->SkipLeaf(p);
        }

        // For test purposes.
        const TPacker* GetPacker() const { 
            return Packer; 
        } 
    };

    // The data you need to traverse the trie without unpacking the values.
    struct TOpaqueTrie {
        const char* Data;
        size_t Length;
        const ILeafSkipper& SkipFunction;

        TOpaqueTrie(const char* data, size_t dataLength, const ILeafSkipper& skipFunction)
            : Data(data)
            , Length(dataLength)
            , SkipFunction(skipFunction)
        { 
        } 

        bool operator==(const TOpaqueTrie& other) const {
            return Data == other.Data &&
                   Length == other.Length && 
                   &SkipFunction == &other.SkipFunction; 
        }

        bool operator!=(const TOpaqueTrie& other) const {
            return !(*this == other);
        }
    };
}