| 233 | } |
| 234 | |
| 235 | static int hash_trigrams(const char **tokens, int token_count, cbm_minhash_t *out) { |
| 236 | for (int k = 0; k < CBM_MINHASH_K; k++) { |
| 237 | out->values[k] = UINT32_MAX; |
| 238 | } |
| 239 | |
| 240 | uniq_trig_set_t uniq; |
| 241 | uniq_trig_init(&uniq, token_count > TRIGRAM_WINDOW ? token_count - TRIGRAM_WINDOW : 0); |
| 242 | char trigram_buf[TRIGRAM_BUF_LEN]; |
| 243 | |
| 244 | for (int i = 0; i + TRIGRAM_WINDOW < token_count; i++) { |
| 245 | int w = |
| 246 | trigram_structural_weight(tokens[i], tokens[i + SKIP_ONE], tokens[i + TRIGRAM_WINDOW]); |
| 247 | if (w == 0) { |
| 248 | continue; |
| 249 | } |
| 250 | |
| 251 | int len = snprintf(trigram_buf, sizeof(trigram_buf), "%s|%s|%s", tokens[i], |
| 252 | tokens[i + SKIP_ONE], tokens[i + TRIGRAM_WINDOW]); |
| 253 | if (len <= 0 || (size_t)len >= sizeof(trigram_buf)) { |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | /* A repeated trigram is the same string with the same weight: its |
| 258 | * update would lower the same minima to the same values. Only a first |
| 259 | * occurrence pays the 64 x w seeded hashes (identical fingerprints; |
| 260 | * the scaling lane had XXH3 at 15 % of all counted work). */ |
| 261 | if (uniq_trig_insert(&uniq, XXH3_64bits(trigram_buf, (size_t)len)) != UNIQ_PRESENT) { |
| 262 | weighted_minhash_update(out, trigram_buf, len, w); |
| 263 | } |
| 264 | } |
| 265 | return uniq.count; |
| 266 | } |
| 267 | |
| 268 | bool cbm_minhash_compute(TSNode func_body, const char *source, int language, cbm_minhash_t *out) { |
| 269 | (void)source; |
no test coverage detected