I'm trying to do a simple Dijkstra pathfinder with genericly typed nodes. For this I have my pathfinder class and a nested data class to help. It look like this
class Dijkstra<T, U: Number >( val graph: Graph<T, U>,
val from: Node<T, U>,
val to: Node<T, U>) {
private var nodesDistances = mutableMapOf<Node<T, U>, DijkstraDistanceHelper<T, U>>()
init {
graph.getNodeList().forEach { nodesDistances[it] = DijkstraDistanceHelper<T, U>(it, null, null) }
val currentNode = from
while (currentNode != to) {
currentNode.getNeighborhood()?.forEach {
if (it.destination != currentNode) {
//it.value type is U and properly recognized as such
val currentDistance = it.value + (nodesDistances[currentNode]!!.distance ?: 0)
if (nodesDistances[it.destination]?.distance == null
|| nodesDistances[it.destination]!!.distance!! > currentDistance) {
//compilator error on the compare too, same reason I assume
nodesDistances[it.destination]!!.distance = currentDistance
nodesDistances[it.destination]!!.parentNode = currentNode
}
}
}
}
}
private data class DijkstraDistanceHelper<T, U: Number>( val node: Node<T, U>,
var distance: U?,
var parentNode: Node<T, U>?)
}
This is not sound algorithmically speaking but what bother me is that it doesn't compile : the compilator can't comprehend that Dijkstra's U generic type is the same as DijkstraDistanceHelper
Is it the wrong way? How can I enforce that Dijkstra's generic types (both T and U) are the same as DijkstraDistanceHelper?