Hi and merry Christmas,
I was trying to port [petagraph](https://github.com/petgraph/petgraph) rust
library to nim and although I made some
[progress](https://gist.github.com/b3liever/de2d4e2267b13832f7d52f10ba3140fe) I
am struggling with some fundamental decisions.
for example, when adding a node in the graph, it returns the index:
var graph: Graph[string, float]
let nodeA = graph.addNode("a")
let nodeB = graph.addNode("b")
let nodeC = graph.addNode("c")
let nodeD = graph.addNode("d")
let nodeE = graph.addNode("e")
let nodeF = graph.addNode("f")
let nodeG = graph.addNode("g")
let nodeH = graph.addNode("h")
# Also returns the edge index when adding an edge, not sure if its useful
let edge1 = graph.addEdge(nodeA, nodeB, 1.0)
Run
These indices are `distinct` types like so:
type
IndexType = int
NodeIndex = distinct IndexType
EdgeIndex = distinct IndexType
Run
That allows to have array element access syntax for nodes and edges:
proc `[]`*[N, E](self: Graph[N, E], a: NodeIndex): N =
## Access the weight for node `a`.
self.nodes[int(a)].weight
proc `[]`*[N, E](self: Graph[N, E], e: EdgeIndex): E =
## Access the weight for edge `e`.
self.edges[int(e)].weight
# works like so:
echo graph[edge1] # 1.0
echo graph[nodeA] # "a"
Run
And this was the only benefit I think there is with `distinct` indices,
consider this code:
import graph, sequtils
let graph2 = fromEdges[int, int](mapLiterals(@[
(0, 1), (0, 2), (0, 3),
(1, 2), (1, 3),
(2, 3)], NodeIndex))
Run
becomes tiresome to write, since everything needs to converted to the correct
`IndexType` subtype. Also the internal code is littered with
`self.nodes[int(nix)]`. My question is does it worth it, or should I just use
int everywhere?