diff --git a/README.md b/README.md index 1f626df..9fc85e6 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ ### How to build **Require:** -- [Zig v0.13 or higher](https://ziglang.org/download), self-hosting (stage3) compiler. +- [Zig v0.14 or higher](https://ziglang.org/download), self-hosting (stage3) compiler. ### Test all diff --git a/build.zig b/build.zig index 15a8cc7..4c8b43f 100644 --- a/build.zig +++ b/build.zig @@ -8,35 +8,35 @@ pub fn build(b: *std.Build) void { // Sort algorithms if (std.mem.eql(u8, op, "sort/quicksort")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "quickSort.zig", .category = "sort", }); if (std.mem.eql(u8, op, "sort/bubblesort")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "bubbleSort.zig", .category = "sort", }); if (std.mem.eql(u8, op, "sort/radixsort")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "radixSort.zig", .category = "sort", }); if (std.mem.eql(u8, op, "sort/mergesort")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "mergeSort.zig", .category = "sort", }); if (std.mem.eql(u8, op, "sort/insertsort")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "insertionSort.zig", @@ -45,14 +45,14 @@ pub fn build(b: *std.Build) void { // Search algorithms if (std.mem.eql(u8, op, "search/bSearchTree")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "binarySearchTree.zig", .category = "search", }); if (std.mem.eql(u8, op, "search/rb")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "redBlackTrees.zig", @@ -61,28 +61,28 @@ pub fn build(b: *std.Build) void { // Data Structures algorithms if (std.mem.eql(u8, op, "ds/linkedlist")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "linkedList.zig", .category = "dataStructures", }); if (std.mem.eql(u8, op, "ds/doublylinkedlist")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "doublyLinkedList.zig", .category = "dataStructures", }); if (std.mem.eql(u8, op, "ds/lrucache")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "lruCache.zig", .category = "dataStructures", }); if (std.mem.eql(u8, op, "ds/stack")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "stack.zig", @@ -91,79 +91,79 @@ pub fn build(b: *std.Build) void { // Dynamic Programming algorithms if (std.mem.eql(u8, op, "dp/coinChange")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "coinChange.zig", - .category = "dynamicProgramming" + .category = "dynamicProgramming", }); if (std.mem.eql(u8, op, "dp/knapsack")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "knapsack.zig", - .category = "dynamicProgramming" + .category = "dynamicProgramming", }); if (std.mem.eql(u8, op, "dp/longestIncreasingSubsequence")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "longestIncreasingSubsequence.zig", - .category = "dynamicProgramming" + .category = "dynamicProgramming", }); if (std.mem.eql(u8, op, "dp/editDistance")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "editDistance.zig", - .category = "dynamicProgramming" + .category = "dynamicProgramming", }); // Math algorithms if (std.mem.eql(u8, op, "math/ceil")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "ceil.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/crt")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "chineseRemainderTheorem.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/fibonacci")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "fibonacciRecursion.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/primes")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "primes.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/euclidianGCDivisor")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "euclidianGreatestCommonDivisor.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/gcd")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "gcd.zig", .category = "math", }); if (std.mem.eql(u8, op, "math/factorial")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "factorial.zig", @@ -172,7 +172,7 @@ pub fn build(b: *std.Build) void { // Concurrent if (std.mem.eql(u8, op, "threads/threadpool")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "ThreadPool.zig", @@ -181,21 +181,21 @@ pub fn build(b: *std.Build) void { // Web if (std.mem.eql(u8, op, "web/httpClient")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "client.zig", .category = "web/http", }); if (std.mem.eql(u8, op, "web/httpServer")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "server.zig", .category = "web/http", }); if (std.mem.eql(u8, op, "web/tls1_3")) - build_algorithm(b, .{ + buildAlgorithm(b, .{ .optimize = optimize, .target = target, .name = "X25519+Kyber768Draft00.zig", @@ -203,7 +203,7 @@ pub fn build(b: *std.Build) void { }); } -fn build_algorithm(b: *std.Build, info: BInfo) void { +fn buildAlgorithm(b: *std.Build, info: BInfo) void { const src = std.mem.concat(b.allocator, u8, &.{ info.category, "/", @@ -217,7 +217,7 @@ fn build_algorithm(b: *std.Build, info: BInfo) void { .target = info.target, .optimize = info.optimize, .root_source_file = b.path(src), - .test_runner = runner, + .test_runner = .{ .path = runner, .mode = .simple }, }); const descr = b.fmt("Test the {s} algorithm", .{info.name}); diff --git a/build.zig.zon b/build.zig.zon index b79abca..f26abff 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -6,16 +6,29 @@ // // It is redundant to include "zig" in this name because it is already // within the Zig package namespace. - .name = "Algorithms-Zig", + .name = .Algorithms, // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. .version = "0.3.0", - // This field is optional. - // This is currently advisory only; Zig does not yet do anything - // with this value. - .minimum_zig_version = "0.13.0", + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0xe67bc23f91330780, // Changing this has security and trust implications. + + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.14.0", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. @@ -25,8 +38,8 @@ .dependencies = .{ // Custom test-runner: see tests output .runner = .{ - .url = "git+https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b/#f303e77231e14b405e4e800072e7deacac4a4881", - .hash = "12208ab39744c8c0909d53b8c9889aff0690930d20e98e4f650f9690310e9a860b14", + .url = "git+https://gist.github.com/karlseguin/c6bea5b35e4e8d26af6f81c22cb5d76b/#1f317ebc9cd09bc50fd5591d09c34255e15d1d85", + .hash = "N-V-__8AANwlAADW-4Yu_sYSZkL_6wcz13gOf9pkOLCjct-F", }, }, .paths = .{ diff --git a/concurrency/threads/ThreadPool.zig b/concurrency/threads/ThreadPool.zig index 1a68a33..afddd9b 100644 --- a/concurrency/threads/ThreadPool.zig +++ b/concurrency/threads/ThreadPool.zig @@ -421,7 +421,7 @@ pub const ThreadPool = struct { // Acquire barrier to ensure operations before the shutdown() are seen after the wait(). // Shutdown is rare so it's better to have an Acquire barrier here instead of on CAS failure + load which are common. if (state == SHUTDOWN) { - @fence(.acquire); + _ = self.state.load(.acquire); return; } diff --git a/dataStructures/doublyLinkedList.zig b/dataStructures/doublyLinkedList.zig index 276a73c..645e954 100644 --- a/dataStructures/doublyLinkedList.zig +++ b/dataStructures/doublyLinkedList.zig @@ -30,7 +30,7 @@ pub fn DoublyLinkedList(comptime T: type) type { // Runs in O(1) // Arguments: // key: T - the key to be inserted to the list - pub fn push_back(self: *Self, key: T) !void { + pub fn pushBack(self: *Self, key: T) !void { const nn = try self.allocator.create(node); nn.* = node{ .info = key, .next = null, .prev = self.tail }; @@ -49,7 +49,7 @@ pub fn DoublyLinkedList(comptime T: type) type { // Runs in O(1) // Arguments: // key: T - the key to be inserted to the list - pub fn push_front(self: *Self, key: T) !void { + pub fn pushFront(self: *Self, key: T) !void { const nn = try self.allocator.create(node); nn.* = node{ .info = key, .next = self.root, .prev = null }; @@ -66,7 +66,7 @@ pub fn DoublyLinkedList(comptime T: type) type { // Function that removes the front of the list // Runs in O(1) - pub fn pop_front(self: *Self) void { + pub fn popFront(self: *Self) void { if (self.root == null) { return; } @@ -80,7 +80,7 @@ pub fn DoublyLinkedList(comptime T: type) type { // Function that removes the back of the list // Runs in O(1) - pub fn pop_back(self: *Self) void { + pub fn popBack(self: *Self) void { if (self.root == null) { return; } @@ -193,9 +193,9 @@ test "Testing Doubly Linked List" { var list = DoublyLinkedList(i32){ .allocator = &allocator }; defer list.destroy(); - try list.push_front(10); - try list.push_front(20); - try list.push_front(30); + try list.pushFront(10); + try list.pushFront(20); + try list.pushFront(30); try testing.expect(list.search(10) == true); try testing.expect(list.search(30) == true); @@ -207,7 +207,7 @@ test "Testing Doubly Linked List" { defer list2.destroy(); inline for (0..4) |el| { - try list2.push_back(el); + try list2.pushBack(el); } inline for (0..4) |el| { @@ -216,10 +216,10 @@ test "Testing Doubly Linked List" { try testing.expect(list2.size == 4); - list2.pop_front(); + list2.popFront(); try testing.expect(list2.search(0) == false); - list2.pop_back(); + list2.popBack(); try testing.expect(list2.size == 2); try testing.expect(list2.search(3) == false); diff --git a/dynamicProgramming/coinChange.zig b/dynamicProgramming/coinChange.zig index 5a6cb14..c3b4912 100644 --- a/dynamicProgramming/coinChange.zig +++ b/dynamicProgramming/coinChange.zig @@ -6,7 +6,7 @@ const testing = std.testing; // Arguments: // arr: the array that contains the coins // N: the total amount of money that you have -pub fn min_cost(arr: []const u32, comptime N: i32) i32 { +pub fn minCost(arr: []const u32, comptime N: i32) i32 { const max_of: i32 = N + 1; var dp: [N + 1]i32 = undefined; @@ -28,11 +28,11 @@ pub fn min_cost(arr: []const u32, comptime N: i32) i32 { test "Testing min_cost algorithm" { const v = [3]u32{ 1, 2, 5 }; - try testing.expect(min_cost(&v, 11) == 3); + try testing.expect(minCost(&v, 11) == 3); const v2 = [1]u32{2}; - try testing.expect(min_cost(&v2, 3) == -1); + try testing.expect(minCost(&v2, 3) == -1); const v3 = [1]u32{1}; - try testing.expect(min_cost(&v3, 0) == 0); + try testing.expect(minCost(&v3, 0) == 0); } diff --git a/dynamicProgramming/longestIncreasingSubsequence.zig b/dynamicProgramming/longestIncreasingSubsequence.zig index bc78012..d7e4f32 100644 --- a/dynamicProgramming/longestIncreasingSubsequence.zig +++ b/dynamicProgramming/longestIncreasingSubsequence.zig @@ -4,7 +4,7 @@ const testing = std.testing; const ArrayList = std.ArrayList; // Function that returns the lower bound in O(logn) -pub fn lower_bound(arr: []const i32, key: i32) usize { +pub fn lowerBound(arr: []const i32, key: i32) usize { var lo: usize = 0; var hi: usize = arr.len; @@ -36,7 +36,7 @@ pub fn lis(arr: []const i32, allocator: anytype) usize { const n = arr.len; for (0..n) |i| { - const it = lower_bound(v.items, arr[i]); + const it = lowerBound(v.items, arr[i]); if (it == v.items.len) { _ = v.append(arr[i]) catch return 0; } else { diff --git a/math/chineseRemainderTheorem.zig b/math/chineseRemainderTheorem.zig index 31eb55f..a71c9eb 100644 --- a/math/chineseRemainderTheorem.zig +++ b/math/chineseRemainderTheorem.zig @@ -2,7 +2,7 @@ const std = @import("std"); const testing = std.testing; // Computes the inverse of a mod m. -pub fn InverseMod(comptime T: type, a: T, m: T) T { +pub fn inverseMod(comptime T: type, a: T, m: T) T { const y: T = @mod(a, m); var x: T = @as(T, 1); @@ -29,7 +29,7 @@ pub fn chineseRemainder(comptime T: type, a: []T, m: []T) T { i = 0; while (i < n) : (i += 1) { const Mi = @divTrunc(M, m[i]); - const z = InverseMod(T, Mi, m[i]); + const z = inverseMod(T, Mi, m[i]); x = @mod((x + a[i] * Mi * z), M); } } diff --git a/math/gcd.zig b/math/gcd.zig index 755bd0b..7ebf74c 100644 --- a/math/gcd.zig +++ b/math/gcd.zig @@ -2,8 +2,8 @@ const std = @import("std"); pub fn gcd(a: anytype, b: anytype) @TypeOf(a, b) { comptime switch (@typeInfo(@TypeOf(a, b))) { - .Int => |int| std.debug.assert(int.signedness == .unsigned), - .ComptimeInt => { + .int => |int| std.debug.assert(int.signedness == .unsigned), + .comptime_int => { std.debug.assert(a >= 0); std.debug.assert(b >= 0); }, diff --git a/sort/mergeSort.zig b/sort/mergeSort.zig index d9f9522..288a03a 100644 --- a/sort/mergeSort.zig +++ b/sort/mergeSort.zig @@ -4,17 +4,17 @@ const assert = std.debug.assert; pub fn sort(A: []i32, B: []i32) void { assert(A.len == B.len); - copy_array(A, 0, A.len, B); - split_merge(B, 0, A.len, A); + copyArray(A, 0, A.len, B); + splitMerge(B, 0, A.len, A); } -fn split_merge(B: []i32, begin: usize, end: usize, A: []i32) void { +fn splitMerge(B: []i32, begin: usize, end: usize, A: []i32) void { if (end - begin <= 1) { return; } const middle = (end + begin) / 2; - split_merge(A, begin, middle, B); - split_merge(A, middle, end, B); + splitMerge(A, begin, middle, B); + splitMerge(A, middle, end, B); merge(B, begin, middle, end, A); } @@ -34,7 +34,7 @@ fn merge(A: []i32, begin: usize, middle: usize, end: usize, B: []i32) void { } } -fn copy_array(A: []i32, begin: usize, end: usize, B: []i32) void { +fn copyArray(A: []i32, begin: usize, end: usize, B: []i32) void { var k = begin; while (k < end) : (k += 1) { B[k] = A[k]; diff --git a/sort/radixSort.zig b/sort/radixSort.zig index b092da7..b89cc59 100644 --- a/sort/radixSort.zig +++ b/sort/radixSort.zig @@ -13,7 +13,7 @@ pub fn max(A: []i32) i32 { return max_val; } -pub fn counting_sort(A: []i32, B: []i32, C: []usize, exp: i32, radix: usize) void { +pub fn countingSort(A: []i32, B: []i32, C: []usize, exp: i32, radix: usize) void { @memset(C, 0); for (A, 0..) |_, index| { @@ -47,7 +47,7 @@ pub fn sort(A: []i32, B: []i32, radix: usize) !void { var exp: i32 = 1; while (@divFloor(k, exp) > 0) : (exp *= 10) { - counting_sort(A, B, C, exp, radix); + countingSort(A, B, C, exp, radix); for (B, 0..) |value, index| { A[index] = value; } diff --git a/web/tls/X25519+Kyber768Draft00.zig b/web/tls/X25519+Kyber768Draft00.zig index 74a5d03..4b86843 100644 --- a/web/tls/X25519+Kyber768Draft00.zig +++ b/web/tls/X25519+Kyber768Draft00.zig @@ -29,7 +29,7 @@ test "HTTPS Client - X25519+Kyber768Draft00" { if (startW(u8, content, "http=")) try testing.expectEqualStrings("http=http/1.1", content); if (startW(u8, content, "uag=")) - try testing.expectEqualStrings("uag=zig/0.13.0 (std.http)", content); + try testing.expectEqualStrings("uag=zig/0.14.0 (std.http)", content); // zig master/nightly change per build (e.g.: zig/0.11.0-dev.2868+1a455b2dd (std.http)) if (startW(u8, content, "tls=")) try testing.expectEqualStrings("tls=TLSv1.3", content);