forked from amix/photoshop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHuffman.p
180 lines (126 loc) · 2.93 KB
/
Huffman.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{Photoshop version 1.0.1, file: Huffman.p
Computer History Museum, www.computerhistory.org
This material is (C)Copyright 1990 Adobe Systems Inc.
It may not be distributed to third parties.
It is licensed for non-commercial use according to
www.computerhistory.org/softwarelicense/photoshop/ }
PROGRAM Huffman;
TYPE
PNode = ^TNode;
TNode = RECORD
leaf: BOOLEAN;
code: INTEGER;
entry: INTEGER;
branch: ARRAY [0..1] OF PNode
END;
Str255 = STRING [255];
VAR
id: INTEGER;
root: PNode;
word: Str255;
code: INTEGER;
entry: INTEGER;
PROCEDURE AddCode (node: PNode; code: INTEGER; VAR word: Str255);
VAR
branch: INTEGER;
BEGIN
IF LENGTH (word) = 0 THEN
BEGIN
IF node^.leaf OR (node^.branch [0] <> NIL) OR
(node^.branch [1] <> NIL) THEN
BEGIN
WRITELN ('? Conflict for code ', code:1);
EXIT (PROGRAM)
END;
node^.leaf := TRUE;
node^.code := code
END
ELSE
BEGIN
IF word [1] = '0' THEN
branch := 0
ELSE IF word [1] = '1' THEN
branch := 1
ELSE
BEGIN
WRITELN ('? Invalid word for code ', code:1);
EXIT (PROGRAM)
END;
DELETE (word, 1, 1);
IF node^.branch [branch] = NIL THEN
BEGIN
NEW (node^.branch [branch]);
node^.branch [branch]^.leaf := FALSE;
node^.branch [branch]^.branch [0] := NIL;
node^.branch [branch]^.branch [1] := NIL
END;
AddCode (node^.branch [branch], code, word)
END
END;
PROCEDURE NumberNode (node: PNode);
BEGIN
node^.entry := entry;
entry := entry + 1;
IF node^.branch [0] <> NIL THEN NumberNode (node^.branch [0]);
IF node^.branch [1] <> NIL THEN NumberNode (node^.branch [1])
END;
PROCEDURE WriteHexDigit (x: INTEGER);
BEGIN
IF x <= 9 THEN
WRITE (x:1)
ELSE
WRITE (CHR (ORD ('A') + x - 10))
END;
PROCEDURE WriteHex (x: INTEGER);
BEGIN
WriteHexDigit (BAND (BSR (x, 12), $F));
WriteHexDigit (BAND (BSR (x, 8), $F));
WriteHexDigit (BAND (BSR (x, 4), $F));
WriteHexDigit (BAND (x, $F))
END;
PROCEDURE WriteNode (node: PNode);
BEGIN
WRITE (' $"');
IF node^.leaf THEN
WriteHex (node^.code)
ELSE
WriteHex (-1);
WRITE (' ');
IF node^.branch [0] <> NIL THEN
WriteHex (node^.branch [0]^.entry)
ELSE
WriteHex (-1);
WRITE (' ');
IF node^.branch [1] <> NIL THEN
WriteHex (node^.branch [1]^.entry)
ELSE
WriteHex (-1);
WRITELN ('"');
IF node^.branch [0] <> NIL THEN WriteNode (node^.branch [0]);
IF node^.branch [1] <> NIL THEN WriteNode (node^.branch [1])
END;
BEGIN
NEW (root);
root^.leaf := FALSE;
root^.branch [0] := NIL;
root^.branch [1] := NIL;
READLN (id);
WHILE NOT EOF DO
BEGIN
READ (code);
READLN (word);
WHILE (word [1] = ' ') OR
(word [1] = CHR (9)) DO
DELETE (word, 1, 1);
WHILE (word [LENGTH (word)] = ' ') OR
(word [LENGTH (word)] = CHR (9)) DO
DELETE (word, LENGTH (word), 1);
AddCode (root, code, word)
END;
entry := 0;
NumberNode (root);
WRITELN ('data ''HUFF'' (', id:1, ', purgeable)');
WRITELN (' {');
WriteNode (root);
WRITELN (' };')
END.