LZSS压缩算法实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
static FILE *g_infile, *g_outfile;
/*****************************************************************************
initialize trees
*****************************************************************************/
2、编程语言:C++
五、实验代码
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
/* size of ring buffer */
#defineN4096
/* index for root of binary search trees */
/* Clear the buffer with any character that will appear often. */
memset(g_ring_buffer+ s, '', r - s);
g_match_len= 0;
while(1)
{
if(cmp>= 0)
{
if(g_right_child[p] != NIL)
p =g_right_child[p];
else
{
g_right_child[p] = r;
g_parent[r] = p;
return;
}
}
else
{
if(g_left_child[p] != NIL)
to facilitate string comparison */
static unsigned charg_ring_buffer[N + F - 1];
/* position and length of longest match; set byinsert_node() */
static unsignedg_match_pos,g_match_len;
static voidinsert_node(intr)
{
unsigned char *key;
unsignedi, p;
intcmp;
cmp= 1;
key = &g_ring_buffer[r];
p = N + 1 + key[0];
g_right_child[r] =g_left_child[r] = NIL;
static voiddelete_node(unsigned p)
{
unsigned q;
if(g_parent[p] == NIL)
return; /* not in tree */
if(g_right_child[p] == NIL)
q =g_left_child[p];
else if(g_left_child[p] == NIL)
"0" a position-and-length pair (2 bytes). Thus, eight units require at most
16 bytes of code. */
code_buf[0] = 0;
code_buf_ptr= mask = 1;
s = 0;
r = N - F;
#define NILN
/* upper limit forg_match_len. Changed from 18 to 16 for binary
compatabilitywith Microsoft COMPRESS.EXE and EXPAND.EXE
#defineF18 */
#defineF16
g_parent[g_left_child[q]] =g_parent[q];
g_left_child[q] =g_left_child[p];
g_parent[g_left_child[p]] = q;
}
g_right_child[q] =g_right_child[p];
g_parent[g_right_child[p]] = q;
*****************************************************************************/
static void compress(void)
{
unsignedi,len, r, s,last_match_length,code_buf_ptr;
/* encode string into position and length
ifmatch_lengthis greater than this: */
#defineTHRESHOLD2
/* these assume little-endian CPU like Intel x86
-- need byte-swap function for bigendianCPU */
实验名称:LZSS压缩算法实验报告
一、实验内容
使用Visual 6..0 C++编程实现LZ77压缩算法。
二、实验目的
用LZSS实现文件的压缩。
三、实验原理
LZSS压缩算法是词典编码无损压缩技术的一种。LZSS压缩算法的字典模型使用了自适应的方式,也就是说,将已经编码过的信息作为字典,
四、实验环境
1、软件环境:Visual C++ 6.0
g_parent[p] = NIL; /* remove p */
}
/*****************************************************************************
deletes node p from tree
*****************************************************************************/
/* left & right children & parent -- these constitute binary search tree */
static unsignedg_left_child[N + 1],g_right_child[N + 257],g_parent[N + 1];
/* input & output files */
p =g_left_child[p];
el来自百度文库e
{
g_left_child[p] = r;
g_parent[r] = p;
return;
}
}
for(i= 1;i< F;i++)
{
cmp= key[i] -g_ring_buffer[p +i];
if(cmp!= 0)
break;
}
if(i>g_match_len)
q =g_right_child[p];
else
{
q =g_left_child[p];
if(g_right_child[q] != NIL)
{
do q =g_right_child[q];
while(g_right_child[q] != NIL);
g_right_child[g_parent[q]] =g_left_child[q];
unsigned charcode_buf[17], mask;
intc;
init_tree(); /* initialize trees */
/*code_buf[1..16] saves eight units of code, andcode_buf[0] works as
eight flags, "1" representing that the unit is anunencodedletter (1 byte),
one, because the old one will be deleted sooner.
Note r plays double role, as tree node and position in buffer.
*****************************************************************************/
for strings that begin with characteri. These are initialized
to NIL. Note there are 256 trees. */
for(i= N + 1;i<= N + 256;i++)
g_right_child[i] = NIL;
for(i= 0;i< N;i++)
trees (g_ring_buffer[r]'thtree) and returns the longest-match position
and length via the global variablesg_match_posandg_match_len.
Ifg_match_len= F, then removes the old node in favor of the new
{
g_match_pos= p;
g_match_len=i;
if(g_match_len>= F)
break;
}
}
g_parent[r] =g_parent[p];
g_left_child[r] =g_left_child[p];
g_right_child[r] =g_right_child[p];
}
g_parent[q] =g_parent[p];
if(g_right_child[g_parent[p]] == p)
g_right_child[g_parent[p]] = q;
else
g_left_child[g_parent[p]] = q;
g_parent[p] = NIL;
}
/*****************************************************************************
Also,g_parent[i] is the parent of nodei. These are initialized to
NIL (= N), which stands for 'not used.'
Fori= 0 to 255,g_right_child[N +i+ 1] is the root of the tree
g_parent[i] = NIL;
}
/*****************************************************************************
Inserts string of length F,g_ring_buffer[r..r+F-1], into one of the
#defineREAD_LE32(X)*(uint32_t *)(X)
#defineWRITE_LE32(X,Y)*(uint32_t *)(X) = (Y)
/* this assumessizeof(long)==4 */
typedefunsigned longuint32_t;
/* text (input) size counter, code (output) size counter,
static voidinit_tree(void)
{
unsignedi;
/* Fori= 0 to N - 1,g_right_child[i] andg_left_child[i] will be the right and
left children of nodei. These nodes need not be initialized.
g_parent[g_left_child[p]] = r;
g_parent[g_right_child[p]] = r;
if(g_right_child[g_parent[p]] == p)
g_right_child[g_parent[p]] = r;
else
g_left_child[g_parent[p]] = r;
and counter for reporting progress every 1K bytes */
static unsigned longg_text_size,g_code_size,g_print_count;
/* ring buffer of size N, with extra F-1 bytes
相关文档
最新文档