Zig utility library
1
fork

Configure Feed

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

Remove `BinaryTree`

IamPyu 946e037f 335f84ba

-142
-137
src/data/BinaryTree.zig
··· 1 - const std = @import("std"); 2 - const mem = std.mem; 3 - const Allocator = mem.Allocator; 4 - const ArrayList = std.ArrayList; 5 - 6 - pub fn BinaryTree(comptime T: type) type { 7 - return struct { 8 - const Self = @This(); 9 - 10 - pub const Node = struct { 11 - data: T, 12 - left: ?*Node = null, 13 - right: ?*Node = null, 14 - 15 - pub fn init(x: T) Node { 16 - return Node{ .data = x }; 17 - } 18 - 19 - pub fn initAlloc(gpa: Allocator, x: T) !*Node { 20 - var node = try gpa.create(Node); 21 - node.data = x; 22 - return node; 23 - } 24 - }; 25 - 26 - pub const Iter = struct { 27 - node: ?*Node, 28 - parent: ?*Node = null, 29 - 30 - pub fn init(root: ?*Node) Iter { 31 - return Iter{ .node = root }; 32 - } 33 - 34 - pub fn next(self: *Iter) ?*Node { 35 - if (self.node) |n| { 36 - if (self.parent) |p| { 37 - self.node = p.right; 38 - self.parent = null; 39 - return p.left; 40 - } else { 41 - self.parent = n; 42 - self.node = n.left; 43 - return n; 44 - } 45 - } else { 46 - return null; 47 - } 48 - } 49 - }; 50 - 51 - root: ?*Node, 52 - 53 - pub fn init() Self { 54 - return Self{ .root = null }; 55 - } 56 - 57 - pub fn insert(self: *Self, node: *Node) *Node { 58 - return self.insertAt(&self.root, node); 59 - } 60 - 61 - pub fn insertAt(self: *Self, ptr: *?*Node, node: *Node) *Node { 62 - if (ptr.*) |p| { 63 - 64 - // TODO: this only segfaults on Debug and ReleaseSafe builds, even with runtime safety disabled 65 - @setRuntimeSafety(false); 66 - const b = p.left != null; 67 - if (b) { 68 - p.right = self.insertAt(&p.right, node); 69 - } else { 70 - p.left = self.insertAt(&p.left, node); 71 - } 72 - 73 - return p; 74 - } else { 75 - ptr.* = node; 76 - return node; 77 - } 78 - } 79 - 80 - // TODO: remove, find, search 81 - 82 - pub fn iter(self: *const Self) Iter { 83 - return Iter.init(self.root); 84 - } 85 - }; 86 - } 87 - 88 - pub fn ArrayBinaryTree(comptime T: type) type { 89 - return struct { 90 - const Self = @This(); 91 - const BT = BinaryTree(T); 92 - const NodeList = ArrayList(*BT.Node); 93 - 94 - allocator: Allocator, 95 - binary_tree: BT, 96 - nodes: NodeList, 97 - 98 - pub fn init(gpa: Allocator) !Self { 99 - return Self{ 100 - .allocator = gpa, 101 - .binary_tree = BT.init(), 102 - .nodes = try NodeList.initCapacity(gpa, 10), 103 - }; 104 - } 105 - 106 - pub fn deinit(self: *Self) void { 107 - for (self.nodes.items) |node| { 108 - self.allocator.destroy(node); 109 - } 110 - self.nodes.deinit(self.allocator); 111 - } 112 - 113 - pub fn insert(self: *Self, value: T) !*BT.Node { 114 - const node = try BT.Node.initAlloc(self.allocator, value); 115 - try self.nodes.append(self.allocator, node); 116 - // const node = self.nodes.items[self.nodes.items.len - 1]; 117 - return self.binary_tree.insert(node); 118 - } 119 - }; 120 - } 121 - 122 - test "binary_tree" { 123 - const T = ArrayBinaryTree(u32); 124 - var abt = try T.init(std.testing.allocator); 125 - defer abt.deinit(); 126 - 127 - for (0..500) |i| { 128 - _ = try abt.insert(@intCast(i + 1)); 129 - } 130 - 131 - var iter = abt.binary_tree.iter(); 132 - 133 - while (iter.next()) |n| { 134 - std.debug.print("{} ", .{n.data}); 135 - } 136 - std.debug.print("\n", .{}); 137 - }
-5
src/data/root.zig
··· 7 7 pub const Queue = queue.Queue; 8 8 pub const ArrayQueue = queue.ArrayQueue; 9 9 10 - const binary_tree = @import("./BinaryTree.zig"); 11 - 12 - // TODO: rope data structure based on `BinaryTree` 13 - 14 10 const ring_buffer = @import("./RingBuffer.zig"); 15 11 pub const RingBuffer = ring_buffer.RingBuffer; 16 12 17 13 test { 18 14 _ = queue; 19 15 _ = sparse_set; 20 - // _ = binary_tree; // failing 21 16 _ = ring_buffer; 22 17 }