-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.zig
197 lines (172 loc) · 5.01 KB
/
main.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
const std = @import("std");
const ArrayList = std.ArrayList;
const mem = std.mem;
const maxInt = std.math.maxInt;
const assert = std.debug.assert;
const stv = @import("./zig/v1.zig");
const builtin = @import("builtin");
const global_allocator = if (builtin.target.cpu.arch == .wasm32) std.heap.wasm_allocator else std.heap.page_allocator;
extern fn debug_string(str_bytes: ?[*]u8, str_len: usize) void;
fn debug(comptime fmt: []const u8, args: anytype) void {
const line = std.fmt.allocPrint(global_allocator, fmt, args) catch unreachable;
defer global_allocator.free(line);
debug_string(line.ptr, line.len);
}
export fn single_transferable_vote(seats: u8, candidate_count: u8, vote_count: u32, data_pointer: [*]const u32) [*]u32 {
const vote_data_size = candidate_count * vote_count;
const memory_size: usize = vote_data_size + candidate_count;
defer global_allocator.free(data_pointer[0..memory_size]);
const elected_candidates = stv.count(
global_allocator,
seats,
data_pointer[0..vote_data_size],
data_pointer[vote_data_size..memory_size],
) catch unreachable;
return elected_candidates.ptr;
}
export fn allocUint32(length: u32) [*]u32 {
const slice = global_allocator.alloc(u32, length) catch
@panic("failed to allocate memory");
return slice.ptr;
}
export fn deallocElectedCandidates(ptr: [*]u32) void {
std.debug.assert(ptr[0] == 0);
const len = ptr[1];
global_allocator.free(ptr[0..len]);
}
test "test 01 - only one winner because of many empty votes" {
try callCount(
\\{
\\ "seats": 2,
\\ "tie_rank": [1, 2, 3],
\\ "votes": [[1, 0, 0], [0, 0, 0], [0, 0, 0]],
\\ "expect": [0]
\\}
);
}
test "test 02" {
try callCount(
\\{
\\ "seats": 1,
\\ "tie_rank": [1, 2, 3],
\\ "votes": [[1, 2, 3], [2, 3, 1], [3, 1, 2]],
\\ "expect": [2]
\\}
);
}
test "test 03" {
try callCount(
\\{
\\ "seats": 2,
\\ "tie_rank": [1, 2, 3, 4],
\\ "votes": [
\\ [4, 1, 3, 2],
\\ [2, 4, 1, 3],
\\ [1, 4, 2, 3],
\\ [1, 2, 4, 3],
\\ [1, 4, 3, 0],
\\ [3, 2, 4, 1],
\\ [3, 4, 1, 2],
\\ [3, 4, 1, 2],
\\ [4, 3, 2, 0],
\\ [2, 3, 4, 1]
\\ ],
\\ "expect": [1, 2]
\\}
);
}
test "test 04" {
try callCount(
\\{
\\ "seats": 2,
\\ "tie_rank": [1, 2, 3, 4],
\\ "votes": [
\\ [4, 1, 3, 2],
\\ [2, 4, 1, 3],
\\ [2, 4, 1, 3],
\\ [1, 2, 4, 3],
\\ [1, 4, 0, 3],
\\ [3, 2, 4, 1],
\\ [3, 4, 1, 2],
\\ [3, 4, 1, 2],
\\ [4, 3, 2, 0],
\\ [2, 3, 4, 1]
\\ ],
\\ "expect": [1, 0]
\\}
);
}
test "test 05" {
try callCount(
\\{
\\ "seats": 2,
\\ "tie_rank": [1, 2, 3, 4],
\\ "votes": [
\\ [0, 0, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 2, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 0, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 0, 0, 0],
\\ [0, 0, 0, 0]
\\ ],
\\ "expect": [1]
\\}
);
}
test "test 06" {
try callCount(
\\{
\\ "seats": 2,
\\ "tie_rank": [1, 2, 3, 4],
\\ "votes": [
\\ [0, 0, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 2, 0, 0],
\\ [0, 4, 0, 0],
\\ [0, 0, 0, 0],
\\ [1, 4, 0, 0],
\\ [0, 4, 0, 1],
\\ [1, 0, 0, 0],
\\ [0, 0, 0, 1]
\\ ],
\\ "expect": [1, 3]
\\}
);
}
fn callCount(json_data: []const u8) !void {
const allocator = std.testing.allocator;
var parsed = try std.json.parseFromSlice(TestData, allocator, json_data, .{});
defer parsed.deinit();
const data = parsed.value;
const raw_votes = try data.rawVotes(allocator);
defer allocator.free(raw_votes);
const result = try stv.count(allocator, data.seats, raw_votes, data.tie_rank);
defer allocator.free(result);
try std.testing.expectEqualSlices(u32, data.expect, result[2..]);
}
const TestData = struct {
seats: u8,
tie_rank: []u32,
votes: [][]u32,
expect: []u32,
pub fn rawVotes(self: TestData, allocator: std.mem.Allocator) ![]u32 {
var result = try allocator.alloc(u32, self.candidateCount() * (self.voteCount()));
for (self.votes, 0..) |vote, i| {
for (vote, 0..) |pref, j| {
result[i * self.candidateCount() + j] = pref;
}
}
return result;
}
pub fn candidateCount(self: TestData) u32 {
return @intCast(self.tie_rank.len);
}
pub fn voteCount(self: TestData) u32 {
return @intCast(self.votes.len);
}
};