Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
### How to build

**Require:**
- [Zig v0.15.1 or higher](https://ziglang.org/download), self-hosting (stage3) compiler.
- [Zig v0.16.0 or higher](https://ziglang.org/download), self-hosting (stage3) compiler.

### Test all

Expand Down
6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

// Tracks the earliest Zig version that the package considers to be a
// supported use case.
.minimum_zig_version = "0.15.1",
.minimum_zig_version = "0.16.0",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand All @@ -38,8 +38,8 @@
.dependencies = .{
// Custom test-runner: see tests output
.runner = .{
.url = "git+https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b#eb15512d6ae49663fa9df6c7a9725b20dab43edd",
.hash = "N-V-__8AAHMkAAC4CUVVTX0UMBJXtfOubskbF9EJ7X6qAGYR",
.url = "git+https://gist.github.com/kassane/f8895bff7f830f3e96840ee9ce911746#1682032ef192bf444b88055ddabf57059883869c",
.hash = "N-V-__8AAAklAAA_YUknri28iZw4tsvgbnbKzfAKZ1qGX4iN",
Comment thread
kassane marked this conversation as resolved.
},
},
.paths = .{
Expand Down
13 changes: 5 additions & 8 deletions concurrency/threads/ThreadPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ pub const ThreadPool = struct {
// Acquiring to WAITING will make the next notify() or shutdown() wake a sleeping futex thread
// who will either exit on SHUTDOWN or acquire with WAITING again, ensuring all threads are awoken.
// This unfortunately results in the last notify() or shutdown() doing an extra futex wake but that's fine.
std.Thread.Futex.wait(&self.state, WAITING);
while (self.state.load(.monotonic) == WAITING) {
std.atomic.spinLoopHint();
}
state = self.state.load(.monotonic);
acquire_with = WAITING;
}
Expand All @@ -600,13 +602,8 @@ pub const ThreadPool = struct {
fn wake(self: *Event, release_with: u32, wake_threads: u32) void {
// Update the Event to notifty it with the new `release_with` state (either NOTIFIED or SHUTDOWN).
// Release barrier to ensure any operations before this are this to happen before the wait() in the other threads.
const state = self.state.swap(release_with, .release);

// Only wake threads sleeping in futex if the state is WAITING.
// Avoids unnecessary wake ups.
if (state == WAITING) {
std.Thread.Futex.wake(&self.state, wake_threads);
}
_ = self.state.swap(release_with, .release);
_ = wake_threads;
}
};

Expand Down
5 changes: 3 additions & 2 deletions dataStructures/doublyLinkedList.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const print = std.debug.print;
const testing = std.testing;
const assert = std.debug.assert;

// Returns a doubly linked list instance.
// Arguments:
Expand Down Expand Up @@ -186,8 +187,8 @@ pub fn DoublyLinkedList(comptime T: type) type {
}

test "Testing Doubly Linked List" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var gpa = std.heap.DebugAllocator(.{}){};
defer assert(gpa.deinit() == .ok);
var allocator = gpa.allocator();

var list = DoublyLinkedList(i32){ .allocator = &allocator };
Expand Down
9 changes: 5 additions & 4 deletions dataStructures/stack.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const testing = std.testing;
const assert = std.debug.assert;

const errors = error{EmptyList};

Expand Down Expand Up @@ -77,8 +78,8 @@ pub fn stack(comptime T: type) type {
}

test "Testing insertion/popping in stack" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var gpa = std.heap.DebugAllocator(.{}){};
defer assert(gpa.deinit() == .ok);
var allocator = gpa.allocator();

var s = stack(i32){ .allocator = &allocator };
Expand All @@ -104,8 +105,8 @@ test "Testing insertion/popping in stack" {
}

test "Testing other formats" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var gpa = std.heap.DebugAllocator(.{}){};
defer assert(gpa.deinit() == .ok);
var allocator = gpa.allocator();

var s = stack(u8){ .allocator = &allocator };
Expand Down
26 changes: 9 additions & 17 deletions dataStructures/trie.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const HashMap = std.AutoArrayHashMap;
const HashMap = std.AutoArrayHashMapUnmanaged;

const TrieError = error{
InvalidNode,
Expand All @@ -13,10 +13,10 @@ pub fn TrieNode(comptime T: type) type {
children: HashMap(u8, *Self),
parent: ?*Self,

fn init(node_data: T, allocator: Allocator, parent: ?*Self) TrieNode(T) {
fn init(node_data: T, parent: ?*Self) TrieNode(T) {
return TrieNode(T){
.node_data = node_data,
.children = HashMap(u8, *Self).init(allocator),
.children = .{},
.parent = parent,
};
}
Expand Down Expand Up @@ -71,17 +71,13 @@ pub fn Trie(comptime T: type) type {
/// Allocate a new node and return its pointer
fn new_node(self: Self, node_data: T, parent: ?*NodeType) !*NodeType {
const node_ptr = try self.allocator.create(NodeType);
node_ptr.* = NodeType.init(
node_data,
self.allocator,
parent,
);
node_ptr.* = NodeType.init(node_data, parent);
return node_ptr;
}

pub fn init(root_data: T, allocator: Allocator) !Self {
const node_ptr = try allocator.create(NodeType);
node_ptr.* = NodeType.init(root_data, allocator, null);
node_ptr.* = NodeType.init(root_data, null);
return Self{
.trie_root = node_ptr,
.allocator = allocator,
Expand All @@ -104,7 +100,7 @@ pub fn Trie(comptime T: type) type {
new_value,
iterator.node_at_iterator,
);
try iterator.node_at_iterator.children.put(char, node);
try iterator.node_at_iterator.children.put(self.allocator, char, node);
iterator = iterator.go_to_child(char).?;
}
}
Expand Down Expand Up @@ -156,7 +152,7 @@ pub fn Trie(comptime T: type) type {
while (it.next()) |entry| {
self.recursive_free(IteratorType.init(entry.value_ptr.*));
}
iterator.node_at_iterator.children.deinit();
iterator.node_at_iterator.children.deinit(self.allocator);
self.allocator.destroy(iterator.node_at_iterator);
}

Expand All @@ -167,9 +163,7 @@ pub fn Trie(comptime T: type) type {
}

test "basic traverse" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const debug_allocator = gpa.allocator();
const trie = try Trie(i32).init(0, debug_allocator);
const trie = try Trie(i32).init(0, std.testing.allocator);
defer trie.deinit();
_ = try trie.add_string("aaa", 0);
_ = try trie.add_string("abb", 0);
Expand All @@ -186,9 +180,7 @@ test "basic traverse" {
}

test "iterator traverse" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const debug_allocator = gpa.allocator();
const trie = try Trie(i32).init(0, debug_allocator);
const trie = try Trie(i32).init(0, std.testing.allocator);
defer trie.deinit();
var it = try trie.add_string("abc", 0); // "abc"
try std.testing.expectEqual(null, it.go_to_child('a'));
Expand Down
5 changes: 3 additions & 2 deletions dynamicProgramming/longestIncreasingSubsequence.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const print = std.debug.print;
const testing = std.testing;
const assert = std.debug.assert;
const ArrayList = std.ArrayList;

// Function that returns the lower bound in O(logn)
Expand Down Expand Up @@ -48,8 +49,8 @@ pub fn lis(arr: []const i32, allocator: anytype) usize {
}

test "testing longest increasing subsequence function" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var gpa = std.heap.DebugAllocator(.{}){};
defer assert(gpa.deinit() == .ok);

const v = [4]i32{ 1, 5, 6, 7 };
try testing.expect(lis(&v, gpa.allocator()) == 4);
Expand Down
2 changes: 1 addition & 1 deletion machine_learning/k_means_clustering.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn KMeans(data: []const Point2D, comptime k: usize) ![k]Cluster {
old_clusters[i].count = 0;
}
while (true) {
var new_clusters: [k]Cluster = .{Cluster.zero} ** k;
var new_clusters: [k]Cluster = @splat(Cluster.zero);
for (data) |point| {
const cluster_idx = calculateNearest(point, old_clusters);
const new = &new_clusters[cluster_idx];
Expand Down
127 changes: 57 additions & 70 deletions runall.zig
Original file line number Diff line number Diff line change
@@ -1,90 +1,77 @@
const std = @import("std");
const Io = std.Io;

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
pub fn main(init: std.process.Init) !void {
const io = init.io;

// Math algorithms
try runTest(allocator, "math/ceil");
try runTest(allocator, "math/crt");
try runTest(allocator, "math/primes");
try runTest(allocator, "math/fibonacci");
try runTest(allocator, "math/factorial");
try runTest(allocator, "math/euclidianGCDivisor");
try runTest(allocator, "math/gcd");
try runTest(io, "math/ceil");
try runTest(io, "math/crt");
try runTest(io, "math/primes");
try runTest(io, "math/fibonacci");
try runTest(io, "math/factorial");
try runTest(io, "math/euclidianGCDivisor");
try runTest(io, "math/gcd");

// Data Structures
try runTest(allocator, "ds/trie");
try runTest(allocator, "ds/linkedlist");
try runTest(allocator, "ds/doublylinkedlist");
try runTest(allocator, "ds/lrucache");
try runTest(allocator, "ds/stack");
try runTest(allocator, "ds/heap");
try runTest(allocator, "ds/queue");
try runTest(io, "ds/trie");
try runTest(io, "ds/linkedlist");
try runTest(io, "ds/doublylinkedlist");
try runTest(io, "ds/lrucache");
try runTest(io, "ds/stack");
try runTest(io, "ds/heap");
try runTest(io, "ds/queue");

// Dynamic Programming
try runTest(allocator, "dp/coinChange");
try runTest(allocator, "dp/knapsack");
try runTest(allocator, "dp/longestIncreasingSubsequence");
try runTest(allocator, "dp/editDistance");
try runTest(io, "dp/coinChange");
try runTest(io, "dp/knapsack");
try runTest(io, "dp/longestIncreasingSubsequence");
try runTest(io, "dp/editDistance");

// Sort
try runTest(allocator, "sort/quicksort");
try runTest(allocator, "sort/bubblesort");
try runTest(allocator, "sort/radixsort");
try runTest(allocator, "sort/mergesort");
try runTest(allocator, "sort/insertsort");
try runTest(allocator, "sort/selectionSort");
try runTest(allocator, "sort/heapSort");
try runTest(io, "sort/quicksort");
try runTest(io, "sort/bubblesort");
try runTest(io, "sort/radixsort");
try runTest(io, "sort/mergesort");
try runTest(io, "sort/insertsort");
try runTest(io, "sort/selectionSort");
try runTest(io, "sort/heapSort");

// Search
try runTest(allocator, "search/bSearchTree");
try runTest(allocator, "search/rb");
try runTest(allocator, "search/linearSearch");
try runTest(io, "search/bSearchTree");
try runTest(io, "search/rb");
try runTest(io, "search/linearSearch");

// Threads
try runTest(allocator, "threads/threadpool");
try runTest(io, "threads/threadpool");

// Web
try runTest(allocator, "web/httpClient");
try runTest(allocator, "web/httpServer");
try runTest(allocator, "web/tls1_3");
try runTest(io, "web/httpClient");
try runTest(io, "web/httpServer");
try runTest(io, "web/tls1_3");

// Machine Learning
try runTest(allocator, "machine_learning/k_means_clustering");
try runTest(io, "machine_learning/k_means_clustering");

// Numerical Methods
try runTest(allocator, "numerical_methods/newton_raphson");
try runTest(io, "numerical_methods/newton_raphson");

// Tiger Style
try runTest(allocator, "tiger_style/time_simulation");
try runTest(allocator, "tiger_style/merge_sort_tiger");
try runTest(allocator, "tiger_style/knapsack_tiger");
try runTest(allocator, "tiger_style/ring_buffer");
try runTest(allocator, "tiger_style/raft_consensus");
try runTest(allocator, "tiger_style/two_phase_commit");
try runTest(allocator, "tiger_style/vsr_consensus");
try runTest(allocator, "tiger_style/robin_hood_hash");
try runTest(allocator, "tiger_style/skip_list");
try runTest(io, "tiger_style/time_simulation");
try runTest(io, "tiger_style/merge_sort_tiger");
try runTest(io, "tiger_style/knapsack_tiger");
try runTest(io, "tiger_style/ring_buffer");
try runTest(io, "tiger_style/raft_consensus");
try runTest(io, "tiger_style/two_phase_commit");
try runTest(io, "tiger_style/vsr_consensus");
try runTest(io, "tiger_style/robin_hood_hash");
try runTest(io, "tiger_style/skip_list");
}

fn runTest(allocator: std.mem.Allocator, comptime algorithm: []const u8) !void {
var child = std.process.Child.init(&[_][]const u8{
const args = [_][]const u8{
"--summary",
"all",
"-freference-trace",
};

fn runTest(io: Io, comptime algorithm: []const u8) !void {
const argv = [_][]const u8{
"zig",
"build",
"test",
"-Dalgorithm=" ++ algorithm,
} ++ args, allocator);

child.stderr = std.fs.File.stderr();
child.stdout = std.fs.File.stdout();
} ++ args;

_ = try child.spawnAndWait();
var child = try std.process.spawn(io, .{ .argv = &argv });
_ = try child.wait(io);
}

const args = [_][]const u8{
"--summary",
"all",
"-freference-trace",
};
Loading
Loading