@@ -174,14 +174,18 @@ unsafe fn ZDICT_analyzePos(
174174 let mut pos = suffix ( start as usize ) as size_t ;
175175 let mut end = start;
176176 let mut solution = DictItem :: default ( ) ;
177+
177178 doneMarks[ pos] = 1 ;
179+
180+ // trivial repetition cases
178181 if MEM_read16 ( b. add ( pos) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
179182 == MEM_read16 ( b. add ( pos) . add ( 2 ) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
180183 || MEM_read16 ( b. add ( pos) . add ( 1 ) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
181184 == MEM_read16 ( b. add ( pos) . add ( 3 ) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
182185 || MEM_read16 ( b. add ( pos) . add ( 2 ) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
183186 == MEM_read16 ( b. add ( pos) . add ( 4 ) as * const core:: ffi:: c_void ) as core:: ffi:: c_int
184187 {
188+ // skip and mark segment
185189 let pattern16 = MEM_read16 ( b. add ( pos) . add ( 4 ) as * const core:: ffi:: c_void ) ;
186190 let mut u: u32 = 0 ;
187191 let mut patternEnd = 6u32 ;
@@ -203,6 +207,8 @@ unsafe fn ZDICT_analyzePos(
203207 }
204208 return solution;
205209 }
210+
211+ // look forward
206212 let mut length: size_t = 0 ;
207213 loop {
208214 end = end. wrapping_add ( 1 ) ;
@@ -214,6 +220,8 @@ unsafe fn ZDICT_analyzePos(
214220 break ;
215221 }
216222 }
223+
224+ // look backward
217225 let mut length_0: size_t = 0 ;
218226 loop {
219227 length_0 = ZDICT_count (
@@ -227,6 +235,8 @@ unsafe fn ZDICT_analyzePos(
227235 break ;
228236 }
229237 }
238+
239+ // exit if not found a minimum number of repetitions
230240 if end. wrapping_sub ( start) < minRatio {
231241 let mut idx: u32 = 0 ;
232242 idx = start;
@@ -236,10 +246,12 @@ unsafe fn ZDICT_analyzePos(
236246 }
237247 return solution;
238248 }
249+
239250 let mut i: core:: ffi:: c_int = 0 ;
240251 let mut mml: u32 = 0 ;
241252 let mut refinedStart = start;
242253 let mut refinedEnd = end;
254+
243255 if notificationLevel >= 4 {
244256 eprintln ! ( ) ;
245257 eprint ! (
@@ -250,6 +262,7 @@ unsafe fn ZDICT_analyzePos(
250262 ) ;
251263 eprintln ! ( ) ;
252264 }
265+
253266 mml = MINMATCHLENGTH as u32 ;
254267 loop {
255268 let mut currentChar = 0 ;
@@ -285,6 +298,8 @@ unsafe fn ZDICT_analyzePos(
285298 refinedEnd = refinedStart. wrapping_add ( selectedCount) ;
286299 mml = mml. wrapping_add ( 1 ) ;
287300 }
301+
302+ // evaluate gain based on new dict
288303 start = refinedStart;
289304 pos = suffix ( refinedStart as usize ) as size_t ;
290305 end = start;
@@ -293,6 +308,8 @@ unsafe fn ZDICT_analyzePos(
293308 0 ,
294309 :: core:: mem:: size_of :: < [ u32 ; 64 ] > ( ) ,
295310 ) ;
311+
312+ // look forward
296313 let mut length_1: size_t = 0 ;
297314 loop {
298315 end = end. wrapping_add ( 1 ) ;
@@ -309,6 +326,8 @@ unsafe fn ZDICT_analyzePos(
309326 break ;
310327 }
311328 }
329+
330+ // look backward
312331 let mut length_2 = MINMATCHLENGTH ;
313332 while ( length_2 >= MINMATCHLENGTH ) as core:: ffi:: c_int & ( start > 0 ) as core:: ffi:: c_int != 0 {
314333 length_2 = ZDICT_count (
@@ -324,6 +343,8 @@ unsafe fn ZDICT_analyzePos(
324343 start = start. wrapping_sub ( 1 ) ;
325344 }
326345 }
346+
347+ // largest useful length
327348 ptr:: write_bytes (
328349 cumulLength. as_mut_ptr ( ) as * mut u8 ,
329350 0 ,
@@ -347,6 +368,8 @@ unsafe fn ZDICT_analyzePos(
347368 u_0 = u_0. wrapping_sub ( 1 ) ;
348369 }
349370 maxLength = u_0 as size_t ;
371+
372+ // reduce maxLength in case of final into repetitive data
350373 let mut l = maxLength as u32 ;
351374 let c = * b. add ( pos. wrapping_add ( maxLength) . wrapping_sub ( 1 ) ) ;
352375 while * b. add ( pos. wrapping_add ( l as size_t ) . wrapping_sub ( 2 ) ) as core:: ffi:: c_int
@@ -356,8 +379,10 @@ unsafe fn ZDICT_analyzePos(
356379 }
357380 maxLength = l as size_t ;
358381 if maxLength < MINMATCHLENGTH {
359- return solution;
382+ return solution; // skip: no long-enough solution available
360383 }
384+
385+ // calculate savings
361386 * savings. as_mut_ptr ( ) . add ( 5 ) = 0 ;
362387 let mut u_1: core:: ffi:: c_uint = 0 ;
363388 u_1 = MINMATCHLENGTH as core:: ffi:: c_uint ;
@@ -368,6 +393,7 @@ unsafe fn ZDICT_analyzePos(
368393 ) ;
369394 u_1 = u_1. wrapping_add ( 1 ) ;
370395 }
396+
371397 if notificationLevel >= 4 {
372398 eprintln ! (
373399 "Selected dict at position {}, of length {} : saves {} (ratio: {:.2}) " ,
@@ -378,9 +404,12 @@ unsafe fn ZDICT_analyzePos(
378404 / maxLength as core:: ffi:: c_double,
379405 ) ;
380406 }
407+
381408 solution. pos = pos as u32 ;
382409 solution. length = maxLength as u32 ;
383410 solution. savings = * savings. as_mut_ptr ( ) . add ( maxLength) ;
411+
412+ // mark positions done
384413 let mut id_0: u32 = 0 ;
385414 id_0 = start;
386415 while id_0 < end {
@@ -407,6 +436,7 @@ unsafe fn ZDICT_analyzePos(
407436 }
408437 id_0 = id_0. wrapping_add ( 1 ) ;
409438 }
439+
410440 solution
411441}
412442
0 commit comments