基于跳表的排序集合
项目描述
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)(退化的跳表)
>>> 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作为键
items、keys 和 values 是视图,并接受 start_key 和 reverse 参数
跳表集
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
版权
原始版本 - 版权所有 (C) 2013 David Wilson
版权所有 (C) 2013 Jakub Stasiak
本源代码遵循 MIT 许可协议,详情请参阅 LICENSE 文件。
项目详情
skiplistcollections-0.0.6.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f7099783b6b521369f44fe6a5320bb951fd567bed61bf914c7f09de61b446387 |
|
MD5 | 0f0909016e62264860a88cce892c6dbd |
|
BLAKE2b-256 | c95095269ccd2fef8580aa96f32e0a9af558361b1259535ab1c926686ef36d7c |
skiplistcollections-0.0.6-py2.py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e4fc2077d2e9fede118efbb27a2a0bd23b789f998b5c62577879793c532594e2 |
|
MD5 | 0e4aa4977d9782decfe6cb801cbfbebc |
|
BLAKE2b-256 | 0649e701126c3af55599d65fd04053676957398a9249d054c9c009cca3eda267 |