Exemplo n.º 1
0
    def top(self, k):
        """
        generate sequence of top k elements in terms of access
        """
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k")
        temp = PositionalList()
        for item in self.data:
            temp.add_last(item)

        for j in range(k):
            high_pos = temp.first()
            walk = temp.after(high_pos)
            while walk is not None:
                if walk.element()._count > high_pos.element()._count:
                    high_pos = walk
                walk = temp.after(walk)
            yield high_pos.element()._value
            temp.delete(high_pos)
class SortedPriorityQueue(PriorityQueue):
    """
    implementing sorted priority queue
    """
    def __init__(self):
        self._data = PositionalList()

    def __len__(self):
        return len(self._data)

    def add(self, key, value):
        """
        add key-value pair to list O(n)
        """
        new = self._Item(key, value)
        walk = self._data.last()
        while walk is not None and new < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(new)
        else:
            self._data.add_after(walk, new)

    def min(self):
        """
        return item with minimum key O(1)
        """
        if self.is_empty():
            raise Exception("priority queue is empty")
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        remove and return item with minimum key O(1)
        """
        if self.is_empty():
            raise Exception("priority queue is empty")
        item = self._data.delete(self._data.first())
        return (item._key, item._value)
Exemplo n.º 3
0
class UnsortedPriorityQueue(PriorityQueue):
    """
    implementing unsorted priority queue
    """
    def __init__(self):
        self._data = PositionalList()

    def __len__(self):
        return len(self._data)

    def add(self, key, value):
        """
        add element to queue O(1)
        """
        self._data.add_last(self._Item(key, value))

    def min(self):
        """
        return item with minimum key O(n)
        """
        p = self._find_min()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        remove and return item with minimum key O(n)
        """
        p = self._find_min()
        item = self._data.delete(p)
        return (item._key, item._value)

    def _find_min(self):
        """
        return position of item with minimum key O(n)
        """
        if self.is_empty():
            raise Exception("priority queue is empty")
        small = self._data.first()
        walk = self._data.after(small)
        while walk is not None:
            if walk.element() < small.element():
                small = walk
            walk = self._data.after(walk)
        return small
Exemplo n.º 4
0
 def __init__(self):
     self._data = PositionalList()
Exemplo n.º 5
0
class FavoritesList:
    """
    list of elements ordered from most frequently accessed to least
    """
    class _Item:
        __slots__ = '_value', '_count'

        def __init__(self, e):
            self._value = e
            self._count = 0

    def _find_position(self, e):
        """
        search for element e and return its Position
        """
        walk = self.data.first()
        while walk is not None and walk.element()._value != e:
            walk = self.data.after(walk)
        return walk

    def _move_up(self, p):
        """
        move item at Position p earlier in list based on access count
        """
        if p != self.data.first():
            count = p.value()._count
            walk = self.data.before(p)
            if count > walk.element()._count:
                while (walk != self.data.first()
                       and count > self.data.before(walk).element()._count):
                    walk = self.data.before(walk)
                self.data.add_before(walk, self.data.delete(p))

    def __init__(self):
        self.data = PositionalList()

    def __len__(self):
        return len(self.data)

    def is_empty(self):
        return len(self.data) == 0

    def access(self, e):
        """
        access element e, thereby incresing its access count
        """
        p = self._find_position(e)
        if p is None:
            p = self.data.add_last(self._Item(e))
        p.element()._count += 1
        self._move_up(p)

    def remove(self, e):
        """
        remove element e from list
        """
        p = self._find_position(e)
        if p is not None:
            self.data.delete(p)

    def top(self, k):
        """
        get sequence of top k elements
        """
        if not 1 <= k <= len(self):
            raise ValueError("Illegal value for k")
        walk = self.data.first()
        for j in range(k):
            item = walk.element()
            yield item._value
            walk = self.data.after(walk)