forked from doctaphred/phrecipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaps.py
More file actions
30 lines (21 loc) · 675 Bytes
/
heaps.py
File metadata and controls
30 lines (21 loc) · 675 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 Heap:
"""Simple wrapper around heapq functions.
Why the standard library doesn't include a class like this is beyond me...
"""
def __init__(self, items=None):
if items is None:
self._items = []
else:
heapq.heapify(items)
self._items = items
def push(self, item):
return heapq.heappush(self._items, item)
def pop(self):
return heapq.heappop(self._items)
def pushpop(self, item):
return heapq.pushpop(self._items)
def replace(self, item):
return heapq.heapreplace(self._items, item)
def peek(self):
return self._items[0]