Cloche

Sorted collections written in pure Swift.


Keywords
dictionary, macos, red-black-tree, red-black-trees, redblacktree, set, sorted-collections, sorted-dictionary, sorted-sets, sortedset, swift, swift-library, ubuntu
Licenses
Apache-2.0/libpng-2.0
Install
pod try Cloche

Documentation

Cloche

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

Cloche is a pure Swift library for sorted collections.

Features

  • Cloche provides SortedSet and SortedDictionary, these have almost the same interfaces as Set and Dictionary in the Swift standard Library, respectively.
  • Cloche.SortedSet and Cloche.SortedDictionary are implemented using Red-Black Tree, so can perform insertion, search, deletion operations in logarithmic time.

Performance Comparisons

macOS

Description
OS macOS Mojave 10.14.6
CPU 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

Description
OS Ubuntu 18.04
CPU Core i9 9900K
Swift 5.0.1

Insertion Performance Comparison under Ubuntu

Search Performance Comparison under Ubuntu

Deletion Performance Comparison under Ubuntu

Requirements

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

Installation

CocoaPods

Add the following line to your Podfile:

pod 'Cloche'

Carthage

Add the following line to your Cartfile:

github "atarashi-y/Cloche"

Swift Package Manager

To integrate Cloche into your project, specify it in your Package.swift file:

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

Example

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"]]

License

Cloche is released under the Apache-2.0 license. See LICENSE for details.