Last active
December 24, 2015 20:29
-
-
Save seraphr/6858219 to your computer and use it in GitHub Desktop.
TypeMember使って実装したGraph
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
TypeMember使って実装したGraph | |
generics版 https://gist.github.com/seraphr/6858351 |
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
trait Graph { | |
graph => | |
type _Node <: Node | |
type _Edge <: Edge | |
private val mNodeEdge: (Seq[_Node], Seq[_Edge]) = init | |
def init: (Seq[_Node], Seq[_Edge]) | |
val (nodes, edges) = mNodeEdge | |
trait Node { | |
def edges: Seq[_Edge] = graph.edges.filter(e => e.node1 == this || e.node2 == this) | |
} | |
trait Edge { | |
val nodes: (_Node, _Node) | |
val node1 = nodes._1 | |
val node2 = nodes._2 | |
} | |
} |
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
trait DirectedGraph extends Graph { | |
type _Edge <: DirectedEdge | |
trait DirectedEdge extends super.Edge { | |
val from: _Node | |
val to: _Node | |
} | |
} |
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
trait WeightedDirectedGraph extends WeightedGraph with DirectedGraph { | |
type _Edge <: WeightedDirectedEdge | |
trait WeightedDirectedEdge extends super.WeightedEdge with super.DirectedEdge | |
} |
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
trait WeightedGraph extends Graph { | |
type _Edge <: WeightedEdge | |
trait WeightedEdge extends super.Edge { | |
val weight: Int | |
} | |
} |
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
object Main { | |
object WeightedDirectedGraph extends WeightedDirectedGraph { | |
type _Node = DefaultNode | |
type _Edge = DefaultEdge | |
override def init: (Seq[_Node], Seq[_Edge]) = { | |
val tNode1 = new DefaultNode | |
val tNode2 = new DefaultNode | |
val tNode3 = new DefaultNode | |
val tEdge1 = DefaultEdge(tNode1, tNode2, 10) | |
val tEdge2 = DefaultEdge(tNode2, tNode3, 12) | |
(Seq(tNode1, tNode2, tNode3), Seq(tEdge1, tEdge2)) | |
} | |
class DefaultNode extends super.Node | |
case class DefaultEdge(from: _Node, to: _Node, weight: Int) extends super.WeightedDirectedEdge { | |
override val nodes = (from, to) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment