Skip to content

Commit

Permalink
simplify BreadFirstMap too
Browse files Browse the repository at this point in the history
  • Loading branch information
anaseto committed Feb 20, 2021
1 parent 722b439 commit 9e7c750
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
44 changes: 13 additions & 31 deletions paths/breadthfirst.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ func (pr *PathRange) BreadthFirstMapAt(p gruid.Point) int {
if !p.In(pr.Rg) || pr.BfMap == nil {
return pr.BfUnreachable
}
node := pr.BfMap[pr.idx(p)]
if node.Idx != pr.BfIdx {
cost := pr.BfMap[pr.idx(p)]
if cost == 0 {
return pr.BfUnreachable
}
return node.Cost
}

type bfNode struct {
Idx int // map number (for caching)
Cost int // path cost from source
return cost - 1
}

// BreadthFirstMap efficiently computes a map of minimal distance costs from
Expand All @@ -37,17 +32,20 @@ func (pr *PathRange) BreadthFirstMap(nb Pather, sources []gruid.Point, maxCost i
max := pr.Rg.Size()
w, h := max.X, max.Y
if pr.BfMap == nil {
pr.BfMap = make([]bfNode, w*h)
pr.BfMap = make([]int, w*h)
pr.BfQueue = make([]Node, w*h)
} else {
for i := range pr.BfMap {
pr.BfMap[i] = 0
}
}
pr.BfIdx++
var qstart, qend int
pr.BfUnreachable = maxCost + 1
for _, p := range sources {
if !p.In(pr.Rg) {
continue
}
pr.BfMap[pr.idx(p)] = bfNode{Idx: pr.BfIdx, Cost: 0}
pr.BfMap[pr.idx(p)] = 1
pr.BfQueue[qend] = Node{P: p, Cost: 0}
qend++
}
Expand All @@ -58,34 +56,18 @@ func (pr *PathRange) BreadthFirstMap(nb Pather, sources []gruid.Point, maxCost i
continue
}
cidx := pr.idx(n.P)
cost := pr.BfMap[cidx].Cost
cost := pr.BfMap[cidx]
for _, q := range nb.Neighbors(n.P) {
if !q.In(pr.Rg) {
continue
}
nidx := pr.idx(q)
if pr.BfMap[nidx].Idx != pr.BfIdx {
c := 1 + cost
pr.BfMap[nidx] = bfNode{Idx: pr.BfIdx, Cost: c}
pr.BfQueue[qend] = Node{P: q, Cost: c}
if pr.BfMap[nidx] == 0 {
pr.BfMap[nidx] = cost + 1
pr.BfQueue[qend] = Node{P: q, Cost: cost}
qend++
}
}
}
pr.checkBfIdx()
return pr.BfQueue[0:qend]
}

func (pr *PathRange) checkBfIdx() {
if pr.BfIdx+1 > 0 {
return
}
for i, n := range pr.BfMap {
idx := 0
if n.Idx == pr.BfIdx {
idx = 1
}
pr.BfMap[i] = bfNode{Cost: n.Cost, Idx: idx}
}
pr.BfIdx = 1
}
7 changes: 3 additions & 4 deletions paths/pathrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ type pathRange struct {
AstarNodes *nodeMap
DijkstraNodes *nodeMap // dijkstra map
DijkstraIterNodes []Node
BfMap []bfNode // breadth first map
BfQueue []Node // map numbers for caching
CC []int // connected components
BfMap []int // breadth first map
BfQueue []Node // map numbers for caching
CC []int // connected components
CCStack []int
CCIterCache []gruid.Point
AstarQueue priorityQueue
DijkstraQueue priorityQueue
Rg gruid.Range
DijkstraUnreachable int
BfIdx int // map number (for caching)
BfUnreachable int // last maxcost + 1
BfEnd int // bf map last index
W int // path range width
Expand Down

0 comments on commit 9e7c750

Please sign in to comment.