Skip to content

Commit 42abb19

Browse files
authored
Merge pull request #24 from Ryoga-exe/zig-0.15.1
Update for Zig 0.15.1
2 parents 8384006 + ea2f4b9 commit 42abb19

File tree

8 files changed

+65
-50
lines changed

8 files changed

+65
-50
lines changed

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .ac_library,
33
.version = "0.3.0",
4-
.minimum_zig_version = "0.14.0",
4+
.minimum_zig_version = "0.15.1",
55
.fingerprint = 0x8ab1cf135fb49dc,
66
.paths = .{
77
"src",

src/internal_csr.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22
const Allocator = std.mem.Allocator;
3+
const ArrayList = std.ArrayList;
34

45
pub fn Csr(comptime E: type) type {
56
return struct {
@@ -9,15 +10,15 @@ pub fn Csr(comptime E: type) type {
910
elist: []E,
1011
allocator: Allocator,
1112

12-
pub fn init(allocator: Allocator, n: usize, edges: std.ArrayList(struct { usize, E }), initialValue: E) !Self {
13+
pub fn init(allocator: Allocator, n: usize, edges: []struct { usize, E }, initialValue: E) !Self {
1314
const self = Self{
1415
.start = try allocator.alloc(usize, n + 1),
15-
.elist = try allocator.alloc(E, edges.items.len),
16+
.elist = try allocator.alloc(E, edges.len),
1617
.allocator = allocator,
1718
};
1819
@memset(self.start, 0);
1920
@memset(self.elist, initialValue);
20-
for (edges.items) |e| {
21+
for (edges) |e| {
2122
self.start[e.@"0" + 1] += 1;
2223
}
2324
for (1..n + 1) |i| {
@@ -26,7 +27,7 @@ pub fn Csr(comptime E: type) type {
2627
var counter = try allocator.alloc(usize, n + 1);
2728
defer allocator.free(counter);
2829
@memcpy(counter, self.start);
29-
for (edges.items) |e| {
30+
for (edges) |e| {
3031
self.elist[counter[e.@"0"]] = e.@"1";
3132
counter[e.@"0"] += 1;
3233
}

src/internal_queue.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn SimpleQueue(comptime T: type) type {
1414
pub fn init(allocator: Allocator) Self {
1515
return Self{
1616
.allocator = allocator,
17-
.payload = .init(allocator),
17+
.payload = .empty,
1818
.pos = 0,
1919
};
2020
}
@@ -28,7 +28,7 @@ pub fn SimpleQueue(comptime T: type) type {
2828
}
2929

3030
pub fn deinit(self: *Self) void {
31-
self.payload.deinit();
31+
self.payload.deinit(self.allocator);
3232
}
3333

3434
pub fn size(self: Self) usize {
@@ -40,7 +40,7 @@ pub fn SimpleQueue(comptime T: type) type {
4040
}
4141

4242
pub fn push(self: *Self, t: T) Allocator.Error!void {
43-
return self.payload.append(t);
43+
return self.payload.append(self.allocator, t);
4444
}
4545

4646
pub fn pushAssumeCapacity(self: *Self, t: T) void {
@@ -55,7 +55,7 @@ pub fn SimpleQueue(comptime T: type) type {
5555
}
5656

5757
pub fn clearAndFree(self: *Self) void {
58-
self.payload.clearAndFree();
58+
self.payload.clearAndFree(self.allocator);
5959
self.pos = 0;
6060
}
6161

src/internal_scc.zig

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const std = @import("std");
22
const Csr = @import("internal_csr.zig").Csr;
33
const Groups = @import("internal_groups.zig");
44
const Allocator = std.mem.Allocator;
5+
const ArrayList = std.ArrayList;
56

67
const SccGraph = @This();
78

@@ -12,39 +13,39 @@ const Env = struct {
1213
g: Csr(Edge),
1314
now_ord: usize,
1415
group_num: usize,
15-
visited: std.ArrayList(usize),
16+
visited: ArrayList(usize),
1617
low: []usize,
1718
ord: []?usize,
1819
ids: []usize,
1920
};
2021

2122
n: usize,
22-
edges: std.ArrayList(struct { usize, Edge }),
23+
edges: ArrayList(struct { usize, Edge }),
2324
allocator: Allocator,
2425

2526
pub fn init(allocator: Allocator, n: usize) SccGraph {
2627
const self = SccGraph{
2728
.n = n,
28-
.edges = std.ArrayList(struct { usize, Edge }).init(allocator),
29+
.edges = .empty,
2930
.allocator = allocator,
3031
};
3132
return self;
3233
}
3334
pub fn deinit(self: *SccGraph) void {
34-
self.edges.deinit();
35+
self.edges.deinit(self.allocator);
3536
}
3637
pub fn numVertices(self: SccGraph) usize {
3738
return self.n;
3839
}
3940
pub fn addEdge(self: *SccGraph, from: usize, to: usize) !void {
40-
try self.edges.append(.{ from, Edge{ .to = to } });
41+
try self.edges.append(self.allocator, .{ from, Edge{ .to = to } });
4142
}
4243
pub fn sccIds(self: SccGraph) !struct { usize, []usize } {
4344
var env = Env{
44-
.g = try Csr(Edge).init(self.allocator, self.n, self.edges, Edge{ .to = 0 }),
45+
.g = try .init(self.allocator, self.n, self.edges.items, Edge{ .to = 0 }),
4546
.now_ord = 0,
4647
.group_num = 0,
47-
.visited = try std.ArrayList(usize).initCapacity(self.allocator, self.n),
48+
.visited = try .initCapacity(self.allocator, self.n),
4849
.low = try self.allocator.alloc(usize, self.n),
4950
.ord = try self.allocator.alloc(?usize, self.n),
5051
.ids = try self.allocator.alloc(usize, self.n),
@@ -56,15 +57,15 @@ pub fn sccIds(self: SccGraph) !struct { usize, []usize } {
5657

5758
defer {
5859
env.g.deinit();
59-
env.visited.deinit();
60+
env.visited.deinit(self.allocator);
6061
self.allocator.free(env.low);
6162
self.allocator.free(env.ord);
6263
self.allocator.free(env.ids);
6364
}
6465

6566
for (0..self.n) |i| {
6667
if (env.ord[i] == null) {
67-
try dfs(i, self.n, &env);
68+
try dfs(self.allocator, i, self.n, &env);
6869
}
6970
}
7071
for (0..self.n) |i| {
@@ -82,20 +83,20 @@ pub fn scc(self: SccGraph) !Groups {
8283
for (0..self.n) |i| {
8384
group_index[i] = ids[i];
8485
}
85-
return try Groups.init(self.allocator, group_num, group_index);
86+
return try .init(self.allocator, group_num, group_index);
8687
}
87-
fn dfs(v: usize, n: usize, env: *Env) !void {
88+
fn dfs(allocator: Allocator, v: usize, n: usize, env: *Env) !void {
8889
env.low[v] = env.now_ord;
8990
env.ord[v] = env.now_ord;
9091
env.now_ord += 1;
91-
try env.visited.append(v);
92+
try env.visited.append(allocator, v);
9293

9394
for (env.g.start[v]..env.g.start[v + 1]) |i| {
9495
const to = env.g.elist[i].to;
9596
if (env.ord[to]) |ord| {
9697
env.low[v] = @min(env.low[v], ord);
9798
} else {
98-
try dfs(to, n, env);
99+
try dfs(allocator, to, n, env);
99100
env.low[v] = @min(env.low[v], env.low[to]);
100101
}
101102
}

src/lib.zig

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,20 @@ pub const Modint998244353 = @import("modint.zig").Modint998244353;
1717
pub const DynamicModint = @import("modint.zig").DynamicModint;
1818
pub const Modint = @import("modint.zig").Modint;
1919

20-
pub usingnamespace @import("math.zig");
21-
pub usingnamespace @import("string.zig");
20+
pub const math = @import("math.zig");
21+
pub const powMod = math.powMod;
22+
pub const invMod = math.invMod;
23+
pub const crt = math.crt;
24+
pub const floorSum = math.floorSum;
25+
26+
pub const string = @import("string.zig");
27+
pub const suffixArrayManual = string.suffixArrayManual;
28+
pub const suffixArrayArbitrary = string.suffixArrayArbitrary;
29+
pub const suffixArray = string.suffixArray;
30+
pub const lcpArrayArbitrary = string.lcpArrayArbitrary;
31+
pub const lcpArray = string.lcpArray;
32+
pub const zAlgorithmArbitrary = string.zAlgorithmArbitrary;
33+
pub const zAlgorithm = string.zAlgorithm;
2234

2335
test {
2436
std.testing.refAllDecls(@This());

src/maxflow.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ pub fn MfGraph(comptime Cap: type) type {
7373
pub fn init(allocator: Allocator, n: usize) Allocator.Error!Self {
7474
const g = try allocator.alloc(Graph, n);
7575
for (g) |*item| {
76-
item.* = .init(allocator);
76+
item.* = .empty;
7777
}
7878
return Self{
7979
.allocator = allocator,
8080
.n = n,
81-
.pos = .init(allocator),
81+
.pos = .empty,
8282
.g = g,
8383
};
8484
}
8585

8686
/// Release all allocated memory.
87-
pub fn deinit(self: Self) void {
88-
self.pos.deinit();
89-
for (self.g) |item| {
90-
item.deinit();
87+
pub fn deinit(self: *Self) void {
88+
self.pos.deinit(self.allocator);
89+
for (self.g) |*item| {
90+
item.deinit(self.allocator);
9191
}
9292
self.allocator.free(self.g);
9393
}
@@ -114,11 +114,11 @@ pub fn MfGraph(comptime Cap: type) type {
114114
assert(to < self.n);
115115
assert(0 <= cap);
116116
const m = self.pos.items.len;
117-
try self.pos.append(.{ from, self.g[from].items.len });
117+
try self.pos.append(self.allocator, .{ from, self.g[from].items.len });
118118
const rev = self.g[to].items.len + @intFromBool(from == to);
119-
try self.g[from].append(InternalEdge{ .to = to, .rev = rev, .cap = cap });
119+
try self.g[from].append(self.allocator, InternalEdge{ .to = to, .rev = rev, .cap = cap });
120120
const rev2 = self.g[from].items.len - 1;
121-
try self.g[to].append(InternalEdge{
121+
try self.g[to].append(self.allocator, InternalEdge{
122122
.to = from,
123123
.rev = rev2,
124124
.cap = 0,

src/mincostflow.zig

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
7171
pub fn init(allocator: Allocator, n: usize) Self {
7272
return Self{
7373
.allocator = allocator,
74-
.edges = .init(allocator),
74+
.edges = .empty,
7575
.n = n,
7676
};
7777
}
7878

7979
/// Release all allocated memory.
8080
pub fn deinit(self: *Self) void {
81-
self.edges.deinit();
81+
self.edges.deinit(self.allocator);
8282
}
8383

8484
/// Adds an edge oriented from `from` to `to` with capacity `cap` and cost `cost`.
@@ -105,7 +105,7 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
105105
assert(0 <= cap);
106106
assert(0 <= cost);
107107
const m = self.edges.items.len;
108-
try self.edges.append(Edge{
108+
try self.edges.append(self.allocator, Edge{
109109
.from = from,
110110
.to = to,
111111
.cap = cap,
@@ -264,7 +264,7 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
264264
defer allocator.free(redge_idx);
265265

266266
var elist = try ArrayList(struct { usize, InsideEdge }).initCapacity(allocator, 2 * m);
267-
defer elist.deinit();
267+
defer elist.deinit(self.allocator);
268268
for (0..m) |i| {
269269
const e = self.edges.items[i];
270270
edge_idx[i] = degree[e.from];
@@ -290,7 +290,7 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
290290
},
291291
});
292292
}
293-
var g = try internal.Csr(InsideEdge).init(allocator, n, elist, .zero);
293+
var g = try internal.Csr(InsideEdge).init(allocator, n, elist.items, .zero);
294294
for (0..m) |i| {
295295
const e = self.edges.items[i];
296296
edge_idx[i] += g.start[e.from];
@@ -315,8 +315,8 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
315315
var flow_current: Cap = 0;
316316
var cost_current: Cost = 0;
317317
var prev_cost_per_flow: Cost = -1;
318-
var result = ArrayList(CapCostPair).init(allocator);
319-
try result.append(CapCostPair{ 0, 0 });
318+
var result: ArrayList(CapCostPair) = .empty;
319+
try result.append(self.allocator, CapCostPair{ 0, 0 });
320320
while (flow_current < flow_limit) {
321321
if (!try self.refineDual(s, t, g, dual_dist, vis, prev_e)) {
322322
break;
@@ -338,11 +338,11 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
338338
if (prev_cost_per_flow == d) {
339339
_ = result.pop();
340340
}
341-
try result.append(CapCostPair{ flow_current, cost_current });
341+
try result.append(self.allocator, CapCostPair{ flow_current, cost_current });
342342
prev_cost_per_flow = d;
343343
}
344344

345-
break :blk try result.toOwnedSlice();
345+
break :blk try result.toOwnedSlice(self.allocator);
346346
};
347347

348348
for (0..m) |i| {
@@ -390,13 +390,13 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
390390
return math.order(a.key, b.key);
391391
}
392392
};
393-
var que_min = ArrayList(usize).init(allocator);
394-
defer que_min.deinit();
393+
var que_min: ArrayList(usize) = .empty;
394+
defer que_min.deinit(self.allocator);
395395
var que = std.PriorityQueue(Q, void, Q.lessThan).init(allocator, {});
396396
defer que.deinit();
397397

398398
dual[s].@"1" = 0;
399-
try que_min.append(s);
399+
try que_min.append(self.allocator, s);
400400
while (que.count() > 0 or que_min.items.len > 0) {
401401
const v = que_min.pop() orelse que.remove().to;
402402
if (vis[v]) {
@@ -418,7 +418,7 @@ pub fn McfGraph(comptime Cap: type, comptime Cost: type) type {
418418
dual[e.to].@"1" = dist_to;
419419
prev_e[e.to] = e.rev;
420420
if (dist_to == dist_v) {
421-
try que_min.append(e.to);
421+
try que_min.append(self.allocator, e.to);
422422
} else {
423423
try que.add(Q{ .key = dist_to, .to = e.to });
424424
}

src/string.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const std = @import("std");
44
const Allocator = std.mem.Allocator;
5+
const ArrayList = std.ArrayList;
56
const assert = std.debug.assert;
67

78
fn saNaive(comptime T: type, allocator: Allocator, s: []const T) Allocator.Error![]usize {
@@ -214,8 +215,8 @@ fn saIs(comptime threshold: Threshold, allocator: Allocator, s: []const usize, u
214215
m += 1;
215216
}
216217
}
217-
var lms = try std.ArrayList(usize).initCapacity(allocator, m);
218-
defer lms.deinit();
218+
var lms = try ArrayList(usize).initCapacity(allocator, m);
219+
defer lms.deinit(allocator);
219220
for (1..n) |i| {
220221
if (!ls[i - 1] and ls[i]) {
221222
lms.appendAssumeCapacity(i);
@@ -225,8 +226,8 @@ fn saIs(comptime threshold: Threshold, allocator: Allocator, s: []const usize, u
225226
try induce.f(context, sa, lms.items);
226227

227228
if (m > 0) {
228-
var sorted_lms = try std.ArrayList(usize).initCapacity(allocator, m);
229-
defer sorted_lms.deinit();
229+
var sorted_lms = try ArrayList(usize).initCapacity(allocator, m);
230+
defer sorted_lms.deinit(allocator);
230231
for (sa) |v| {
231232
if (lms_map[v - 1] != 0) {
232233
sorted_lms.appendAssumeCapacity(v - 1);

0 commit comments

Comments
 (0)