-/* Advance the suffix array match-finder to the next position. */
-static void
-lzx_lz_update_salink(input_idx_t i,
- const input_idx_t SA[restrict],
- const input_idx_t ISA[restrict],
- struct salink link[restrict])
-{
- /* r = Rank of the suffix at the current position. */
- const input_idx_t r = ISA[i];
-
- /* next = rank of LOWEST ranked suffix that is ranked HIGHER than the
- * current suffix AND has a LOWER position, or -1 if none exists. */
- const input_idx_t next = link[r].next;
-
- /* prev = rank of HIGHEST ranked suffix that is ranked LOWER than the
- * current suffix AND has a LOWER position, or -1 if none exists. */
- const input_idx_t prev = link[r].prev;
-
- /* Link the suffix at the current position into the linked list that
- * contains all suffixes in the suffix array that are appear at or
- * before the current position, sorted by rank.
- *
- * Save the values of all fields we overwrite so that rollback is
- * possible. */
- if (next != (input_idx_t)~0U) {
-
- link[next].prev = r;
- link[next].lcpprev = link[r].lcpnext;
- }
-
- if (prev != (input_idx_t)~0U) {
-
- link[prev].next = r;
- link[prev].lcpnext = link[r].lcpprev;
- }
-}
-
-/*
- * Use the suffix array match-finder to retrieve a list of LZ matches at the
- * current position.
- *
- * [in] @i Current position in the window.
- * [in] @SA Suffix array for the window.
- * [in] @ISA Inverse suffix array for the window.
- * [inout] @link Suffix array links used internally by the match-finder.
- * [out] @matches The (length, offset) pairs of the resulting matches will
- * be written here, sorted in decreasing order by
- * length. All returned lengths will be unique.
- * [in] @queue Recently used match offsets, used when evaluating the
- * cost of matches.
- * [in] @min_match_len Minimum match length to return.
- * [in] @max_matches_to_consider Maximum number of matches to consider at
- * the position.
- * [in] @max_matches_to_return Maximum number of matches to return.
- *
- * The return value is the number of matches found and written to @matches.
- */
-static unsigned
-lzx_lz_get_matches(const input_idx_t i,
- const input_idx_t SA[const restrict],
- const input_idx_t ISA[const restrict],
- struct salink link[const restrict],
- struct raw_match matches[const restrict],
- const struct lzx_lru_queue * const restrict queue,
- const unsigned min_match_len,
- const u32 max_matches_to_consider,
- const u32 max_matches_to_return)
-{
- /* r = Rank of the suffix at the current position. */
- const input_idx_t r = ISA[i];
-
- /* Prepare for searching the current position. */
- lzx_lz_update_salink(i, SA, ISA, link);
-
- /* L = rank of next suffix to the left;
- * R = rank of next suffix to the right;
- * lenL = length of match between current position and the suffix with rank L;
- * lenR = length of match between current position and the suffix with rank R.
- *
- * This is left and right relative to the rank of the current suffix.
- * Since the suffixes in the suffix array are sorted, the longest
- * matches are immediately to the left and right (using the linked list
- * to ignore all suffixes that occur later in the window). The match
- * length decreases the farther left and right we go. We shall keep the
- * length on both sides in sync in order to choose the lowest-cost match
- * of each length.
- */
- input_idx_t L = link[r].prev;
- input_idx_t R = link[r].next;
- input_idx_t lenL = link[r].lcpprev;
- input_idx_t lenR = link[r].lcpnext;
-
- /* nmatches = number of matches found so far. */
- unsigned nmatches = 0;
-
- /* best_cost = cost of lowest-cost match found so far.
- *
- * We keep track of this so that we can ignore shorter matches that do
- * not have lower costs than a longer matches already found.
- */
- block_cost_t best_cost = INFINITE_BLOCK_COST;
-
- /* count_remaining = maximum number of possible matches remaining to be
- * considered. */
- u32 count_remaining = max_matches_to_consider;
-
- /* pending = match currently being considered for a specific length. */
- struct raw_match pending;
- block_cost_t pending_cost;
-
- while (lenL >= min_match_len || lenR >= min_match_len)
- {
- pending.len = lenL;
- pending_cost = INFINITE_BLOCK_COST;
- block_cost_t cost;
-
- /* Extend left. */
- if (lenL >= min_match_len && lenL >= lenR) {
- for (;;) {
-
- if (--count_remaining == 0)
- goto out_save_pending;
-
- input_idx_t offset = i - SA[L];
-
- /* Save match if it has smaller cost. */
- cost = lzx_match_cost_fast(offset, queue);
- if (cost < pending_cost) {
- pending.offset = offset;
- pending_cost = cost;
- }
-
- if (link[L].lcpprev < lenL) {
- /* Match length decreased. */
-
- lenL = link[L].lcpprev;
-
- /* Save the pending match unless the
- * right side still may have matches of
- * this length to be scanned, or if a
- * previous (longer) match had lower
- * cost. */
- if (pending.len > lenR) {
- if (pending_cost < best_cost) {
- best_cost = pending_cost;
- matches[nmatches++] = pending;
- if (nmatches == max_matches_to_return)
- return nmatches;
- }
- pending.len = lenL;
- pending_cost = INFINITE_BLOCK_COST;
- }
- if (lenL < min_match_len || lenL < lenR)
- break;
- }
- L = link[L].prev;
- }
- }
-
- pending.len = lenR;
-
- /* Extend right. */
- if (lenR >= min_match_len && lenR > lenL) {
- for (;;) {
-
- if (--count_remaining == 0)
- goto out_save_pending;
-
- input_idx_t offset = i - SA[R];
-
- /* Save match if it has smaller cost. */
- cost = lzx_match_cost_fast(offset, queue);
- if (cost < pending_cost) {
- pending.offset = offset;
- pending_cost = cost;
- }
-
- if (link[R].lcpnext < lenR) {
- /* Match length decreased. */
-
- lenR = link[R].lcpnext;
-
- /* Save the pending match unless a
- * previous (longer) match had lower
- * cost. */
- if (pending_cost < best_cost) {
- matches[nmatches++] = pending;
- best_cost = pending_cost;
- if (nmatches == max_matches_to_return)
- return nmatches;
- }
-
- if (lenR < min_match_len || lenR <= lenL)
- break;
-
- pending.len = lenR;
- pending_cost = INFINITE_BLOCK_COST;
- }
- R = link[R].next;
- }
- }
- }
- goto out;
-
-out_save_pending:
- if (pending_cost != INFINITE_BLOCK_COST)
- matches[nmatches++] = pending;
-
-out:
- return nmatches;
-}
-
-