SplayDict
以 Swift 编写的 SplayDict 是一种像特殊有序的 集合,基于 Splay 树 响应搜索输入数据的倾向。
什么是 SplayDict?
SplayDict 是使用 splay 树实现的 Dictionary,而不是红色-黑色树。由于其 splay 树属性,频繁搜索的数据能以非常快的速度获得。
在现实世界中,有很多搜索请求。很多人请求相同或类似的数据。每一次请求,每一次新的搜索。都非常消耗时间。
使用 SplayDict,不需要浪费时间。频繁搜索的数据能够以非常短的时间获得。(但偶尔搜索的数据可能需要更多一点时间。)根据 帕累托定律,使用 SplayDict 可以使程序更高效,甚至更快。
待办事项
- SplayTree
- flatMap
- SplayDict to Json
- 下标
- 更多方法
- 采用协议,如 CustomStringConvertible、Sequence 等。
用法
- 声明
var a: SplayDict<Int, Double> = SplayDict() // make a empty splay set.
var b = SplayDict([1, 3, 2]) // make a set which contains 1, 2, 3.
// and more constructor is building...
- 插入
var a: SplayDict<Int, Int> = SplayDict()
a.insert(key: 3, value: 7)
// Insert [3 : 7] into SplayDict.
a[5] = 12
// Insert [5 : 12] into SplayDict.
// If you want to insert key which is already exists,
// then being value is replaced with newValue.
- 顶部
_ = a.top
// Return a top element of SplayDict.
// It's a latest searched data.
// Read-only property
- 查找
var a: SplayDict<Int, String> = SplayDict()
let value: String? = a.find(key: 7)
// Return value if search value exists, else return nil.
// And this make searched key(or nearest key) be on a top of SplayDict.
- 删除
a.delete(key: 5)
// Delete element corresponded with key in SplayDict.
// Make a biggest key under input be a top element.
- indexOf
var a: SplayDict<Int, Double> = SplayDict()
let value: Double? = a.indexOf(3)
// Return n-th value if SplayDict.
// If index is out of range, returns nil.
- 反转
a.reverse(from: 3, to: 6)
// Reverse elements. (from 3 to 6)
// Implement using Lazy Propagtion.
// example
// Let splay set consists of [0, 1, 2, 3, 4, 5, 6].
// reverse(from: 3, to : 6).
// The result is [0, 1, 2, 6, 5, 4, 3].
- iOS 10.0+
- Xcode 9+
pod 'SplayDict'
QuqqU(KiUng Jung)
版权 QuqqU
(KiUng Jung) 2018 ~
SplayDict 在 MIT
许可下可用。有关更多信息,请参阅 LICENSE 文件。