tangled
alpha
login
or
join now
altagos.dev
/
codings
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
add zigzag coding
altagos.dev
6 months ago
1e6a350c
a12b8e42
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+38
2 changed files
expand all
collapse all
unified
split
src
ZigZag.zig
root.zig
+37
src/ZigZag.zig
reviewed
···
1
1
+
pub fn encode(value: isize) usize {
2
2
+
if (value < 0) return @as(usize, @intCast(value * -1)) * 2 - 1;
3
3
+
return @as(usize, @intCast(value)) * 2;
4
4
+
}
5
5
+
6
6
+
pub fn decode(value: usize) isize {
7
7
+
if (value & 1 == 1) return -@as(isize, @intCast(value / 2)) - 1;
8
8
+
return @as(isize, @intCast(value / 2));
9
9
+
}
10
10
+
11
11
+
test "zigzag.encode val = 10" {
12
12
+
const testing = @import("std").testing;
13
13
+
14
14
+
const encoded = encode(10);
15
15
+
try testing.expectEqual(20, encoded);
16
16
+
}
17
17
+
18
18
+
test "zigzag.encode val = -42" {
19
19
+
const testing = @import("std").testing;
20
20
+
21
21
+
const encoded = encode(-42);
22
22
+
try testing.expectEqual(83, encoded);
23
23
+
}
24
24
+
25
25
+
test "zigzag.decode val = 20" {
26
26
+
const testing = @import("std").testing;
27
27
+
28
28
+
const decoded = decode(20);
29
29
+
try testing.expectEqual(10, decoded);
30
30
+
}
31
31
+
32
32
+
test "zigzag.decode val = 83" {
33
33
+
const testing = @import("std").testing;
34
34
+
35
35
+
const decoded = decode(83);
36
36
+
try testing.expectEqual(-42, decoded);
37
37
+
}
+1
src/root.zig
reviewed
···
2
2
const std = @import("std");
3
3
4
4
pub const Golomb = @import("Golomb.zig");
5
5
+
pub const ZigZag = @import("ZigZag.zig");
5
6
6
7
test {
7
8
std.testing.refAllDecls(@This());