Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
3e0a3faa
Kaydet (Commit)
3e0a3faa
authored
Eki 09, 2011
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Clean-up and improve the priority queue example in the heapq docs.
üst
fc45bbac
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
25 deletions
+25
-25
heapq.rst
Doc/library/heapq.rst
+25
-25
No files found.
Doc/library/heapq.rst
Dosyayı görüntüle @
3e0a3faa
...
@@ -187,36 +187,36 @@ changes to its priority or removing it entirely. Finding a task can be done
...
@@ -187,36 +187,36 @@ changes to its priority or removing it entirely. Finding a task can be done
with a dictionary pointing to an entry in the queue.
with a dictionary pointing to an entry in the queue.
Removing the entry or changing its priority is more difficult because it would
Removing the entry or changing its priority is more difficult because it would
break the heap structure invariants. So, a possible solution is to mark an
break the heap structure invariants. So, a possible solution is to mark the
entry as invalid and optionally add a new entry with the revised priority::
existing entry as removed and add a new entry with the revised priority::
pq = [] # the priority queue list
pq = [] # list of entries arranged in a heap
counter = itertools.count(1) # unique sequence count
entry_finder = {} # mapping of tasks to entries
task_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>' # placeholder for a removed task
INVALID = 0 # mark an entry as deleted
counter = itertools.count() # unique sequence count
def add_task(priority, task, count=None):
def add_task(task, priority=0):
if count is None:
'Add a new task or update the priority of an existing task'
count = next(counter)
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry = [priority, count, task]
task
_finder[task] = entry
entry
_finder[task] = entry
heappush(pq, entry)
heappush(pq, entry)
def get_top_priority():
def remove_task(task):
while True:
'Mark an existing task as REMOVED. Raise KeyError if not found.'
entry = entry_finder.pop(task)
entry[-1] = REMOVED
def pop_task():
'Remove and return the lowest priority task. Raise KeyError if empty.'
while pq:
priority, count, task = heappop(pq)
priority, count, task = heappop(pq)
if
count is not INVALI
D:
if
task is not REMOVE
D:
del
task
_finder[task]
del
entry
_finder[task]
return task
return task
raise KeyError('pop from an empty priority queue')
def delete_task(task):
entry = task_finder[task]
entry[1] = INVALID
def reprioritize(priority, task):
entry = task_finder[task]
add_task(priority, task, entry[1])
entry[1] = INVALID
Theory
Theory
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment