okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge the Aurora gfx1151 branch (f-halo-int + HCD_EXACT + the expert bundle) onto 0.1.39bc6b6f87a

main
3.4 KiB98 linesraw
1// src/spec/suffix_drafter.cpp - see include/strata/spec/suffix_drafter.hpp.
2#include "strata/spec/suffix_drafter.hpp"
3
4#include <algorithm>
5
6namespace strata::spec {
7
8namespace {
9uint64_t mix(uint64_t x) {
10    x ^= x >> 33;
11    x *= 0xff51afd7ed558ccdull;
12    x ^= x >> 33;
13    x *= 0xc4ceb9fe1a85ec53ull;
14    return x ^ (x >> 33);
15}
16}  // namespace
17
18SuffixDrafter::SuffixDrafter(int min_match, int max_match, size_t capacity_tokens)
19    : min_match_(std::max(3, min_match)), max_match_(std::max(min_match, max_match)) {
20    size_t cap = 1;
21    while (cap < capacity_tokens * 2) cap <<= 1;          // load factor <= 0.5 at the nominal capacity
22    table_.assign(cap, Slot{});
23    mask_ = cap - 1;
24    hist_.reserve(capacity_tokens);
25}
26
27void SuffixDrafter::reset() {
28    hist_.clear();
29    std::fill(table_.begin(), table_.end(), Slot{});
30    last_match_ = 0;
31}
32
33uint64_t SuffixDrafter::key_at(size_t end) const {
34    const uint64_t a = (uint32_t) hist_[end - 2], b = (uint32_t) hist_[end - 1], c = (uint32_t) hist_[end];
35    return mix(a * 0x9E3779B97F4A7C15ull ^ mix(b + 0x632BE59BD9B4E019ull) ^ (c << 1)) | 1ull;   // never 0
36}
37
38SuffixDrafter::Slot* SuffixDrafter::find_slot(uint64_t key, bool insert) {
39    for (size_t i = key & mask_, probes = 0; probes <= mask_; i = (i + 1) & mask_, ++probes) {
40        Slot& s = table_[i];
41        if (s.key == key) return &s;
42        if (s.key == 0) {
43            if (!insert) return nullptr;
44            s.key = key;
45            return &s;
46        }
47    }
48    return nullptr;                                         // table full: the history outgrew its capacity
49}
50
51void SuffixDrafter::append(const int32_t* tokens, size_t n) {
52    for (size_t i = 0; i < n; ++i) {
53        hist_.push_back(tokens[i]);
54        const size_t end = hist_.size() - 1;
55        if (end < 2) continue;
56        Slot* s = find_slot(key_at(end), true);
57        if (s == nullptr) continue;
58        for (int w = WAYS - 1; w > 0; --w) s->pos[w] = s->pos[w - 1];
59        s->pos[0] = (uint32_t) end;
60        if (s->n < WAYS) ++s->n;
61    }
62}
63
64int SuffixDrafter::propose(int max_k, int32_t* out) {
65    last_match_ = 0;
66    const size_t n = hist_.size();
67    if (n < 4 || max_k <= 0) return 0;
68    const size_t cur = n - 1;
69    const Slot* s = find_slot(key_at(cur), false);
70    if (s == nullptr) return 0;
71    size_t best_end = 0;
72    int best_len = 0;
73    for (int w = 0; w < s->n; ++w) {
74        const size_t p = s->pos[w];
75        if (p >= cur) continue;                             // the current suffix itself
76        int len = 0;
77        while (len < max_match_ && len <= (int) p && hist_[p - len] == hist_[cur - len]) ++len;
78        if (len > best_len) { best_len = len; best_end = p; }   // most recent first, so ties keep the newer
79    }
80    if (best_len < min_match_) return 0;
81    last_match_ = best_len;
82    int k = 0;
83    // The continuation may run into the current suffix (periodic text); reading history up to `cur` is valid.
84    for (size_t q = best_end + 1; q <= cur && k < max_k; ++q) out[k++] = hist_[q];
85    return k;
86}
87
88int SuffixDrafter::propose_after(const int32_t* extra, int n_extra, int max_k, int32_t* out) {
89    if (n_extra <= 0) return propose(max_k, out);
90    // the index holds the history's trigrams only: appended without indexing, the extra tokens just form the suffix
91    const size_t n0 = hist_.size();
92    hist_.insert(hist_.end(), extra, extra + n_extra);
93    const int k = propose(max_k, out);
94    hist_.resize(n0);
95    return k;
96}
97
98}  // namespace strata::spec