Beispiel #1
0
class SortedPQ(PriorityQueueBase):
    """ Sorted priority queue"""

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

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

    def add(self, k, v):
        newest = self._Item(k, v)
        walk = self._data.last()
        while walk is not None and newest < walk.item():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)
        else:
            self._data.add_after(newest, walk)

    def min(self):
        item = self._data.first().item()
        return item._key, item._val

    def remove_min(self):
        p = self._data.first()
        item = p.item()
        self._data.delete(p)
        return item._key, item._val

    def __iter__(self):
        for item in self._data:
            yield item
Beispiel #2
0
class SortedPriorityQueue(PriorityQueueBase):
    def __init__(self) -> None:
        self._data = PositionalList()

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

    def __iter__(self):
        return iter(self._data)

    def add(self, key, value):
        newest: self._Item = self._Item(key, value)
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)
        else:
            self._data.add_after(walk, newest)

    def min(self):
        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):
        if self.is_empty():
            raise Exception('Priority queue is empty.')
        item = self._data.delete(self._data.first())
        return (item._key, item._value)
Beispiel #3
0
class SortedPriorityQueue(PriorityQueueBase):
    """A min-oriented priority queue implemented with a sorted list"""
    def __init__(self):
        self._data = PositionalList()

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

    def min(self):
        if self.is_empty():
            raise ValueError("Empty Priority Queue")
        item = self._data.first().element()
        return item._key, item._value

    def remove_min(self):
        if self.is_empty():
            raise ValueError("Empty Priority Queue")
        item = self._data.delete(self._data.first())
        return item._key, item._value

    def add(self, key, value):
        new_item = self._Item(key, value)
        walk = self._data.last()
        # walk backwards for smaller key
        while walk != None and new_item < walk.element():
            walk = self._data.before(walk)
        if walk:
            self._data.add_after(new_item, walk)
        else:  # walk is the header
            self._data.add_first(new_item)
Beispiel #4
0
class SortedPriorityQueue(PriorityQueueBase):
    """
    a min-oriented priority queue implemented with a sorted list
    """

    # base class defines _Item

    def __init__(self):
        """
        create a new empty Priority queue
        """
        self._data = PositionalList()

    def __len__(self):
        """
        return the number of items in the priority queue
        :return:
        """
        return len(self._data)

    def add(self, key, value):
        """
        add a key-value pair
        :param key:
        :param value:
        :return:
        """
        newest = self._Item(key, value)  # make new item instance
        walk = self._data.last(
        )  # walk backward looking for smaller key, [last() return a position]
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:
            self._data.add_first(newest)  # new key is smallest
        else:
            self._data.add_after(walk, newest)  # newest goes after walk

    def min(self):
        """
        return but do not remove (k,v) tuple with minimum key
        :return:
        """
        if self.is_empty():
            raise Empty('priority queue is empty')
        p = self._data.first()  # return a position
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """
        remove and return (k,v) tuple with minimum key
        :return:
        """
        if self.is_empty():
            raise Empty('priority queue is empty')
        item = self._data.delete(
            self._data.first())  # delete() return the element
        return (item._key, item._value)
class SortedPriorityQueue(PriorityQueueBase):
    """ A min-oriented priority queue implementation with sorted list
        min() : O(1)
        remove_min : O(1)
        add() : O(n)
    """
    def __init__(self):
        self._data = PositionalList()

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

    def add(self, key, value):
        """  Add a key-value pair at a correct position
        """
        newest = self._Item(key, value)
        # walk backward
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is None:        # new key is smallest
            self._data.add_first(newest)
        else:
            self._data.add_after(walk, newest)

    def min(self):
        """
        :return: tuple (key, value) with minimum key
        """
        if self.is_empty():
            raise EmptyException('Priority Queue is empty')
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        if self.is_empty():
            raise EmptyException('Priority Queue is Empty')
        item = self._data.delete(self._data.first())
        return (item._key, item._value)

    def __str__(self):
        res = []
        walk = self._data.first()
        while walk is not None:
            item = walk.element()
            res.append((item._key, item._value))
            walk = self._data.after(walk)
        return str(res)
Beispiel #6
0
class TextEditor:
    def __init__(self):
        self._data = PositionalList()
        self._data.add_first(None)
        self._cursor = self._data.first()
        self._size = 0

    def left(self):
        prev = self._data.before(self._cursor)
        if prev is not None:
            self._cursor = prev

    def right(self):
        nex = self._data.after(self._cursor)
        if nex is not None:
            self._cursor = nex

    def insert(self, char):
        self._size += 1
        self._data.add_after(self._cursor, char)
        self._cursor = self._data.after(self._cursor)

    def delete(self):
        nex = self._data.after(self._cursor)
        if nex is not None:
            self._data.delete(nex)
            self._size -= 1

    def __str__(self):
        end = ''
        node = self._data.first()

        for x in range(self._size + 1):
            if node == self._cursor:
                cursor = x
            node = self._data.after(node)
            if node is not None:
                end += node.element()
        end += '\n'
        end += ' ' * cursor + '^'
        return end
Beispiel #7
0
class SortedPriorityQueue(PriorityQueueBase):
    """A min-oriented priority queue implemented with a sorted list"""

    def __init__(self):
        """Create a new empty priority queue"""
        self._data = PositionalList()

    def __len__(self):
        """Return the number of items in the priority queue"""
        return len(self._data)

    def add(self, key, value):
        """Add an item into priority queue"""
        newest = self._Item(key, value)
        walk = self._data.last()
        while walk is not None and newest < walk.element():
            walk = self._data.before(walk)
        if walk is not None:
            self._data.add_after(walk, newest)
        else:
            self._data.add_first(newest)

    def min(self):
        """Return(but do not remove) (k, v) tuple with minimum key"""
        if self.is_empty():
            raise Empty("Priority queue is empty")
        p = self._data.first()
        item = p.element()
        return (item._key, item._value)

    def remove_min(self):
        """Remove and return (k,v) tuple with minimum key"""
        if self.is_empty():
            raise Empty("Priority queue is empty")
        item = self._data.delete(self._data.first())
        return (item._key, item._value)
Beispiel #8
0
def main():
    global screen, SCREEN_WIDTH, SCREEN_SIZE, deltaX, deltaY
    vertex = PositionalList()
    running = True
    done = False
    clickTrue = False
    getPrev = False
    deltaX = 0
    deltaY = 0
    x = 0
    y = 0
    screen.fill((255, 255, 255))
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                (posx, posy) = pygame.mouse.get_pos()
                if done == False and len(vertex) >= 0:
                    if (x - posx)**2 + (y - posy)**2 <= 25:
                        vertex.add_last(vertex.first().element())
                    else:
                        vertex.add_last((posx, posy))
                    if len(vertex) > 1:
                        (x, y) = vertex.first().element()
                        if (x - posx)**2 + (y - posy)**2 <= 25:
                            done = True
                            (posx, posy) = vertex.first().element()
                        p = vertex.before(vertex.last())
                        pygame.draw.line(screen, (0, 0, 0), p.element(),
                                         (posx, posy))
                    pygame.draw.circle(screen, (0, 0, 0), (posx, posy), 5)
                else:
                    p = click(vertex, (posx, posy))
                    if click(vertex, (posx, posy)) != None:
                        clickTrue = not clickTrue
                    # if not clickTrue:
                    #     p=click(vertex,(posx,posy))
                    #     if p!=None:
                    #         clickTrue=True
                    # else:
                    #     (posx,posy)=pygame.mouse.get_pos()
                    #     vertex.add_after(p,(posx,posy))
                    #     screen.fill((255,255,255))
                    #     drawPolygon(vertex)
                    #     p=None
                    #     clickTrue=False
                    #     getPrev=False

            elif event.type == pygame.MOUSEMOTION and clickTrue:
                if done == True:
                    (posx, posy) = pygame.mouse.get_pos()
                    screen.fill((255, 255, 255))
                    vertex.add_after(p, (posx, posy))
                    # if getPrev:
                    #     deltaX=(posx-pPosx)
                    #     deltaY=(posy-pPosy)
                    # updateVertex(vertex,deltaX,deltaY)
                    drawPolygon(vertex)

                    vertex.delete(vertex.after(p))
                    # getPrev=True
                    # (pPosx, pPosy) = (posx,posy)
            elif event.type == pygame.MOUSEBUTTONUP:
                if clickTrue and done:
                    vertex.add_after(p, (posx, posy))
                clickTrue = False
        pygame.display.flip()
def main():
    global screen,SCREEN_WIDTH,SCREEN_SIZE,deltaX,deltaY
    vertex=PositionalList()
    running=True
    done=False
    clickTrue=False
    getPrev=False
    deltaX=0
    deltaY=0
    x=0
    y=0
    screen.fill((255,255,255))
    while running:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                running=False
            elif event.type==pygame.MOUSEBUTTONDOWN:
                (posx,posy)=pygame.mouse.get_pos()
                if done==False and len(vertex)>=0:
                    if (x-posx)**2+(y-posy)**2<=25:
                        vertex.add_last(vertex.first().element())
                    else:
                        vertex.add_last((posx,posy))
                    if len(vertex)>1:
                        (x,y)=vertex.first().element()
                        if (x-posx)**2+(y-posy)**2<=25:
                            done=True
                            (posx,posy)=vertex.first().element()
                        p=vertex.before(vertex.last())
                        pygame.draw.line(screen,(0,0,0),p.element(),(posx,posy))
                    pygame.draw.circle(screen,(0,0,0),(posx,posy),5)
                else:
                    p=click(vertex,(posx,posy))
                    if click(vertex,(posx,posy))!=None:
                        clickTrue=not clickTrue
                    # if not clickTrue:
                    #     p=click(vertex,(posx,posy))
                    #     if p!=None:
                    #         clickTrue=True
                    # else:
                    #     (posx,posy)=pygame.mouse.get_pos()
                    #     vertex.add_after(p,(posx,posy))
                    #     screen.fill((255,255,255))
                    #     drawPolygon(vertex)
                    #     p=None
                    #     clickTrue=False
                    #     getPrev=False

            elif event.type==pygame.MOUSEMOTION and clickTrue:
                    if done==True:
                        (posx,posy)=pygame.mouse.get_pos()
                        screen.fill((255,255,255))
                        vertex.add_after(p,(posx,posy))
                        # if getPrev:
                        #     deltaX=(posx-pPosx)
                        #     deltaY=(posy-pPosy)
                        # updateVertex(vertex,deltaX,deltaY)
                        drawPolygon(vertex)
                        
                        vertex.delete(vertex.after(p))
                        # getPrev=True
                        # (pPosx, pPosy) = (posx,posy)
            elif event.type==pygame.MOUSEBUTTONUP:
                if clickTrue and done:
                    vertex.add_after(p,(posx,posy))
                clickTrue=False
        pygame.display.flip()