数据压缩 -- 源码[10]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 luckyjan 的 blog

  pos:=0;
  for i:=0 to pred(size) do begin
    if (i and 7)=0 then
      begin
        flags:=buf^[pos];inc(pos);
      end else
        flags:=flags shl 1;
    if (flags and (1 shl 7))<>0 then
      begin
        k:=buf^[pos]+(1 shl 8);inc(pos);encodec(k);
        k:=buf^[pos]shl 8;inc(pos);inc(k,buf^[pos]);inc(pos);encodep(k);
      end else begin
        k:=buf^[pos];inc(pos);encodec(k);
      end;
  end;
  for i:=0 to pred(nc) do
    cfreq[i]:=0;
  for i:=0 to pred(np) do
    pfreq[i]:=0;
end;

procedure output(c,p:word);
begin
  outputmask:=outputmask shr 1;
  if outputmask=0 then
    begin
      outputmask:=1 shl 7;
      if (outputpos>=windowsize-24) then
        begin
          sendblock;outputpos:=0;
        end;
      cpos:=outputpos;inc(outputpos);buf^[cpos]:=0;
    end;
  buf^[outputpos]:=c;inc(outputpos);inc(cfreq[c]);
  if c>=(1 shl 8) then
    begin
      buf^[cpos]:=buf^[cpos] or outputmask;
      buf^[outputpos]:=(p shr 8);inc(outputpos);
      buf^[outputpos]:=p;inc(outputpos);c:=0;
      while p<>0 do begin
        p:=p shr 1;inc(c);
      end;
      inc(pfreq[c]);
    end;
end;

{------------------------------- lempel-ziv part ------------------------------}

procedure initslide;
var
  i:word;
begin
  for i:=dicsiz to (dicsiz+ucharmax) do begin
    level^[i]:=1;
{$ifdef percolate}
    position^[i]:=nul;
{$endif}
  end;
  for i:=dicsiz to pred(2*dicsiz) do
    parent^[i]:=nul;
  avail:=1;
  for i:=1 to dicsiz-2 do
    next^[i]:=succ(i);
  next^[pred(dicsiz)]:=nul;
  for i:=(2*dicsiz) to maxhashval do
    next^[i]:=nul;
end;

{ hash function }
function hash(p:twobyteint;c:byte):twobyteint;
begin
  hash:=p+(c shl (dicbit-9))+2*dicsiz;
end;

function child(q:twobyteint;c:byte):twobyteint;
var
  r:twobyteint;
begin
  r:=next^[hash(q,c)];parent^[nul]:=q;
  while parent^[r]<>q do
    r:=next^[r];
  child:=r;
end;

procedure makechild(q:twobyteint;c:byte;r:twobyteint);
var
  h,t:twobyteint;
begin
  h:=hash(q,c);
  t:=next^[h];next^[h]:=r;next^[r]:=t;
  prev^[t]:=r;prev^[r]:=h;parent^[r]:=q;
  inc(childcount^[q]);
end;

procedure split(old:twobyteint);
var
  new,t:twobyteint;
begin
  new:=avail;avail:=next^[new];
  childcount^[new]:=0;
  t:=prev^[old];prev^[new]:=t;
  next^[t]:=new;
  t:=next^[old];next^[new]:=t;
  prev^[t]:=new;
  parent^[new]:=parent^[old];
  level^[new]:=matchlen;
  position^[new]:=pos;
  makechild(new,text^[matchpos+matchlen],old);
  makechild(new,text^[pos+matchlen],pos);
end;

procedure insertnode;
var
  q,r,j,t:twobyteint;
  c:byte;
  t1,t2:pchar;
begin
  if matchlen>=4 then
    begin
      dec(matchlen);
      r:=succ(matchpos) or dicsiz;
      q:=parent^[r];
      while q=nul do begin
        r:=next^[r];q:=parent^[r];
      end;
      while level^[q]>=matchlen do begin
        r:=q;q:=parent^[q];
      end;
      t:=q;
{$ifdef percolate}
      while position^[t]<0 do begin

本文关键:数据压缩 -- 源码
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top