快速、符合语言习惯的 Swift 排序集合数据结构。
使用 Swift Package Manager
.package(url: "https://github.com/sciv-img/OSet", from: "0.5.0")
如果您不想或不能使用 SwiftPM,您可以随时复制源代码(只有一个文件)。
OSet
通过实现 SetAlgebra
协议,支持标准 Set
中提供的所有相同操作。例如
1> import OSet
2> let oset1 = OSet([1, 2, 3]); print(oset1)
OSet([1, 2, 3])
3> let oset2 = OSet([3, 4, 5]); print(oset2)
OSet([3, 4, 5])
4> print(oset1.union(oset2))
OSet([1, 2, 3, 4, 5])
5> print(oset1.symmetricDifference(oset2))
OSet([1, 2, 4, 5])
6> print(oset1.intersection(oset2))
OSet([3])
它还实现了 MutableCollection
和 RandomAccessCollection
协议。 这使其可以进行迭代、下标获取/设置、交换、排序等操作。例如
1> import OSet
2> var oset = OSet([3, 1, 2]); print(oset)
OSet([3, 1, 2])
3> print(oset[1])
1
4> oset[1] = 7; print(oset[1])
7
5> oset.swapAt(0, 2); print(oset)
OSet([2, 7, 3])
6> oset.sort(); print(oset)
OSet([2, 3, 7])
7> for item in oset { print(item) }
2
3
7
完整的文档可以直接在源代码中阅读。
可在这里获取。