bzlib_huffman.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include <config.h>
  2. /*-------------------------------------------------------------*/
  3. /*--- Huffman coding low-level stuff ---*/
  4. /*--- huffman.c ---*/
  5. /*-------------------------------------------------------------*/
  6. /*--
  7. This file is a part of bzip2 and/or libbzip2, a program and
  8. library for lossless, block-sorting data compression.
  9. Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. 1. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. 2. The origin of this software must not be misrepresented; you must
  16. not claim that you wrote the original software. If you use this
  17. software in a product, an acknowledgment in the product
  18. documentation would be appreciated but is not required.
  19. 3. Altered source versions must be plainly marked as such, and must
  20. not be misrepresented as being the original software.
  21. 4. The name of the author may not be used to endorse or promote
  22. products derived from this software without specific prior written
  23. permission.
  24. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  25. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  28. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. Julian Seward, Cambridge, UK.
  36. jseward@acm.org
  37. bzip2/libbzip2 version 1.0 of 21 March 2000
  38. This program is based on (at least) the work of:
  39. Mike Burrows
  40. David Wheeler
  41. Peter Fenwick
  42. Alistair Moffat
  43. Radford Neal
  44. Ian H. Witten
  45. Robert Sedgewick
  46. Jon L. Bentley
  47. For more information on these sources, see the manual.
  48. --*/
  49. #include "bzlib_private.h"
  50. /*---------------------------------------------------*/
  51. #define WEIGHTOF(zz0) ((zz0) & 0xffffff00)
  52. #define DEPTHOF(zz1) ((zz1) & 0x000000ff)
  53. #define MYMAX(zz2,zz3) ((zz2) > (zz3) ? (zz2) : (zz3))
  54. #define ADDWEIGHTS(zw1,zw2) \
  55. (WEIGHTOF(zw1)+WEIGHTOF(zw2)) | \
  56. (1 + MYMAX(DEPTHOF(zw1),DEPTHOF(zw2)))
  57. #define UPHEAP(z) \
  58. { \
  59. Int32 zz, tmp; \
  60. zz = z; tmp = heap[zz]; \
  61. while (weight[tmp] < weight[heap[zz >> 1]]) { \
  62. heap[zz] = heap[zz >> 1]; \
  63. zz >>= 1; \
  64. } \
  65. heap[zz] = tmp; \
  66. }
  67. #define DOWNHEAP(z) \
  68. { \
  69. Int32 zz, yy, tmp; \
  70. zz = z; tmp = heap[zz]; \
  71. while (True) { \
  72. yy = zz << 1; \
  73. if (yy > nHeap) break; \
  74. if (yy < nHeap && \
  75. weight[heap[yy+1]] < weight[heap[yy]]) \
  76. yy++; \
  77. if (weight[tmp] < weight[heap[yy]]) break; \
  78. heap[zz] = heap[yy]; \
  79. zz = yy; \
  80. } \
  81. heap[zz] = tmp; \
  82. }
  83. /*---------------------------------------------------*/
  84. void BZ2_hbMakeCodeLengths ( UChar *len,
  85. Int32 *freq,
  86. Int32 alphaSize,
  87. Int32 maxLen )
  88. {
  89. /*--
  90. Nodes and heap entries run from 1. Entry 0
  91. for both the heap and nodes is a sentinel.
  92. --*/
  93. Int32 nNodes, nHeap, n1, n2, i, j, k;
  94. Bool tooLong;
  95. Int32 heap [ BZ_MAX_ALPHA_SIZE + 2 ];
  96. Int32 weight [ BZ_MAX_ALPHA_SIZE * 2 ];
  97. Int32 parent [ BZ_MAX_ALPHA_SIZE * 2 ];
  98. for (i = 0; i < alphaSize; i++)
  99. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  100. while (True) {
  101. nNodes = alphaSize;
  102. nHeap = 0;
  103. heap[0] = 0;
  104. weight[0] = 0;
  105. parent[0] = -2;
  106. for (i = 1; i <= alphaSize; i++) {
  107. parent[i] = -1;
  108. nHeap++;
  109. heap[nHeap] = i;
  110. UPHEAP(nHeap);
  111. }
  112. AssertH( nHeap < (BZ_MAX_ALPHA_SIZE+2), 2001 );
  113. while (nHeap > 1) {
  114. n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  115. n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
  116. nNodes++;
  117. parent[n1] = parent[n2] = nNodes;
  118. weight[nNodes] = ADDWEIGHTS(weight[n1], weight[n2]);
  119. parent[nNodes] = -1;
  120. nHeap++;
  121. heap[nHeap] = nNodes;
  122. UPHEAP(nHeap);
  123. }
  124. AssertH( nNodes < (BZ_MAX_ALPHA_SIZE * 2), 2002 );
  125. tooLong = False;
  126. for (i = 1; i <= alphaSize; i++) {
  127. j = 0;
  128. k = i;
  129. while (parent[k] >= 0) { k = parent[k]; j++; }
  130. len[i-1] = j;
  131. if (j > maxLen) tooLong = True;
  132. }
  133. if (! tooLong) break;
  134. for (i = 1; i < alphaSize; i++) {
  135. j = weight[i] >> 8;
  136. j = 1 + (j / 2);
  137. weight[i] = j << 8;
  138. }
  139. }
  140. }
  141. /*---------------------------------------------------*/
  142. void BZ2_hbAssignCodes ( Int32 *code,
  143. UChar *length,
  144. Int32 minLen,
  145. Int32 maxLen,
  146. Int32 alphaSize )
  147. {
  148. Int32 n, vec, i;
  149. vec = 0;
  150. for (n = minLen; n <= maxLen; n++) {
  151. for (i = 0; i < alphaSize; i++)
  152. if (length[i] == n) { code[i] = vec; vec++; };
  153. vec <<= 1;
  154. }
  155. }
  156. /*---------------------------------------------------*/
  157. void BZ2_hbCreateDecodeTables ( Int32 *limit,
  158. Int32 *base,
  159. Int32 *perm,
  160. UChar *length,
  161. Int32 minLen,
  162. Int32 maxLen,
  163. Int32 alphaSize )
  164. {
  165. Int32 pp, i, j, vec;
  166. pp = 0;
  167. for (i = minLen; i <= maxLen; i++)
  168. for (j = 0; j < alphaSize; j++)
  169. if (length[j] == i) { perm[pp] = j; pp++; };
  170. for (i = 0; i < BZ_MAX_CODE_LEN; i++) base[i] = 0;
  171. for (i = 0; i < alphaSize; i++) base[length[i]+1]++;
  172. for (i = 1; i < BZ_MAX_CODE_LEN; i++) base[i] += base[i-1];
  173. for (i = 0; i < BZ_MAX_CODE_LEN; i++) limit[i] = 0;
  174. vec = 0;
  175. for (i = minLen; i <= maxLen; i++) {
  176. vec += (base[i+1] - base[i]);
  177. limit[i] = vec-1;
  178. vec <<= 1;
  179. }
  180. for (i = minLen + 1; i <= maxLen; i++)
  181. base[i] = ((limit[i-1] + 1) << 1) - base[i];
  182. }
  183. /*-------------------------------------------------------------*/
  184. /*--- end huffman.c ---*/
  185. /*-------------------------------------------------------------*/