Esempio n. 1
0
    def enqueue(self, v, p):
        """Enqueue (v, p) entry into priority queue."""
        if self.N == len(self.storage) - 1:
            self.resize(self.N * 2)
        self.N += 1

        self.storage[self.N] = Entry(v, p)
        self.swim(self.N)
Esempio n. 2
0
    def enqueue(self, v, p):
        """Enqueue (v, p) entry into priority queue."""
        if self.N == self.size:
            raise RuntimeError('Priority Queue is Full!')
        self.N += 1
        if self.N > _sums[self.level + 1]:
            self.level += 1

        self.storage[self.N] = Entry(v, p)
        self.swim(self.N)
Esempio n. 3
0
    def enqueue(self, v, p):
        """Enqueue (v, p) entry into priority queue."""
        if self.N == self.size:
            raise RuntimeError('Priority Queue is Full!')

        to_add = Entry(v, p)
        idx = binary_array_search(self.storage, self.N - 1, p)
        if idx < 0:
            self.storage.insert(-idx - 1, to_add)
        else:
            self.storage.insert(idx, to_add)

        self.N += 1
Esempio n. 4
0
    def enqueue(self, v, p):
        """Enqueue (v, p) entry into priority queue."""
        if self.N == self.size:
            raise RuntimeError('Priority Queue is Full!')
        idx = binary_array_search(self.storage, self.N - 1, p)

        if idx < 0:  # might be duplicate,  might not...
            idx = -idx - 1

        # move each element up to make room
        for i in range(self.N, idx, -1):
            self.storage[i] = self.storage[i - 1]
        self.storage[idx] = Entry(v, p)

        self.N += 1
Esempio n. 5
0
def heap_enqueue_animation():
    """Show changes to storage with each swim()."""
    from ch04.entry import Entry

    heap = initial_heap()
    heap.N += 1
    heap.storage[heap.N] = Entry(12, 12)
    fig_num = 8
    print('Fig. 4-{:<2d} : '.format(fig_num),' -- |' + '|'.join([' {:>3} '.format(e.priority) for e in heap.storage[1:heap.N+1]]))
    print()
    child = heap.N
    while child > 1 and heap.less(child//2, child):
        heap.swap(child, child//2)
        child = child // 2
        fig_num += 1
        print('Fig. 4-{:<2d} : '.format(fig_num),' -- |' + '|'.join([' {:>3} '.format(e.priority) for e in heap.storage[1:heap.N+1]]))
Esempio n. 6
0
 def enqueue(self, v, p):
     """Enqueue (v, p) entry into priority queue."""
     if len(self.storage) == self.size:
         raise RuntimeError('Priority Queue is Full!')
     self.storage.append(Entry(v, p))
Esempio n. 7
0
 def enqueue(self, v, p):
     """Enqueue (v, p) entry into priority queue."""
     if self.N == self.size:
         raise RuntimeError('Priority Queue is full!')
     self.storage[self.N] = Entry(v, p)
     self.N += 1
Esempio n. 8
0
 def test_entry(self):
     from ch04.entry import Entry
     e = Entry('99', 101)
     self.assertEqual('[99 p=101]', str(e))