跳转到主要内容

基于跳表的排序集合

项目描述

Build status

skiplistcollections 是一个包含基于跳表的排序集合的Python模块。 skiplistcollections 使用Python编写,并与

  • CPython 2.6+,3.2+

  • PyPy 1.9+

GitHub上的项目页面: https://github.com/jstasiak/skiplistcollections

PyPI上的项目页面: https://pypi.python.org/pypi/skiplistcollections

跳表字典

SkipListDict 是一个提供类似于字典的接口并使用跳表实现的容器。它按键永久排序。

  • 遍历容器(从任何键开始,支持逆序)是 O(n)

  • 获取、设置和删除任意键的平均时间为 O(log n),最坏情况下的时间为 O(n)(退化的跳表)

有关详细信息,请参阅 http://pythonsweetness.tumblr.com/post/45227295342/fast-pypy-compatible-ordered-map-in-89-lines-of-python

>>> from skiplistcollections import SkipListDict
>>> things = SkipListDict(capacity=16)
>>> len(things)
0
>>> things['x'] = 1
>>> things.setdefault('x', 'DEFAULT')
1
>>> 'x' in things
True
>>> len(things)
1
>>> things['g'] = 2
>>> things['z'] = 3
>>> tuple(things.keys())
('g', 'x', 'z')
>>> tuple(things.values())
(2, 1, 3)
>>> tuple(things.items())
(('g', 2), ('x', 1), ('z', 3))
>>> tuple(things.items(start_key='x'))
(('x', 1), ('z', 3))
>>> tuple(things.items(start_key='x', reverse=True))
(('x', 1), ('g', 2))
>>> del things['z']
>>> things.update({'a': 'A', 'b': 'B'})
>>> len(things)
4
>>> things
SkipListDict({'a': 'A', 'b': 'B', 'g': 2, 'x': 1}, capacity=16)

如您所见,SkipListDict 非常接近Python字典接口。事实上,它继承了 MutableMapping 抽象基类。

当然也有一些差异

  • 在创建时需要设置最大字典大小

  • 尚未支持使用其他映射进行初始化

  • 不能使用None作为键

  • itemskeysvalues 是视图,并接受 start_keyreverse 参数

跳表集

SkipListSet 是使用跳表实现的集合。它永久按键排序。

  • 迭代容器的时间复杂度为 O(n)

  • 添加、删除以及检查键是否存在于容器中的平均时间复杂度为 O(log n),最坏情况为 O(n)(跳表退化为链表)

>>> from skiplistcollections import SkipListSet
>>> things = SkipListSet(capacity=16)
>>> len(things)
0
>>> things.add(3)
>>> len(things)
1
>>> things.add(1)
>>> things.add(4)
>>> things
SkipListSet((1, 3, 4), capacity=16)
>>> tuple(things)
(1, 3, 4)
>>> things.remove(2)
Traceback (most recent call last):
KeyError: 2

变更

0.0.6

  • 修复了 SkipListDict 在 start_key 未找到时返回过多项的问题(GitHub 问题 #1)

0.0.5

  • 修复了 SkipListDict 的 repr

  • 创建了 SkipListSet

0.0.4

  • 在视图 repr 中包含了 start_key 和 reverse 值

  • 改进了 README

0.0.3

  • items()values()keys() 现在返回视图

0.0.2

  • 改进了 README

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

skiplistcollections-0.0.6.tar.gz (4.8 kB 查看哈希)

上传时间

构建分布

skiplistcollections-0.0.6-py2.py3-none-any.whl (6.9 kB 查看哈希)

上传时间 Python 2 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面