mirror of
https://github.com/gonum/gonum.git
synced 2025-10-13 02:43:59 +08:00
graph/path/...: update for int64 IDs
This commit is contained in:
@@ -44,8 +44,8 @@ func AStar(s, t graph.Node, g graph.Graph, h Heuristic) (path Shortest, expanded
|
|||||||
path = newShortestFrom(s, g.Nodes())
|
path = newShortestFrom(s, g.Nodes())
|
||||||
tid := t.ID()
|
tid := t.ID()
|
||||||
|
|
||||||
visited := make(set.Ints)
|
visited := make(set.Int64s)
|
||||||
open := &aStarQueue{indexOf: make(map[int]int)}
|
open := &aStarQueue{indexOf: make(map[int64]int)}
|
||||||
heap.Push(open, aStarNode{node: s, gscore: 0, fscore: h(s, t)})
|
heap.Push(open, aStarNode{node: s, gscore: 0, fscore: h(s, t)})
|
||||||
|
|
||||||
for open.Len() != 0 {
|
for open.Len() != 0 {
|
||||||
@@ -101,7 +101,7 @@ type aStarNode struct {
|
|||||||
|
|
||||||
// aStarQueue is an A* priority queue.
|
// aStarQueue is an A* priority queue.
|
||||||
type aStarQueue struct {
|
type aStarQueue struct {
|
||||||
indexOf map[int]int
|
indexOf map[int64]int
|
||||||
nodes []aStarNode
|
nodes []aStarNode
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ func (q *aStarQueue) Pop() interface{} {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *aStarQueue) update(id int, g, f float64) {
|
func (q *aStarQueue) update(id int64, g, f float64) {
|
||||||
i, ok := q.indexOf[id]
|
i, ok := q.indexOf[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@@ -142,7 +142,7 @@ func (q *aStarQueue) update(id int, g, f float64) {
|
|||||||
heap.Fix(q, i)
|
heap.Fix(q, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *aStarQueue) node(id int) (aStarNode, bool) {
|
func (q *aStarQueue) node(id int64) (aStarNode, bool) {
|
||||||
loc, ok := q.indexOf[id]
|
loc, ok := q.indexOf[id]
|
||||||
if ok {
|
if ok {
|
||||||
return q.nodes[loc], true
|
return q.nodes[loc], true
|
||||||
|
@@ -20,9 +20,9 @@ var aStarTests = []struct {
|
|||||||
name string
|
name string
|
||||||
g graph.Graph
|
g graph.Graph
|
||||||
|
|
||||||
s, t int
|
s, t int64
|
||||||
heuristic Heuristic
|
heuristic Heuristic
|
||||||
wantPath []int
|
wantPath []int64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "simple path",
|
name: "simple path",
|
||||||
@@ -36,7 +36,7 @@ var aStarTests = []struct {
|
|||||||
}(),
|
}(),
|
||||||
|
|
||||||
s: 1, t: 14,
|
s: 1, t: 14,
|
||||||
wantPath: []int{1, 2, 6, 10, 14},
|
wantPath: []int64{1, 2, 6, 10, 14},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "small open graph",
|
name: "small open graph",
|
||||||
@@ -143,7 +143,7 @@ func TestAStar(t *testing.T) {
|
|||||||
t.Errorf("unexpected cost for %q: got:%v want:%v", test.name, cost, want)
|
t.Errorf("unexpected cost for %q: got:%v want:%v", test.name, cost, want)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got = make([]int, 0, len(p))
|
var got = make([]int64, 0, len(p))
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -211,11 +211,11 @@ func TestExhaustiveAStar(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type locatedNode struct {
|
type locatedNode struct {
|
||||||
id int
|
id int64
|
||||||
x, y float64
|
x, y float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n locatedNode) ID() int { return n.id }
|
func (n locatedNode) ID() int64 { return n.id }
|
||||||
|
|
||||||
type weightedEdge struct {
|
type weightedEdge struct {
|
||||||
from, to graph.Node
|
from, to graph.Node
|
||||||
@@ -285,7 +285,7 @@ func TestAStarNullHeuristic(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
|
@@ -45,7 +45,7 @@ func TestBellmanFordFrom(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
|
@@ -72,10 +72,10 @@ func navigableSmallWorldUndirected(n, p, q int, r float64) graph.Undirected {
|
|||||||
|
|
||||||
func coordinatesForID(n graph.Node, c, r int) [2]int {
|
func coordinatesForID(n graph.Node, c, r int) [2]int {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
if id >= c*r {
|
if id >= int64(c*r) {
|
||||||
panic("out of range")
|
panic("out of range")
|
||||||
}
|
}
|
||||||
return [2]int{id / r, id % r}
|
return [2]int{int(id) / r, int(id) % r}
|
||||||
}
|
}
|
||||||
|
|
||||||
// manhattanBetween returns the Manhattan distance between a and b.
|
// manhattanBetween returns the Manhattan distance between a and b.
|
||||||
|
@@ -13,10 +13,10 @@ import (
|
|||||||
// prune for strict post-dominators, immediate dominators etc.
|
// prune for strict post-dominators, immediate dominators etc.
|
||||||
//
|
//
|
||||||
// A dominates B if and only if the only path through B travels through A.
|
// A dominates B if and only if the only path through B travels through A.
|
||||||
func Dominators(start graph.Node, g graph.Graph) map[int]set.Nodes {
|
func Dominators(start graph.Node, g graph.Graph) map[int64]set.Nodes {
|
||||||
allNodes := make(set.Nodes)
|
allNodes := make(set.Nodes)
|
||||||
nlist := g.Nodes()
|
nlist := g.Nodes()
|
||||||
dominators := make(map[int]set.Nodes, len(nlist))
|
dominators := make(map[int64]set.Nodes, len(nlist))
|
||||||
for _, node := range nlist {
|
for _, node := range nlist {
|
||||||
allNodes.Add(node)
|
allNodes.Add(node)
|
||||||
}
|
}
|
||||||
@@ -71,10 +71,10 @@ func Dominators(start graph.Node, g graph.Graph) map[int]set.Nodes {
|
|||||||
// prune for strict post-dominators, immediate post-dominators etc.
|
// prune for strict post-dominators, immediate post-dominators etc.
|
||||||
//
|
//
|
||||||
// A post-dominates B if and only if all paths from B travel through A.
|
// A post-dominates B if and only if all paths from B travel through A.
|
||||||
func PostDominators(end graph.Node, g graph.Graph) map[int]set.Nodes {
|
func PostDominators(end graph.Node, g graph.Graph) map[int64]set.Nodes {
|
||||||
allNodes := make(set.Nodes)
|
allNodes := make(set.Nodes)
|
||||||
nlist := g.Nodes()
|
nlist := g.Nodes()
|
||||||
dominators := make(map[int]set.Nodes, len(nlist))
|
dominators := make(map[int64]set.Nodes, len(nlist))
|
||||||
for _, node := range nlist {
|
for _, node := range nlist {
|
||||||
allNodes.Add(node)
|
allNodes.Add(node)
|
||||||
}
|
}
|
||||||
|
@@ -57,7 +57,7 @@ func TestDijkstraFrom(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -125,7 +125,7 @@ func TestDijkstraAllPaths(t *testing.T) {
|
|||||||
test.Name, unique, test.HasUniquePath)
|
test.Name, unique, test.HasUniquePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -154,9 +154,9 @@ func TestDijkstraAllPaths(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got [][]int
|
var got [][]int64
|
||||||
if len(paths) != 0 {
|
if len(paths) != 0 {
|
||||||
got = make([][]int, len(paths))
|
got = make([][]int64, len(paths))
|
||||||
}
|
}
|
||||||
for i, p := range paths {
|
for i, p := range paths {
|
||||||
for _, v := range p {
|
for _, v := range p {
|
||||||
|
@@ -18,7 +18,7 @@ package path
|
|||||||
// two sets when an edge is created between two vertices, and refuses to make an edge between two
|
// two sets when an edge is created between two vertices, and refuses to make an edge between two
|
||||||
// vertices if they're part of the same set.
|
// vertices if they're part of the same set.
|
||||||
type disjointSet struct {
|
type disjointSet struct {
|
||||||
master map[int]*disjointSetNode
|
master map[int64]*disjointSetNode
|
||||||
}
|
}
|
||||||
|
|
||||||
type disjointSetNode struct {
|
type disjointSetNode struct {
|
||||||
@@ -27,11 +27,11 @@ type disjointSetNode struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newDisjointSet() *disjointSet {
|
func newDisjointSet() *disjointSet {
|
||||||
return &disjointSet{master: make(map[int]*disjointSetNode)}
|
return &disjointSet{master: make(map[int64]*disjointSetNode)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the element isn't already somewhere in there, adds it to the master set and its own tiny set.
|
// If the element isn't already somewhere in there, adds it to the master set and its own tiny set.
|
||||||
func (ds *disjointSet) makeSet(e int) {
|
func (ds *disjointSet) makeSet(e int64) {
|
||||||
if _, ok := ds.master[e]; ok {
|
if _, ok := ds.master[e]; ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ func (ds *disjointSet) makeSet(e int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns the set the element belongs to, or nil if none.
|
// Returns the set the element belongs to, or nil if none.
|
||||||
func (ds *disjointSet) find(e int) *disjointSetNode {
|
func (ds *disjointSet) find(e int64) *disjointSetNode {
|
||||||
dsNode, ok := ds.master[e]
|
dsNode, ok := ds.master[e]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
|
@@ -35,7 +35,7 @@ type DStarLite struct {
|
|||||||
type WorldModel interface {
|
type WorldModel interface {
|
||||||
graph.DirectedBuilder
|
graph.DirectedBuilder
|
||||||
graph.Weighter
|
graph.Weighter
|
||||||
Node(id int) graph.Node
|
Node(id int64) graph.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDStarLite returns a new DStarLite planner for the path from s to t in g using the
|
// NewDStarLite returns a new DStarLite planner for the path from s to t in g using the
|
||||||
|
@@ -69,7 +69,7 @@ func TestDStarLiteNullHeuristic(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -101,7 +101,7 @@ var dynamicDStarLiteTests = []struct {
|
|||||||
|
|
||||||
want []graph.Node
|
want []graph.Node
|
||||||
weight float64
|
weight float64
|
||||||
wantedPaths map[int][]graph.Node
|
wantedPaths map[int64][]graph.Node
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
// This is the example shown in figures 6 and 7 of doi:10.1109/tro.2004.838026.
|
// This is the example shown in figures 6 and 7 of doi:10.1109/tro.2004.838026.
|
||||||
@@ -167,7 +167,7 @@ var dynamicDStarLiteTests = []struct {
|
|||||||
simple.Node(14),
|
simple.Node(14),
|
||||||
},
|
},
|
||||||
weight: 7.242640687119285,
|
weight: 7.242640687119285,
|
||||||
wantedPaths: map[int][]graph.Node{
|
wantedPaths: map[int64][]graph.Node{
|
||||||
12: {simple.Node(12), simple.Node(7), simple.Node(3), simple.Node(9), simple.Node(14)},
|
12: {simple.Node(12), simple.Node(7), simple.Node(3), simple.Node(9), simple.Node(14)},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -558,7 +558,7 @@ func TestDStarLiteDynamic(t *testing.T) {
|
|||||||
Location: test.s,
|
Location: test.s,
|
||||||
}
|
}
|
||||||
if remember {
|
if remember {
|
||||||
l.Known = make(map[int]bool)
|
l.Known = make(map[int64]bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Grid.AllVisible = test.all
|
l.Grid.AllVisible = test.all
|
||||||
|
@@ -30,9 +30,9 @@ func (d *dumper) dump(withpath bool) {
|
|||||||
if d == nil {
|
if d == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var pathStep map[int]int
|
var pathStep map[int64]int
|
||||||
if withpath {
|
if withpath {
|
||||||
pathStep = make(map[int]int)
|
pathStep = make(map[int64]int)
|
||||||
path, _ := d.dStarLite.Path()
|
path, _ := d.dStarLite.Path()
|
||||||
for i, n := range path {
|
for i, n := range path {
|
||||||
pathStep[n.ID()] = i
|
pathStep[n.ID()] = i
|
||||||
|
@@ -49,7 +49,7 @@ func TestFloydWarshall(t *testing.T) {
|
|||||||
test.Name, unique, test.HasUniquePath)
|
test.Name, unique, test.HasUniquePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -78,9 +78,9 @@ func TestFloydWarshall(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got [][]int
|
var got [][]int64
|
||||||
if len(paths) != 0 {
|
if len(paths) != 0 {
|
||||||
got = make([][]int, len(paths))
|
got = make([][]int64, len(paths))
|
||||||
}
|
}
|
||||||
for i, p := range paths {
|
for i, p := range paths {
|
||||||
for _, v := range p {
|
for _, v := range p {
|
||||||
|
@@ -108,14 +108,14 @@ func (g *Grid) Has(n graph.Node) bool {
|
|||||||
return g.has(n.ID())
|
return g.has(n.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Grid) has(id int) bool {
|
func (g *Grid) has(id int64) bool {
|
||||||
return id >= 0 && id < len(g.open) && (g.AllVisible || g.open[id])
|
return 0 <= id && id < int64(len(g.open)) && (g.AllVisible || g.open[id])
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasOpen returns whether n is an open node in the grid.
|
// HasOpen returns whether n is an open node in the grid.
|
||||||
func (g *Grid) HasOpen(n graph.Node) bool {
|
func (g *Grid) HasOpen(n graph.Node) bool {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
return id >= 0 && id < len(g.open) && g.open[id]
|
return 0 <= id && id < int64(len(g.open)) && g.open[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets the node at position (r, c) to the specified open state.
|
// Set sets the node at position (r, c) to the specified open state.
|
||||||
@@ -136,11 +136,11 @@ func (g *Grid) Dims() (r, c int) {
|
|||||||
|
|
||||||
// RowCol returns the row and column of the id. RowCol will panic if the
|
// RowCol returns the row and column of the id. RowCol will panic if the
|
||||||
// node id is outside the range of the grid.
|
// node id is outside the range of the grid.
|
||||||
func (g *Grid) RowCol(id int) (r, c int) {
|
func (g *Grid) RowCol(id int64) (r, c int) {
|
||||||
if id < 0 || id >= len(g.open) {
|
if id < 0 || int64(len(g.open)) <= id {
|
||||||
panic("grid: illegal node id")
|
panic("grid: illegal node id")
|
||||||
}
|
}
|
||||||
return id / g.c, id % g.c
|
return int(id) / g.c, int(id) % g.c
|
||||||
}
|
}
|
||||||
|
|
||||||
// XY returns the cartesian coordinates of n. If n is not a node
|
// XY returns the cartesian coordinates of n. If n is not a node
|
||||||
@@ -266,7 +266,7 @@ func (g *Grid) Render(path []graph.Node) ([]byte, error) {
|
|||||||
for i, n := range path {
|
for i, n := range path {
|
||||||
if !g.Has(n) || (i != 0 && !g.HasEdgeBetween(path[i-1], n)) {
|
if !g.Has(n) || (i != 0 && !g.HasEdgeBetween(path[i-1], n)) {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
if id >= 0 && id < len(g.open) {
|
if 0 <= id && id < int64(len(g.open)) {
|
||||||
r, c := g.RowCol(n.ID())
|
r, c := g.RowCol(n.ID())
|
||||||
b[r*(g.c+1)+c] = '!'
|
b[r*(g.c+1)+c] = '!'
|
||||||
}
|
}
|
||||||
|
@@ -19,9 +19,9 @@ var _ graph.Graph = (*Grid)(nil)
|
|||||||
|
|
||||||
func join(g ...string) string { return strings.Join(g, "\n") }
|
func join(g ...string) string { return strings.Join(g, "\n") }
|
||||||
|
|
||||||
type node int
|
type node int64
|
||||||
|
|
||||||
func (n node) ID() int { return int(n) }
|
func (n node) ID() int64 { return int64(n) }
|
||||||
|
|
||||||
func TestGrid(t *testing.T) {
|
func TestGrid(t *testing.T) {
|
||||||
g := NewGrid(4, 4, false)
|
g := NewGrid(4, 4, false)
|
||||||
@@ -205,7 +205,7 @@ func TestGrid(t *testing.T) {
|
|||||||
|
|
||||||
var coords = []struct {
|
var coords = []struct {
|
||||||
r, c int
|
r, c int
|
||||||
id int
|
id int64
|
||||||
}{
|
}{
|
||||||
{r: 0, c: 0, id: 0},
|
{r: 0, c: 0, id: 0},
|
||||||
{r: 0, c: 3, id: 3},
|
{r: 0, c: 3, id: 3},
|
||||||
|
@@ -29,7 +29,7 @@ type LimitedVisionGrid struct {
|
|||||||
|
|
||||||
// Known holds a store of known
|
// Known holds a store of known
|
||||||
// nodes, if not nil.
|
// nodes, if not nil.
|
||||||
Known map[int]bool
|
Known map[int64]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// MoveTo moves to the node n on the grid and returns a slice of newly seen and
|
// MoveTo moves to the node n on the grid and returns a slice of newly seen and
|
||||||
@@ -39,7 +39,7 @@ func (l *LimitedVisionGrid) MoveTo(n graph.Node) (new, old []graph.Edge) {
|
|||||||
row, column := l.RowCol(n.ID())
|
row, column := l.RowCol(n.ID())
|
||||||
x := float64(column)
|
x := float64(column)
|
||||||
y := float64(row)
|
y := float64(row)
|
||||||
seen := make(map[[2]int]bool)
|
seen := make(map[[2]int64]bool)
|
||||||
bound := int(l.VisionRadius + 0.5)
|
bound := int(l.VisionRadius + 0.5)
|
||||||
for r := row - bound; r <= row+bound; r++ {
|
for r := row - bound; r <= row+bound; r++ {
|
||||||
for c := column - bound; c <= column+bound; c++ {
|
for c := column - bound; c <= column+bound; c++ {
|
||||||
@@ -52,10 +52,10 @@ func (l *LimitedVisionGrid) MoveTo(n graph.Node) (new, old []graph.Edge) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, v := range l.allPossibleFrom(u) {
|
for _, v := range l.allPossibleFrom(u) {
|
||||||
if seen[[2]int{u.ID(), v.ID()}] {
|
if seen[[2]int64{u.ID(), v.ID()}] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[[2]int{u.ID(), v.ID()}] = true
|
seen[[2]int64{u.ID(), v.ID()}] = true
|
||||||
|
|
||||||
vx, vy := l.XY(v)
|
vx, vy := l.XY(v)
|
||||||
if !l.Known[v.ID()] && math.Hypot(x-vx, y-vy) > l.VisionRadius {
|
if !l.Known[v.ID()] && math.Hypot(x-vx, y-vy) > l.VisionRadius {
|
||||||
@@ -128,7 +128,7 @@ func (l *LimitedVisionGrid) allPossibleFrom(u graph.Node) []graph.Node {
|
|||||||
|
|
||||||
// RowCol returns the row and column of the id. RowCol will panic if the
|
// RowCol returns the row and column of the id. RowCol will panic if the
|
||||||
// node id is outside the range of the grid.
|
// node id is outside the range of the grid.
|
||||||
func (l *LimitedVisionGrid) RowCol(id int) (r, c int) {
|
func (l *LimitedVisionGrid) RowCol(id int64) (r, c int) {
|
||||||
return l.Grid.RowCol(id)
|
return l.Grid.RowCol(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,8 +161,8 @@ func (l *LimitedVisionGrid) Has(n graph.Node) bool {
|
|||||||
return l.has(n.ID())
|
return l.has(n.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LimitedVisionGrid) has(id int) bool {
|
func (l *LimitedVisionGrid) has(id int64) bool {
|
||||||
return id >= 0 && id < len(l.Grid.open)
|
return 0 <= id && id < int64(len(l.Grid.open))
|
||||||
}
|
}
|
||||||
|
|
||||||
// From returns nodes that are optimistically reachable from u.
|
// From returns nodes that are optimistically reachable from u.
|
||||||
@@ -268,7 +268,7 @@ func (l *LimitedVisionGrid) Render(path []graph.Node) ([]byte, error) {
|
|||||||
b := make([]byte, rows*(cols+1)-1)
|
b := make([]byte, rows*(cols+1)-1)
|
||||||
for r := 0; r < rows; r++ {
|
for r := 0; r < rows; r++ {
|
||||||
for c := 0; c < cols; c++ {
|
for c := 0; c < cols; c++ {
|
||||||
if !l.Known[r*cols+c] {
|
if !l.Known[int64(r*cols+c)] {
|
||||||
b[r*(cols+1)+c] = Unknown
|
b[r*(cols+1)+c] = Unknown
|
||||||
} else if l.Grid.open[r*cols+c] {
|
} else if l.Grid.open[r*cols+c] {
|
||||||
b[r*(cols+1)+c] = Open
|
b[r*(cols+1)+c] = Open
|
||||||
@@ -286,7 +286,7 @@ func (l *LimitedVisionGrid) Render(path []graph.Node) ([]byte, error) {
|
|||||||
for i, n := range path {
|
for i, n := range path {
|
||||||
if !l.Has(n) || (i != 0 && !l.HasEdgeBetween(path[i-1], n)) {
|
if !l.Has(n) || (i != 0 && !l.HasEdgeBetween(path[i-1], n)) {
|
||||||
id := n.ID()
|
id := n.ID()
|
||||||
if id >= 0 && id < len(l.Grid.open) {
|
if 0 <= id && id < int64(len(l.Grid.open)) {
|
||||||
r, c := l.RowCol(n.ID())
|
r, c := l.RowCol(n.ID())
|
||||||
b[r*(cols+1)+c] = '!'
|
b[r*(cols+1)+c] = '!'
|
||||||
}
|
}
|
||||||
|
@@ -1161,7 +1161,7 @@ func TestLimitedVisionGrid(t *testing.T) {
|
|||||||
Location: test.path[0],
|
Location: test.path[0],
|
||||||
}
|
}
|
||||||
if test.remember {
|
if test.remember {
|
||||||
l.Known = make(map[int]bool)
|
l.Known = make(map[int64]bool)
|
||||||
}
|
}
|
||||||
l.Grid.AllowDiagonal = test.diag
|
l.Grid.AllowDiagonal = test.diag
|
||||||
|
|
||||||
|
@@ -32,7 +32,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query simple.Edge
|
Query simple.Edge
|
||||||
Weight float64
|
Weight float64
|
||||||
WantPaths [][]int
|
WantPaths [][]int64
|
||||||
HasUniquePath bool
|
HasUniquePath bool
|
||||||
|
|
||||||
NoPathFor simple.Edge
|
NoPathFor simple.Edge
|
||||||
@@ -65,7 +65,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1},
|
{0, 1},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
@@ -81,7 +81,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(0)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(0)},
|
||||||
Weight: 0,
|
Weight: 0,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0},
|
{0},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
@@ -97,7 +97,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1},
|
{0, 1},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
@@ -115,7 +115,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
|
||||||
Weight: 2,
|
Weight: 2,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2},
|
{0, 1, 2},
|
||||||
{0, 2},
|
{0, 2},
|
||||||
},
|
},
|
||||||
@@ -134,7 +134,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
|
||||||
Weight: 2,
|
Weight: 2,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2},
|
{0, 1, 2},
|
||||||
{0, 2},
|
{0, 2},
|
||||||
},
|
},
|
||||||
@@ -167,7 +167,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 5},
|
{0, 1, 2, 3, 5},
|
||||||
{0, 2, 3, 5},
|
{0, 2, 3, 5},
|
||||||
{0, 5},
|
{0, 5},
|
||||||
@@ -201,7 +201,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 5},
|
{0, 1, 2, 3, 5},
|
||||||
{0, 2, 3, 5},
|
{0, 2, 3, 5},
|
||||||
{0, 5},
|
{0, 5},
|
||||||
@@ -236,7 +236,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 5},
|
{0, 1, 2, 3, 5},
|
||||||
{0, 2, 3, 5},
|
{0, 2, 3, 5},
|
||||||
{0, 6, 5},
|
{0, 6, 5},
|
||||||
@@ -271,7 +271,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 5},
|
{0, 1, 2, 3, 5},
|
||||||
{0, 2, 3, 5},
|
{0, 2, 3, 5},
|
||||||
{0, 6, 5},
|
{0, 6, 5},
|
||||||
@@ -297,7 +297,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
HasUniquePath: false,
|
HasUniquePath: false,
|
||||||
@@ -324,7 +324,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
HasUniquePath: false,
|
HasUniquePath: false,
|
||||||
@@ -353,7 +353,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
{0, 1, 5, 4},
|
{0, 1, 5, 4},
|
||||||
},
|
},
|
||||||
@@ -384,7 +384,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
HasUniquePath: false,
|
HasUniquePath: false,
|
||||||
@@ -417,7 +417,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
{0, 1, 5, 4},
|
{0, 1, 5, 4},
|
||||||
{0, 1, 5, 6, 4},
|
{0, 1, 5, 6, 4},
|
||||||
@@ -453,7 +453,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
{0, 5, 3, 4},
|
{0, 5, 3, 4},
|
||||||
{0, 6, 5, 3, 4},
|
{0, 6, 5, 3, 4},
|
||||||
@@ -489,7 +489,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
HasUniquePath: false,
|
HasUniquePath: false,
|
||||||
@@ -522,7 +522,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
},
|
},
|
||||||
HasUniquePath: false,
|
HasUniquePath: false,
|
||||||
@@ -567,7 +567,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
|
||||||
Weight: 4,
|
Weight: 4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1, 2, 3, 4},
|
{0, 1, 2, 3, 4},
|
||||||
{0, 1, 2, 6, 10, 14, 20, 4},
|
{0, 1, 2, 6, 10, 14, 20, 4},
|
||||||
},
|
},
|
||||||
@@ -587,7 +587,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
|
||||||
Weight: -1,
|
Weight: -1,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{0, 1},
|
{0, 1},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
@@ -621,7 +621,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node('z'), T: simple.Node('y')},
|
Query: simple.Edge{F: simple.Node('z'), T: simple.Node('y')},
|
||||||
Weight: -4,
|
Weight: -4,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{'z', 'x', 'y'},
|
{'z', 'x', 'y'},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
@@ -644,7 +644,7 @@ var ShortestPathTests = []struct {
|
|||||||
|
|
||||||
Query: simple.Edge{F: simple.Node('a'), T: simple.Node('y')},
|
Query: simple.Edge{F: simple.Node('a'), T: simple.Node('y')},
|
||||||
Weight: -6,
|
Weight: -6,
|
||||||
WantPaths: [][]int{
|
WantPaths: [][]int64{
|
||||||
{'a', 'b', 'c', 'y'},
|
{'a', 'b', 'c', 'y'},
|
||||||
},
|
},
|
||||||
HasUniquePath: true,
|
HasUniquePath: true,
|
||||||
|
@@ -30,11 +30,11 @@ func JohnsonAllPaths(g graph.Graph) (paths AllShortest, ok bool) {
|
|||||||
|
|
||||||
paths = newAllShortest(g.Nodes(), false)
|
paths = newAllShortest(g.Nodes(), false)
|
||||||
|
|
||||||
sign := -1
|
sign := int64(-1)
|
||||||
for {
|
for {
|
||||||
// Choose a random node ID until we find
|
// Choose a random node ID until we find
|
||||||
// one that is not in g.
|
// one that is not in g.
|
||||||
jg.q = sign * rand.Int()
|
jg.q = sign * rand.Int63()
|
||||||
if _, exists := paths.indexOf[jg.q]; !exists {
|
if _, exists := paths.indexOf[jg.q]; !exists {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ func JohnsonAllPaths(g graph.Graph) (paths AllShortest, ok bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type johnsonWeightAdjuster struct {
|
type johnsonWeightAdjuster struct {
|
||||||
q int
|
q int64
|
||||||
g graph.Graph
|
g graph.Graph
|
||||||
|
|
||||||
from func(graph.Node) []graph.Node
|
from func(graph.Node) []graph.Node
|
||||||
@@ -133,6 +133,6 @@ func (johnsonWeightAdjuster) HasEdgeBetween(_, _ graph.Node) bool {
|
|||||||
panic("path: unintended use of johnsonWeightAdjuster")
|
panic("path: unintended use of johnsonWeightAdjuster")
|
||||||
}
|
}
|
||||||
|
|
||||||
type johnsonGraphNode int
|
type johnsonGraphNode int64
|
||||||
|
|
||||||
func (n johnsonGraphNode) ID() int { return int(n) }
|
func (n johnsonGraphNode) ID() int64 { return int64(n) }
|
||||||
|
@@ -49,7 +49,7 @@ func TestJohnsonAllPaths(t *testing.T) {
|
|||||||
test.Name, unique, test.HasUniquePath)
|
test.Name, unique, test.HasUniquePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []int
|
var got []int64
|
||||||
for _, n := range p {
|
for _, n := range p {
|
||||||
got = append(got, n.ID())
|
got = append(got, n.ID())
|
||||||
}
|
}
|
||||||
@@ -78,9 +78,9 @@ func TestJohnsonAllPaths(t *testing.T) {
|
|||||||
test.Name, weight, test.Weight)
|
test.Name, weight, test.Weight)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got [][]int
|
var got [][]int64
|
||||||
if len(paths) != 0 {
|
if len(paths) != 0 {
|
||||||
got = make([][]int, len(paths))
|
got = make([][]int64, len(paths))
|
||||||
}
|
}
|
||||||
for i, p := range paths {
|
for i, p := range paths {
|
||||||
for _, v := range p {
|
for _, v := range p {
|
||||||
|
@@ -26,7 +26,7 @@ type Shortest struct {
|
|||||||
// the id-dense representation of the
|
// the id-dense representation of the
|
||||||
// graph and the potentially id-sparse
|
// graph and the potentially id-sparse
|
||||||
// nodes held in nodes.
|
// nodes held in nodes.
|
||||||
indexOf map[int]int
|
indexOf map[int64]int
|
||||||
|
|
||||||
// dist and next represent the shortest
|
// dist and next represent the shortest
|
||||||
// paths between nodes.
|
// paths between nodes.
|
||||||
@@ -45,7 +45,7 @@ type Shortest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newShortestFrom(u graph.Node, nodes []graph.Node) Shortest {
|
func newShortestFrom(u graph.Node, nodes []graph.Node) Shortest {
|
||||||
indexOf := make(map[int]int, len(nodes))
|
indexOf := make(map[int64]int, len(nodes))
|
||||||
uid := u.ID()
|
uid := u.ID()
|
||||||
for i, n := range nodes {
|
for i, n := range nodes {
|
||||||
indexOf[n.ID()] = i
|
indexOf[n.ID()] = i
|
||||||
@@ -115,7 +115,7 @@ type AllShortest struct {
|
|||||||
// the id-dense representation of the
|
// the id-dense representation of the
|
||||||
// graph and the potentially id-sparse
|
// graph and the potentially id-sparse
|
||||||
// nodes held in nodes.
|
// nodes held in nodes.
|
||||||
indexOf map[int]int
|
indexOf map[int64]int
|
||||||
|
|
||||||
// dist, next and forward represent
|
// dist, next and forward represent
|
||||||
// the shortest paths between nodes.
|
// the shortest paths between nodes.
|
||||||
@@ -147,7 +147,7 @@ type AllShortest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newAllShortest(nodes []graph.Node, forward bool) AllShortest {
|
func newAllShortest(nodes []graph.Node, forward bool) AllShortest {
|
||||||
indexOf := make(map[int]int, len(nodes))
|
indexOf := make(map[int64]int, len(nodes))
|
||||||
for i, n := range nodes {
|
for i, n := range nodes {
|
||||||
indexOf[n.ID()] = i
|
indexOf[n.ID()] = i
|
||||||
}
|
}
|
||||||
|
@@ -32,7 +32,7 @@ func Prim(dst graph.UndirectedBuilder, g UndirectedWeighter) float64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
q := &primQueue{
|
q := &primQueue{
|
||||||
indexOf: make(map[int]int, len(nodes)-1),
|
indexOf: make(map[int64]int, len(nodes)-1),
|
||||||
nodes: make([]simple.Edge, 0, len(nodes)-1),
|
nodes: make([]simple.Edge, 0, len(nodes)-1),
|
||||||
}
|
}
|
||||||
for _, u := range nodes[1:] {
|
for _, u := range nodes[1:] {
|
||||||
@@ -77,7 +77,7 @@ func Prim(dst graph.UndirectedBuilder, g UndirectedWeighter) float64 {
|
|||||||
// a node in the set of nodes already connected to the minimum
|
// a node in the set of nodes already connected to the minimum
|
||||||
// spanning forest.
|
// spanning forest.
|
||||||
type primQueue struct {
|
type primQueue struct {
|
||||||
indexOf map[int]int
|
indexOf map[int64]int
|
||||||
nodes []simple.Edge
|
nodes []simple.Edge
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user