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