graph/path/...: update for int64 IDs

This commit is contained in:
kortschak
2017-06-16 11:05:28 +09:30
committed by Dan Kortschak
parent 9d34456e6d
commit 15ecc07d40
20 changed files with 93 additions and 93 deletions

View File

@@ -44,8 +44,8 @@ func AStar(s, t graph.Node, g graph.Graph, h Heuristic) (path Shortest, expanded
path = newShortestFrom(s, g.Nodes())
tid := t.ID()
visited := make(set.Ints)
open := &aStarQueue{indexOf: make(map[int]int)}
visited := make(set.Int64s)
open := &aStarQueue{indexOf: make(map[int64]int)}
heap.Push(open, aStarNode{node: s, gscore: 0, fscore: h(s, t)})
for open.Len() != 0 {
@@ -101,7 +101,7 @@ type aStarNode struct {
// aStarQueue is an A* priority queue.
type aStarQueue struct {
indexOf map[int]int
indexOf map[int64]int
nodes []aStarNode
}
@@ -132,7 +132,7 @@ func (q *aStarQueue) Pop() interface{} {
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]
if !ok {
return
@@ -142,7 +142,7 @@ func (q *aStarQueue) update(id int, g, f float64) {
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]
if ok {
return q.nodes[loc], true

View File

@@ -20,9 +20,9 @@ var aStarTests = []struct {
name string
g graph.Graph
s, t int
s, t int64
heuristic Heuristic
wantPath []int
wantPath []int64
}{
{
name: "simple path",
@@ -36,7 +36,7 @@ var aStarTests = []struct {
}(),
s: 1, t: 14,
wantPath: []int{1, 2, 6, 10, 14},
wantPath: []int64{1, 2, 6, 10, 14},
},
{
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)
}
var got = make([]int, 0, len(p))
var got = make([]int64, 0, len(p))
for _, n := range p {
got = append(got, n.ID())
}
@@ -211,11 +211,11 @@ func TestExhaustiveAStar(t *testing.T) {
}
type locatedNode struct {
id int
id int64
x, y float64
}
func (n locatedNode) ID() int { return n.id }
func (n locatedNode) ID() int64 { return n.id }
type weightedEdge struct {
from, to graph.Node
@@ -285,7 +285,7 @@ func TestAStarNullHeuristic(t *testing.T) {
test.Name, weight, test.Weight)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}

View File

@@ -45,7 +45,7 @@ func TestBellmanFordFrom(t *testing.T) {
test.Name, weight, test.Weight)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}

View File

@@ -72,10 +72,10 @@ func navigableSmallWorldUndirected(n, p, q int, r float64) graph.Undirected {
func coordinatesForID(n graph.Node, c, r int) [2]int {
id := n.ID()
if id >= c*r {
if id >= int64(c*r) {
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.

View File

@@ -13,10 +13,10 @@ import (
// prune for strict post-dominators, immediate dominators etc.
//
// 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)
nlist := g.Nodes()
dominators := make(map[int]set.Nodes, len(nlist))
dominators := make(map[int64]set.Nodes, len(nlist))
for _, node := range nlist {
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.
//
// 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)
nlist := g.Nodes()
dominators := make(map[int]set.Nodes, len(nlist))
dominators := make(map[int64]set.Nodes, len(nlist))
for _, node := range nlist {
allNodes.Add(node)
}

View File

@@ -57,7 +57,7 @@ func TestDijkstraFrom(t *testing.T) {
test.Name, weight, test.Weight)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
@@ -125,7 +125,7 @@ func TestDijkstraAllPaths(t *testing.T) {
test.Name, unique, test.HasUniquePath)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
@@ -154,9 +154,9 @@ func TestDijkstraAllPaths(t *testing.T) {
test.Name, weight, test.Weight)
}
var got [][]int
var got [][]int64
if len(paths) != 0 {
got = make([][]int, len(paths))
got = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {

View File

@@ -18,7 +18,7 @@ package path
// 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.
type disjointSet struct {
master map[int]*disjointSetNode
master map[int64]*disjointSetNode
}
type disjointSetNode struct {
@@ -27,11 +27,11 @@ type disjointSetNode struct {
}
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.
func (ds *disjointSet) makeSet(e int) {
func (ds *disjointSet) makeSet(e int64) {
if _, ok := ds.master[e]; ok {
return
}
@@ -41,7 +41,7 @@ func (ds *disjointSet) makeSet(e int) {
}
// 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]
if !ok {
return nil

View File

@@ -35,7 +35,7 @@ type DStarLite struct {
type WorldModel interface {
graph.DirectedBuilder
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

View File

@@ -69,7 +69,7 @@ func TestDStarLiteNullHeuristic(t *testing.T) {
test.Name, weight, test.Weight)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
@@ -101,7 +101,7 @@ var dynamicDStarLiteTests = []struct {
want []graph.Node
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.
@@ -167,7 +167,7 @@ var dynamicDStarLiteTests = []struct {
simple.Node(14),
},
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)},
},
},
@@ -558,7 +558,7 @@ func TestDStarLiteDynamic(t *testing.T) {
Location: test.s,
}
if remember {
l.Known = make(map[int]bool)
l.Known = make(map[int64]bool)
}
l.Grid.AllVisible = test.all

View File

@@ -30,9 +30,9 @@ func (d *dumper) dump(withpath bool) {
if d == nil {
return
}
var pathStep map[int]int
var pathStep map[int64]int
if withpath {
pathStep = make(map[int]int)
pathStep = make(map[int64]int)
path, _ := d.dStarLite.Path()
for i, n := range path {
pathStep[n.ID()] = i

View File

@@ -49,7 +49,7 @@ func TestFloydWarshall(t *testing.T) {
test.Name, unique, test.HasUniquePath)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
@@ -78,9 +78,9 @@ func TestFloydWarshall(t *testing.T) {
test.Name, weight, test.Weight)
}
var got [][]int
var got [][]int64
if len(paths) != 0 {
got = make([][]int, len(paths))
got = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {

View File

@@ -108,14 +108,14 @@ func (g *Grid) Has(n graph.Node) bool {
return g.has(n.ID())
}
func (g *Grid) has(id int) bool {
return id >= 0 && id < len(g.open) && (g.AllVisible || g.open[id])
func (g *Grid) has(id int64) bool {
return 0 <= id && id < int64(len(g.open)) && (g.AllVisible || g.open[id])
}
// HasOpen returns whether n is an open node in the grid.
func (g *Grid) HasOpen(n graph.Node) bool {
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.
@@ -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
// node id is outside the range of the grid.
func (g *Grid) RowCol(id int) (r, c int) {
if id < 0 || id >= len(g.open) {
func (g *Grid) RowCol(id int64) (r, c int) {
if id < 0 || int64(len(g.open)) <= 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
@@ -266,7 +266,7 @@ func (g *Grid) Render(path []graph.Node) ([]byte, error) {
for i, n := range path {
if !g.Has(n) || (i != 0 && !g.HasEdgeBetween(path[i-1], n)) {
id := n.ID()
if id >= 0 && id < len(g.open) {
if 0 <= id && id < int64(len(g.open)) {
r, c := g.RowCol(n.ID())
b[r*(g.c+1)+c] = '!'
}

View File

@@ -19,9 +19,9 @@ var _ graph.Graph = (*Grid)(nil)
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) {
g := NewGrid(4, 4, false)
@@ -205,7 +205,7 @@ func TestGrid(t *testing.T) {
var coords = []struct {
r, c int
id int
id int64
}{
{r: 0, c: 0, id: 0},
{r: 0, c: 3, id: 3},

View File

@@ -29,7 +29,7 @@ type LimitedVisionGrid struct {
// Known holds a store of known
// 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
@@ -39,7 +39,7 @@ func (l *LimitedVisionGrid) MoveTo(n graph.Node) (new, old []graph.Edge) {
row, column := l.RowCol(n.ID())
x := float64(column)
y := float64(row)
seen := make(map[[2]int]bool)
seen := make(map[[2]int64]bool)
bound := int(l.VisionRadius + 0.5)
for r := row - bound; r <= row+bound; r++ {
for c := column - bound; c <= column+bound; c++ {
@@ -52,10 +52,10 @@ func (l *LimitedVisionGrid) MoveTo(n graph.Node) (new, old []graph.Edge) {
continue
}
for _, v := range l.allPossibleFrom(u) {
if seen[[2]int{u.ID(), v.ID()}] {
if seen[[2]int64{u.ID(), v.ID()}] {
continue
}
seen[[2]int{u.ID(), v.ID()}] = true
seen[[2]int64{u.ID(), v.ID()}] = true
vx, vy := l.XY(v)
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
// 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)
}
@@ -161,8 +161,8 @@ func (l *LimitedVisionGrid) Has(n graph.Node) bool {
return l.has(n.ID())
}
func (l *LimitedVisionGrid) has(id int) bool {
return id >= 0 && id < len(l.Grid.open)
func (l *LimitedVisionGrid) has(id int64) bool {
return 0 <= id && id < int64(len(l.Grid.open))
}
// 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)
for r := 0; r < rows; r++ {
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
} else if l.Grid.open[r*cols+c] {
b[r*(cols+1)+c] = Open
@@ -286,7 +286,7 @@ func (l *LimitedVisionGrid) Render(path []graph.Node) ([]byte, error) {
for i, n := range path {
if !l.Has(n) || (i != 0 && !l.HasEdgeBetween(path[i-1], n)) {
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())
b[r*(cols+1)+c] = '!'
}

View File

@@ -1161,7 +1161,7 @@ func TestLimitedVisionGrid(t *testing.T) {
Location: test.path[0],
}
if test.remember {
l.Known = make(map[int]bool)
l.Known = make(map[int64]bool)
}
l.Grid.AllowDiagonal = test.diag

View File

@@ -32,7 +32,7 @@ var ShortestPathTests = []struct {
Query simple.Edge
Weight float64
WantPaths [][]int
WantPaths [][]int64
HasUniquePath bool
NoPathFor simple.Edge
@@ -65,7 +65,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
Weight: 1,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1},
},
HasUniquePath: true,
@@ -81,7 +81,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(0)},
Weight: 0,
WantPaths: [][]int{
WantPaths: [][]int64{
{0},
},
HasUniquePath: true,
@@ -97,7 +97,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(1)},
Weight: 1,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1},
},
HasUniquePath: true,
@@ -115,7 +115,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
Weight: 2,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2},
{0, 2},
},
@@ -134,7 +134,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(2)},
Weight: 2,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2},
{0, 2},
},
@@ -167,7 +167,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 5},
{0, 2, 3, 5},
{0, 5},
@@ -201,7 +201,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 5},
{0, 2, 3, 5},
{0, 5},
@@ -236,7 +236,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 5},
{0, 2, 3, 5},
{0, 6, 5},
@@ -271,7 +271,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(5)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 5},
{0, 2, 3, 5},
{0, 6, 5},
@@ -297,7 +297,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
},
HasUniquePath: false,
@@ -324,7 +324,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
},
HasUniquePath: false,
@@ -353,7 +353,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
{0, 1, 5, 4},
},
@@ -384,7 +384,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
},
HasUniquePath: false,
@@ -417,7 +417,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
{0, 1, 5, 4},
{0, 1, 5, 6, 4},
@@ -453,7 +453,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
{0, 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)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
},
HasUniquePath: false,
@@ -522,7 +522,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 4},
},
HasUniquePath: false,
@@ -567,7 +567,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node(0), T: simple.Node(4)},
Weight: 4,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1, 2, 3, 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)},
Weight: -1,
WantPaths: [][]int{
WantPaths: [][]int64{
{0, 1},
},
HasUniquePath: true,
@@ -621,7 +621,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node('z'), T: simple.Node('y')},
Weight: -4,
WantPaths: [][]int{
WantPaths: [][]int64{
{'z', 'x', 'y'},
},
HasUniquePath: true,
@@ -644,7 +644,7 @@ var ShortestPathTests = []struct {
Query: simple.Edge{F: simple.Node('a'), T: simple.Node('y')},
Weight: -6,
WantPaths: [][]int{
WantPaths: [][]int64{
{'a', 'b', 'c', 'y'},
},
HasUniquePath: true,

View File

@@ -30,11 +30,11 @@ func JohnsonAllPaths(g graph.Graph) (paths AllShortest, ok bool) {
paths = newAllShortest(g.Nodes(), false)
sign := -1
sign := int64(-1)
for {
// Choose a random node ID until we find
// one that is not in g.
jg.q = sign * rand.Int()
jg.q = sign * rand.Int63()
if _, exists := paths.indexOf[jg.q]; !exists {
break
}
@@ -65,7 +65,7 @@ func JohnsonAllPaths(g graph.Graph) (paths AllShortest, ok bool) {
}
type johnsonWeightAdjuster struct {
q int
q int64
g graph.Graph
from func(graph.Node) []graph.Node
@@ -133,6 +133,6 @@ func (johnsonWeightAdjuster) HasEdgeBetween(_, _ graph.Node) bool {
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) }

View File

@@ -49,7 +49,7 @@ func TestJohnsonAllPaths(t *testing.T) {
test.Name, unique, test.HasUniquePath)
}
var got []int
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
@@ -78,9 +78,9 @@ func TestJohnsonAllPaths(t *testing.T) {
test.Name, weight, test.Weight)
}
var got [][]int
var got [][]int64
if len(paths) != 0 {
got = make([][]int, len(paths))
got = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {

View File

@@ -26,7 +26,7 @@ type Shortest struct {
// the id-dense representation of the
// graph and the potentially id-sparse
// nodes held in nodes.
indexOf map[int]int
indexOf map[int64]int
// dist and next represent the shortest
// paths between nodes.
@@ -45,7 +45,7 @@ type Shortest struct {
}
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()
for i, n := range nodes {
indexOf[n.ID()] = i
@@ -115,7 +115,7 @@ type AllShortest struct {
// the id-dense representation of the
// graph and the potentially id-sparse
// nodes held in nodes.
indexOf map[int]int
indexOf map[int64]int
// dist, next and forward represent
// the shortest paths between nodes.
@@ -147,7 +147,7 @@ type AllShortest struct {
}
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 {
indexOf[n.ID()] = i
}

View File

@@ -32,7 +32,7 @@ func Prim(dst graph.UndirectedBuilder, g UndirectedWeighter) float64 {
}
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),
}
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
// spanning forest.
type primQueue struct {
indexOf map[int]int
indexOf map[int64]int
nodes []simple.Edge
}