Skip to content

[6.0][Basics] Switch IdentifiableSet to use OrderedDictionary #7634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Sources/Basics/Collections/IdentifiableSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

import struct OrderedCollections.OrderedDictionary

/// Replacement for `Set` elements that can't be `Hashable`, but can be `Identifiable`.
public struct IdentifiableSet<Element: Identifiable>: Collection {
public init() {
Expand All @@ -20,7 +22,7 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
self.storage = .init(pickLastWhenDuplicateFound: sequence)
}

fileprivate typealias Storage = [Element.ID: Element]
fileprivate typealias Storage = OrderedDictionary<Element.ID, Element>

public struct Index: Comparable {
public static func < (lhs: IdentifiableSet<Element>.Index, rhs: IdentifiableSet<Element>.Index) -> Bool {
Expand All @@ -33,23 +35,23 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
private var storage: Storage

public var startIndex: Index {
Index(storageIndex: storage.startIndex)
Index(storageIndex: self.storage.elements.startIndex)
}

public var endIndex: Index {
Index(storageIndex: storage.endIndex)
Index(storageIndex: self.storage.elements.endIndex)
}

public subscript(position: Index) -> Element {
self.storage[position.storageIndex].value
self.storage.elements[position.storageIndex].value
}

public subscript(id: Element.ID) -> Element? {
self.storage[id]
}

public func index(after i: Index) -> Index {
Index(storageIndex: self.storage.index(after: i.storageIndex))
Index(storageIndex: self.storage.elements.index(after: i.storageIndex))
}

public func union(_ otherSequence: some Sequence<Element>) -> Self {
Expand Down Expand Up @@ -88,14 +90,14 @@ public struct IdentifiableSet<Element: Identifiable>: Collection {
}
}

extension Dictionary where Value: Identifiable, Key == Value.ID {
extension OrderedDictionary where Value: Identifiable, Key == Value.ID {
fileprivate init(pickLastWhenDuplicateFound sequence: some Sequence<Value>) {
self.init(sequence.map { ($0.id, $0) }, uniquingKeysWith: { $1 })
}
}

extension IdentifiableSet: Equatable {
public static func ==(_ lhs: Self, _ rhs: Self) -> Bool {
public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
lhs.storage.keys == rhs.storage.keys
}
}
Expand Down