aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/comptrie/opaque_trie_iterator.cpp
blob: 7434e3dbc54a9a7f2942d01b74b65048309fb40c (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
#include "opaque_trie_iterator.h"
#include "comptrie_impl.h"
#include "node.h"

namespace NCompactTrie {
    TOpaqueTrieIterator::TOpaqueTrieIterator(const TOpaqueTrie& trie, const char* emptyValue, bool atend,
                                             size_t maxKeyLength)
        : Trie(trie)
        , EmptyValue(emptyValue)
        , AtEmptyValue(emptyValue && !atend)
        , MaxKeyLength(maxKeyLength)
    {
        if (!AtEmptyValue && !atend)
            Forward();
    }

    bool TOpaqueTrieIterator::operator==(const TOpaqueTrieIterator& rhs) const {
        return (Trie == rhs.Trie &&
                Forks == rhs.Forks &&
                EmptyValue == rhs.EmptyValue &&
                AtEmptyValue == rhs.AtEmptyValue &&
                MaxKeyLength == rhs.MaxKeyLength);
    }
 
    bool TOpaqueTrieIterator::HasMaxKeyLength() const {
        return MaxKeyLength != size_t(-1) && MeasureNarrowKey() == MaxKeyLength;
    }
 
    bool TOpaqueTrieIterator::Forward() {
        if (AtEmptyValue) {
            AtEmptyValue = false;
            bool res = Forward(); // TODO delete this after format change
            if (res && MeasureNarrowKey() != 0) {
                return res; // there was not "\0" key
            }
            // otherwise we are skipping "\0" key
        } 
 
        if (!Trie.Length)
            return false;
 
        if (Forks.Empty()) {
            TFork fork(Trie.Data, 0, Trie.Length, Trie.SkipFunction);
            Forks.Push(fork);
        } else {
            TFork* topFork = &Forks.Top();
            while (!topFork->NextDirection()) {
                if (topFork->Node.GetOffset() >= Trie.Length)
                    return false;
                Forks.Pop();
                if (Forks.Empty())
                    return false;
                topFork = &Forks.Top();
            }
        }
 
        Y_ASSERT(!Forks.Empty());
        while (Forks.Top().CurrentDirection != D_FINAL && !HasMaxKeyLength()) {
            TFork nextFork = Forks.Top().NextFork(Trie.SkipFunction);
            Forks.Push(nextFork);
        }
        TFork& top = Forks.Top();
        static_assert(D_FINAL < D_NEXT, "relative order of NEXT and FINAL directions has changed");
        if (HasMaxKeyLength() && top.CurrentDirection == D_FINAL && top.HasDirection(D_NEXT)) {
            top.NextDirection();
        }
        return true;
    } 
 
    bool TOpaqueTrieIterator::Backward() {
        if (AtEmptyValue)
            return false;

        if (!Trie.Length) {
            if (EmptyValue) {
                // A trie that has only the empty value;
                // we are not at the empty value, so move to it.
                AtEmptyValue = true;
                return true;
            } else {
                // Empty trie.
                return false;
            }
        }

        if (Forks.Empty()) {
            TFork fork(Trie.Data, 0, Trie.Length, Trie.SkipFunction);
            fork.LastDirection();
            Forks.Push(fork);
        } else {
            TFork* topFork = &Forks.Top();
            while (!topFork->PrevDirection()) {
                if (topFork->Node.GetOffset() >= Trie.Length)
                    return false;
                Forks.Pop();
                if (!Forks.Empty()) {
                    topFork = &Forks.Top();
                } else {
                    // When there are no more forks,
                    // we have to iterate over the empty value.
                    if (!EmptyValue)
                        return false;
                    AtEmptyValue = true;
                    return true;
                }
            }
        }

        Y_ASSERT(!Forks.Empty());
        while (Forks.Top().CurrentDirection != D_FINAL && !HasMaxKeyLength()) {
            TFork nextFork = Forks.Top().NextFork(Trie.SkipFunction);
            nextFork.LastDirection();
            Forks.Push(nextFork);
        }
        TFork& top = Forks.Top();
        static_assert(D_FINAL < D_NEXT, "relative order of NEXT and FINAL directions has changed");
        if (HasMaxKeyLength() && top.CurrentDirection == D_NEXT && top.HasDirection(D_FINAL)) {
            top.PrevDirection();
        }
        if (MeasureNarrowKey() == 0) {
            // This is the '\0' key, skip it and get to the EmptyValue.
            AtEmptyValue = true;
            Forks.Clear();
        }
        return true;
    }

    const char* TOpaqueTrieIterator::GetValuePtr() const {
        if (!Forks.Empty()) {
            const TFork& lastFork = Forks.Top();
            Y_ASSERT(lastFork.Node.IsFinal() && lastFork.CurrentDirection == D_FINAL);
            return Trie.Data + lastFork.GetValueOffset();
        }
        Y_ASSERT(AtEmptyValue);
        return EmptyValue;
    }

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

    TString TForkStack::GetKey() const {
        if (HasEmptyKey()) {
            return TString();
        }
 
        TString result(Key);
        if (TopHasLabelInKey()) {
            result.append(Top().GetLabel());
        }
        return result;
    }
 
    bool TForkStack::HasEmptyKey() const {
        // Special case: if we get a single zero label, treat it as an empty key
        // TODO delete this after format change
        if (TopHasLabelInKey()) {
            return Key.size() == 0 && Top().GetLabel() == '\0';
        } else {
            return Key.size() == 1 && Key[0] == '\0';
        }
    }

    size_t TForkStack::MeasureKey() const {
        size_t result = Key.size() + (TopHasLabelInKey() ? 1 : 0);
        if (result == 1 && HasEmptyKey()) {
            return 0;
        }
        return result;
    } 
 
    //-------------------------------------------------------------------------

    TFork::TFork(const char* data, size_t offset, size_t limit, const ILeafSkipper& skipper)
        : Node(data, offset, skipper)
        , Data(data)
        , Limit(limit)
        , CurrentDirection(TDirection(0))
    {
#if COMPTRIE_DATA_CHECK
        if (Node.GetOffset() >= Limit - 1)
            ythrow yexception() << "gone beyond the limit, data is corrupted";
#endif
        while (CurrentDirection < D_MAX && !HasDirection(CurrentDirection)) {
            ++CurrentDirection;
        }
    }
 
    bool TFork::operator==(const TFork& rhs) const {
        return (Data == rhs.Data &&
                Node.GetOffset() == rhs.Node.GetOffset() &&
                CurrentDirection == rhs.CurrentDirection);
    }
 
    inline bool TFork::NextDirection() {
        do {
            ++CurrentDirection;
        } while (CurrentDirection < D_MAX && !HasDirection(CurrentDirection));
        return CurrentDirection < D_MAX;
    }
 
    inline bool TFork::PrevDirection() {
        if (CurrentDirection == TDirection(0)) {
            return false;
        }
        do {
            --CurrentDirection;
        } while (CurrentDirection > 0 && !HasDirection(CurrentDirection));
        return HasDirection(CurrentDirection);
    }

    void TFork::LastDirection() {
        CurrentDirection = D_MAX;
        PrevDirection();
    }

    bool TFork::SetDirection(TDirection direction) {
        if (!HasDirection(direction)) {
            return false;
        }
        CurrentDirection = direction;
        return true;
    }

    char TFork::GetLabel() const {
        return Node.GetLabel();
    }

    size_t TFork::GetValueOffset() const {
        return Node.GetLeafOffset();
    }

}