Last active
April 11, 2021 01:13
-
-
Save creationix/3c650fe030e91c2edabb684a2e9ebf85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
fn AutoMap(comptime BLOCK_POWER: comptime_int) type { | |
return struct { | |
const BLOCK_SIZE = 2 << (BLOCK_POWER - 1); | |
const HASH_SIZE = 2 << (HASH_POWER - 1); | |
const BRANCH_POWER = std.math.log2(@sizeOf(Leaf) / @sizeOf(*Node)); | |
const BRANCH_FACTOR = 2 << (BRANCH_POWER - 1); | |
const Hash = u256; | |
const Block = [BLOCK_SIZE]u8; | |
const Leaf = struct { hash: Hash, block: Block }; | |
const Branch = [BRANCH_FACTOR]?*Node; | |
const Node = union(enum) { branch: Branch, leaf: Leaf }; | |
}; | |
} | |
test "Proper branch factor" { | |
// 4Kib block | |
const M4K = AutoMap(12); | |
std.debug.print("\n", .{}); | |
std.debug.print("@sizeOf(M4K.Hash) = {}\n", .{@sizeOf(M4K.Hash)}); | |
std.debug.print("@sizeOf(M4K.Block) = {}\n", .{@sizeOf(M4K.Block)}); | |
std.debug.print("@sizeOf(M4K.Leaf) = {}\n", .{@sizeOf(M4K.Leaf)}); | |
std.debug.print("@sizeOf(?*M4K.Node) = {}\n", .{@sizeOf(?*M4K.Node)}); | |
std.debug.print("M4K.BRANCH_POWER = {}\n", .{M4K.BRANCH_POWER}); | |
std.debug.print("M4K.BRANCH_FACTOR = {}\n", .{M4K.BRANCH_FACTOR}); | |
// 32Kib block | |
const M32K = AutoMap(15); | |
std.debug.print("\n", .{}); | |
std.debug.print("@sizeOf(M32K.Hash) = {}\n", .{@sizeOf(M32K.Hash)}); | |
std.debug.print("@sizeOf(M32K.Block) = {}\n", .{@sizeOf(M32K.Block)}); | |
std.debug.print("@sizeOf(M32K.Leaf) = {}\n", .{@sizeOf(M32K.Leaf)}); | |
std.debug.print("@sizeOf(?*M32K.Node) = {}\n", .{@sizeOf(?*M32K.Node)}); | |
std.debug.print("M32K.BRANCH_POWER = {}\n", .{M32K.BRANCH_POWER}); | |
std.debug.print("M32K.BRANCH_FACTOR = {}\n", .{M32K.BRANCH_FACTOR}); | |
} | |
// pub fn main() anyerror!void { | |
// std.log.info("All your codebase are belong to us.", .{}); | |
// } |
Author
creationix
commented
Apr 11, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment