.. image:: https://travis-ci.org/Ofekmeister/depq.svg?branch=master
:target: https://travis-ci.org/Ofekmeister/depq
.. image:: https://coveralls.io/repos/Ofekmeister/depq/badge.svg?branch=master
:target: https://coveralls.io/r/Ofekmeister/depq?branch=master
==================================
depq - Double-ended priority queue
==================================
- Python implementation of a thread-safe and efficient
double-ended priority queue (DEPQ) in which items and their
priority values are stored in a deque object as tuples.
- This of course can also be used as a regular priority queue, or
simply a FIFO/LIFO queue.
- Priority queues have many uses such as scheduling, event driven
simulation, heuristic analysis, spam filtering, graph searching, etc.
Features & advantages of this implementation:
---------------------------------------------
- Completely thread-safe
- Serializable via pickling or JSON
- Priority values can be ints/floats, numpy types, strings, or
any other comparable type you choose!
- popfirst() and poplast() have O(1) performance instead of
running in logarithmic time like in a standard DEPQ or other
heap-derived structure
- Naturally fast also because deque object is implemented in C
- Items with equal priorities are sorted in the order they were
originally added
- Specific items can be deleted or their priorities changed
- Membership testing with 'in' operator occurs in O(1) as does
getting an item's frequency in DEPQ via count(item)
Implementation:
---------------
- Priorities are always in proper order, thus, a binary search
is performed to find the right index with which to insert new
items when specifying priority. Normally, this would result in
O(n log n) performance when adding items via insert(item, priority)
where self.high() > priority > self.low() because deque (as a
doubly linked list) random access is O(n).
Though, ACTUALLY that is not the case here as I've been able to
reduce that to O(n) by modifying the binary search to operate while
the internal deque is concurrently rotating.
Examples:
---------
>>> from textwrap import fill # For nice wrapped printing
>>> from depq import DEPQ
>>>
>>> # Defaults. If iterable is not None, extend(iterable) will be
>>> # called (example below). If maxlen is not None, abs(int(maxlen))
>>> # will become the length limit. If a maxlen is set and an item
>>> # is added with a priority > lowest prioritized item, it will be
>>> # added and the last item will be popped. After instantiation, the
>>> # maxlen can be retrieved with maxlen() and set with set_maxlen(length).
>>> depq = DEPQ(iterable=None, maxlen=None)
>>>
>>> # Add some characters with their ordinal
>>> # values as priority and keep count
>>> for c in 'AN_ERRONEOUS_STRING':
... count = list( # This is hacky and not important, skip next 4 lines :)
... x + 1 if '{} #{}'.format(c, x + 1) in depq
... else next(iter(())) if x != 0 else 0
... for x in range(len(depq) + 1)
... )[-1]
...
... depq.insert('{} #{}'.format(c, count + 1), ord(c)) # item, priority
...
>>> print(fill(str(depq), 77))
DEPQ([('_ #1', 95), ('_ #2', 95), ('U #1', 85), ('T #1', 84), ('S #1', 83),
('S #2', 83), ('R #1', 82), ('R #2', 82), ('R #3', 82), ('O #1', 79), ('O
#2', 79), ('N #1', 78), ('N #2', 78), ('N #3', 78), ('I #1', 73), ('G #1',
71), ('E #1', 69), ('E #2', 69), ('A #1', 65)])
>>>
>>> # As you can see items with equal priorities are sorted in the order
>>> # they were originally added. Also note DEPQ root (depq[0]) is highest
>>> # priority like a max heap.
>>>
>>> depq.first()
'_ #1'
>>> depq.last()
'A #1'
>>> depq.high()
95
>>> depq.low()
65
>>> depq[7] # Returns tuple(item, priority)
('R #2', 82)
>>>
>>> depq.poplast()
('A #1', 65)
>>> depq.last()
'E #2'
>>>
>>> depq.size() # Alias for len(DEPQ)
18
>>> depq.is_empty()
False
>>> depq.clear()
>>> depq.is_empty()
True
>>>
>>> # Extend any length iterable of iterables of length >= 2
>>> depq.extend([('bar', 1, 'arbitrary'), (None, 5), ('foo', 2, 'blah')])
>>> depq
DEPQ([(None, 5), ('foo', 2), ('bar', 1)])
>>>
>>> depq.clear()
>>>
>>> depq.addfirst('starter') # For an empty DEPQ, addfirst & addlast are
>>> # functionally identical; they add item to DEPQ
>>> depq # with given priority, or default 0
DEPQ([('starter', 0)])
>>>
>>> depq.addfirst('high', depq.high() + 1)
>>> depq.addlast('low', depq.low() - 1)
>>> depq
DEPQ([('high', 1), ('starter', 0), ('low', -1)])
>>>
>>> depq.addfirst('higher') # Default priority DEPQ.high()
>>> depq.addlast('lower') # Default priority DEPQ.low()
>>> depq
DEPQ([('higher', 1), ('high', 1), ('starter', 0), ('low', -1), ('lower', -1)])
>>>
>>> depq.addfirst('highest', 0) # Invalid priority raises exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\depq.py", line 340, in addfirst
raise ValueError('Priority must be >= '
ValueError: Priority must be >= highest priority.
>>>
>>> del depq[0] # As does del
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\depq.py", line 639, in __delitem__
raise NotImplementedError('Items cannot be deleted by '
NotImplementedError: Items cannot be deleted by referencing arbitrary indices.
>>>
>>> depq.clear()
>>> depq.count(None)
0
>>> for i in range(10):
... depq.insert(None, i)
...
>>> print(fill(str(depq), 77))
DEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5), (None, 4),
(None, 3), (None, 2), (None, 1), (None, 0)])
>>>
>>> None in depq
True
>>> depq.count(None)
10
>>> depq.remove(None) # Removes item from DEPQ, default # of removals is 1
[(None, 0)]
>>>
>>> print(fill(str(depq), 77))
DEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5), (None, 4),
(None, 3), (None, 2), (None, 1)])
>>>
>>> depq.remove(None, 4) # As you see, returns list of tuple(item, priority)
[(None, 1), (None, 2), (None, 3), (None, 4)]
>>> print(fill(str(depq), 77))
DEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5)])
>>>
>>> depq[None] = 7 # Alias for DEPQ.insert(item, priority)
>>> print(fill(str(depq), 77))
DEPQ([(None, 9), (None, 8), (None, 7), (None, 7), (None, 6), (None, 5)])
>>>
>>> depq.elim(None) # This simply calls DEPQ.remove(item, -1)
[(None, 5), (None, 6), (None, 7), (None, 7), (None, 8), (None, 9)]
>>> print(fill(str(depq), 77))
DEPQ([])
>>>
>>> import pickle # Pickling won't work if items aren't picklable
>>> import json # JSON won't work if items aren't JSON serializable
>>>
>>> for i in range(5):
... depq.insert([i], i) # Unhashable types allowed but don't mutate them!
...
>>> depq
DEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])
>>>
>>> binary_depq = pickle.dumps(depq)
>>> print(fill(str(binary_depq), 77))
b'\x80\x03cdepq\nDEPQ\nq\x00)\x81q\x01}q\x02(X\x05\x00\x00\x00itemsq\x03}q\x0
4(X\x03\x00\x00\x00[1]q\x05K\x01X\x03\x00\x00\x00[3]q\x06K\x01X\x03\x00\x00\x
00[2]q\x07K\x01X\x03\x00\x00\x00[4]q\x08K\x01X\x03\x00\x00\x00[0]q\tK\x01uX\x
04\x00\x00\x00dataq\nccollections\ndeque\nq\x0b]q\x0c(]q\rK\x04aK\x04\x86q\x0
e]q\x0fK\x03aK\x03\x86q\x10]q\x11K\x02aK\x02\x86q\x12]q\x13K\x01aK\x01\x86q\x
14]q\x15K\x00aK\x00\x86q\x16e\x85q\x17Rq\x18X\x05\x00\x00\x00startq\x19K\x00u
b.'
>>>
>>> json_depq = json.dumps(depq.to_json())
>>> print(fill(json_depq, 77))
{"items": {"[1]": 1, "[3]": 1, "[2]": 1, "[4]": 1, "[0]": 1}, "data": [[[4],
4], [[3], 3], [[2], 2], [[1], 1], [[0], 0]], "start": 0}
>>>
>>> depq_from_pickle = pickle.loads(binary_depq)
>>> depq_from_json = DEPQ.from_json(json_depq) # Classmethod returns new DEPQ
>>>
>>> depq
DEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])
>>> depq_from_pickle
DEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])
>>> depq_from_json
DEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])
>>>
Notes:
------
- The items in DEPQ are also stored along with their frequency in a
separate dict for O(1) lookup. If item is un-hashable, the repr()
of that item is stored instead. So 'item in DEPQ' would check the
dict for item and if TypeError is raised it would try repr(item).
- This implementation inserts in the middle in linear time whereas
a textbook DEPQ is O(log n). In actual use cases though, this
infinitesimal increase in run time is irrelevant, especially when
one considers the extra functionality gained coupled with the
fact that the other 2 main operations popfirst() and poplast() now
occur in constant time.
Raw data
{
"_id": null,
"home_page": "https://github.com/Ofekmeister/depq",
"name": "depq",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "double ended priority queue,depq,priority queue,data structure,scheduling,heuristic analysis",
"author": "Ofek Lev",
"author_email": "ofekmeister@gmail.com",
"download_url": "https://github.com/Ofekmeister/depq",
"platform": "",
"description": ".. image:: https://travis-ci.org/Ofekmeister/depq.svg?branch=master\n :target: https://travis-ci.org/Ofekmeister/depq\n\n.. image:: https://coveralls.io/repos/Ofekmeister/depq/badge.svg?branch=master\n :target: https://coveralls.io/r/Ofekmeister/depq?branch=master\n\n==================================\ndepq - Double-ended priority queue\n==================================\n\n- Python implementation of a thread-safe and efficient\n double-ended priority queue (DEPQ) in which items and their\n priority values are stored in a deque object as tuples.\n- This of course can also be used as a regular priority queue, or\n simply a FIFO/LIFO queue.\n- Priority queues have many uses such as scheduling, event driven\n simulation, heuristic analysis, spam filtering, graph searching, etc.\n\nFeatures & advantages of this implementation:\n---------------------------------------------\n\n- Completely thread-safe\n- Serializable via pickling or JSON\n- Priority values can be ints/floats, numpy types, strings, or\n any other comparable type you choose!\n- popfirst() and poplast() have O(1) performance instead of\n running in logarithmic time like in a standard DEPQ or other\n heap-derived structure\n- Naturally fast also because deque object is implemented in C\n- Items with equal priorities are sorted in the order they were\n originally added\n- Specific items can be deleted or their priorities changed\n- Membership testing with 'in' operator occurs in O(1) as does\n getting an item's frequency in DEPQ via count(item)\n\nImplementation:\n---------------\n\n- Priorities are always in proper order, thus, a binary search\n is performed to find the right index with which to insert new\n items when specifying priority. Normally, this would result in\n O(n log n) performance when adding items via insert(item, priority)\n where self.high() > priority > self.low() because deque (as a\n doubly linked list) random access is O(n).\n\n Though, ACTUALLY that is not the case here as I've been able to\n reduce that to O(n) by modifying the binary search to operate while\n the internal deque is concurrently rotating.\n\nExamples:\n---------\n\n>>> from textwrap import fill # For nice wrapped printing\n>>> from depq import DEPQ\n>>>\n>>> # Defaults. If iterable is not None, extend(iterable) will be\n>>> # called (example below). If maxlen is not None, abs(int(maxlen))\n>>> # will become the length limit. If a maxlen is set and an item\n>>> # is added with a priority > lowest prioritized item, it will be\n>>> # added and the last item will be popped. After instantiation, the\n>>> # maxlen can be retrieved with maxlen() and set with set_maxlen(length).\n>>> depq = DEPQ(iterable=None, maxlen=None)\n>>>\n>>> # Add some characters with their ordinal\n>>> # values as priority and keep count\n>>> for c in 'AN_ERRONEOUS_STRING':\n... count = list( # This is hacky and not important, skip next 4 lines :)\n... x + 1 if '{} #{}'.format(c, x + 1) in depq\n... else next(iter(())) if x != 0 else 0\n... for x in range(len(depq) + 1)\n... )[-1]\n...\n... depq.insert('{} #{}'.format(c, count + 1), ord(c)) # item, priority\n...\n>>> print(fill(str(depq), 77))\nDEPQ([('_ #1', 95), ('_ #2', 95), ('U #1', 85), ('T #1', 84), ('S #1', 83),\n('S #2', 83), ('R #1', 82), ('R #2', 82), ('R #3', 82), ('O #1', 79), ('O\n#2', 79), ('N #1', 78), ('N #2', 78), ('N #3', 78), ('I #1', 73), ('G #1',\n71), ('E #1', 69), ('E #2', 69), ('A #1', 65)])\n>>>\n>>> # As you can see items with equal priorities are sorted in the order\n>>> # they were originally added. Also note DEPQ root (depq[0]) is highest\n>>> # priority like a max heap.\n>>>\n>>> depq.first()\n'_ #1'\n>>> depq.last()\n'A #1'\n>>> depq.high()\n95\n>>> depq.low()\n65\n>>> depq[7] # Returns tuple(item, priority)\n('R #2', 82)\n>>>\n>>> depq.poplast()\n('A #1', 65)\n>>> depq.last()\n'E #2'\n>>>\n>>> depq.size() # Alias for len(DEPQ)\n18\n>>> depq.is_empty()\nFalse\n>>> depq.clear()\n>>> depq.is_empty()\nTrue\n>>>\n>>> # Extend any length iterable of iterables of length >= 2\n>>> depq.extend([('bar', 1, 'arbitrary'), (None, 5), ('foo', 2, 'blah')])\n>>> depq\nDEPQ([(None, 5), ('foo', 2), ('bar', 1)])\n>>>\n>>> depq.clear()\n>>>\n>>> depq.addfirst('starter') # For an empty DEPQ, addfirst & addlast are\n>>> # functionally identical; they add item to DEPQ\n>>> depq # with given priority, or default 0\nDEPQ([('starter', 0)])\n>>>\n>>> depq.addfirst('high', depq.high() + 1)\n>>> depq.addlast('low', depq.low() - 1)\n>>> depq\nDEPQ([('high', 1), ('starter', 0), ('low', -1)])\n>>>\n>>> depq.addfirst('higher') # Default priority DEPQ.high()\n>>> depq.addlast('lower') # Default priority DEPQ.low()\n>>> depq\nDEPQ([('higher', 1), ('high', 1), ('starter', 0), ('low', -1), ('lower', -1)])\n>>>\n>>> depq.addfirst('highest', 0) # Invalid priority raises exception\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\n File \"C:\\Python34\\lib\\depq.py\", line 340, in addfirst\n raise ValueError('Priority must be >= '\nValueError: Priority must be >= highest priority.\n>>>\n>>> del depq[0] # As does del\nTraceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\n File \"C:\\Python34\\lib\\depq.py\", line 639, in __delitem__\n raise NotImplementedError('Items cannot be deleted by '\nNotImplementedError: Items cannot be deleted by referencing arbitrary indices.\n>>>\n>>> depq.clear()\n>>> depq.count(None)\n0\n>>> for i in range(10):\n... depq.insert(None, i)\n...\n>>> print(fill(str(depq), 77))\nDEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5), (None, 4),\n(None, 3), (None, 2), (None, 1), (None, 0)])\n>>>\n>>> None in depq\nTrue\n>>> depq.count(None)\n10\n>>> depq.remove(None) # Removes item from DEPQ, default # of removals is 1\n[(None, 0)]\n>>>\n>>> print(fill(str(depq), 77))\nDEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5), (None, 4),\n(None, 3), (None, 2), (None, 1)])\n>>>\n>>> depq.remove(None, 4) # As you see, returns list of tuple(item, priority)\n[(None, 1), (None, 2), (None, 3), (None, 4)]\n>>> print(fill(str(depq), 77))\nDEPQ([(None, 9), (None, 8), (None, 7), (None, 6), (None, 5)])\n>>>\n>>> depq[None] = 7 # Alias for DEPQ.insert(item, priority)\n>>> print(fill(str(depq), 77))\nDEPQ([(None, 9), (None, 8), (None, 7), (None, 7), (None, 6), (None, 5)])\n>>>\n>>> depq.elim(None) # This simply calls DEPQ.remove(item, -1)\n[(None, 5), (None, 6), (None, 7), (None, 7), (None, 8), (None, 9)]\n>>> print(fill(str(depq), 77))\nDEPQ([])\n>>>\n>>> import pickle # Pickling won't work if items aren't picklable\n>>> import json # JSON won't work if items aren't JSON serializable\n>>>\n>>> for i in range(5):\n... depq.insert([i], i) # Unhashable types allowed but don't mutate them!\n...\n>>> depq\nDEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])\n>>>\n>>> binary_depq = pickle.dumps(depq)\n>>> print(fill(str(binary_depq), 77))\nb'\\x80\\x03cdepq\\nDEPQ\\nq\\x00)\\x81q\\x01}q\\x02(X\\x05\\x00\\x00\\x00itemsq\\x03}q\\x0\n4(X\\x03\\x00\\x00\\x00[1]q\\x05K\\x01X\\x03\\x00\\x00\\x00[3]q\\x06K\\x01X\\x03\\x00\\x00\\x\n00[2]q\\x07K\\x01X\\x03\\x00\\x00\\x00[4]q\\x08K\\x01X\\x03\\x00\\x00\\x00[0]q\\tK\\x01uX\\x\n04\\x00\\x00\\x00dataq\\nccollections\\ndeque\\nq\\x0b]q\\x0c(]q\\rK\\x04aK\\x04\\x86q\\x0\ne]q\\x0fK\\x03aK\\x03\\x86q\\x10]q\\x11K\\x02aK\\x02\\x86q\\x12]q\\x13K\\x01aK\\x01\\x86q\\x\n14]q\\x15K\\x00aK\\x00\\x86q\\x16e\\x85q\\x17Rq\\x18X\\x05\\x00\\x00\\x00startq\\x19K\\x00u\nb.'\n>>>\n>>> json_depq = json.dumps(depq.to_json())\n>>> print(fill(json_depq, 77))\n{\"items\": {\"[1]\": 1, \"[3]\": 1, \"[2]\": 1, \"[4]\": 1, \"[0]\": 1}, \"data\": [[[4],\n4], [[3], 3], [[2], 2], [[1], 1], [[0], 0]], \"start\": 0}\n>>>\n>>> depq_from_pickle = pickle.loads(binary_depq)\n>>> depq_from_json = DEPQ.from_json(json_depq) # Classmethod returns new DEPQ\n>>>\n>>> depq\nDEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])\n>>> depq_from_pickle\nDEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])\n>>> depq_from_json\nDEPQ([([4], 4), ([3], 3), ([2], 2), ([1], 1), ([0], 0)])\n>>>\n\nNotes:\n------\n\n- The items in DEPQ are also stored along with their frequency in a\n separate dict for O(1) lookup. If item is un-hashable, the repr()\n of that item is stored instead. So 'item in DEPQ' would check the\n dict for item and if TypeError is raised it would try repr(item).\n- This implementation inserts in the middle in linear time whereas\n a textbook DEPQ is O(log n). In actual use cases though, this\n infinitesimal increase in run time is irrelevant, especially when\n one considers the extra functionality gained coupled with the\n fact that the other 2 main operations popfirst() and poplast() now\n occur in constant time.\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Double-ended priority queue",
"version": "1.5.5",
"project_urls": {
"Download": "https://github.com/Ofekmeister/depq",
"Homepage": "https://github.com/Ofekmeister/depq"
},
"split_keywords": [
"double ended priority queue",
"depq",
"priority queue",
"data structure",
"scheduling",
"heuristic analysis"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ce6ffec71bb1b2d91aa086eb55f7982b4f5853d819827c0b7df2d26faf3f20c1",
"md5": "6d31e25e3849fc3acd8574b558b99641",
"sha256": "3c05b915d1e4e4ff8f50d0572c6894b014423c5094a1d3fb82292c1c46c97619"
},
"downloads": -1,
"filename": "depq-1.5.5-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "6d31e25e3849fc3acd8574b558b99641",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 14263,
"upload_time": "2017-06-16T19:01:47",
"upload_time_iso_8601": "2017-06-16T19:01:47.563306Z",
"url": "https://files.pythonhosted.org/packages/ce/6f/fec71bb1b2d91aa086eb55f7982b4f5853d819827c0b7df2d26faf3f20c1/depq-1.5.5-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2017-06-16 19:01:47",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Ofekmeister",
"github_project": "depq",
"travis_ci": true,
"coveralls": true,
"github_actions": false,
"tox": true,
"lcname": "depq"
}