Ozzypig

GraphNode

Feb 25th, 2018
133
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- GraphNode class
  2. -- ModuleScript inside Graph ModuleScript
  3. -- by Ozzypig (Twitter: @Ozzypig, ozzypig.com)
  4.  
  5. local GraphEdge = require(script.Parent:WaitForChild("GraphEdge"))
  6.  
  7. local GraphNode = {}
  8. GraphNode.__index = GraphNode
  9.  
  10. function GraphNode.new(value)
  11.     local self = setmetatable({
  12.         value = value or 1;
  13.         edges = {};
  14.     }, GraphNode)
  15.    
  16.     return self
  17. end
  18.  
  19. function GraphNode:eachEdge()
  20.     return pairs(self.edges)
  21. end
  22.  
  23. function GraphNode:addEdge(edge)
  24.     self.edges[edge] = edge.value
  25. end
  26.  
  27. function GraphNode:newEdgeTo(nodeTarget, ...)
  28.     local edge = GraphEdge.new(self, nodeTarget, ...)
  29.     self:addEdge(edge)
  30.     return edge
  31. end
  32.  
  33. return GraphNode
RAW Paste Data