I’m trying to write a solution to a problem I found online using Mojo. The problem is a graph theory related so I wanted to implement the data structure by hand to get more familiar with the language.
from memory import Pointer
from collections import List
struct NodeEdge:
var node1P: Pointer[Node]
var node2P: Pointer[Node]
var edgeWeight: UInt16
fn __init__(inout self, inout node1: Node, inout node2: Node, weight: UInt16) -> NoneType:
self.node1P = Pointer[Node].address_of(node1)
self.node2P = Pointer[Node].address_of(node2)
self.edgeWeight = weight
node1.addEdge(self)
node2.addEdge(self)
fn __copyinit__(inout self, node1: NodeEdge) -> NoneType:
self.node1P = node1.node1P
self.node2P = node1.node2P
self.edgeWeight = node1.edgeWeight
fn __moveinit__(inout self, owned node1: NodeEdge) -> NoneType:
self.node1P = node1.node1P
self.node2P = node1.node2P
self.edgeWeight = node1.edgeWeight
struct Node:
var nodeType: String
var nodeData: List[UInt16]
var nodeEdges: List[NodeEdge]
fn __init__(inout self) -> NoneType:
self.nodeType = ''
self.nodeData = List[UInt16]()
self.nodeEdges = List[NodeEdge]()
fn __init__(inout self, ntype: String, data: List[UInt16], edges: List[NodeEdge]) -> NoneType:
self.nodeType = ntype
self.nodeData = data
self.nodeEdges = edges
fn addEdge(inout self, edge: NodeEdge) -> NoneType:
self.nodeEdges.append(edge)
fn setNodeType(inout self, ntype: String) -> NoneType:
self.nodeType = ntype
fn setNodeData(inout self, data: List[UInt16]) -> NoneType:
self.nodeData = data
fn setNodeEdgeData(inout self, data: List[NodeEdge]) -> NoneType:
self.nodeEdges = data
fn printNode(inout self) -> String:
var res: String
res = self.nodeType + "n"
for i in range(self.nodeData.size):
res.__iadd__(self.nodeData[i])
res.__iadd__(" ")
res.__iadd__("n")
for i in range(self.nodeEdges.size):
res.__iadd__(self.nodeEdges[i].edgeWeight)
res.__iadd__(" ")
res.__iadd__("n")
return res
fn printFirstEdgeNode(inout self) -> String:
var res: String
var firstNodeEdge = self.nodeEdges[0]
var pSelf = Pointer[Node].address_of(self)
if (pSelf.__eq__(firstNodeEdge.node1P)):
#ERROR Occurrs here
var nodeToPrint = firstNodeEdge.node2P.load()
else:
#print other node
pass
return ''
fn main():
var node1 = Node()
var ndata1: List[UInt16]
var node2 = Node()
var ndata2: List[UInt16]
ndata1 = List[UInt16](17)
ndata2 = List[UInt16](12,2)
node1.setNodeType("Sum")
node2.setNodeType("Path")
node1.setNodeData(ndata1)
node2.setNodeData(ndata2)
createEdges(node1, node2)
print(node1.printFirstEdgeNode())
fn createEdges(inout node1: Node, inout node2: Node) -> NoneType:
var edge = NodeEdge(node1, node2, 5)
I want the function printFirstEdgeNode to be able to look at its list of edge nodes and select the first one and print that node using the printNode function. The problem I’m encountering is in trying to load the pointer from the list of edges. Since each edge has a link to the nodes its connecting, I figure if I was able to load the Node object from that pointer then call the print node function on that object I could get the result I want, but I’m thinking that since it’s a memory-only struct I’m not going to be able to do it like this. Is there a similar way to accomplish this?