aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/comptrie/opaque_trie_iterator.h
blob: 8e615d7e707df36ddf3d2c6ac915015a67f3268e (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#pragma once

#include "comptrie_impl.h"
#include "node.h"
#include "key_selector.h"
#include "leaf_skipper.h"

#include <util/generic/vector.h>
#include <util/generic/yexception.h>

namespace NCompactTrie {
    class ILeafSkipper; 

    class TFork { // Auxiliary class for a branching point in the iterator 
    public: 
        TNode Node; 
        const char* Data; 
        size_t Limit; // valid data is in range [Data + Node.GetOffset(), Data + Limit) 
        TDirection CurrentDirection; 

    public: 
        TFork(const char* data, size_t offset, size_t limit, const ILeafSkipper& skipper); 

        bool operator==(const TFork& rhs) const; 

        bool HasLabelInKey() const { 
            return CurrentDirection == D_NEXT || CurrentDirection == D_FINAL; 
        } 

        bool NextDirection(); 
        bool PrevDirection(); 
        void LastDirection(); 

        bool HasDirection(TDirection direction) const { 
            return Node.GetOffsetByDirection(direction); 
        } 
        // If the fork doesn't have the specified direction, 
        // leaves the direction intact and returns false. 
        // Otherwise returns true. 
        bool SetDirection(TDirection direction); 
        TFork NextFork(const ILeafSkipper& skipper) const; 

        char GetLabel() const; 
        size_t GetValueOffset() const; 
    }; 
 
    inline TFork TFork::NextFork(const ILeafSkipper& skipper) const { 
        Y_ASSERT(CurrentDirection != D_FINAL); 
        size_t offset = Node.GetOffsetByDirection(CurrentDirection); 
        return TFork(Data, offset, Limit, skipper); 
    }

    //------------------------------------------------------------------------------------------------ 
    class TForkStack { 
    public: 
        void Push(const TFork& fork) { 
            if (TopHasLabelInKey()) { 
                Key.push_back(Top().GetLabel()); 
            } 
            Forks.push_back(fork); 
        } 

        void Pop() { 
            Forks.pop_back(); 
            if (TopHasLabelInKey()) { 
                Key.pop_back(); 
            } 
        } 

        TFork& Top() { 
            return Forks.back(); 
        }
        const TFork& Top() const { 
            return Forks.back(); 
        } 

        bool Empty() const { 
            return Forks.empty(); 
        } 

        void Clear() { 
            Forks.clear(); 
            Key.clear(); 
        }

        bool operator==(const TForkStack& other) const { 
            return Forks == other.Forks; 
        } 
        bool operator!=(const TForkStack& other) const { 
            return !(*this == other); 
        } 

        TString GetKey() const; 
        size_t MeasureKey() const; 

    private: 
        TVector<TFork> Forks; 
        TString Key; 

    private: 
        bool TopHasLabelInKey() const { 
            return !Empty() && Top().HasLabelInKey(); 
        } 
        bool HasEmptyKey() const; 
    }; 

    //------------------------------------------------------------------------------------------------ 

    template <class TSymbol> 
    struct TConvertRawKey { 
        typedef typename TCompactTrieKeySelector<TSymbol>::TKey TKey; 
        static TKey Get(const TString& rawkey) { 
            TKey result; 
            const size_t sz = rawkey.size(); 
            result.reserve(sz / sizeof(TSymbol)); 
            for (size_t i = 0; i < sz; i += sizeof(TSymbol)) { 
                TSymbol sym = 0; 
                for (size_t j = 0; j < sizeof(TSymbol); j++) { 
                    if (sizeof(TSymbol) <= 1) 
                        sym = 0; 
                    else 
                        sym <<= 8; 
                    if (i + j < sz) 
                        sym |= TSymbol(0x00FF & rawkey[i + j]); 
                } 
                result.push_back(sym); 
            } 
            return result; 
        } 

        static size_t Size(size_t rawsize) { 
            return rawsize / sizeof(TSymbol); 
        } 
    }; 

    template <> 
    struct TConvertRawKey<char> { 
        static TString Get(const TString& rawkey) { 
            return rawkey; 
        } 

        static size_t Size(size_t rawsize) { 
            return rawsize; 
        }
    }; 

    //------------------------------------------------------------------------------------------------ 
    class TOpaqueTrieIterator { // Iterator stuff. Stores a stack of visited forks. 
    public: 
        TOpaqueTrieIterator(const TOpaqueTrie& trie, const char* emptyValue, bool atend, 
                            size_t maxKeyLength = size_t(-1)); 

        bool operator==(const TOpaqueTrieIterator& rhs) const; 
        bool operator!=(const TOpaqueTrieIterator& rhs) const { 
            return !(*this == rhs); 
        } 

        bool Forward(); 
        bool Backward(); 

        template <class TSymbol> 
        bool UpperBound(const typename TCompactTrieKeySelector<TSymbol>::TKeyBuf& key); // True if matched exactly. 

        template <class TSymbol> 
        typename TCompactTrieKeySelector<TSymbol>::TKey GetKey() const { 
            return TConvertRawKey<TSymbol>::Get(GetNarrowKey()); 
        } 

        template <class TSymbol> 
        size_t MeasureKey() const { 
            return TConvertRawKey<TSymbol>::Size(MeasureNarrowKey()); 
        } 

        TString GetNarrowKey() const { 
            return Forks.GetKey(); 
        } 
        size_t MeasureNarrowKey() const { 
            return Forks.MeasureKey(); 
        } 

        const char* GetValuePtr() const; // 0 if none 
        const TNode& GetNode() const {   // Could be called for non-empty key and not AtEnd. 
            return Forks.Top().Node; 
        } 
        const TOpaqueTrie& GetTrie() const { 
            return Trie; 
        } 

    private: 
        TOpaqueTrie Trie; 
        TForkStack Forks; 
        const char* const EmptyValue; 
        bool AtEmptyValue; 
        const size_t MaxKeyLength; 

    private: 
        bool HasMaxKeyLength() const; 

        template <class TSymbol> 
        int LongestPrefix(const typename TCompactTrieKeySelector<TSymbol>::TKeyBuf& key); // Used in UpperBound. 
    }; 

    template <class TSymbol>
    int TOpaqueTrieIterator::LongestPrefix(const typename TCompactTrieKeySelector<TSymbol>::TKeyBuf& key) { 
        Forks.Clear(); 
        TFork next(Trie.Data, 0, Trie.Length, Trie.SkipFunction); 
        for (size_t i = 0; i < key.size(); i++) { 
            TSymbol symbol = key[i]; 
            const bool isLastSymbol = (i + 1 == key.size()); 
            for (i64 shift = (i64)NCompactTrie::ExtraBits<TSymbol>(); shift >= 0; shift -= 8) { 
                const unsigned char label = (unsigned char)(symbol >> shift); 
                const bool isLastByte = (isLastSymbol && shift == 0); 
                do { 
                    Forks.Push(next); 
                    TFork& top = Forks.Top(); 
                    if (label < (unsigned char)top.GetLabel()) { 
                        if (!top.SetDirection(D_LEFT)) 
                            return 1; 
                    } else if (label > (unsigned char)top.GetLabel()) { 
                        if (!top.SetDirection(D_RIGHT)) { 
                            Forks.Pop(); // We don't pass this fork on the way to the upper bound. 
                            return -1; 
                        } 
                    } else if (isLastByte) { // Here and below label == top.GetLabel(). 
                        if (top.SetDirection(D_FINAL)) { 
                            return 0; // Skip the NextFork() call at the end of the cycle. 
                        } else { 
                            top.SetDirection(D_NEXT); 
                            return 1; 
                        } 
                    } else if (!top.SetDirection(D_NEXT)) { 
                        top.SetDirection(D_FINAL); 
                        return -1;
                    }
                    next = top.NextFork(Trie.SkipFunction); 
                } while (Forks.Top().CurrentDirection != D_NEXT); // Proceed to the next byte. 
            } 
        }
        // We get here only if the key was empty. 
        Forks.Push(next); 
        return 1; 
    }

    template <class TSymbol> 
    bool TOpaqueTrieIterator::UpperBound(const typename TCompactTrieKeySelector<TSymbol>::TKeyBuf& key) { 
        Forks.Clear(); 
        if (key.empty() && EmptyValue) { 
            AtEmptyValue = true; 
            return true; 
        } else { 
            AtEmptyValue = false; 
        }
        const int defect = LongestPrefix<TSymbol>(key); 
        if (defect > 0) { 
            // Continue the constructed forks with the smallest key possible. 
            while (Forks.Top().CurrentDirection != D_FINAL) { 
                TFork next = Forks.Top().NextFork(Trie.SkipFunction); 
                Forks.Push(next); 
            } 
        } else if (defect < 0) { 
            Forward(); 
        } 
        return defect == 0; 
    }
 
}