compress.go 17 KB

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