二进制堆优先级队列

bhpq的Python项目详细描述


HitCountGitHub contributorsVersionLicenseBuild StatusDownloads

bhpq-二进制堆优先级队列

二进制堆优先级队列实现,线程安全

安装

您可以从PyPI

安装bhpq
pip install bhpq

python 3.7支持bhpq

用法

from bhpq import BinaryHeapPriorityQueue

# The BinaryHeapPriorityQueue constructor takes two input params:

# - prefer (required param)
#    the preferred object is pushed to the top of the queue
# the prefer input is a lambda function eg:
# prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs)

# - size
#    The initial size allocation of the queue, default value is 10

示例

class Node(object):
def __init__(self, val):
    self.val = val

A = BinaryHeapPriorityQueue(
        prefer=(lambda lhs, rhs: lhs if lhs.val >= rhs.val else rhs), size=5
    )

A.add(Node(1))
A.add(Node(4))
A.add(Node(3))
A.add(Node(5))
A.add(Node(2))

assert 5 == A.pop().val
assert 4 == A.pop().val
assert 3 == A.pop().val
assert 2 == A.pop().val
assert 1 == A.pop().val
assert None == A.pop()

方法

  • size()

返回优先级队列的当前大小

  • peek()

返回优先级队列顶部的对象,如果存在,则返回No.<

  • pop()

如果存在优先级,则移除并返回优先级队列顶部的对象,否则返回NO/

  • add(val)

将元素添加到优先级队列

维护人员

Aayush Uppal

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在表被注释到配置之前获取表的元数据?   java滚动条不会出现在JList上   java JOGL监视器GPU内存   java为什么要使用RecyclerView onDraw延迟   java定制Oppo Reno 2 Z CPH1951(手机型号)的固件(闪存文件)   java自定义线程池执行器   java如何解决发布版本中重复的jar条目[com/安卓/volley/R.class]?   java如何使用Bukkit API触发事件?   java在blazemeter jmeter RTE插件中使用ctrl+w输入   C#/Visual Studio的java JDT等价物   java为什么当maxread值很大而收到的消息数量很小时,卡夫卡消费者会无限期消费?   java游戏2。x:包含模板列表的绑定模型   带压缩的java日志旋转   运行时。exec用java运行程序读取它正在做什么