add sorted queue type, make it the default

This commit is contained in:
Mahmoud Hashemi 2013-03-22 19:45:30 -07:00
parent a238ce9e3c
commit 5aeec30dba
1 changed files with 21 additions and 6 deletions

View File

@ -12,9 +12,9 @@ except ImportError:
try:
from listutils import BList
list_type = BList # see BarrelList docstring for notes
# see BarrelList docstring for notes
except ImportError:
list_type = list
BList = list
__all__ = ['PriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue']
@ -27,10 +27,10 @@ __all__ = ['PriorityQueue', 'HeapPriorityQueue', 'SortedPriorityQueue']
class BasePriorityQueue(object):
# negating priority means larger numbers = higher priority
_default_priority_key = staticmethod(lambda p: -int(p or 0))
_list_type = list
_backend_type = list
def __init__(self, **kw):
self._pq = self._list_type()
self._pq = self._backend_type()
self._entry_map = {}
self._counter = itertools.count()
self._get_priority = kw.pop('priority_key', self._default_priority_key)
@ -102,10 +102,18 @@ class HeapPriorityQueue(BasePriorityQueue):
class SortedPriorityQueue(BasePriorityQueue):
pass
_backend_type = BList
@staticmethod
def _pop_entry(backend):
return backend.pop(0)
@staticmethod
def _push_entry(backend, entry):
insort(backend, entry)
PriorityQueue = HeapPriorityQueue
PriorityQueue = SortedPriorityQueue
# tests
@ -119,6 +127,13 @@ def main():
assert len(pq) == 1
assert func == pq.pop()
assert len(pq) == 0
try:
pq.pop()
except IndexError:
pass
else:
assert False, 'priority queue should be empty'
import pdb;pdb.set_trace()
if __name__ == '__main__':
try: