From 934db013a2da00903f0738cdd326e117df98d135 Mon Sep 17 00:00:00 2001 From: Tomasz Polgrabia Date: Sat, 1 Feb 2025 01:41:26 +0100 Subject: [PATCH] Preparing changes for btree add (defs + add stub). --- current/algorithms/numbers-zig/numbers.zig | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/current/algorithms/numbers-zig/numbers.zig b/current/algorithms/numbers-zig/numbers.zig index 91f382d..bdbf8da 100644 --- a/current/algorithms/numbers-zig/numbers.zig +++ b/current/algorithms/numbers-zig/numbers.zig @@ -132,6 +132,38 @@ pub fn bipartition(arr: []u64, t1: u64, t2: u64, v: u64) u64 { return x2; } -pub fn main() !void { - print("Hello World {}!!!\n", .{1}); +const BTree = struct { + n: u64, + values: []u64, + children: []?BTree, +}; + +pub fn BTree_Add(q: *BTree, v: u64) *BTree { + if (q.n <= q.values.len) { + const idx = bipartition(q.values, 0, q.n, v); + std.log.warn("Idx {}", idx); + const i: u64 = q.n; + while (i >= idx) { + q.values[idx + 1] = q.values[idx]; + // TODO add here moving children pointers + idx -= 1; + } + q.n += 1; + q.values[idx] = v; + // TODO insert here element + } + return q; +} + +pub fn main() !void { + const numbers = [_]u64{ 1, 2, 3 }; + const numbers_sl: []const u64 = &numbers; + const children = [_]?BTree{ null, null, null, null }; + const q = BTree{ + .n = 0, + .values = numbers_sl, + .children = &children, + }; + BTree_Add(q, 4); + print("Hello World {}!!!\n", .{q.values}); }