From 966387ffcd172e5aae6cf6815ca41af6314960ec Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Tue, 15 Aug 2023 18:42:58 +0200 Subject: [PATCH] refactor: moved out the sorted_stack to utils --- solver/dijkstra.go | 22 ---------------------- solver/utils.go | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 solver/utils.go diff --git a/solver/dijkstra.go b/solver/dijkstra.go index 68fac5e..26dcaba 100644 --- a/solver/dijkstra.go +++ b/solver/dijkstra.go @@ -3,7 +3,6 @@ package solver import ( "maze-solver/maze" "maze-solver/utils" - "slices" "sort" ) @@ -13,8 +12,6 @@ type DijkstraSolver struct { stack sorted_stack } -type sorted_stack []*maze.Node - func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze { defer utils.Timer("Dijkstra algorithm", 2)() s.dist_from_start = make(map[*maze.Node]int, len(m.Nodes)) @@ -64,22 +61,3 @@ func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze { Solution: solution, } } - -func (s *sorted_stack) insert(node *maze.Node, dists *map[*maze.Node]int) { - var dummy *maze.Node - *s = append(*s, dummy) // extend the slice - - i, _ := slices.BinarySearchFunc(*s, node, func(e, t *maze.Node) int { - return (*dists)[t] - (*dists)[e] - }) - - copy((*s)[i+1:], (*s)[i:]) // make room - (*s)[i] = node -} - -func (s *sorted_stack) pop() *maze.Node { - last_i := len(*s) - 1 - ret := (*s)[last_i] - *s = (*s)[:last_i] - return ret -} diff --git a/solver/utils.go b/solver/utils.go new file mode 100644 index 0000000..c886d0f --- /dev/null +++ b/solver/utils.go @@ -0,0 +1,27 @@ +package solver + +import ( + "maze-solver/maze" + "slices" +) + +type sorted_stack []*maze.Node + +func (s *sorted_stack) insert(node *maze.Node, weights *map[*maze.Node]int) { + var dummy *maze.Node + *s = append(*s, dummy) // extend the slice + + i, _ := slices.BinarySearchFunc(*s, node, func(e, t *maze.Node) int { + return (*weights)[t] - (*weights)[e] + }) + + copy((*s)[i+1:], (*s)[i:]) // make room + (*s)[i] = node +} + +func (s *sorted_stack) pop() *maze.Node { + last_i := len(*s) - 1 + ret := (*s)[last_i] + *s = (*s)[:last_i] + return ret +}