compress.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // Copyright 2018 Klaus Post. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Based on work Copyright (c) 2013, Yann Collet, released under BSD License.
  5. package fse
  6. import (
  7. "errors"
  8. "fmt"
  9. )
  10. // Compress the input bytes. Input must be < 2GB.
  11. // Provide a Scratch buffer to avoid memory allocations.
  12. // Note that the output is also kept in the scratch buffer.
  13. // If input is too hard to compress, ErrIncompressible is returned.
  14. // If input is a single byte value repeated ErrUseRLE is returned.
  15. func Compress(in []byte, s *Scratch) ([]byte, error) {
  16. if len(in) <= 1 {
  17. return nil, ErrIncompressible
  18. }
  19. if len(in) > (2<<30)-1 {
  20. return nil, errors.New("input too big, must be < 2GB")
  21. }
  22. s, err := s.prepare(in)
  23. if err != nil {
  24. return nil, err
  25. }
  26. // Create histogram, if none was provided.
  27. maxCount := s.maxCount
  28. if maxCount == 0 {
  29. maxCount = s.countSimple(in)
  30. }
  31. // Reset for next run.
  32. s.clearCount = true
  33. s.maxCount = 0
  34. if maxCount == len(in) {
  35. // One symbol, use RLE
  36. return nil, ErrUseRLE
  37. }
  38. if maxCount == 1 || maxCount < (len(in)>>7) {
  39. // Each symbol present maximum once or too well distributed.
  40. return nil, ErrIncompressible
  41. }
  42. s.optimalTableLog()
  43. err = s.normalizeCount()
  44. if err != nil {
  45. return nil, err
  46. }
  47. err = s.writeCount()
  48. if err != nil {
  49. return nil, err
  50. }
  51. if false {
  52. err = s.validateNorm()
  53. if err != nil {
  54. return nil, err
  55. }
  56. }
  57. err = s.buildCTable()
  58. if err != nil {
  59. return nil, err
  60. }
  61. err = s.compress(in)
  62. if err != nil {
  63. return nil, err
  64. }
  65. s.Out = s.bw.out
  66. // Check if we compressed.
  67. if len(s.Out) >= len(in) {
  68. return nil, ErrIncompressible
  69. }
  70. return s.Out, nil
  71. }
  72. // cState contains the compression state of a stream.
  73. type cState struct {
  74. bw *bitWriter
  75. stateTable []uint16
  76. state uint16
  77. }
  78. // init will initialize the compression state to the first symbol of the stream.
  79. func (c *cState) init(bw *bitWriter, ct *cTable, tableLog uint8, first symbolTransform) {
  80. c.bw = bw
  81. c.stateTable = ct.stateTable
  82. nbBitsOut := (first.deltaNbBits + (1 << 15)) >> 16
  83. im := int32((nbBitsOut << 16) - first.deltaNbBits)
  84. lu := (im >> nbBitsOut) + first.deltaFindState
  85. c.state = c.stateTable[lu]
  86. }
  87. // encode the output symbol provided and write it to the bitstream.
  88. func (c *cState) encode(symbolTT symbolTransform) {
  89. nbBitsOut := (uint32(c.state) + symbolTT.deltaNbBits) >> 16
  90. dstState := int32(c.state>>(nbBitsOut&15)) + symbolTT.deltaFindState
  91. c.bw.addBits16NC(c.state, uint8(nbBitsOut))
  92. c.state = c.stateTable[dstState]
  93. }
  94. // encode the output symbol provided and write it to the bitstream.
  95. func (c *cState) encodeZero(symbolTT symbolTransform) {
  96. nbBitsOut := (uint32(c.state) + symbolTT.deltaNbBits) >> 16
  97. dstState := int32(c.state>>(nbBitsOut&15)) + symbolTT.deltaFindState
  98. c.bw.addBits16ZeroNC(c.state, uint8(nbBitsOut))
  99. c.state = c.stateTable[dstState]
  100. }
  101. // flush will write the tablelog to the output and flush the remaining full bytes.
  102. func (c *cState) flush(tableLog uint8) {
  103. c.bw.flush32()
  104. c.bw.addBits16NC(c.state, tableLog)
  105. c.bw.flush()
  106. }
  107. // compress is the main compression loop that will encode the input from the last byte to the first.
  108. func (s *Scratch) compress(src []byte) error {
  109. if len(src) <= 2 {
  110. return errors.New("compress: src too small")
  111. }
  112. tt := s.ct.symbolTT[:256]
  113. s.bw.reset(s.Out)
  114. // Our two states each encodes every second byte.
  115. // Last byte encoded (first byte decoded) will always be encoded by c1.
  116. var c1, c2 cState
  117. // Encode so remaining size is divisible by 4.
  118. ip := len(src)
  119. if ip&1 == 1 {
  120. c1.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-1]])
  121. c2.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-2]])
  122. c1.encodeZero(tt[src[ip-3]])
  123. ip -= 3
  124. } else {
  125. c2.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-1]])
  126. c1.init(&s.bw, &s.ct, s.actualTableLog, tt[src[ip-2]])
  127. ip -= 2
  128. }
  129. if ip&2 != 0 {
  130. c2.encodeZero(tt[src[ip-1]])
  131. c1.encodeZero(tt[src[ip-2]])
  132. ip -= 2
  133. }
  134. // Main compression loop.
  135. switch {
  136. case !s.zeroBits && s.actualTableLog <= 8:
  137. // We can encode 4 symbols without requiring a flush.
  138. // We do not need to check if any output is 0 bits.
  139. for ip >= 4 {
  140. s.bw.flush32()
  141. v3, v2, v1, v0 := src[ip-4], src[ip-3], src[ip-2], src[ip-1]
  142. c2.encode(tt[v0])
  143. c1.encode(tt[v1])
  144. c2.encode(tt[v2])
  145. c1.encode(tt[v3])
  146. ip -= 4
  147. }
  148. case !s.zeroBits:
  149. // We do not need to check if any output is 0 bits.
  150. for ip >= 4 {
  151. s.bw.flush32()
  152. v3, v2, v1, v0 := src[ip-4], src[ip-3], src[ip-2], src[ip-1]
  153. c2.encode(tt[v0])
  154. c1.encode(tt[v1])
  155. s.bw.flush32()
  156. c2.encode(tt[v2])
  157. c1.encode(tt[v3])
  158. ip -= 4
  159. }
  160. case s.actualTableLog <= 8:
  161. // We can encode 4 symbols without requiring a flush
  162. for ip >= 4 {
  163. s.bw.flush32()
  164. v3, v2, v1, v0 := src[ip-4], src[ip-3], src[ip-2], src[ip-1]
  165. c2.encodeZero(tt[v0])
  166. c1.encodeZero(tt[v1])
  167. c2.encodeZero(tt[v2])
  168. c1.encodeZero(tt[v3])
  169. ip -= 4
  170. }
  171. default:
  172. for ip >= 4 {
  173. s.bw.flush32()
  174. v3, v2, v1, v0 := src[ip-4], src[ip-3], src[ip-2], src[ip-1]
  175. c2.encodeZero(tt[v0])
  176. c1.encodeZero(tt[v1])
  177. s.bw.flush32()
  178. c2.encodeZero(tt[v2])
  179. c1.encodeZero(tt[v3])
  180. ip -= 4
  181. }
  182. }
  183. // Flush final state.
  184. // Used to initialize state when decoding.
  185. c2.flush(s.actualTableLog)
  186. c1.flush(s.actualTableLog)
  187. return s.bw.close()
  188. }
  189. // writeCount will write the normalized histogram count to header.
  190. // This is read back by readNCount.
  191. func (s *Scratch) writeCount() error {
  192. var (
  193. tableLog = s.actualTableLog
  194. tableSize = 1 << tableLog
  195. previous0 bool
  196. charnum uint16
  197. maxHeaderSize = ((int(s.symbolLen) * int(tableLog)) >> 3) + 3
  198. // Write Table Size
  199. bitStream = uint32(tableLog - minTablelog)
  200. bitCount = uint(4)
  201. remaining = int16(tableSize + 1) /* +1 for extra accuracy */
  202. threshold = int16(tableSize)
  203. nbBits = uint(tableLog + 1)
  204. )
  205. if cap(s.Out) < maxHeaderSize {
  206. s.Out = make([]byte, 0, s.br.remain()+maxHeaderSize)
  207. }
  208. outP := uint(0)
  209. out := s.Out[:maxHeaderSize]
  210. // stops at 1
  211. for remaining > 1 {
  212. if previous0 {
  213. start := charnum
  214. for s.norm[charnum] == 0 {
  215. charnum++
  216. }
  217. for charnum >= start+24 {
  218. start += 24
  219. bitStream += uint32(0xFFFF) << bitCount
  220. out[outP] = byte(bitStream)
  221. out[outP+1] = byte(bitStream >> 8)
  222. outP += 2
  223. bitStream >>= 16
  224. }
  225. for charnum >= start+3 {
  226. start += 3
  227. bitStream += 3 << bitCount
  228. bitCount += 2
  229. }
  230. bitStream += uint32(charnum-start) << bitCount
  231. bitCount += 2
  232. if bitCount > 16 {
  233. out[outP] = byte(bitStream)
  234. out[outP+1] = byte(bitStream >> 8)
  235. outP += 2
  236. bitStream >>= 16
  237. bitCount -= 16
  238. }
  239. }
  240. count := s.norm[charnum]
  241. charnum++
  242. max := (2*threshold - 1) - remaining
  243. if count < 0 {
  244. remaining += count
  245. } else {
  246. remaining -= count
  247. }
  248. count++ // +1 for extra accuracy
  249. if count >= threshold {
  250. count += max // [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[
  251. }
  252. bitStream += uint32(count) << bitCount
  253. bitCount += nbBits
  254. if count < max {
  255. bitCount--
  256. }
  257. previous0 = count == 1
  258. if remaining < 1 {
  259. return errors.New("internal error: remaining<1")
  260. }
  261. for remaining < threshold {
  262. nbBits--
  263. threshold >>= 1
  264. }
  265. if bitCount > 16 {
  266. out[outP] = byte(bitStream)
  267. out[outP+1] = byte(bitStream >> 8)
  268. outP += 2
  269. bitStream >>= 16
  270. bitCount -= 16
  271. }
  272. }
  273. out[outP] = byte(bitStream)
  274. out[outP+1] = byte(bitStream >> 8)
  275. outP += (bitCount + 7) / 8
  276. if charnum > s.symbolLen {
  277. return errors.New("internal error: charnum > s.symbolLen")
  278. }
  279. s.Out = out[:outP]
  280. return nil
  281. }
  282. // symbolTransform contains the state transform for a symbol.
  283. type symbolTransform struct {
  284. deltaFindState int32
  285. deltaNbBits uint32
  286. }
  287. // String prints values as a human readable string.
  288. func (s symbolTransform) String() string {
  289. return fmt.Sprintf("dnbits: %08x, fs:%d", s.deltaNbBits, s.deltaFindState)
  290. }
  291. // cTable contains tables used for compression.
  292. type cTable struct {
  293. tableSymbol []byte
  294. stateTable []uint16
  295. symbolTT []symbolTransform
  296. }
  297. // allocCtable will allocate tables needed for compression.
  298. // If existing tables a re big enough, they are simply re-used.
  299. func (s *Scratch) allocCtable() {
  300. tableSize := 1 << s.actualTableLog
  301. // get tableSymbol that is big enough.
  302. if cap(s.ct.tableSymbol) < tableSize {
  303. s.ct.tableSymbol = make([]byte, tableSize)
  304. }
  305. s.ct.tableSymbol = s.ct.tableSymbol[:tableSize]
  306. ctSize := tableSize
  307. if cap(s.ct.stateTable) < ctSize {
  308. s.ct.stateTable = make([]uint16, ctSize)
  309. }
  310. s.ct.stateTable = s.ct.stateTable[:ctSize]
  311. if cap(s.ct.symbolTT) < 256 {
  312. s.ct.symbolTT = make([]symbolTransform, 256)
  313. }
  314. s.ct.symbolTT = s.ct.symbolTT[:256]
  315. }
  316. // buildCTable will populate the compression table so it is ready to be used.
  317. func (s *Scratch) buildCTable() error {
  318. tableSize := uint32(1 << s.actualTableLog)
  319. highThreshold := tableSize - 1
  320. var cumul [maxSymbolValue + 2]int16
  321. s.allocCtable()
  322. tableSymbol := s.ct.tableSymbol[:tableSize]
  323. // symbol start positions
  324. {
  325. cumul[0] = 0
  326. for ui, v := range s.norm[:s.symbolLen-1] {
  327. u := byte(ui) // one less than reference
  328. if v == -1 {
  329. // Low proba symbol
  330. cumul[u+1] = cumul[u] + 1
  331. tableSymbol[highThreshold] = u
  332. highThreshold--
  333. } else {
  334. cumul[u+1] = cumul[u] + v
  335. }
  336. }
  337. // Encode last symbol separately to avoid overflowing u
  338. u := int(s.symbolLen - 1)
  339. v := s.norm[s.symbolLen-1]
  340. if v == -1 {
  341. // Low proba symbol
  342. cumul[u+1] = cumul[u] + 1
  343. tableSymbol[highThreshold] = byte(u)
  344. highThreshold--
  345. } else {
  346. cumul[u+1] = cumul[u] + v
  347. }
  348. if uint32(cumul[s.symbolLen]) != tableSize {
  349. return fmt.Errorf("internal error: expected cumul[s.symbolLen] (%d) == tableSize (%d)", cumul[s.symbolLen], tableSize)
  350. }
  351. cumul[s.symbolLen] = int16(tableSize) + 1
  352. }
  353. // Spread symbols
  354. s.zeroBits = false
  355. {
  356. step := tableStep(tableSize)
  357. tableMask := tableSize - 1
  358. var position uint32
  359. // if any symbol > largeLimit, we may have 0 bits output.
  360. largeLimit := int16(1 << (s.actualTableLog - 1))
  361. for ui, v := range s.norm[:s.symbolLen] {
  362. symbol := byte(ui)
  363. if v > largeLimit {
  364. s.zeroBits = true
  365. }
  366. for nbOccurrences := int16(0); nbOccurrences < v; nbOccurrences++ {
  367. tableSymbol[position] = symbol
  368. position = (position + step) & tableMask
  369. for position > highThreshold {
  370. position = (position + step) & tableMask
  371. } /* Low proba area */
  372. }
  373. }
  374. // Check if we have gone through all positions
  375. if position != 0 {
  376. return errors.New("position!=0")
  377. }
  378. }
  379. // Build table
  380. table := s.ct.stateTable
  381. {
  382. tsi := int(tableSize)
  383. for u, v := range tableSymbol {
  384. // TableU16 : sorted by symbol order; gives next state value
  385. table[cumul[v]] = uint16(tsi + u)
  386. cumul[v]++
  387. }
  388. }
  389. // Build Symbol Transformation Table
  390. {
  391. total := int16(0)
  392. symbolTT := s.ct.symbolTT[:s.symbolLen]
  393. tableLog := s.actualTableLog
  394. tl := (uint32(tableLog) << 16) - (1 << tableLog)
  395. for i, v := range s.norm[:s.symbolLen] {
  396. switch v {
  397. case 0:
  398. case -1, 1:
  399. symbolTT[i].deltaNbBits = tl
  400. symbolTT[i].deltaFindState = int32(total - 1)
  401. total++
  402. default:
  403. maxBitsOut := uint32(tableLog) - highBits(uint32(v-1))
  404. minStatePlus := uint32(v) << maxBitsOut
  405. symbolTT[i].deltaNbBits = (maxBitsOut << 16) - minStatePlus
  406. symbolTT[i].deltaFindState = int32(total - v)
  407. total += v
  408. }
  409. }
  410. if total != int16(tableSize) {
  411. return fmt.Errorf("total mismatch %d (got) != %d (want)", total, tableSize)
  412. }
  413. }
  414. return nil
  415. }
  416. // countSimple will create a simple histogram in s.count.
  417. // Returns the biggest count.
  418. // Does not update s.clearCount.
  419. func (s *Scratch) countSimple(in []byte) (max int) {
  420. for _, v := range in {
  421. s.count[v]++
  422. }
  423. m := uint32(0)
  424. for i, v := range s.count[:] {
  425. if v > m {
  426. m = v
  427. }
  428. if v > 0 {
  429. s.symbolLen = uint16(i) + 1
  430. }
  431. }
  432. return int(m)
  433. }
  434. // minTableLog provides the minimum logSize to safely represent a distribution.
  435. func (s *Scratch) minTableLog() uint8 {
  436. minBitsSrc := highBits(uint32(s.br.remain()-1)) + 1
  437. minBitsSymbols := highBits(uint32(s.symbolLen-1)) + 2
  438. if minBitsSrc < minBitsSymbols {
  439. return uint8(minBitsSrc)
  440. }
  441. return uint8(minBitsSymbols)
  442. }
  443. // optimalTableLog calculates and sets the optimal tableLog in s.actualTableLog
  444. func (s *Scratch) optimalTableLog() {
  445. tableLog := s.TableLog
  446. minBits := s.minTableLog()
  447. maxBitsSrc := uint8(highBits(uint32(s.br.remain()-1))) - 2
  448. if maxBitsSrc < tableLog {
  449. // Accuracy can be reduced
  450. tableLog = maxBitsSrc
  451. }
  452. if minBits > tableLog {
  453. tableLog = minBits
  454. }
  455. // Need a minimum to safely represent all symbol values
  456. if tableLog < minTablelog {
  457. tableLog = minTablelog
  458. }
  459. if tableLog > maxTableLog {
  460. tableLog = maxTableLog
  461. }
  462. s.actualTableLog = tableLog
  463. }
  464. var rtbTable = [...]uint32{0, 473195, 504333, 520860, 550000, 700000, 750000, 830000}
  465. // normalizeCount will normalize the count of the symbols so
  466. // the total is equal to the table size.
  467. func (s *Scratch) normalizeCount() error {
  468. var (
  469. tableLog = s.actualTableLog
  470. scale = 62 - uint64(tableLog)
  471. step = (1 << 62) / uint64(s.br.remain())
  472. vStep = uint64(1) << (scale - 20)
  473. stillToDistribute = int16(1 << tableLog)
  474. largest int
  475. largestP int16
  476. lowThreshold = (uint32)(s.br.remain() >> tableLog)
  477. )
  478. for i, cnt := range s.count[:s.symbolLen] {
  479. // already handled
  480. // if (count[s] == s.length) return 0; /* rle special case */
  481. if cnt == 0 {
  482. s.norm[i] = 0
  483. continue
  484. }
  485. if cnt <= lowThreshold {
  486. s.norm[i] = -1
  487. stillToDistribute--
  488. } else {
  489. proba := (int16)((uint64(cnt) * step) >> scale)
  490. if proba < 8 {
  491. restToBeat := vStep * uint64(rtbTable[proba])
  492. v := uint64(cnt)*step - (uint64(proba) << scale)
  493. if v > restToBeat {
  494. proba++
  495. }
  496. }
  497. if proba > largestP {
  498. largestP = proba
  499. largest = i
  500. }
  501. s.norm[i] = proba
  502. stillToDistribute -= proba
  503. }
  504. }
  505. if -stillToDistribute >= (s.norm[largest] >> 1) {
  506. // corner case, need another normalization method
  507. return s.normalizeCount2()
  508. }
  509. s.norm[largest] += stillToDistribute
  510. return nil
  511. }
  512. // Secondary normalization method.
  513. // To be used when primary method fails.
  514. func (s *Scratch) normalizeCount2() error {
  515. const notYetAssigned = -2
  516. var (
  517. distributed uint32
  518. total = uint32(s.br.remain())
  519. tableLog = s.actualTableLog
  520. lowThreshold = total >> tableLog
  521. lowOne = (total * 3) >> (tableLog + 1)
  522. )
  523. for i, cnt := range s.count[:s.symbolLen] {
  524. if cnt == 0 {
  525. s.norm[i] = 0
  526. continue
  527. }
  528. if cnt <= lowThreshold {
  529. s.norm[i] = -1
  530. distributed++
  531. total -= cnt
  532. continue
  533. }
  534. if cnt <= lowOne {
  535. s.norm[i] = 1
  536. distributed++
  537. total -= cnt
  538. continue
  539. }
  540. s.norm[i] = notYetAssigned
  541. }
  542. toDistribute := (1 << tableLog) - distributed
  543. if (total / toDistribute) > lowOne {
  544. // risk of rounding to zero
  545. lowOne = (total * 3) / (toDistribute * 2)
  546. for i, cnt := range s.count[:s.symbolLen] {
  547. if (s.norm[i] == notYetAssigned) && (cnt <= lowOne) {
  548. s.norm[i] = 1
  549. distributed++
  550. total -= cnt
  551. continue
  552. }
  553. }
  554. toDistribute = (1 << tableLog) - distributed
  555. }
  556. if distributed == uint32(s.symbolLen)+1 {
  557. // all values are pretty poor;
  558. // probably incompressible data (should have already been detected);
  559. // find max, then give all remaining points to max
  560. var maxV int
  561. var maxC uint32
  562. for i, cnt := range s.count[:s.symbolLen] {
  563. if cnt > maxC {
  564. maxV = i
  565. maxC = cnt
  566. }
  567. }
  568. s.norm[maxV] += int16(toDistribute)
  569. return nil
  570. }
  571. if total == 0 {
  572. // all of the symbols were low enough for the lowOne or lowThreshold
  573. for i := uint32(0); toDistribute > 0; i = (i + 1) % (uint32(s.symbolLen)) {
  574. if s.norm[i] > 0 {
  575. toDistribute--
  576. s.norm[i]++
  577. }
  578. }
  579. return nil
  580. }
  581. var (
  582. vStepLog = 62 - uint64(tableLog)
  583. mid = uint64((1 << (vStepLog - 1)) - 1)
  584. rStep = (((1 << vStepLog) * uint64(toDistribute)) + mid) / uint64(total) // scale on remaining
  585. tmpTotal = mid
  586. )
  587. for i, cnt := range s.count[:s.symbolLen] {
  588. if s.norm[i] == notYetAssigned {
  589. var (
  590. end = tmpTotal + uint64(cnt)*rStep
  591. sStart = uint32(tmpTotal >> vStepLog)
  592. sEnd = uint32(end >> vStepLog)
  593. weight = sEnd - sStart
  594. )
  595. if weight < 1 {
  596. return errors.New("weight < 1")
  597. }
  598. s.norm[i] = int16(weight)
  599. tmpTotal = end
  600. }
  601. }
  602. return nil
  603. }
  604. // validateNorm validates the normalized histogram table.
  605. func (s *Scratch) validateNorm() (err error) {
  606. var total int
  607. for _, v := range s.norm[:s.symbolLen] {
  608. if v >= 0 {
  609. total += int(v)
  610. } else {
  611. total -= int(v)
  612. }
  613. }
  614. defer func() {
  615. if err == nil {
  616. return
  617. }
  618. fmt.Printf("selected TableLog: %d, Symbol length: %d\n", s.actualTableLog, s.symbolLen)
  619. for i, v := range s.norm[:s.symbolLen] {
  620. fmt.Printf("%3d: %5d -> %4d \n", i, s.count[i], v)
  621. }
  622. }()
  623. if total != (1 << s.actualTableLog) {
  624. return fmt.Errorf("warning: Total == %d != %d", total, 1<<s.actualTableLog)
  625. }
  626. for i, v := range s.count[s.symbolLen:] {
  627. if v != 0 {
  628. return fmt.Errorf("warning: Found symbol out of range, %d after cut", i)
  629. }
  630. }
  631. return nil
  632. }