本文整理汇总了Python中heapq._siftdown方法的典型用法代码示例。如果您正苦于以下问题:Python heapq._siftdown方法的具体用法?Python heapq._siftdown怎么用?Python heapq._siftdown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类heapq
的用法示例。
在下文中一共展示了heapq._siftdown方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: newPeer
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import _siftdown [as 别名]
def newPeer(self, ipp, seen):
# Called by PeerAddressManager
try:
p = self.peers[ipp]
except KeyError:
p = self.peers[ipp] = self.PeerInfo(ipp, seen)
heapq.heappush(self.heap, p)
self.scheduleInitRequest()
else:
if seen > p.seen:
p.seen = seen
# Bubble it up the heap.
# This takes O(n) and uses an undocumented heapq function...
if p.inheap:
heapq._siftdown(self.heap, 0, self.heap.index(p))
示例2: removeNumFromHeap
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import _siftdown [as 别名]
def removeNumFromHeap(self, heap, element):
idx = heap.index(element) # find the element
heap[idx] = heap[-1] # replace the last to the element to be deleted
del heap[-1] # delete the last element
# we can use heapify to readjust the elements but that would be O(N),
# instead, we will adjust only one element which will O(logN)
if idx < len(heap):
heapq._siftup(heap, idx)
heapq._siftdown(heap, 0, idx)
示例3: _rank_cycle_function
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import _siftdown [as 别名]
def _rank_cycle_function(self, cycle, function, ranks):
"""Dijkstra's shortest paths algorithm.
See also:
- http://en.wikipedia.org/wiki/Dijkstra's_algorithm
"""
import heapq
Q = []
Qd = {}
p = {}
visited = set([function])
ranks[function] = 0
for call in compat_itervalues(function.calls):
if call.callee_id != function.id:
callee = self.functions[call.callee_id]
if callee.cycle is cycle:
ranks[callee] = 1
item = [ranks[callee], function, callee]
heapq.heappush(Q, item)
Qd[callee] = item
while Q:
cost, parent, member = heapq.heappop(Q)
if member not in visited:
p[member]= parent
visited.add(member)
for call in compat_itervalues(member.calls):
if call.callee_id != member.id:
callee = self.functions[call.callee_id]
if callee.cycle is cycle:
member_rank = ranks[member]
rank = ranks.get(callee)
if rank is not None:
if rank > 1 + member_rank:
rank = 1 + member_rank
ranks[callee] = rank
Qd_callee = Qd[callee]
Qd_callee[0] = rank
Qd_callee[1] = member
heapq._siftdown(Q, 0, Q.index(Qd_callee))
else:
rank = 1 + member_rank
ranks[callee] = rank
item = [rank, member, callee]
heapq.heappush(Q, item)
Qd[callee] = item
示例4: heap_operations
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import _siftdown [as 别名]
def heap_operations():
heap = [4, 2, 1, 3]
# heapify
heapq.heapify(heap)
# top
top = heap[0]
# heappop
top = heapq.heappop(heap)
# heappush
heapq.heappush(heap, 5)
# heappushpop = push + pop
heapq.heappushpop(heap, 0)
# heapreplace = pop + push
heapq.heapreplace(heap, 0)
data = [10, 5, 18, 2, 37, 3, 8, 7, 19, 1]
heapq.heapify(data)
old, new = 8, 22 # increase the 8 to 22
i = data.index(old)
data[i] = new
# _siftup, from root to leaf, when increase
heapq._siftup(data, i)
old, new = 10, 4 # decrease the 10 to 4
i = data.index(old)
data[i] = new
# _siftdown, from leaf to root, when decrease
heapq._siftdown(data, 0, i)
# find n largest by queue
heapq.nlargest(data, 3)
# find n smallest by queue
heapq.nsmallest(data, 3)
# Merge multiple sorted inputs into a single sorted output
# e.g. merge timestamped entries from multiple log files
heapq.merge([1, 3, 5, 7], [0, 2, 4, 8], [5, 10, 15, 20], [], [25])