-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpriority_queue.py
More file actions
30 lines (22 loc) · 794 Bytes
/
priority_queue.py
File metadata and controls
30 lines (22 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import heapq
class PriorityQueue:
class BoardPathPair:
def __init__(self, board, path, astar_value):
self.board = board
self.path = path
self.value = astar_value
def __lt__(self, other):
return self.value < other.value
def __init__(self, heuristic):
self.heap = []
self.heuristic = heuristic
def put(self, board_path_tup):
board, path = board_path_tup
heapq.heappush(self.heap, PriorityQueue.BoardPathPair(board, path, self.heuristic(board) + len(path)))
def get(self):
board_path = heapq.heappop(self.heap)
return board_path.board, board_path.path
def empty(self):
return len(self.heap) == 0
def __len__(self):
return len(self.heap)