_marker : raise return default else : entry_to_delete = heap pkey = entry_to_delete. pop ( dkey ) #raises appropriate KeyError except KeyError : if default is self. dkey del self return dkey try : pos = position. _marker : if not heap : raise KeyError ( 'PQDict is empty' ) dkey = heap. If dkey is not provided, remove and return the top-priority dictionary key or raise KeyError if the PQD is empty. If default is not provided and dkey is not in the PQD, raise a KeyError. def pop ( self, dkey = _marker, default = _marker ): """ If dkey is in the PQD, remove it and return its priority key, else return default. """ if len ( args ) > 1 : raise TypeError ( 'Too many arguments' ) self. setdefault def _init_ ( self, * args, ** kwargs ): """ Same input signature as dict: Accepts at most one positional argument: - a sequence/iterator of (dkey, pkey) pairs - a mapping object Accepts keyword arguments The default priority ordering for entries is in decreasing pkey value (i.e., a min-pq: SMALLER pkey values have a HIGHER rank). keys values = prioritykeys = MutableMapping. """ _entry_class = _MinEntry _eq_ = MutableMapping. The mapping is mutable so items may be added, removed and have their priorities updated without breaking the heap. PQDicts maintain an internal heap so that the highest priority item can always be obtained in constant time. class PQDict ( MutableMapping ): """ A mapping object that maps dictionary keys (dkeys) to priority keys (pkeys). """ class _CustomEntry ( _AbstractEntry ): _lt_ = comparator return _CustomEntry The function should return True if self has higher priority than other and False otherwise. The comparator should have the form: cmp( self, other ) -> bool where self and other are entry instances (have dkey and pkey attributes). pkey def new_entry_class ( comparator ): """ Define entries for a PQDict that uses a custom comparator to sort entries. Implementation details: _heap (list): stores (dkey, pkey) pairs as "entry" objects that implement _lt_ which defines how their priority keys are compared _position (dict): maps each dkey to the index of its entry in the heap """ _version_ = ( 0, 5, 0 ) _all_ = import sys from collections import Mapping, MutableMapping if sys. I stuck to the textbook convention, but using the sink/swim nomenclature from Sedgewick et al: the way I like to think of it, an item that is too "heavy" (low-priority) should sink down the tree, while one that is too "light" should float or swim up. * The names of the methods in heapq (sift up/down) refer to the motion of the items being compared to, rather than the item being operated on as is usually done in textbooks (i.e. As a result, PQD also supports: - O(1) lookup of an arbitrary element's priority key - O(log n) removal of an arbitrary element - O(log n) updating of an arbitrary element's priority key The standard heap operations used internally are based on those in the python heapq module (here, called "sink" and "swim").* These operations are extended to maintain the internal dictionary. This index is kept up-to-date when the heap is manipulated. The priority queue is implemented as a binary heap, which supports: - O(1) access to the top priority element - O(log n) removal of the top priority element - O(log n) insertion of a new element In addition, an internal dictionary or "index" maps elements to their position in the heap array. A dict-like heap queue to prioritize hashable objects while providing random access and updatable priorities. Inspired by the Python implementation of the heapq module, which was written by Kevin O'Connor and augmented by Tim Peters and Raymond Hettinger. """ """Priority Queue Dictionary - An indexed priority queue data structure. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. """Copyright (c) 2012 Nezar Abdennur Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |