buildCTable will populate the compression table so it is ready to be used.
()
| 100 | |
| 101 | // buildCTable will populate the compression table so it is ready to be used. |
| 102 | func (s *fseEncoder) buildCTable() error { |
| 103 | tableSize := uint32(1 << s.actualTableLog) |
| 104 | highThreshold := tableSize - 1 |
| 105 | var cumul [256]int16 |
| 106 | |
| 107 | s.allocCtable() |
| 108 | tableSymbol := s.ct.tableSymbol[:tableSize] |
| 109 | // symbol start positions |
| 110 | { |
| 111 | cumul[0] = 0 |
| 112 | for ui, v := range s.norm[:s.symbolLen-1] { |
| 113 | u := byte(ui) // one less than reference |
| 114 | if v == -1 { |
| 115 | // Low proba symbol |
| 116 | cumul[u+1] = cumul[u] + 1 |
| 117 | tableSymbol[highThreshold] = u |
| 118 | highThreshold-- |
| 119 | } else { |
| 120 | cumul[u+1] = cumul[u] + v |
| 121 | } |
| 122 | } |
| 123 | // Encode last symbol separately to avoid overflowing u |
| 124 | u := int(s.symbolLen - 1) |
| 125 | v := s.norm[s.symbolLen-1] |
| 126 | if v == -1 { |
| 127 | // Low proba symbol |
| 128 | cumul[u+1] = cumul[u] + 1 |
| 129 | tableSymbol[highThreshold] = byte(u) |
| 130 | highThreshold-- |
| 131 | } else { |
| 132 | cumul[u+1] = cumul[u] + v |
| 133 | } |
| 134 | if uint32(cumul[s.symbolLen]) != tableSize { |
| 135 | return fmt.Errorf("internal error: expected cumul[s.symbolLen] (%d) == tableSize (%d)", cumul[s.symbolLen], tableSize) |
| 136 | } |
| 137 | cumul[s.symbolLen] = int16(tableSize) + 1 |
| 138 | } |
| 139 | // Spread symbols |
| 140 | s.zeroBits = false |
| 141 | { |
| 142 | step := tableStep(tableSize) |
| 143 | tableMask := tableSize - 1 |
| 144 | var position uint32 |
| 145 | // if any symbol > largeLimit, we may have 0 bits output. |
| 146 | largeLimit := int16(1 << (s.actualTableLog - 1)) |
| 147 | for ui, v := range s.norm[:s.symbolLen] { |
| 148 | symbol := byte(ui) |
| 149 | if v > largeLimit { |
| 150 | s.zeroBits = true |
| 151 | } |
| 152 | for range v { |
| 153 | tableSymbol[position] = symbol |
| 154 | position = (position + step) & tableMask |
| 155 | for position > highThreshold { |
| 156 | position = (position + step) & tableMask |
| 157 | } /* Low proba area */ |
| 158 | } |
| 159 | } |
no test coverage detected