huf.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. Type
  2. PTree = ^TTree;
  3. TTree = record
  4. info : byte;
  5. ti : longint;
  6. Left : PTree;
  7. Right: PTree;
  8. end;
  9. ps = ^el;
  10. el = record
  11. data : PTree;
  12. prev : ps;
  13. end;
  14. CONST
  15. fname : string[13] = 'input.txt';
  16. fout : string[13] = 'input.huf';
  17. VAR
  18. counts : array [0..255] of longint;
  19. table : array [0..255] of string[200];
  20. INP,OUT : FILE;
  21. Nums : ps;
  22. THead : PTree;
  23. Code : string;
  24. GO,GNum : byte;
  25. Size,Csize : longint;
  26. {---------------------------------}
  27. Procedure DelBranch(br:PTree);
  28. begin
  29. if br^.left <> nil then DelBranch(br^.left);
  30. if br^.right <> nil then DelBranch(br^.right);
  31. Dispose(br);
  32. end;{DelBranch}
  33. {---------------------------------}
  34. Procedure SetTimes(var F:File);
  35. var
  36. curpos,rsize : word;
  37. ch : byte;
  38. begin
  39. FillChar(counts,256,0);
  40. CurPos := FilePos(F);
  41. Seek(F,0);
  42. while not eof(F) do
  43. begin
  44. BlockRead(F,ch,1,rsize);
  45. inc(counts[ch])
  46. end;
  47. Seek(F,CurPos);
  48. end;{SetTimes}
  49. {---------------------------------}
  50. Procedure GetTimes(var F:file);
  51. begin BlockRead(F,counts,256*4) end;{GetTimes}
  52. {---------------------------------}
  53. Procedure PutTimes(var F:file);
  54. begin BlockWrite(F,counts,256*4) end;{GetTimes}
  55. {---------------------------------}
  56. Procedure AddItem(var HEAD:ps; VAL : PTree);
  57. var
  58. c,NEWs : ps;
  59. Begin
  60. c := HEAD;
  61. New(News);
  62. News^.data := VAL;
  63. News^.prev := nil;
  64. if c=nil then begin HEAD := NEWs; exit end;
  65. if val^.ti <= C^.DATA^.ti then begin News^.prev:=C; HEAD:=News; exit end;
  66. while (c^.prev <> nil) and
  67. (not ((val^.ti>C^.data^.ti) and (val^.ti<=C^.prev^.data^.ti))) do
  68. c:=c^.prev;
  69. News^.prev := c^.prev;
  70. C^.prev := News;
  71. end;{AddItem}
  72. {---------------------------------}
  73. Procedure RemoveItem(var HEAD:ps; VAL : PTree);
  74. var
  75. c,cur : ps;
  76. Begin
  77. c := HEAD;
  78. if c=nil then exit;
  79. if c^.data = val then begin HEAD:=C^.prev; Dispose(C); exit end;
  80. while (c^.prev <> nil) and (C^.prev^.data <> val) do c:=c^.prev;
  81. if c^.prev^.data <> val then exit;
  82. cur := c^.prev;
  83. C^.prev := cur^.prev;
  84. Dispose(cur);
  85. end;{RemoveItem}
  86. {---------------------------------}
  87. Procedure DeleteAll(var HEAD:ps);
  88. var
  89. c:ps;
  90. begin
  91. while HEAD <> nil do
  92. begin
  93. c:=HEAD^.prev;
  94. Dispose(HEAD);
  95. HEAD := C;
  96. end;
  97. end;{DeleteAll}
  98. {---------------------------------}
  99. Procedure CreateTree;
  100. var
  101. i : byte;
  102. News : PTree;
  103. begin
  104. for i := 0 to 255 do
  105. if counts[i] <> 0 then
  106. begin
  107. New(News);News^.info:=i;News^.ti:=counts[i];News^.Left:=nil;News^.Right:=nil;
  108. AddItem(Nums,News);
  109. end;
  110. while NUMS^.prev <> nil do
  111. begin
  112. New(News);
  113. News^.ti := NUMS^.data^.ti+NUMS^.prev^.data^.ti;
  114. News^.left := Nums^.data;
  115. News^.right := Nums^.prev^.data;
  116. RemoveItem(NUMS,NUMS^.data);
  117. RemoveItem(NUMS,NUMS^.data);
  118. AddItem(NUMS,News);
  119. end;
  120. THead := News;
  121. RemoveItem(NUMS,News);
  122. end;{CreateTree}
  123. {---------------------------------}
  124. Procedure GetCode(VAL:Byte;BRA:PTree;cur:string);
  125. begin
  126. if (Bra^.left=nil) and (Bra^.right=nil) and (BRA^.info = VAL) then
  127. Code := cur;
  128. if (Bra^.left<>nil) then begin
  129. GetCode(val,Bra^.left,cur+'1');
  130. GetCode(val,Bra^.right,cur+'0')
  131. end;
  132. end;{GetCode}
  133. {---------------------------------}
  134. Procedure SetTable;
  135. var i : byte;
  136. begin
  137. for i := 1 to 255 do
  138. if counts[i] <> 0 then
  139. begin
  140. GetCode(i,THead,'');
  141. table[i] := Code;
  142. end;
  143. end;{SetTable}
  144. {---------------------------------}
  145. Function GetByte(cur:string):byte;
  146. var ret:byte;
  147. begin
  148. ret:=0;
  149. while length(cur)>0 do
  150. begin
  151. ret:=ret shl 1 + ord(cur[1])-ord('0');
  152. delete(cur,1,1);
  153. end;
  154. GetByte:=ret;
  155. end;{GetByte}
  156. {---------------------------------}
  157. Procedure IfNeedWrite(var GGG:string;var F:FILE);
  158. var
  159. LLL : string;
  160. FFF : byte;
  161. begin
  162. while length(GGG)>7 do
  163. begin
  164. LLL := Copy(GGG,1,8);
  165. Delete(GGG,1,8);
  166. fff := GetByte(LLL);
  167. BlockWrite(F,Fff,1);
  168. end;
  169. end;{IfNeedWrite}
  170. {---------------------------------}
  171. Function GetBit(var F:File):boolean;
  172. begin
  173. if GNum = 0 then
  174. begin
  175. GNum:=8;
  176. inc(CSize);
  177. if (not eof(F)) and (csize<>Size-1) then BlockRead(F,GO,1)
  178. else
  179. begin
  180. BlockRead(F,GO,1);
  181. BlockRead(F,GNum,1)
  182. end;
  183. end;
  184. Dec(GNum);
  185. GetBit := (GO and 128) = 128;
  186. Go := GO shl 1;
  187. end;{GetBit}
  188. {---------------------------------}
  189. VAR
  190. CURCODE : string;
  191. CUR : byte;
  192. Ctr : Ptree;
  193. BEGIN
  194. if (ParamCount<1) then
  195. begin
  196. WriteLn('Usage: HUF.exe <FILENAME.EXT> [OUTNAME.ext]');
  197. Halt(10);
  198. end;
  199. Fname := ParamStr(1);
  200. for size := 1 to Length(Fname) do
  201. fname[size] := UpCase(fname[size]);
  202. if pos('.HUF',fname) = 0 then
  203. begin
  204. if Paramcount = 2 then fout := ParamStr(2) else
  205. fout := Copy(Fname,1,pos('.',Fname)) + 'HUF';
  206. Assign(INP,Fname);
  207. Reset(INP,1);
  208. Size:=FileSize(INP);
  209. SetTimes(INP);
  210. CreateTree;
  211. SetTable;
  212. Assign(OUT,Fout);
  213. Rewrite(OUT,1);
  214. BlockWrite(OUT,Fname,14);
  215. PutTimes(OUT);
  216. curcode:='';
  217. csize:=0;
  218. while not eof(INP) do
  219. begin
  220. inc(csize);
  221. BlockRead(INP,CUR,1);
  222. curcode := curcode + table[cur];
  223. IfNeedWrite(curcode,OUT);
  224. Write(#13'Complete: ',round((csize/size)*100),'%');
  225. end;
  226. cur := length(curcode);
  227. if (cur <> 0) then
  228. begin
  229. for size := 0 to 8-cur do curcode := curcode+'0';
  230. IfNeedWrite(curcode,OUT);
  231. end
  232. else
  233. cur:=8;
  234. BlockWrite(OUT,cur,1);
  235. Close(OUT);
  236. Close(INP);
  237. end
  238. else
  239. begin
  240. Assign(INP,Fname);
  241. Reset(INP,1);
  242. if ParamCount = 2 then FOut := ParamStr(2)
  243. else BlockRead(INP,Fout,14);
  244. Assign(OUT,Fout);
  245. Rewrite(OUT,1);
  246. Size:=FileSize(INP)-256*4-14;
  247. GetTimes(INP);
  248. CreateTree;
  249. Ctr := THead;
  250. while not eof(INP) do
  251. begin
  252. while CTR^.left <> nil do
  253. begin
  254. if GetBit(INP) then
  255. CTR:=CTR^.left
  256. else
  257. CTR:=CTR^.right;
  258. end;
  259. Write(#13'Complete: ',round((csize/size)*100),'%');
  260. BlockWrite(OUT,CTR^.INFO,1);
  261. CTR:=THead;
  262. end;
  263. close(INP);
  264. Close(OUT);
  265. end;
  266. END.