-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.zig
302 lines (250 loc) · 8.23 KB
/
encode.zig
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const std = @import("std");
const Allocator = std.mem.Allocator;
const testing = std.testing;
const Node = @import("node.zig").Node;
const BitVec = @import("./bitvec.zig");
// The tree for the input "hello world" would look like this:
//
// [Internal]
// / \
// [Internal] [Internal]
// / \ / \
// [Internal] [Internal] 'l' [Internal]
// / \ / \ / \
// 'd' 'h' 'e' 'w' 'o' [Internal]
// / \
// =========================== ' ' 'r'
// | "hello world" tree |
// ===========================
//
const test_input = "hello world";
const Self = @This();
input: []const u8,
allocator: Allocator,
pub fn init(allocator: Allocator, input: []const u8) Self {
return .{ .allocator = allocator, .input = input };
}
pub fn encode(self: *Self) !BitVec {
var root = try self.buildTree();
defer root.deinit(self.allocator);
var bits = BitVec.init(self.allocator);
try self.addHeader(&bits);
try serializeTree(root, &bits);
try self.encodeBytes(root, &bits);
return bits;
}
test "encode" {
var encoder = Self.init(testing.allocator, test_input);
var bits = try encoder.encode();
defer bits.deinit();
var it = bits.iterator();
while (it.next()) |b| {
std.debug.print("{d}", .{b});
}
std.debug.print("\n", .{});
}
fn addHeader(self: *Self, bits: *BitVec) !void {
const length: u64 = self.input.len;
try bits.pushBits(64, length);
}
fn buildTree(self: *Self) !*const Node {
var frequencies = try self.calculate_frequencies();
defer frequencies.deinit();
var queue = try self.prioritize(&frequencies);
defer queue.deinit();
while (queue.count() >= 2) {
const left = queue.remove();
const right = queue.remove();
const internal_node = try Node.internal(
self.allocator,
left.node,
right.node,
);
const priority = left.priority + right.priority;
try queue.add(.{ .node = internal_node, .priority = priority });
}
const root = queue.remove().node;
return root;
}
test "buildTree" {
var encoder = Self.init(testing.allocator, test_input);
var root = try encoder.buildTree();
defer root.deinit(testing.allocator);
std.debug.print("{s}\n", .{root});
}
const Frequencies = std.AutoHashMap(u8, u32);
fn calculate_frequencies(self: *Self) !Frequencies {
var frequencies = Frequencies.init(self.allocator);
for (self.input) |byte| {
const result = try frequencies.getOrPut(byte);
if (!result.found_existing) {
result.value_ptr.* = 0;
}
result.value_ptr.* += 1;
}
return frequencies;
}
test "test frequencies" {
const allocator = std.testing.allocator;
var encoder = Self.init(allocator, test_input);
var frequencies = try encoder.calculate_frequencies();
defer frequencies.deinit();
try testing.expectEqual(8, frequencies.count());
var total: u32 = 0;
var iterator = frequencies.iterator();
while (iterator.next()) |entry| {
const count = entry.value_ptr.*;
total += count;
}
try testing.expectEqual(total, test_input.len);
}
const NodeAndPriority = struct { node: *const Node, priority: u32 };
const math = std.math;
fn greather_than(ctx: void, a: NodeAndPriority, b: NodeAndPriority) math.Order {
_ = ctx;
return math.order(a.priority, b.priority);
}
const PQgt = std.PriorityQueue(NodeAndPriority, void, greather_than);
fn prioritize(self: *Self, frequencies: *const Frequencies) !PQgt {
var queue = PQgt.init(self.allocator, {});
var iter = frequencies.iterator();
while (iter.next()) |entry| {
try queue.add(.{
.node = try Node.leaf(self.allocator, entry.key_ptr.*),
.priority = entry.value_ptr.*,
});
}
return queue;
}
test "test prioritize" {
const allocator = testing.allocator;
var encoder = Self.init(allocator, test_input);
var frequencies = try encoder.calculate_frequencies();
defer frequencies.deinit();
var queue = try encoder.prioritize(&frequencies);
defer queue.deinit();
defer while (queue.removeOrNull()) |nap| {
nap.node.deinit(allocator);
};
var it = queue.iterator();
while (it.next()) |n| {
std.debug.print("{s} - {d}\n", .{ n.node, n.priority });
}
try testing.expectEqual(frequencies.count(), queue.count());
}
fn serializeTree(root: *const Node, bits: *BitVec) !void {
try serializeNode(root, bits);
}
fn serializeNode(node: *const Node, bits: *BitVec) !void {
switch (node.*) {
.leaf => |leaf_node| {
try bits.push(1);
try bits.pushBits(8, leaf_node.byte);
},
.internal => |internal_node| {
try bits.push(0);
try serializeNode(internal_node.left, bits);
try serializeNode(internal_node.right, bits);
},
}
}
test "serialize tree" {
var encoder = Self.init(testing.allocator, test_input);
var root = try encoder.buildTree();
defer root.deinit(testing.allocator);
var bits = BitVec.init(testing.allocator);
try serializeTree(root, &bits);
defer bits.deinit();
std.debug.print("Serialized Tree: ", .{});
var it = bits.iterator();
while (it.next()) |b| {
std.debug.print("{d}", .{b});
}
std.debug.print("\n", .{});
}
fn encodeBytes(self: *Self, root: *const Node, bits: *BitVec) !void {
var codes_by_char = try self.walkPathsForCodes(root);
defer deinitCodesByChar(&codes_by_char);
for (self.input) |byte| {
const char_bits = codes_by_char.get(byte).?;
try bits.append(&char_bits);
}
}
test "encode bytes" {
var encoder = Self.init(testing.allocator, test_input);
var root = try encoder.buildTree();
defer root.deinit(testing.allocator);
var bits = BitVec.init(testing.allocator);
defer bits.deinit();
try encoder.encodeBytes(root, &bits);
var bit = bits.iterator();
while (bit.next()) |b| {
std.debug.print("{d}", .{b});
}
std.debug.print("\n", .{});
}
const CodesByChar = std.AutoHashMap(u8, BitVec);
fn walkPathsForCodes(self: *Self, root: *const Node) !CodesByChar {
var codes_by_char = CodesByChar.init(self.allocator);
var bits = BitVec.init(self.allocator);
defer bits.deinit();
try self.walkNodeForCodes(
root,
&bits,
&codes_by_char,
);
return codes_by_char;
}
fn walkNodeForCodes(
self: *Self,
node: *const Node,
bits: *BitVec,
codes_by_char: *CodesByChar,
) !void {
switch (node.*) {
.leaf => |leaf_node| {
const bits_copy = try bits.copy(self.allocator);
try codes_by_char.put(leaf_node.byte, bits_copy);
},
.internal => |internal_node| {
try bits.push(0);
try self.walkNodeForCodes(internal_node.left, bits, codes_by_char);
_ = bits.pop();
try bits.push(1);
try self.walkNodeForCodes(internal_node.right, bits, codes_by_char);
_ = bits.pop();
},
}
}
fn deinitCodesByChar(codes_by_char: *CodesByChar) void {
var it = codes_by_char.iterator();
while (it.next()) |entry| {
entry.value_ptr.*.deinit();
}
codes_by_char.deinit();
}
test "walk tree" {
var encoder = Self.init(testing.allocator, test_input);
var root = try encoder.buildTree();
defer root.deinit(testing.allocator);
var codes_by_char = try encoder.walkPathsForCodes(root);
defer deinitCodesByChar(&codes_by_char);
var codes_it = codes_by_char.iterator();
while (codes_it.next()) |entry| {
const c = entry.key_ptr.*;
var bv = BitVec.init(testing.allocator);
defer bv.deinit();
try bv.pushBits(8, c);
var it = bv.iterator();
while (it.next()) |bit| {
std.debug.print("{d}", .{bit});
}
std.debug.print("- {c} ({0b})- ", .{c});
const bits = entry.value_ptr.*;
var bit = bits.iterator();
while (bit.next()) |b| {
std.debug.print("{d}", .{b});
}
std.debug.print("\n", .{});
}
}