normalizeCount will normalize the count of the symbols so the total is equal to the table size. If successful, compression tables will also be made ready.
(length int)
| 257 | // the total is equal to the table size. |
| 258 | // If successful, compression tables will also be made ready. |
| 259 | func (s *fseEncoder) normalizeCount(length int) error { |
| 260 | if s.reUsed { |
| 261 | return nil |
| 262 | } |
| 263 | s.optimalTableLog(length) |
| 264 | var ( |
| 265 | tableLog = s.actualTableLog |
| 266 | scale = 62 - uint64(tableLog) |
| 267 | step = (1 << 62) / uint64(length) |
| 268 | vStep = uint64(1) << (scale - 20) |
| 269 | stillToDistribute = int16(1 << tableLog) |
| 270 | largest int |
| 271 | largestP int16 |
| 272 | lowThreshold = (uint32)(length >> tableLog) |
| 273 | ) |
| 274 | if s.maxCount == length { |
| 275 | s.useRLE = true |
| 276 | return nil |
| 277 | } |
| 278 | s.useRLE = false |
| 279 | for i, cnt := range s.count[:s.symbolLen] { |
| 280 | // already handled |
| 281 | // if (count[s] == s.length) return 0; /* rle special case */ |
| 282 | |
| 283 | if cnt == 0 { |
| 284 | s.norm[i] = 0 |
| 285 | continue |
| 286 | } |
| 287 | if cnt <= lowThreshold { |
| 288 | s.norm[i] = -1 |
| 289 | stillToDistribute-- |
| 290 | } else { |
| 291 | proba := (int16)((uint64(cnt) * step) >> scale) |
| 292 | if proba < 8 { |
| 293 | restToBeat := vStep * uint64(rtbTable[proba]) |
| 294 | v := uint64(cnt)*step - (uint64(proba) << scale) |
| 295 | if v > restToBeat { |
| 296 | proba++ |
| 297 | } |
| 298 | } |
| 299 | if proba > largestP { |
| 300 | largestP = proba |
| 301 | largest = i |
| 302 | } |
| 303 | s.norm[i] = proba |
| 304 | stillToDistribute -= proba |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if -stillToDistribute >= (s.norm[largest] >> 1) { |
| 309 | // corner case, need another normalization method |
| 310 | err := s.normalizeCount2(length) |
| 311 | if err != nil { |
| 312 | return err |
| 313 | } |
| 314 | if debugAsserts { |
| 315 | err = s.validateNorm() |
| 316 | if err != nil { |
no test coverage detected