七行プログラミング

このエントリーをはてなブックマークに追加
696comp
Range Encoder と適応的モデル
g++ 2.95.3 で動作確認。16MB近くのファイルまで圧縮可能。
(Borland C++ ではコンパイル不可)
圧縮データはエンディアンの異なるシステムでは非互換。
符号化: exec E infile outfile 復号: exec D infile outfile

モデル部と符号化部に分かれている上に、8行もあるのが・・・

// main.cpp (モデル部)
#include "range.h"
L u,i;main(int,char **V){L b=256,a[b],l,z=ios::binary;fstream g(V[2],ios::in|z)
,o(V[3],ios::out|z);g.seekg(0,ios::end);C s[l=g.tellg()];g.seekg(0,ios::beg);
g.read(s,l);g.close();R r,x;while(u<b)a[u++]=1;if(*V[1]-68){C t[l*2];r.p=t;
while(i<l){for(x.w=b=0;b<s[i];)x.w+=a[b++];x.r=a[s[i++]]++;x.e=u++;r.T(x);}
r.F();o.write(&l,4);o.write(t,r.o);}else{C t[l=*(L *)s];r.D(s);while(i<l){x.e=
u++;L v=r.V(x),w=b=0;for(;v>=w;w+=a[b++]);t[i++]=--b;x.w=w-(x.r=a[b]++);r.S(x);
}o.write(t,l);}}

// range.h (符号化部)
#include <fstream>
typedef unsigned long L;typedef unsigned char C;class R{public:L w,r,e,i,o;C
*g,*p;R(){r=-1;w=o=0;};L D(C *n){g=n+(i=4);w=g[0]<<24|g[1]<<16|g[2]<<8|g[3];};
L V(R s){while(r<1<<24)w=w<<8|g[i++],r<<=8;return w/(r/=s.e);};L S(R s){w-=r
*s.w;r*=s.r;};L F(){p[o++]=w>>24;p[o++]=w>>16;p[o++]=w>>8;p[o++]=w;};L T(R s){
L t=r/s.e,l=w;w+=s.w*t;r=s.r*t;if(l>w){for(t=o-1;t>0,p[t]==255;p[t--]++);
p[t]++;}while(r<1<<24)p[o++]=w>>24,w<<=8,r<<=8;};};