- initial_state.lru = ctx->lru.lz;
- initial_state.main_state = ctx->main_range_encoder.state;
- initial_state.match_state = ctx->match_range_encoder.state;
- initial_state.lz_match_state = ctx->lz_match_range_encoder.state;
- for (int i = 0; i < LZMS_NUM_RECENT_OFFSETS - 1; i++)
- 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;
-
- matchptr = matches;
- for (u32 len = 2; len <= longest_len; len++) {
- u32 offset = matchptr->offset;
-
- ctx->optimum[len].state = initial_state;
- ctx->optimum[len].prev.link = 0;
- ctx->optimum[len].prev.match_offset = offset;
- ctx->optimum[len].cost = lzms_get_lz_match_cost(ctx,
- &ctx->optimum[len].state,
- len, offset);
- if (len == matchptr->len)
- matchptr++;
- }
- 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;
- }
- }