Example #1
0
def test_construction(backend):
    h = PairingHeap(backend=backend)
    assert h.findmin() is None
    with py.test.raises(StandardError):
        h.deletemin()

    h = PairingHeap(backend=backend, items=xrange(5))
    assert h.findmin() == 0
    for x in xrange(5):
        assert h.getkey(x) == x
    for x in xrange(5):
        assert h.deletemin() == x

    alphabet = 'abcdefghij'
    h = PairingHeap(backend=backend, 
                    keys=dict(zip(alphabet, xrange(len(alphabet)))))
    assert h.findmin() == 'a'
    for x in alphabet:
        assert h.getkey(x) == ord(x) - ord('a')
    for x in alphabet:
        assert h.deletemin() == x
Example #2
0
 def sorted(self, iterable, cmp=cmp, key=None, reverse=False):
     from yaupon.data_structures.pairing_heaps import PairingHeap
     if reverse:
         mycmp = lambda x,y : -1 * cmp(x, y)
     else:
         mycmp = cmp
     heap = PairingHeap(backend=self, cmp=mycmp)
     for item in iterable:
         item_key = heap.getkey(item)
         if item_key is not None:
             heap.modifykey(item, (item_key[0], item_key[1] + 1))
         else:
             item_key = item
             if key is not None:
                 item_key = key(item)
             heap.insert(item, (item_key, 1))
     while heap:
         min_key = heap.findminkey()
         min_item = heap.deletemin()
         for i in xrange(min_key[1]):
             yield min_item