aboutsummaryrefslogtreecommitdiffstats
path: root/util/charset/unicode_table.h
blob: 3876d7d1b7ecb4d7e7b66145b6537d16db1afb4d (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once

#include <util/system/yassert.h>
#include <util/system/defaults.h>
#include <util/generic/typetraits.h>

namespace NUnicodeTable {
    template <class Value>
    struct TValueSelector;

    template <class Value>
    struct TValueSelector {
        using TStored = const Value;
        using TValueRef = const Value&;
        using TValuePtr = const Value*;

        static inline TValueRef Get(TValuePtr val) {
            return *val;
        }
    };

    template <class Value>
    struct TValueSelector<const Value*> {
        using TStored = const Value[];
        using TValueRef = const Value*;
        using TValuePtr = const Value*;

        static inline TValueRef Get(TValuePtr val) {
            return val;
        }
    };

    template <class Value>
    struct TValues {
        using TSelector = TValueSelector<Value>;

        using TStored = typename TSelector::TStored;
        using TValueRef = typename TSelector::TValueRef;
        using TValuePtr = typename TSelector::TValuePtr;

        using TData = const TValuePtr*;

        static inline TValuePtr Get(TData table, size_t index) {
            static_assert(std::is_pointer<TData>::value, "expect std::is_pointer<TData>::value");
            return table[index];
        }

        static inline TValueRef Get(TValuePtr val) {
            return TSelector::Get(val);
        }
    };

    template <int Shift, class TChild>
    struct TSubtable {
        using TStored = typename TChild::TStored;
        using TValueRef = typename TChild::TValueRef;
        using TValuePtr = typename TChild::TValuePtr;
        using TData = const typename TChild::TData*;

        static inline TValuePtr Get(TData table, size_t key) {
            static_assert(std::is_pointer<TData>::value, "expect std::is_pointer<TData>::value");
            return TChild::Get(table[key >> Shift], key & ((1 << Shift) - 1));
        }

        static inline TValueRef Get(TValuePtr val) {
            return TChild::Get(val);
        }
    };

    template <class T>
    class TTable {
    private:
        using TImpl = T;
        using TData = typename TImpl::TData;

        const TData Data;
        const size_t MSize;

    public:
        using TStored = typename TImpl::TStored;
        using TValueRef = typename TImpl::TValueRef;
        using TValuePtr = typename TImpl::TValuePtr;

    private:
        inline TValueRef GetImpl(size_t key) const {
            TValuePtr val = TImpl::Get(Data, key);

            return TImpl::Get(val);
        }

        inline TValueRef Get(size_t key) const {
            return GetImpl(key);
        }

    public:
        TTable(TData data, size_t size)
            : Data(data)
            , MSize(size)
        {
            static_assert(std::is_pointer<TData>::value, "expect std::is_pointer<TData>::value");
        }

        inline TValueRef Get(size_t key, TValueRef value) const {
            if (key >= Size())
                return value;

            return GetImpl(key);
        }

        inline TValueRef Get(size_t key, size_t defaultKey) const {
            if (key >= Size())
                return Get(defaultKey);

            return GetImpl(key);
        }

        inline size_t Size() const {
            return MSize;
        }
    };

    const size_t UNICODE_TABLE_SHIFT = 5;
} // namespace NUnicodeTable