Patches item #1162363, was opened at 2005-03-13 11:45 Message generated for change (Comment added) made by scoder You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1162363&group_id=5470
Category: Library (Lib) Group: None Status: Open Resolution: None Priority: 5 Submitted By: Stefan Behnel (scoder) Assigned to: Nobody/Anonymous (nobody) Summary: Heap class for heapq module Initial Comment: Bug 1158313 (requesting functions for heap iteration) was rejected, one of the reasons being that iteration does not fit well with the module design providing helper functions. Here is an implementation of a Heap class for that module that encapsulates a list. It supports iteration as well as the usual keyword arguments passed to sort() or sorted(): key, cmp, reversed. It further supports skipping the heapify step (heapify=True/False) when constructing the heap as well as copying the list if unnecessary (copy=True/False). The latter is generally discouraged and not the default. It is, however, useful for one-shot sequence generation as in >>> h = Heap([ random() for i in range(1000) ], copy=False) A further feature (somewhat obvious for heaps) is that iteration is not interrupted by items being pushed on the heap, they are simply integrated. "heapreplace" is supported by a "pushpop" function as initially proposed by R. Hettinger. It always returns the smallest item, also considering the one being pushed. This implementation is complementary to the existing functions that work on arbitrary (mutable) sequences. Both have their use cases and both make sense in the module. The Heap class has the additional advantage of encapsulating the list and thus allowing to support special item comparisons in a consistent way. There is not yet any documentation or unit tests, but they should be easy to write once the Heap class is actually considered for integration. ---------------------------------------------------------------------- >Comment By: Stefan Behnel (scoder) Date: 2005-03-14 13:15 Message: Logged In: YES user_id=313935 The semantics of combining cmp and key are not specified in the Python documentation and my last implementation differed from the semantics of sort() and sorted() in that regard. The new patch fixes this and does some clean up. It also introduces a method iter_clone() that returns an independent iterator. ---------------------------------------------------------------------- Comment By: Stefan Behnel (scoder) Date: 2005-03-13 12:00 Message: Logged In: YES user_id=313935 Forgot undecoration in __getitem__ method. Updated patch. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1162363&group_id=5470 _______________________________________________ Patches mailing list [email protected] http://mail.python.org/mailman/listinfo/patches
