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.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

Expand Down
64 changes: 32 additions & 32 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -181,29 +181,29 @@ 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",
.category = "web/tls",
});
}

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,
"/",
Expand All @@ -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});
Expand Down
27 changes: 20 additions & 7 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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 = .{
Expand Down
2 changes: 1 addition & 1 deletion concurrency/threads/ThreadPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 10 additions & 10 deletions dataStructures/doublyLinkedList.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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 };

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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| {
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions dynamicProgramming/coinChange.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Loading