@@ -330,29 +330,20 @@ static void apultra_optimize_forward(apultra_compressor *pCompressor, const unsi
330330
331331 nLiteralScore = nShortOffset ? 3 : 1 ;
332332
333- for (j = 0 ; j < nArrivalsPerPosition && cur_arrival [j ].from_slot ; j ++ ) {
334- int nPrevCost = cur_arrival [j ].cost & 0x3fffffff ;
335- int nCodingChoiceCost = nPrevCost + nLiteralCost ;
336- int nScore = cur_arrival [j ].score + nLiteralScore ;
337-
338- apultra_arrival * pDestSlots = & cur_arrival [nArrivalsPerPosition ];
339- if (nCodingChoiceCost < pDestSlots [nArrivalsPerPosition - 1 ].cost ||
340- (nCodingChoiceCost == pDestSlots [nArrivalsPerPosition - 1 ].cost && nScore < pDestSlots [nArrivalsPerPosition - 1 ].score )) {
341- int nRepOffset = cur_arrival [j ].rep_offset ;
342- int exists = 0 ;
343-
344- for (n = 0 ;
345- n < nArrivalsPerPosition && pDestSlots [n ].cost < nCodingChoiceCost ;
346- n ++ ) {
347- if (pDestSlots [n ].rep_offset == nRepOffset ) {
348- exists = 1 ;
349- break ;
350- }
351- }
352-
353- if (!exists ) {
354- for (;
355- n < nArrivalsPerPosition && pDestSlots [n ].cost == nCodingChoiceCost && nScore >= pDestSlots [n ].score ;
333+ if (cur_arrival [nArrivalsPerPosition ].from_slot ) {
334+ for (j = 0 ; j < nArrivalsPerPosition && cur_arrival [j ].from_slot ; j ++ ) {
335+ int nPrevCost = cur_arrival [j ].cost & 0x3fffffff ;
336+ int nCodingChoiceCost = nPrevCost + nLiteralCost ;
337+ int nScore = cur_arrival [j ].score + nLiteralScore ;
338+
339+ apultra_arrival * pDestSlots = & cur_arrival [nArrivalsPerPosition ];
340+ if (nCodingChoiceCost < pDestSlots [nArrivalsPerPosition - 1 ].cost ||
341+ (nCodingChoiceCost == pDestSlots [nArrivalsPerPosition - 1 ].cost && nScore < pDestSlots [nArrivalsPerPosition - 1 ].score )) {
342+ int nRepOffset = cur_arrival [j ].rep_offset ;
343+ int exists = 0 ;
344+
345+ for (n = 0 ;
346+ n < nArrivalsPerPosition && pDestSlots [n ].cost < nCodingChoiceCost ;
356347 n ++ ) {
357348 if (pDestSlots [n ].rep_offset == nRepOffset ) {
358349 exists = 1 ;
@@ -361,46 +352,76 @@ static void apultra_optimize_forward(apultra_compressor *pCompressor, const unsi
361352 }
362353
363354 if (!exists ) {
364- if (n < nArrivalsPerPosition ) {
365- int nn ;
366-
367- for (nn = n ;
368- nn < nArrivalsPerPosition && pDestSlots [nn ].cost == nCodingChoiceCost ;
369- nn ++ ) {
370- if (pDestSlots [nn ].rep_offset == nRepOffset ) {
371- exists = 1 ;
372- break ;
373- }
355+ for (;
356+ n < nArrivalsPerPosition && pDestSlots [n ].cost == nCodingChoiceCost && nScore >= pDestSlots [n ].score ;
357+ n ++ ) {
358+ if (pDestSlots [n ].rep_offset == nRepOffset ) {
359+ exists = 1 ;
360+ break ;
374361 }
362+ }
375363
376- if (!exists ) {
377- int z ;
364+ if (!exists ) {
365+ if (n < nArrivalsPerPosition ) {
366+ int nn ;
378367
379- for (z = n ; z < nArrivalsPerPosition - 1 && pDestSlots [z ].from_slot ; z ++ ) {
380- if (pDestSlots [z ].rep_offset == nRepOffset )
368+ for (nn = n ;
369+ nn < nArrivalsPerPosition && pDestSlots [nn ].cost == nCodingChoiceCost ;
370+ nn ++ ) {
371+ if (pDestSlots [nn ].rep_offset == nRepOffset ) {
372+ exists = 1 ;
381373 break ;
374+ }
382375 }
383376
384- apultra_arrival * pDestArrival = & pDestSlots [n ];
385- memmove (& pDestSlots [n + 1 ],
386- & pDestSlots [n ],
387- sizeof (apultra_arrival ) * (z - n ));
388-
389- pDestArrival -> cost = nCodingChoiceCost ;
390- pDestArrival -> from_pos = i ;
391- pDestArrival -> from_slot = j + 1 ;
392- pDestArrival -> follows_literal = 1 ;
393- pDestArrival -> rep_offset = nRepOffset ;
394- pDestArrival -> short_offset = nShortOffset ;
395- pDestArrival -> rep_pos = cur_arrival [j ].rep_pos ;
396- pDestArrival -> match_len = nShortLen ;
397- pDestArrival -> score = nScore ;
377+ if (!exists ) {
378+ int z ;
379+
380+ for (z = n ; z < nArrivalsPerPosition - 1 && pDestSlots [z ].from_slot ; z ++ ) {
381+ if (pDestSlots [z ].rep_offset == nRepOffset )
382+ break ;
383+ }
384+
385+ apultra_arrival * pDestArrival = & pDestSlots [n ];
386+ memmove (& pDestSlots [n + 1 ],
387+ & pDestSlots [n ],
388+ sizeof (apultra_arrival ) * (z - n ));
389+
390+ pDestArrival -> cost = nCodingChoiceCost ;
391+ pDestArrival -> from_pos = i ;
392+ pDestArrival -> from_slot = j + 1 ;
393+ pDestArrival -> follows_literal = 1 ;
394+ pDestArrival -> rep_offset = nRepOffset ;
395+ pDestArrival -> short_offset = nShortOffset ;
396+ pDestArrival -> rep_pos = cur_arrival [j ].rep_pos ;
397+ pDestArrival -> match_len = nShortLen ;
398+ pDestArrival -> score = nScore ;
399+ }
398400 }
399401 }
400402 }
401403 }
402404 }
403405 }
406+ else {
407+ for (j = 0 ; j < nArrivalsPerPosition && cur_arrival [j ].from_slot ; j ++ ) {
408+ int nPrevCost = cur_arrival [j ].cost & 0x3fffffff ;
409+ int nCodingChoiceCost = nPrevCost + nLiteralCost ;
410+ int nScore = cur_arrival [j ].score + nLiteralScore ;
411+
412+ apultra_arrival * pDestArrival = & cur_arrival [nArrivalsPerPosition + j ];
413+
414+ pDestArrival -> cost = nCodingChoiceCost ;
415+ pDestArrival -> from_pos = i ;
416+ pDestArrival -> from_slot = j + 1 ;
417+ pDestArrival -> follows_literal = 1 ;
418+ pDestArrival -> rep_offset = cur_arrival [j ].rep_offset ;
419+ pDestArrival -> short_offset = nShortOffset ;
420+ pDestArrival -> rep_pos = cur_arrival [j ].rep_pos ;
421+ pDestArrival -> match_len = nShortLen ;
422+ pDestArrival -> score = nScore ;
423+ }
424+ }
404425
405426 if (i == nStartOffset && (nBlockFlags & 1 )) continue ;
406427
0 commit comments