# swift-collectionsのSortedCollectionsを試す方法

## swift-collectionsを読み込む際にmainブランチを指定する

2024年4月14日現在のswift-collectionsのリリースバージョン1.1.0では、SortedCollectionsが含まれていません（[取り除かれました…][removed]）。そのため、SortedCollectionsを利用するにはリリースブランチではなく、mainブランチを利用する必要があります。

package.swiftで指定する場合は、下記のようにdependenciesでブランチを指定します。

```swift
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "test-sorted",
    dependencies: [
        .package(url: "https://github.com/apple/swift-collections.git", branch: "main"),
    ],
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "test-sorted",
            dependencies: [
                .product(name: "Collections", package: "swift-collections")
            ])
        
    ]
)
```


## 直接 `import SortedCollections` で呼び出す

SortedCollectionsは[安定していない][unstable]、という判断から `import Collections` とするだけでは、SortedCollectionsは利用できません。そのため、SortedCollectionsを使いたい場合は、 `import SortedCollections` とする必要があります。

## SortedSetを使った利用例

### コードの例
```swift
import Collections
import SortedCollections

// Create random array
let randomArray = (1...15).map {_ in Int.random(in:0..<10)}

print("randomArray: \(randomArray)")

// Create different types of Sets
let normalSet = Set(randomArray)
let sortedSet = SortedSet(randomArray)
let orderedSet = OrderedSet(randomArray)

print("normalSet: \(normalSet)")
print("sortedSet: \(sortedSet)")
print("orderedSet: \(orderedSet)")
```

### 出力例

```
randomArray: [1, 4, 3, 6, 4, 3, 2, 5, 5, 0, 2, 3, 2, 1, 3]
normalSet: [4, 3, 0, 1, 6, 2, 5]
sortedSet: [0, 1, 2, 3, 4, 5, 6]
orderedSet: [1, 4, 3, 6, 2, 5, 0]
```

まだ、[b-treesの影響でindexがおかしくなる報告][reported-issue]もありますが、表面上は期待通りの動作をしているようです。


## `import Collections` で読み込まれるようにする方法

余談ですが、 `import Collection` だと読み込まれないのは、[`Sources/Collections/Collections.swift`][collections.swift]に追加されていないからです。ファイルを確認するとSoretedCollectionsとは別にRopeModuleもまだAPIが定まっていないという理由で正式にリリースされていないとの記載があります。

swift-collectionsを[フォークして編集][experimental-fork]したら、 `import Collections` だけでも読み込めるようになりました。

お試しになりたい方は先ほどと同様に、package.swiftのdependenciesで下記のように指定してください。

```swift
.package(url: "https://github.com/tockrock/swift-collections.git", branch: "exp-sorted-collections"),
```

早くSortedCollectionsが正式にリリースされて、この記事が不要になるといいですね。

[removed]: https://github.com/apple/swift-collections/pull/205
[unstable]: https://github.com/apple/swift-collections/blob/ca8b4ab855f4b8075c1fd29eb50db756b1688e61/README.md?plain=1#L36
[reported-issue]: https://github.com/apple/swift-collections/issues/138
[collections.swift]: https://github.com/apple/swift-collections/blob/ca8b4ab855f4b8075c1fd29eb50db756b1688e61/Sources/Collections/Collections.swift
[experimental-fork]: https://github.com/tockrock/swift-collections/commit/76fcbd36d7ed6e09647b5902eeb4187ed1494cd7

