HeapDict-1.0.1/0000755036046502575230000000000013535520146016015 5ustar stutzbachprimarygroup00000000000000HeapDict-1.0.1/heapdict.py0000644036046502575230000000560313535507365020164 0ustar stutzbachprimarygroup00000000000000try: from collections.abc import MutableMapping except ImportError: from collections import MutableMapping def doc(s): if hasattr(s, '__call__'): s = s.__doc__ def f(g): g.__doc__ = s return g return f class heapdict(MutableMapping): __marker = object() def __init__(self, *args, **kw): self.heap = [] self.d = {} self.update(*args, **kw) @doc(dict.clear) def clear(self): del self.heap[:] self.d.clear() @doc(dict.__setitem__) def __setitem__(self, key, value): if key in self.d: self.pop(key) wrapper = [value, key, len(self)] self.d[key] = wrapper self.heap.append(wrapper) self._decrease_key(len(self.heap)-1) def _min_heapify(self, i): n = len(self.heap) h = self.heap while True: # calculate the offset of the left child l = (i << 1) + 1 # calculate the offset of the right child r = (i + 1) << 1 if l < n and h[l][0] < h[i][0]: low = l else: low = i if r < n and h[r][0] < h[low][0]: low = r if low == i: break self._swap(i, low) i = low def _decrease_key(self, i): while i: # calculate the offset of the parent parent = (i - 1) >> 1 if self.heap[parent][0] < self.heap[i][0]: break self._swap(i, parent) i = parent def _swap(self, i, j): h = self.heap h[i], h[j] = h[j], h[i] h[i][2] = i h[j][2] = j @doc(dict.__delitem__) def __delitem__(self, key): wrapper = self.d[key] while wrapper[2]: # calculate the offset of the parent parentpos = (wrapper[2] - 1) >> 1 parent = self.heap[parentpos] self._swap(wrapper[2], parent[2]) self.popitem() @doc(dict.__getitem__) def __getitem__(self, key): return self.d[key][0] @doc(dict.__iter__) def __iter__(self): return iter(self.d) def popitem(self): """D.popitem() -> (k, v), remove and return the (key, value) pair with lowest\nvalue; but raise KeyError if D is empty.""" wrapper = self.heap[0] if len(self.heap) == 1: self.heap.pop() else: self.heap[0] = self.heap.pop() self.heap[0][2] = 0 self._min_heapify(0) del self.d[wrapper[1]] return wrapper[1], wrapper[0] @doc(dict.__len__) def __len__(self): return len(self.d) def peekitem(self): """D.peekitem() -> (k, v), return the (key, value) pair with lowest value;\n but raise KeyError if D is empty.""" return (self.heap[0][1], self.heap[0][0]) del doc __all__ = ['heapdict'] HeapDict-1.0.1/LICENSE0000644036046502575230000000265313535503204017024 0ustar stutzbachprimarygroup00000000000000Copyright 2009 Stutzbach Enterprises, LLC (daniel@stutzbachenterprises.com) Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. HeapDict-1.0.1/test_heap.py0000755036046502575230000000633113535507365020361 0ustar stutzbachprimarygroup00000000000000#!/usr/bin/python from __future__ import print_function import random import sys import unittest from heapdict import heapdict try: # Python 3 import test.support as test_support except ImportError: # Python 2 import test.test_support as test_support N = 100 class TestHeap(unittest.TestCase): def check_invariants(self, h): # the 3rd entry of each heap entry is the position in the heap for i, e in enumerate(h.heap): self.assertEqual(e[2], i) # the parent of each heap element must not be larger than the element for i in range(1, len(h.heap)): parent = (i - 1) >> 1 self.assertLessEqual(h.heap[parent][0], h.heap[i][0]) def make_data(self): pairs = [(random.random(), random.random()) for i in range(N)] h = heapdict() d = {} for k, v in pairs: h[k] = v d[k] = v pairs.sort(key=lambda x: x[1], reverse=True) return h, pairs, d def test_popitem(self): h, pairs, _ = self.make_data() while pairs: v = h.popitem() v2 = pairs.pop(-1) self.assertEqual(v, v2) self.assertEqual(len(h), 0) def test_popitem_ties(self): h = heapdict() for i in range(N): h[i] = 0 for i in range(N): _, v = h.popitem() self.assertEqual(v, 0) self.check_invariants(h) def test_peek(self): h, pairs, _ = self.make_data() while pairs: v = h.peekitem()[0] h.popitem() v2 = pairs.pop(-1) self.assertEqual(v, v2[0]) self.assertEqual(len(h), 0) def test_iter(self): h, _, d = self.make_data() self.assertEqual(list(h), list(d)) def test_keys(self): h, _, d = self.make_data() self.assertEqual(list(sorted(h.keys())), list(sorted(d.keys()))) def test_values(self): h, _, d = self.make_data() self.assertEqual(list(sorted(h.values())), list(sorted(d.values()))) def test_del(self): h, pairs, _ = self.make_data() k, v = pairs.pop(N//2) del h[k] while pairs: v = h.popitem() v2 = pairs.pop(-1) self.assertEqual(v, v2) self.assertEqual(len(h), 0) def test_change(self): h, pairs, _ = self.make_data() k, v = pairs[N//2] h[k] = 0.5 pairs[N//2] = (k, 0.5) pairs.sort(key=lambda x: x[1], reverse=True) while pairs: v = h.popitem() v2 = pairs.pop(-1) self.assertEqual(v, v2) self.assertEqual(len(h), 0) def test_clear(self): h, _, _ = self.make_data() h.clear() self.assertEqual(len(h), 0) def test_main(verbose=None): test_classes = [TestHeap] test_support.run_unittest(*test_classes) # verify reference counting if verbose and hasattr(sys, "gettotalrefcount"): import gc counts = [None] * 5 for i in range(len(counts)): test_support.run_unittest(*test_classes) gc.collect() counts[i] = sys.gettotalrefcount() print(counts) if __name__ == "__main__": test_main(verbose=True) HeapDict-1.0.1/HeapDict.egg-info/0000755036046502575230000000000013535520146021170 5ustar stutzbachprimarygroup00000000000000HeapDict-1.0.1/HeapDict.egg-info/SOURCES.txt0000644036046502575230000000033313535520145023052 0ustar stutzbachprimarygroup00000000000000LICENSE MANIFEST.in README.rst heapdict.py setup.py test_heap.py HeapDict.egg-info/PKG-INFO HeapDict.egg-info/SOURCES.txt HeapDict.egg-info/dependency_links.txt HeapDict.egg-info/top_level.txt HeapDict.egg-info/zip-safeHeapDict-1.0.1/HeapDict.egg-info/zip-safe0000644036046502575230000000000113535516144022623 0ustar stutzbachprimarygroup00000000000000 HeapDict-1.0.1/HeapDict.egg-info/top_level.txt0000644036046502575230000000001113535520145023711 0ustar stutzbachprimarygroup00000000000000heapdict HeapDict-1.0.1/HeapDict.egg-info/PKG-INFO0000644036046502575230000000422113535520145022263 0ustar stutzbachprimarygroup00000000000000Metadata-Version: 1.1 Name: HeapDict Version: 1.0.1 Summary: a heap with decrease-key and increase-key operations Home-page: http://stutzbachenterprises.com/ Author: Stutzbach Enterprises, LLC Author-email: daniel@stutzbachenterprises.com License: BSD Description: heapdict: a heap with decreased-key and increase-key operations =============================================================== heapdict implements the MutableMapping ABC, meaning it works pretty much like a regular Python dict. It's designed to be used as a priority queue, where items are added and consumed as follows: :: hd = heapdict() hd[obj1] = priority1 hd[obj2] = priority2 # ... (obj, priority) = hd.popitem() Compared to an ordinary dict, a heapdict has the following differences: popitem(): Remove and return the (key, priority) pair with the lowest priority, instead of a random object. peekitem(): Return the (key, priority) pair with the lowest priority, without removing it. Unlike the Python standard library's heapq module, the heapdict supports efficiently changing the priority of an existing object (often called "decrease-key" in textbooks). Altering the priority is important for many algorithms such as Dijkstra's Algorithm and A*. Keywords: heap decrease-key increase-key dictionary Dijkstra A* priority queue Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Operating System :: OS Independent Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.0 Classifier: Programming Language :: Python :: 3.1 Classifier: Programming Language :: Python :: 3.2 Provides: heapdict HeapDict-1.0.1/HeapDict.egg-info/dependency_links.txt0000644036046502575230000000000113535520145025235 0ustar stutzbachprimarygroup00000000000000 HeapDict-1.0.1/MANIFEST.in0000644036046502575230000000013513535516176017562 0ustar stutzbachprimarygroup00000000000000include heapdict.py include LICENSE include test_heap.py include setup.py include README.rst HeapDict-1.0.1/README.rst0000644036046502575230000000175213535510040017501 0ustar stutzbachprimarygroup00000000000000heapdict: a heap with decreased-key and increase-key operations =============================================================== heapdict implements the MutableMapping ABC, meaning it works pretty much like a regular Python dict. It's designed to be used as a priority queue, where items are added and consumed as follows: :: hd = heapdict() hd[obj1] = priority1 hd[obj2] = priority2 # ... (obj, priority) = hd.popitem() Compared to an ordinary dict, a heapdict has the following differences: popitem(): Remove and return the (key, priority) pair with the lowest priority, instead of a random object. peekitem(): Return the (key, priority) pair with the lowest priority, without removing it. Unlike the Python standard library's heapq module, the heapdict supports efficiently changing the priority of an existing object (often called "decrease-key" in textbooks). Altering the priority is important for many algorithms such as Dijkstra's Algorithm and A*. HeapDict-1.0.1/PKG-INFO0000644036046502575230000000422113535520146017111 0ustar stutzbachprimarygroup00000000000000Metadata-Version: 1.1 Name: HeapDict Version: 1.0.1 Summary: a heap with decrease-key and increase-key operations Home-page: http://stutzbachenterprises.com/ Author: Stutzbach Enterprises, LLC Author-email: daniel@stutzbachenterprises.com License: BSD Description: heapdict: a heap with decreased-key and increase-key operations =============================================================== heapdict implements the MutableMapping ABC, meaning it works pretty much like a regular Python dict. It's designed to be used as a priority queue, where items are added and consumed as follows: :: hd = heapdict() hd[obj1] = priority1 hd[obj2] = priority2 # ... (obj, priority) = hd.popitem() Compared to an ordinary dict, a heapdict has the following differences: popitem(): Remove and return the (key, priority) pair with the lowest priority, instead of a random object. peekitem(): Return the (key, priority) pair with the lowest priority, without removing it. Unlike the Python standard library's heapq module, the heapdict supports efficiently changing the priority of an existing object (often called "decrease-key" in textbooks). Altering the priority is important for many algorithms such as Dijkstra's Algorithm and A*. Keywords: heap decrease-key increase-key dictionary Dijkstra A* priority queue Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Operating System :: OS Independent Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.0 Classifier: Programming Language :: Python :: 3.1 Classifier: Programming Language :: Python :: 3.2 Provides: heapdict HeapDict-1.0.1/setup.py0000644036046502575230000000227613535520121017527 0ustar stutzbachprimarygroup00000000000000#!/usr/bin/env from setuptools import setup setup(name='HeapDict', version='1.0.1', description='a heap with decrease-key and increase-key operations', author='Stutzbach Enterprises, LLC', author_email='daniel@stutzbachenterprises.com', url='http://stutzbachenterprises.com/', license = "BSD", keywords = "heap decrease-key increase-key dictionary Dijkstra A* priority queue", provides = ['heapdict'], py_modules = ['heapdict'], test_suite = "test_heap", zip_safe = True, classifiers = [ 'Development Status :: 5 - Production/Stable', 'Operating System :: OS Independent', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Topic :: Software Development :: Libraries :: Python Modules', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.0', 'Programming Language :: Python :: 3.1', 'Programming Language :: Python :: 3.2', ], long_description=open('README.rst').read(), ) HeapDict-1.0.1/setup.cfg0000644036046502575230000000004613535520146017636 0ustar stutzbachprimarygroup00000000000000[egg_info] tag_build = tag_date = 0