- 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++;
+ 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);