from skewheap import SkewHeap
s = SkewHeap()
# Insert a bunch of items
for i in big_random_number_list:
s.put(i)
# Drain the heap
items = s.items()
for i in items:
print(i)
# Drain the heap manually
while not s.is_empty:
print(s.take())
# Merge heaps (non-destructively)
new_heap = SkewHeap.merge(a, b, c)
# Merge heaps into an existing heap
s.adopt(a, b, c)
A skew heap is a min heap or priority queue which ammortizes the cost of rebalancing using an elegant merge algorithm. All operations on a skew heap are defined in terms of the merge algorithm.
An interesting side effect of this is that skew heaps can be quickly and easily merged non-destructively.
Items added to the heap will be returned in order from lowest to highest. To
control the ordering, implement __gt__ on the class of the items being
inserted.
write me
Non-destructively merges multiple skew heaps together. The new heap will contain all of the items in each of the heaps passed in, in order. Any number of heaps may be merged together.
a = SkewHeap() b = SkewHeap() c = SkewHeap()
d = SkewHeap.merge(a, b, c)
True when there are no elements in the queue.
Creates a new, empty skew heap.
s = SkewHeap()
Adds any number of items to the queue.
s.put("some message") s.put(1, 2, 3) s.put(Task())
Removes the top element from the queue. Returns None if the queue is empty.
item = s.take()
Returns the top element from the queue without removing it. Returns None if
the queue is empty.
item = s.peek()
Merges other heaps into this one. The other heaps are left intact.
s.merge(heap_x, heap_y, heap_z)
Returns a generator that yields each element from the queue in turn.
items = s.items() for item in items: do_stuff_with(item)
Removes and returns all items from the queue as a list.
all_items = s.drain()
True when there are no items in the heap and a call to take() would block.
True after shutdown() has been called. When a queue has been shut down, all
waiters to take() will be awoken and None will be returned to them. New items
may be added to the heap, and will be returned by take(), but the queue will
no longer block when no items are available.
Shuts down the queue. See the notes in is_shutdown regarding the behavior of
shutdown queues.
Blocks until the heap is shut down.
Blocks until an item is available on the queue and returns it.
Adds any number of elements to the queue.
Merges other queues into this one (either SkewHeap or AsyncSkewHeap). The
other queues are left intact.