Skip to main content

Command Palette

Search for a command to run...

swift-collectionsのSortedCollectionsを試す方法

Updated
1 min read

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

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

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

// 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は安定していない、という判断から import Collections とするだけでは、SortedCollectionsは利用できません。そのため、SortedCollectionsを使いたい場合は、 import SortedCollections とする必要があります。

SortedSetを使った利用例

コードの例

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がおかしくなる報告もありますが、表面上は期待通りの動作をしているようです。

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

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

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

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

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

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

More from this blog

Swift 5.9からの `swift package init` の変更点

先日、 swift package init コマンドを使った際にswift 5.9に合わせた変更点に気づきました。 情報があまりなく、私自身が戸惑ったこともあり、コマンドを実行する時の--typeを基準にどういった変更があったのか概要をまとめたいと思います。 Init template cleanup #6144 Swift Fromsでの議論によると、狙いとしてはシンプルなユースケースへの最適化にあるようです。 system-module、manifestと、empty これまであった、 s...

Dec 25, 20231 min read

アルゴ式: 961 Q4. 二部グラフ判定 (DFS ver.) (Swift)

Q4. 二部グラフ判定 (DFS ver.) なんとか毎日問題は解いていたけれども、ブログは空いてしまった Bool?に対してSwitch文を書こうとしたけれども、Optionalの場合は、.some()を挟むというので方針を変更 提出したコードはその名残が残ってしまった 提出はしていないけれども簡単な修正もした このブログを書いている際に、改めてSwitch分で書いたりもした(1, 2) 提出 AC

Jun 18, 20221 min read

Continuous Tumbling

123 posts

Learning Swift/Swift UI, and competitive programming. (he/him)