+ initial_state.lz_repeat_match_state[i] = ctx->lz_repeat_match_range_encoders[i].state;
+
+ ctx->optimum[1].state = initial_state;
+ ctx->optimum[1].cost = lzms_get_literal_cost(ctx,
+ &ctx->optimum[1].state,
+ *(lz_bt_get_window_ptr(&ctx->mf) - 1));
+ ctx->optimum[1].prev.link = 0;
+
+ for (u32 i = 0, len = 2; i < num_matches; i++) {
+ u32 offset = matches[i].offset;
+ struct lzms_adaptive_state state;
+ u32 position_cost;
+
+ state = initial_state;
+ position_cost = 0;
+ position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
+
+ do {
+ u32 cost;
+
+ cost = position_cost;
+ cost += lzms_get_length_cost(&ctx->length_encoder, len);
+
+ ctx->optimum[len].state = state;
+ ctx->optimum[len].prev.link = 0;
+ ctx->optimum[len].prev.match_offset = offset;
+ ctx->optimum[len].cost = cost;
+ } while (++len <= matches[i].len);
+ }
+ end_pos = longest_len;
+
+ if (longest_rep_len >= ctx->params.min_match_length) {
+ struct lzms_adaptive_state state;
+ u32 cost;
+
+ while (end_pos < longest_rep_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = initial_state;
+ cost = lzms_get_lz_match_cost(ctx,
+ &state,
+ longest_rep_len,
+ longest_rep_offset);
+ if (cost <= ctx->optimum[longest_rep_len].cost) {
+ ctx->optimum[longest_rep_len].state = state;
+ ctx->optimum[longest_rep_len].prev.link = 0;
+ ctx->optimum[longest_rep_len].prev.match_offset = longest_rep_offset;
+ ctx->optimum[longest_rep_len].cost = cost;
+ }
+ }
+
+ cur_pos = 0;
+ for (;;) {
+ u32 cost;
+ struct lzms_adaptive_state state;
+
+ cur_pos++;
+
+ if (cur_pos == end_pos || cur_pos == ctx->params.optim_array_length)
+ return lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ longest_rep_len = ctx->params.min_match_length - 1;
+ if (lz_bt_get_position(&ctx->mf) >= LZMS_MAX_INIT_RECENT_OFFSET) {
+ u32 limit = min(ctx->params.max_match_length,
+ lz_bt_get_remaining_size(&ctx->mf));
+ for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS; i++) {
+ u32 offset = ctx->optimum[cur_pos].state.lru.recent_offsets[i];
+ const u8 *strptr = lz_bt_get_window_ptr(&ctx->mf);
+ const u8 *matchptr = strptr - offset;
+ u32 len = 0;
+ while (len < limit && strptr[len] == matchptr[len])
+ len++;
+ if (len > longest_rep_len) {
+ longest_rep_len = len;
+ longest_rep_offset = offset;
+ }
+ }
+ }
+
+ if (longest_rep_len >= ctx->params.nice_match_length) {
+ match = lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ ctx->optimum[cur_pos].next.match_offset = longest_rep_offset;
+ ctx->optimum[cur_pos].next.link = cur_pos + longest_rep_len;
+ ctx->optimum_end_idx = cur_pos + longest_rep_len;
+
+ lzms_skip_bytes(ctx, longest_rep_len);
+
+ return match;
+ }
+
+ num_matches = lzms_get_matches(ctx, &matches);
+
+ if (num_matches) {
+ longest_len = matches[num_matches - 1].len;
+ if (longest_len >= ctx->params.nice_match_length) {
+ match = lzms_match_chooser_reverse_list(ctx, cur_pos);
+
+ ctx->optimum[cur_pos].next.match_offset =
+ matches[num_matches - 1].offset;
+ ctx->optimum[cur_pos].next.link = cur_pos + longest_len;
+ ctx->optimum_end_idx = cur_pos + longest_len;
+
+ lzms_skip_bytes(ctx, longest_len - 1);
+
+ return match;
+ }
+ } else {
+ longest_len = 1;
+ }
+
+ while (end_pos < cur_pos + longest_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = ctx->optimum[cur_pos].state;
+ cost = ctx->optimum[cur_pos].cost +
+ lzms_get_literal_cost(ctx,
+ &state,
+ *(lz_bt_get_window_ptr(&ctx->mf) - 1));
+ if (cost < ctx->optimum[cur_pos + 1].cost) {
+ ctx->optimum[cur_pos + 1].state = state;
+ ctx->optimum[cur_pos + 1].cost = cost;
+ ctx->optimum[cur_pos + 1].prev.link = cur_pos;
+ }
+
+ for (u32 i = 0, len = 2; i < num_matches; i++) {
+ u32 offset = matches[i].offset;
+ struct lzms_adaptive_state state;
+ u32 position_cost;
+
+ state = ctx->optimum[cur_pos].state;
+ position_cost = ctx->optimum[cur_pos].cost;
+ position_cost += lzms_get_lz_match_cost_nolen(ctx, &state, offset);
+
+ do {
+ u32 cost;
+
+ cost = position_cost;
+ cost += lzms_get_length_cost(&ctx->length_encoder, len);
+
+ if (cost < ctx->optimum[cur_pos + len].cost) {
+ ctx->optimum[cur_pos + len].state = state;
+ ctx->optimum[cur_pos + len].prev.link = cur_pos;
+ ctx->optimum[cur_pos + len].prev.match_offset = offset;
+ ctx->optimum[cur_pos + len].cost = cost;
+ }
+ } while (++len <= matches[i].len);
+ }
+
+ if (longest_rep_len >= ctx->params.min_match_length) {
+
+ while (end_pos < cur_pos + longest_rep_len)
+ ctx->optimum[++end_pos].cost = MC_INFINITE_COST;
+
+ state = ctx->optimum[cur_pos].state;
+
+ cost = ctx->optimum[cur_pos].cost +
+ lzms_get_lz_match_cost(ctx,
+ &state,
+ longest_rep_len,
+ longest_rep_offset);
+ if (cost <= ctx->optimum[cur_pos + longest_rep_len].cost) {
+ ctx->optimum[cur_pos + longest_rep_len].state =
+ state;
+ ctx->optimum[cur_pos + longest_rep_len].prev.link =
+ cur_pos;
+ ctx->optimum[cur_pos + longest_rep_len].prev.match_offset =
+ longest_rep_offset;
+ ctx->optimum[cur_pos + longest_rep_len].cost =
+ cost;
+ }
+ }
+ }