huf2.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. CONST
  2. bufSize = 32768;
  3. Type
  4. PTree = ^TTree;
  5. TTree = record
  6. info : byte;
  7. ti : longint;
  8. Left : PTree;
  9. Right: PTree;
  10. end;
  11. ps = ^el;
  12. el = record
  13. data : PTree;
  14. prev : ps;
  15. end;
  16. CONST
  17. fname : string[13] = 'input.txt';
  18. fout : string[13] = 'input.huf';
  19. VAR
  20. counts : array [0..255] of longint;
  21. table : array [0..255] of longint;
  22. tbllens : array [0..255] of byte;
  23. INP,OUT : FILE;
  24. Nums : ps;
  25. THead : PTree;
  26. {-----}
  27. Code : longint;
  28. CodeLen : byte;
  29. {-----}
  30. GO,GNum : byte;
  31. done : boolean;
  32. Size,Csize : longint;
  33. {---------------------------------}
  34. var
  35. {FReadBuf}
  36. RBBuf, RBread : ^BYTE;
  37. RBcount : word;
  38. RBsize : word;
  39. RBFirst, RBLast : boolean;
  40. {FReadBuf}
  41. Function FReadBuf(var F:FILE;var val:byte):boolean;
  42. begin
  43. if RBFirst then
  44. begin
  45. RBfirst:=false;
  46. GetMem(RBBuf,BUFSize);
  47. RBcount:=0;
  48. RBSize:=0;
  49. end;
  50. if RBLast then
  51. begin
  52. RBLast:=false;
  53. FreeMem(RBBuf,BufSize);
  54. RBFirst:=true;
  55. exit;
  56. end;
  57. If RBcount=RBsize then
  58. begin
  59. BlockRead(F,RBBuf^,BufSize,RBsize);
  60. if Rbsize=0 then
  61. begin
  62. FReadBuf:=false;
  63. exit;
  64. end;
  65. RBcount:=0;
  66. end;
  67. RBread:=PTR(SeG(RBBUF^),Ofs(RBbuf^)+RBcount);
  68. inc(RBcount);
  69. val:=RBread^;
  70. FReadBuf:=true;
  71. end; {FReadBuf}
  72. {-----------------------------------------}
  73. var
  74. {FWriteBuf}
  75. WBBuf,WBwrt : ^BYTE;
  76. WBcount : word;
  77. WBFirst, WBLast : boolean;
  78. {FWritebuf}
  79. Procedure FWriteBuf(var F:FILE;val:byte);
  80. begin
  81. if WBFirst then
  82. begin
  83. WBfirst:=false;
  84. GetMem(WBBuf,BUFSize);
  85. WBcount:=0;
  86. end;
  87. if WBLast then
  88. begin
  89. WBLast:=false;
  90. BlockWrite(F,WBBuf^,WBcount);
  91. FreeMem(WBBuf,BufSize);
  92. WBFirst:=true;
  93. exit;
  94. end;
  95. WBwrt:=PTR(SeG(WBBUF^),Ofs(WBbuf^)+WBcount);
  96. inc(WBcount);
  97. WBwrt^:=val;
  98. If WBcount=BUFsize then
  99. begin
  100. BlockWrite(F,WBBuf^,WBcount);
  101. WBcount:=0;
  102. end;
  103. end;{FWriteBuf}
  104. {-----------------------------------------}
  105. Procedure DelBranch(br:PTree);
  106. begin
  107. if br^.left <> nil then DelBranch(br^.left);
  108. if br^.right <> nil then DelBranch(br^.right);
  109. Dispose(br);
  110. end;{DelBranch}
  111. {---------------------------------}
  112. Procedure SetTimes(var F:File);
  113. var
  114. curpos,rsize : word;
  115. ch : byte;
  116. begin
  117. FillChar(counts,256,0);
  118. CurPos := FilePos(F);
  119. Seek(F,0);
  120. RBFirst:=true;
  121. RBLast:=false;
  122. while FreadBuf(F,ch) do inc(counts[ch]);
  123. RBLast:=true;
  124. FReadBuf(F,ch);
  125. Seek(F,CurPos);
  126. end;{SetTimes}
  127. {---------------------------------}
  128. Procedure GetTimes(var F:file);
  129. begin BlockRead(F,counts,256*4) end;{GetTimes}
  130. {---------------------------------}
  131. Procedure PutTimes(var F:file);
  132. begin BlockWrite(F,counts,256*4) end;{GetTimes}
  133. {---------------------------------}
  134. Procedure AddItem(var HEAD:ps; VAL : PTree);
  135. var
  136. c,NEWs : ps;
  137. Begin
  138. c := HEAD;
  139. New(News);
  140. News^.data := VAL;
  141. News^.prev := nil;
  142. if c=nil then begin HEAD := NEWs; exit end;
  143. if val^.ti <= C^.DATA^.ti then begin News^.prev:=C; HEAD:=News; exit end;
  144. while (c^.prev <> nil) and
  145. (not ((val^.ti>C^.data^.ti) and (val^.ti<=C^.prev^.data^.ti))) do
  146. c:=c^.prev;
  147. News^.prev := c^.prev;
  148. C^.prev := News;
  149. end;{AddItem}
  150. {---------------------------------}
  151. Procedure RemoveItem(var HEAD:ps; VAL : PTree);
  152. var
  153. c,cur : ps;
  154. Begin
  155. c := HEAD;
  156. if c=nil then exit;
  157. if c^.data = val then begin HEAD:=C^.prev; Dispose(C); exit end;
  158. while (c^.prev <> nil) and (C^.prev^.data <> val) do c:=c^.prev;
  159. if c^.prev^.data <> val then exit;
  160. cur := c^.prev;
  161. C^.prev := cur^.prev;
  162. Dispose(cur);
  163. end;{RemoveItem}
  164. {---------------------------------}
  165. Procedure DeleteAll(var HEAD:ps);
  166. var
  167. c:ps;
  168. begin
  169. while HEAD <> nil do
  170. begin
  171. c:=HEAD^.prev;
  172. Dispose(HEAD);
  173. HEAD := C;
  174. end;
  175. end;{DeleteAll}
  176. {---------------------------------}
  177. Procedure CreateTree;
  178. var
  179. i : byte;
  180. News : PTree;
  181. begin
  182. for i := 0 to 255 do
  183. if counts[i] <> 0 then
  184. begin
  185. New(News);News^.info:=i;News^.ti:=counts[i];News^.Left:=nil;News^.Right:=nil;
  186. AddItem(Nums,News);
  187. end;
  188. while NUMS^.prev <> nil do
  189. begin
  190. New(News);
  191. News^.ti := NUMS^.data^.ti+NUMS^.prev^.data^.ti;
  192. News^.left := Nums^.data;
  193. News^.right := Nums^.prev^.data;
  194. RemoveItem(NUMS,NUMS^.data);
  195. RemoveItem(NUMS,NUMS^.data);
  196. AddItem(NUMS,News);
  197. end;
  198. THead := News;
  199. RemoveItem(NUMS,News);
  200. end;{CreateTree}
  201. {---------------------------------}
  202. Procedure GetCode(VAL:Byte;BRA:PTree;cur:longint;len:byte);
  203. begin
  204. if (Bra^.left=nil) and (Bra^.right=nil) and (BRA^.info = VAL) then
  205. begin
  206. Code := cur;
  207. CodeLen:=len;
  208. end;
  209. if (Bra^.left<>nil) then begin
  210. GetCode(val,Bra^.left,(cur shl 1) + 1,len+1);
  211. GetCode(val,Bra^.right,(cur shl 1),len+1)
  212. end;
  213. end;{GetCode}
  214. {---------------------------------}
  215. Procedure SetTable;
  216. var i : byte;
  217. begin
  218. for i := 0 to 255 do
  219. if counts[i] <> 0 then
  220. begin
  221. GetCode(i,THead,0,0);
  222. table[i] := Code;
  223. tbllens[i] := codelen;
  224. end;
  225. end;{SetTable}
  226. {---------------------------------}
  227. Function GetByte(cur:string):byte;
  228. var ret:byte;
  229. begin
  230. ret:=0;
  231. while length(cur)>0 do
  232. begin
  233. ret:=ret shl 1 + ord(cur[1])-ord('0');
  234. delete(cur,1,1);
  235. end;
  236. GetByte:=ret;
  237. end;{GetByte}
  238. {---------------------------------}
  239. Procedure IfNeedWrite(var gg:longint;var F:FILE);
  240. var
  241. FFF : byte;
  242. begin
  243. FFF := gg and $0FF;
  244. FWriteBuf(F,Fff);
  245. end;{IfNeedWrite}
  246. {---------------------------------}
  247. Function GetBit(var F:File):boolean;
  248. begin
  249. if GNum = 0 then
  250. begin
  251. GNum:=8;
  252. inc(CSize);
  253. done:=FReadBuf(F,GO);
  254. if not done then
  255. begin
  256. RBLast:=true;
  257. FReadBuf(F,GO);
  258. WBLast:=true;
  259. FWriteBuf(OUT,0);
  260. close(INP);
  261. Close(OUT);
  262. Halt(0);
  263. end;
  264. if csize=Size-1 then done:=FReadBuf(F,GNum);
  265. end;
  266. Dec(GNum);
  267. GetBit := (GO and 128) = 128;
  268. Go := GO shl 1;
  269. end;{GetBit}
  270. {---------------------------------}
  271. VAR
  272. CURCODE : longint;
  273. curlen : byte;
  274. CUR : byte;
  275. i : byte;
  276. Ctr : Ptree;
  277. BEGIN
  278. if (ParamCount<1) then
  279. begin
  280. WriteLn('Usage: HUF.exe <FILENAME.EXT> [OUTNAME.ext]');
  281. Halt(10);
  282. end;
  283. Fname := ParamStr(1);
  284. for size := 1 to Length(Fname) do
  285. fname[size] := UpCase(fname[size]);
  286. if pos('.HUF',fname) = 0 then
  287. begin
  288. if Paramcount = 2 then fout := ParamStr(2) else
  289. fout := Copy(Fname,1,pos('.',Fname)) + 'HUF';
  290. Assign(INP,Fname);
  291. Reset(INP,1);
  292. Size:=FileSize(INP);
  293. SetTimes(INP);
  294. CreateTree;
  295. SetTable;
  296. Assign(OUT,Fout);
  297. Rewrite(OUT,1);
  298. BlockWrite(OUT,Fname,14);
  299. PutTimes(OUT);
  300. curcode:=0;
  301. curlen:=0;
  302. csize:=0;
  303. RBFirst:=true;
  304. RBLast:=false;
  305. WBFirst:=true;
  306. WBLast:=false;
  307. while FReadBuf(INP,CUR) do
  308. begin
  309. inc(csize);
  310. for i := 0 to tbllens[cur] do
  311. begin
  312. if curlen = 8 then
  313. begin
  314. IfNeedWrite(curcode,OUT);
  315. curlen:=0;
  316. curcode:=0;
  317. end;
  318. curcode := (curcode shl 1) + (table[cur] and 1);
  319. inc(curlen);
  320. end;
  321. Write(#13'Complete: ',round((csize/size)*100),'%');
  322. end;
  323. if (curlen <> 0) then
  324. begin
  325. curcode := curcode shl (7-curlen);
  326. ifNeedWrite(curcode,OUT);
  327. end
  328. else
  329. curlen:=7;
  330. FWriteBuf(OUT,curlen);
  331. RBLast:=true;
  332. FReadBuf(INP,cur);
  333. WBLast:=true;
  334. FWriteBuf(OUT,0);
  335. Close(OUT);
  336. Close(INP);
  337. end
  338. else
  339. begin
  340. Assign(INP,Fname);
  341. Reset(INP,1);
  342. if ParamCount = 2 then FOut := ParamStr(2)
  343. else BlockRead(INP,Fout,14);
  344. Assign(OUT,Fout);
  345. Rewrite(OUT,1);
  346. Size:=FileSize(INP)-256*4-14;
  347. GetTimes(INP);
  348. CreateTree;
  349. Ctr := THead;
  350. RBFirst:=true;
  351. WBFirst:=true;
  352. RBLast:=false;
  353. WBLast:=false;
  354. while true do
  355. begin
  356. while CTR^.left <> nil do
  357. begin
  358. if GetBit(INP) then
  359. CTR:=CTR^.left
  360. else
  361. CTR:=CTR^.right;
  362. end;
  363. Write(#13'Complete: ',round((csize/size)*100),'%');
  364. FWriteBuf(OUT,CTR^.INFO);
  365. CTR:=THead;
  366. end;
  367. end;
  368. END.