Zig utility library
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add (basic, not done) binary tree among other minor changes

IamPyu ec4e6a62 fb635189

+148 -4
+2
default.nix
··· 1 1 {zig, lib, stdenv}: stdenv.mkDerivation { 2 + pname = "mitochondria"; 3 + version = "latest"; 2 4 src = lib.cleanSource ./.; 3 5 4 6 nativeBuildInputs = [
+4
flake.nix
··· 34 34 }; 35 35 36 36 devShells.default = pkgs.mkShell { 37 + # inputsFrom = [ 38 + # (pkgs.callPackage ./default.nix {}) 39 + # ]; 40 + 37 41 buildInputs = with pkgs; [ 38 42 zig 39 43 zls
+3
src/Queue.zig src/queue.zig
··· 1 1 const std = @import("std"); 2 2 const Allocator = std.mem.Allocator; 3 3 4 + /// General-purpose queue 4 5 pub fn Queue(comptime T: type) type { 5 6 return struct { 6 7 const Self = @This(); ··· 84 85 }; 85 86 } 86 87 88 + /// Queue where allocated nodes are stored in an array 87 89 pub fn ArrayQueue(comptime T: type) type { 88 90 return struct { 89 91 const Self = @This(); ··· 125 127 test "queue" { 126 128 var queue = try ArrayQueue(i32).init(std.testing.allocator); 127 129 defer queue.deinit(); 130 + 128 131 try queue.enqueue(1); 129 132 try queue.enqueue(2); 130 133 _ = queue.dequeue();
src/SliceIterator.zig src/slice_iterator.zig
+135
src/binary_tree.zig
··· 1 + const std = @import("std"); 2 + const mem = std.mem; 3 + const Allocator = mem.Allocator; 4 + const ArrayList = std.ArrayList; 5 + 6 + const ARRAY_LIST_CAPACITY = 30; 7 + 8 + pub fn BinaryTree(comptime T: type) type { 9 + return struct { 10 + const Self = @This(); 11 + 12 + pub const Node = struct { 13 + data: T, 14 + left: ?*Node = null, 15 + right: ?*Node = null, 16 + 17 + pub fn init(x: T) Node { 18 + return Node{ .data = x }; 19 + } 20 + }; 21 + 22 + pub const Iter = struct { 23 + node: ?*Node, 24 + parent: ?*Node = null, 25 + 26 + pub fn init(root: ?*Node) Iter { 27 + return Iter{ .node = root }; 28 + } 29 + 30 + pub fn next(self: *Iter) ?*Node { 31 + if (self.node) |n| { 32 + if (self.parent) |p| { 33 + self.node = p.right; 34 + self.parent = null; 35 + return p.left; 36 + } else { 37 + self.parent = n; 38 + self.node = n.left; 39 + return n; 40 + } 41 + } else { 42 + return null; 43 + } 44 + } 45 + }; 46 + 47 + root: ?*Node, 48 + 49 + pub fn init() Self { 50 + return Self{ .root = null }; 51 + } 52 + 53 + pub fn insert(self: *Self, node: *Node) *Node { 54 + return self.insertAt(&self.root, node); 55 + } 56 + 57 + pub fn insertAt(self: *Self, ptr: *?*Node, node: *Node) *Node { 58 + if (ptr.*) |p| { 59 + // TODO: fix segfault when checking for null pointer 60 + // TODO: i found the same issue in our queue 61 + if (p.left) |_| { 62 + p.right = self.insertAt(&p.right, node); 63 + } else { 64 + p.left = self.insertAt(&p.left, node); 65 + } 66 + 67 + return ptr.*.?; 68 + } else { 69 + ptr.* = node; 70 + return ptr.*.?; 71 + } 72 + } 73 + 74 + // TODO: remove, find, search 75 + 76 + pub fn iter(self: *const Self) Iter { 77 + return Iter.init(self.root); 78 + } 79 + }; 80 + } 81 + 82 + pub fn ArrayBinaryTree(comptime T: type) type { 83 + return struct { 84 + const Self = @This(); 85 + const BT = BinaryTree(T); 86 + const NodeList = ArrayList(BT.Node); 87 + 88 + allocator: Allocator, 89 + binary_tree: BT, 90 + nodes: NodeList, 91 + 92 + pub fn init(allocator: Allocator) !Self { 93 + return Self{ 94 + .allocator = allocator, 95 + .binary_tree = BT.init(), 96 + .nodes = try NodeList.initCapacity(allocator, ARRAY_LIST_CAPACITY), 97 + }; 98 + } 99 + 100 + pub fn deinit(self: *Self) void { 101 + self.nodes.deinit(self.allocator); 102 + } 103 + 104 + pub fn insert(self: *Self, value: T) !*BT.Node { 105 + try self.nodes.append(self.allocator, BT.Node.init(value)); 106 + const node = &self.nodes.items[self.nodes.items.len - 1]; 107 + return self.binary_tree.insert(node); 108 + } 109 + }; 110 + } 111 + 112 + fn traverse(node: anytype) void { 113 + if (node == null) return; 114 + std.debug.print("{d} ", .{node.?.data}); 115 + traverse(node.?.left); 116 + traverse(node.?.right); 117 + } 118 + 119 + test "binary_tree" { 120 + const T = ArrayBinaryTree(u32); 121 + var abt = try T.init(std.testing.allocator); 122 + defer abt.deinit(); 123 + 124 + // FIXME: segfaults past 31 when checking if p.left == null 125 + for (0..15) |i| { 126 + _ = try abt.insert(@intCast(i + 1)); 127 + } 128 + 129 + var iter = abt.binary_tree.iter(); 130 + 131 + while (iter.next()) |n| { 132 + std.debug.print("{} ", .{n.data}); 133 + } 134 + std.debug.print("\n", .{}); 135 + }
+4 -4
src/root.zig
··· 13 13 pub const path = @import("./path.zig"); 14 14 pub const mem = @import("./mem.zig"); 15 15 16 - const slice_iterator = @import("./SliceIterator.zig"); 16 + const slice_iterator = @import("./slice_iterator.zig"); 17 17 pub const SliceIterator = slice_iterator.SliceIterator; 18 18 19 - const queue = @import("./Queue.zig"); 20 - pub const Queue = queue.Queue; 21 - pub const ArrayQueue = queue.ArrayQueue; 19 + pub const queue = @import("./queue.zig"); 20 + pub const binary_tree = @import("./binary_tree.zig"); 22 21 23 22 test { 24 23 _ = la; ··· 30 29 _ = mem; 31 30 _ = slice_iterator; 32 31 _ = queue; 32 + _ = binary_tree; 33 33 }