def poppush(self, value): """Pop from the heap then push value into the heap. More efficient then pop() followed by push(). """ top_val = self.peek() self._vals[0] = value down_heap(self, 0) return top_val
def extend(self, iterable): """Add values to the heap from an iterable. Use the heapify algorithm to rebuild the heap, to achieve O(n) performance for heap creation/merge. """ self._vals.extend(iterable) len_half = len(self) // 2 for idx in reversed(xrange(len_half)): down_heap(self, idx)
def pushpop(self, value): """Push value into the heap then pop from the heap. More efficient then push() followed by pop(). """ top_val = self.peek() if self._upper_eq(value, top_val): return value self._vals[0] = value down_heap(self, 0) return top_val
def __delitem__(self, key): """Remove the key and its value from the container.""" if key not in self: raise KeyError("key not in container") last_idx = len(self) - 1 idx_curr = self._key2idx[key] self._swap(idx_curr, last_idx) del self._key2idx[key] self._val_keys.pop() if self: down_heap(self, idx_curr)
def pop(self): """Remove and return the top element of the heap (min element in min-heap; max element in max-heap). """ if not self: raise IndexError("peek/pop from an empty container") top_val = self.peek() last_idx = len(self) - 1 self._swap(0, last_idx) self._vals.pop() if self: down_heap(self, 0) return top_val
def update(self, *args, **kwds): """D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] Use the heapify algorithm to rebuild the heap, to achieve O(n) performance for heap creation/merge. """ def add(key, val): if key in self: idx = self._key2idx[key] self._val_keys[idx] = (val, key) else: self._key2idx[key] = len(self) self._val_keys.append((val, key)) def add_from_mapping(mapping): for key in mapping: val = mapping[key] add(key, val) def add_from_iterable(iterable): for key, val in iterable: add(key, val) for i, container in enumerate(args): try: if hasattr(container, "keys"): add_from_mapping(container) else: add_from_iterable(container) except Exception: raise TypeError( "cannot convert update sequence element " "#{} to a sequence".format(i)) add_from_mapping(kwds) len_half = len(self) // 2 for idx in reversed(xrange(len_half)): down_heap(self, idx)
def update(self, *args, **kwds): """D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] Use the heapify algorithm to rebuild the heap, to achieve O(n) performance for heap creation/merge. """ def add(key, val): if key in self: idx = self._key2idx[key] self._val_keys[idx] = (val, key) else: self._key2idx[key] = len(self) self._val_keys.append((val, key)) def add_from_mapping(mapping): for key in mapping: val = mapping[key] add(key, val) def add_from_iterable(iterable): for key, val in iterable: add(key, val) for i, container in enumerate(args): try: if hasattr(container, "keys"): add_from_mapping(container) else: add_from_iterable(container) except Exception: raise TypeError("cannot convert update sequence element " "#{} to a sequence".format(i)) add_from_mapping(kwds) len_half = len(self) // 2 for idx in reversed(xrange(len_half)): down_heap(self, idx)
def __setitem__(self, key, value): """Set value of the key. If key does not in container, add the key-value into container. Otherwise update the value of the key. The update of key value uses the heap decrease/increase key algorithm. """ if key not in self: self._val_keys.append((value, key)) idx = len(self) - 1 self._key2idx[key] = idx up_heap(self, idx) else: idx = self._key2idx[key] val_old = self._idx2val(idx) if val_old == value: return self._val_keys[idx] = (value, key) if self._upper_eq(value, val_old): up_heap(self, idx) else: down_heap(self, idx)