Cloche 0.1.3

Cloche 0.1.3

Yoshinori Atarashi 维护。



Cloche 0.1.3

  • Yoshinori Atarashi

Cloche

Swift >=5.0 Platform License Carthage compatible CocoaPods Compatible Build Status codecov

Cloche 是一个用于排序集合的纯 Swift 库。

特性

  • Cloche 提供了 SortedSet 和 SortedDictionary,这些与 Swift 标准库中的 Set 和 Dictionary 接口几乎相同。
  • Cloche.SortedSet 和 Cloche.SortedDictionary 使用红黑树实现,因此可以在对数时间内执行插入、搜索、删除操作。

性能比较

macOS

描述
操作系统 macOS Mojave 10.14.6
处理器 Core i5 8259U
Swift 5.0.1 (Xcode 10.3)

Insertion Performance Comparison under macOS

Search Performance Comparison under macOS

Deletion Performance Comparison under macOS

Ubuntu

描述
操作系统 Ubuntu 18.04
处理器 Core i9 9900K
Swift 5.0.1

Insertion Performance Comparison under Ubuntu

Search Performance Comparison under Ubuntu

Deletion Performance Comparison under Ubuntu

要求

  • iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+ / Ubuntu 16.04+
  • Xcode 10.2+
  • Swift 5.0+

安装

CocoaPods

在您的 Podfile 文件中添加以下行:

pod 'Cloche'

Carthage

在您的 Cartfile 文件中添加以下行:

github "atarashi-y/Cloche"

Swift Package Manager

要将 Cloche 集成到您的项目中,请在您的 Package.swift 文件中指定它:

let package = Package(
    name: "YourProject",
    dependencies: [
        .package(
            url: "https://github.com/atarashi-y/Cloche.git",
            from: "0.1.3")
    ])

示例

SortedSet

import Cloche

var s1: SortedSet = [5, 2, 3, 4]
print(s1) // [2, 3, 4, 5]

s1.insert(1)
print(s1[s1.startIndex]) // 1
print(s1) // [1, 2, 3, 4, 5]

print(s1.remove(5)!) // 5
print(s1.last!) // 4
print(s1) // [1, 2, 3, 4]

let s2: SortedSet = [3, 7, 1, 9]
print(s1.union(s2)) // [1, 2, 3, 4, 7, 9]

SortedDictionary

import Cloche

let countries = ["Singapore", "Canada", "Sweden", "Egypt", "Croatia"]
var d1 = SortedDictionary(grouping: countries) {
    country in String(country.first!)
}
print(d1["C"]!)	// ["Canada", "Croatia"]
print(d1) // ["C": ["Canada", "Croatia"], "E": ["Egypt"], "S": ["Singapore", "Sweden"]]

d1["G", default: []].append("Greece")
d1["E", default: []].append("Ecuador")
print(d1) // ["C": ["Canada", "Croatia"], "E": ["Egypt", "Ecuador"], "G": ["Greece"], "S": ["Singapore", "Sweden"]]

let d2 = d1.compactMapValues {
    v in v.count > 1 ? v.map { $0.uppercased() } : nil
}
print(d2) // ["C": ["CANADA", "CROATIA"], "E": ["EGYPT", "ECUADOR"], "S": ["SINGAPORE", "SWEDEN"]]

许可

Cloche 采用 Apache-2.0 许可证发布。详情请见 LICENSE