-
-
Save hasanyasin/48ad92a96a09c59652970c020d13dad4 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"); | |
const log = std.debug.warn; | |
pub const Node = struct { | |
isEmtpyFn:fn(*Node) bool = undefined, | |
appendFn:fn(*Node, *Node) void = undefined, | |
pub fn isEmtpy(n:*Node) bool { | |
return n.isEmtpyFn(n); | |
} | |
pub fn append(n:*Node, child:*Node) void { | |
return n.appendFn(n, child); | |
} | |
}; | |
pub const ElementNode = struct { | |
i:u64, | |
node:Node, | |
firstChild:?*Node = undefined, | |
fn isEmtpy(n:*Node) bool { | |
const element = @fieldParentPtr(ElementNode, "node", n).*; | |
if (element.firstChild) |_| { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
fn append(n:*Node, child:*Node) void { | |
var element = @fieldParentPtr(ElementNode, "node", n).*; | |
element.firstChild = child; | |
} | |
}; | |
pub fn NewElementNode(i:u64) *Node { | |
var d:ElementNode = ElementNode{ | |
.i = i, | |
.node = Node{ | |
.isEmtpyFn = ElementNode.isEmtpy, | |
.appendFn = ElementNode.append, | |
}, | |
}; | |
return &d.node; | |
} | |
pub fn main() void { | |
var node = NewElementNode(1); | |
var child = NewElementNode(2); | |
var ptr = @fieldParentPtr(ElementNode, "node", node); | |
var element = ptr.*; | |
log("Node : {}\n", @ptrToInt(node)); | |
log("Child: {}\n", @ptrToInt(child)); | |
log("Node Empty: {b}\n", node.isEmtpy()); | |
node.append(child); | |
log("Child Empty: {b}\n", child.isEmtpy()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment