Update examples for Zig (#287)
* update zig in chapters 1-6 * fix zig dijkstras algo * fix zig greedy algo * fix longest_common_subsequence in zig * cleanup * test: use testing allocator
This commit is contained in:
@@ -37,10 +37,10 @@ fn search(
|
||||
var arena = heap.ArenaAllocator.init(allocator);
|
||||
defer arena.deinit();
|
||||
var searched = std.BufSet.init(arena.allocator());
|
||||
const Q = std.TailQueue([]const u8);
|
||||
const Q = std.DoublyLinkedList([]const u8);
|
||||
var queue = Q{};
|
||||
|
||||
var name_edges = graph.get(name);
|
||||
const name_edges = graph.get(name);
|
||||
if (name_edges) |edges| {
|
||||
var nodes = try arena.allocator().alloc(Q.Node, edges.len);
|
||||
var i: usize = 0;
|
||||
@@ -53,28 +53,26 @@ fn search(
|
||||
}
|
||||
|
||||
while (queue.len > 0) {
|
||||
var first = queue.popFirst();
|
||||
if (first) |person| {
|
||||
if (!searched.contains(person.data)) {
|
||||
if (personIsSeller(person.data)) {
|
||||
std.debug.print("{s} is a mango seller!\n", .{person.data});
|
||||
return;
|
||||
} else {
|
||||
var ee = graph.get(person.data);
|
||||
if (ee) |edges| {
|
||||
var nodes = try arena.allocator().alloc(Q.Node, edges.len);
|
||||
var i: usize = 0;
|
||||
while (i < edges.len) : (i += 1) {
|
||||
nodes[i].data = edges[i];
|
||||
}
|
||||
for (nodes) |*node| {
|
||||
queue.append(node);
|
||||
}
|
||||
}
|
||||
try searched.insert(person.data);
|
||||
}
|
||||
const person = queue.popFirst() orelse unreachable; // we always have at least one node if len > 0
|
||||
if (searched.contains(person.data)) {
|
||||
continue;
|
||||
}
|
||||
if (personIsSeller(person.data)) {
|
||||
std.debug.print("{s} is a mango seller!\n", .{person.data});
|
||||
return;
|
||||
}
|
||||
const ee = graph.get(person.data);
|
||||
if (ee) |edges| {
|
||||
var nodes = try arena.allocator().alloc(Q.Node, edges.len);
|
||||
var i: usize = 0;
|
||||
while (i < edges.len) : (i += 1) {
|
||||
nodes[i].data = edges[i];
|
||||
}
|
||||
for (nodes) |*node| {
|
||||
queue.append(node);
|
||||
}
|
||||
}
|
||||
try searched.insert(person.data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,14 +81,9 @@ fn personIsSeller(name: []const u8) bool {
|
||||
}
|
||||
|
||||
test "search" {
|
||||
var gpa = heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
var graph = std.StringHashMap([][]const u8).init(gpa.allocator());
|
||||
defer {
|
||||
graph.deinit();
|
||||
const leaked = gpa.deinit();
|
||||
if (leaked) std.testing.expect(false) catch @panic("TEST FAIL"); //fail test; can't try in defer as defer is executed after we return
|
||||
}
|
||||
const allocator = std.testing.allocator;
|
||||
var graph = std.StringHashMap([][]const u8).init(allocator);
|
||||
defer graph.deinit();
|
||||
|
||||
var you = [_][]const u8{ "alice", "bob", "claire" };
|
||||
var bob = [_][]const u8{ "anuj", "peggy" };
|
||||
@@ -110,5 +103,5 @@ test "search" {
|
||||
try graph.put("thom", &thom);
|
||||
try graph.put("jonny", &jonny);
|
||||
|
||||
try search(gpa.allocator(), &graph, "you");
|
||||
try search(allocator, &graph, "you");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user