v0.0.5
How to Use
To use this package, add it as a dependency by running the following command.
zig fetch --save https://github.com/alexbishop/zig-rbtree/archive/refs/tags/v0.0.5.zip
This will update your build.zig.zon file with the correct url and hash for the package.
You can then import this package as a module by adding the following to the main function of you build.zig file
const rbtree = b.dependency("rbtree", .{
.target = target,
.optimize = optimize,
});
const rbtree_module = rbtree.module("rbtree");For each dependency, you may then ads this module as follows:
dep.root_module.addImport("rbtree", rbtree_module);Example
Add the module to the appropriate libraries and executables in build.zig. For example:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// get the module for red-black trees
const rbtree = b.dependency("rbtree", .{
.target = target,
.optimize = optimize,
});
const rbtree_module = rbtree.module("rbtree");
const lib = b.addStaticLibrary(.{
.name = "example-lib",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// add the module
lib.root_module.addImport("rbtree", rbtree_module);
b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "example-exe",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// add the module
exe.root_module.addImport("rbtree", rbtree_module);
b.installArtifact(exe);
}You can now import it as rbtree: For example, the contents of main.zig could be
const std = @import("std");
const rbtreelib = @import("rbtree");
pub const DefaultRBTree = rbtreelib.DefaultRBTree;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const Tree = DefaultRBTree(i32, f32);
var tree = Tree.init(allocator, void{});
defer tree.deinit();
// insert some stuff into the tree
var index: i32 = -19;
while (index < 20) : (index += 1) {
const value = std.math.pow(
f32,
0.5,
@floatFromInt(index),
);
try tree.put(index, value);
}
// print the contents of the tree
{
std.debug.print("First print\n", .{});
var current: ?*Tree.Node = tree.findMin();
while (current) |c| : (current = c.next()) {
std.debug.print("Node {} -> {}\n", .{ c.key, c.value });
}
}
// remove some entries from the tree
_ = tree.remove(7);
_ = tree.remove(3);
_ = tree.remove(5);
// print it again
{
std.debug.print("\nSecond print\n", .{});
var current: ?*Tree.Node = tree.findMin();
while (current) |c| : (current = c.next()) {
std.debug.print("Node {} -> {}\n", .{ c.key, c.value });
}
}
}