Skip to content

Instantly share code, notes, and snippets.

@ajrouvoet
Created December 16, 2014 20:07
Show Gist options
  • Save ajrouvoet/e46d14ee4d6cbf61019c to your computer and use it in GitHub Desktop.
Save ajrouvoet/e46d14ee4d6cbf61019c to your computer and use it in GitHub Desktop.
case class Node {
val connections: ListBuffer[Connection] = new ListBuffer[Connection]
}
case class Connection(
from: Node,
val to: Node,
val cost: Int,
private val directed: Boolean = true
) {
from.connections += this
if(!directed) to.connections += new Connection(to, from, cost, true)
}
class Graph {
val nodes: ListBuffer[Node] = new ListBuffer[Node]
def getConnections(node: Node): Option[List[Connection]] = {
nodes.find(n => n == node) match {
case Some(node) =>
Some(node.connections.toList)
case None =>
None
}
}
}
object GraphTest1 extends App {
val nodeA = new Node()
val nodeB = new Node()
val con: Connection = new Connection(nodeA, nodeB, 5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment