⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.java

📁 zlib 算法在j2me 中的应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* -*-mode:java; c-basic-offset:2; -*- *//*Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions are met:  1. Redistributions of source code must retain the above copyright notice,     this list of conditions and the following disclaimer.  2. Redistributions in binary form must reproduce the above copyright      notice, this list of conditions and the following disclaimer in      the documentation and/or other materials provided with the distribution.  3. The names of the authors may not be used to endorse or promote products     derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* * This program is based on zlib-1.1.3, so all credit should go authors * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu) * and contributors of zlib. *//*                                                                               *Modified by Asoft ltd. at 2003 for running on J2ME platform                    *www.asoft.ru                                                                   *Alexandre Rusev rusev@asoft.ru                                                 *Alexey Soloviev soloviev@asoft.ru                                              */package com.asoft.midp.jzlib;import com.asoft.midp.dynarray.*;final class Tree{  static final private int MAX_BITS=15;  static final private int BL_CODES=19;  static final private int D_CODES=30;  static final private int LITERALS=256;  static final private int LENGTH_CODES=29;  static final private int L_CODES=(LITERALS+1+LENGTH_CODES);  static final private int HEAP_SIZE=(2*L_CODES+1);  // Bit length codes must not exceed MAX_BL_BITS bits  static final int MAX_BL_BITS=7;   // end of block literal code  static final int END_BLOCK=256;   // repeat previous bit length 3-6 times (2 bits of repeat count)  static final int REP_3_6=16;   // repeat a zero length 3-10 times  (3 bits of repeat count)  static final int REPZ_3_10=17;   // repeat a zero length 11-138 times  (7 bits of repeat count)  static final int REPZ_11_138=18;   // extra bits for each length code  static final int[] extra_lbits={    0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0  };  // extra bits for each distance code  static final int[] extra_dbits={    0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13  };  // extra bits for each bit length code  static final int[] extra_blbits={    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7  };  static final byte[] bl_order={    16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};  // The lengths of the bit length codes are sent in order of decreasing  // probability, to avoid transmitting the lengths for unused bit  // length codes.  static final int Buf_size=8*2;  // see definition of array dist_code below  static final int DIST_CODE_LEN=512;  static final byte[] _dist_code = {    0,  1,  2,  3,  4,  4,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,    8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9, 10, 10, 10, 10, 10, 10, 10, 10,    10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,    11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,    12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,    13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,    13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,    14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,    14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,    14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,  0,  0, 16, 17,    18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,    23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,    24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,    26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,    26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,    27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,    28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,    28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,    28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,    29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,    29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,    29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29  };  static final byte[] _length_code={    0,  1,  2,  3,  4,  5,  6,  7,  8,  8,  9,  9, 10, 10, 11, 11, 12, 12, 12, 12,    13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,    17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,    19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,    21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,    22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,    23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,    24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,    25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,    25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,    26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,    26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,    27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28  };  static final int[] base_length = {    0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,    64, 80, 96, 112, 128, 160, 192, 224, 0  };  static final int[] base_dist = {       0,   1,      2,     3,     4,    6,     8,    12,    16,     24,      32,  48,     64,    96,   128,  192,   256,   384,   512,    768,    1024, 1536,  2048,  3072,  4096,  6144,  8192, 12288, 16384, 24576  };  // Mapping from a distance to a distance code. dist is the distance - 1 and  // must not have side effects. _dist_code[256] and _dist_code[257] are never  // used.  static int d_code(int dist){    return ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>>7)]);  }  short_array dyn_tree;      // the dynamic tree  int     max_code;      // largest code with non zero frequency  StaticTree stat_desc;  // the corresponding static tree  // Compute the optimal bit lengths for a tree and update the total bit length  // for the current block.  // IN assertion: the fields freq and dad are set, heap[heap_max] and  //    above are the tree nodes sorted by increasing frequency.  // OUT assertions: the field len is set to the optimal bit length, the  //     array bl_count contains the frequencies for each bit length.  //     The length opt_len is updated; static_len is also updated if stree is  //     not null.  void gen_bitlen(Deflate s){    short_array tree = dyn_tree;    short[] stree = stat_desc.static_tree;    int[] extra = stat_desc.extra_bits;    int base = stat_desc.extra_base;    int max_length = stat_desc.max_length;    int h;              // heap index    int n, m;           // iterate over the tree elements    int bits;           // bit length    int xbits;          // extra bits    short f;            // frequency    int overflow = 0;   // number of elements with bit length too large    for (bits = 0; bits <= MAX_BITS; bits++) s.bl_count[bits] = 0;    // In a first pass, compute the optimal bit lengths (which may    // overflow in the case of the bit length tree).    //tree[s.heap[s.heap_max]*2+1] = 0; // root of the heap    tree.set(s.heap[s.heap_max]*2+1,(short)0); // root of the heap        for(h=s.heap_max+1; h<HEAP_SIZE; h++){      n = s.heap[h];      //bits = tree[tree[n*2+1]*2+1] + 1;      bits = tree.get(tree.get(n*2+1)*2+1) + 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -