A mapping object that maps dictionary keys (dkeys) to priority keys (pkeys). PQDicts maintain an internal heap so that the highest priority item can always be obtained in constant time. The mapping is mutable so items may be added, removed and have their priorities updated without breaking the heap.
Create a new Min-PQDict. Smaller priority keys confer higher rank.
Create a new Max-PQDict. Larger priority keys confer higher rank.
Create an empty PQDict that uses a custom comparator. The comparator should have the form:
prio( self, other ) –> bool
where self and other are entry instances (have dkey and pkey members). The function should return True if self has higher priority than other and False otherwise.
If prio is a PQDict instance instead of a function, then an empty PQDict using the same comparator is returned.
Create a new PQDict with dictionary keys from an iterable and priority keys set to value (default value is +inf or -inf to start items off at the bottom of the queue). If a function rank_by is provided instead, that function is used to compute a priority key for each object in the iterable.
These should work as expected. See dict for more details.
- len(pq)¶
- pq[dkey]
- pq[dkey] = pkey
- del pq[dkey]
- dkey in pq
- PQDict.keys()¶
- PQDict.values()¶
- PQDict.items()¶
- PQDict.get(dkey[, default])¶
- PQDict.clear()¶
- PQDict.update([other])¶
- PQDict.setdefault(dkey[, default])¶
- PQDict.copy()¶
Return a new PQD containing the same dkeys associated with the same priority key values.
- PQDict.prioritykeys()¶
Equivalent to values()
- PQDict.pop([dkey[, default]])[source]¶
If dkey is in the PQD, remove it and return its priority key, else return default. If default is not provided and dkey is not in the PQD, raise a KeyError.
If dkey is not provided, remove and return the top-priority dictionary key or raise KeyError if the PQD is empty.
- PQDict.updateitem(dkey, new_pkey)[source]¶
Update the priority key of an existing item. Raises KeyError if dkey is not in the PQD.
- PQDict.topitem()[source]¶
Get top priority dictionary key and priority key. Raises KeyError if PQD is empty.
- PQDict.popitem()[source]¶
Extract top priority dictionary key and priority key. Raises KeyError if PQD is empty.
- PQDict.pushpopitem(dkey, pkey)[source]¶
Equivalent to inserting a new item followed by removing the top priority item, but faster. Raises KeyError if the new dkey is already in the PQD.
Takes a mapping and, treating the values as priority keys, sorts its items by value via heapsort using a PQDict.
Equivalent to: sorted(mapping.items(), key=itemgetter(1), reverse=reverse), except it returns a generator.
Takes a mapping and returns the n keys with the smallest values.