Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 15, 2024
1 parent c0021e5 commit 189031b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ extension IdentifiedArray {
/// implements high-quality hashing.
@inlinable
@_disfavoredOverload
public init<S>(
uncheckedUniqueElements elements: S,
public init(
uncheckedUniqueElements elements: some Sequence<Element>,
id: KeyPath<Element, ID>
)
where S: Sequence, S.Element == Element {
) {
self.init(
id: id,
_id: { $0[keyPath: id] },
Expand All @@ -46,11 +45,10 @@ extension IdentifiedArray {
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
/// implements high-quality hashing.
@inlinable
public init<S>(
public init<S: Sequence<Element>>(
uniqueElements elements: S,
id: KeyPath<Element, ID>
)
where S: Sequence, S.Element == Element {
) {
if S.self == Self.self {
self = elements as! Self
return
Expand Down Expand Up @@ -82,11 +80,11 @@ extension IdentifiedArray {
/// - Returns: A new array initialized with the unique elements of `elements`.
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
/// implements high-quality hashing.
public init<S: Sequence>(
_ elements: S,
public init(
_ elements: some Sequence<Element>,
id: KeyPath<Element, ID>,
uniquingIDsWith combine: (Element, Element) throws -> Element
) rethrows where S.Element == Element {
) rethrows {
try self.init(
id: id,
_id: { $0[keyPath: id] },
Expand Down Expand Up @@ -146,7 +144,7 @@ extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
/// implements high-quality hashing.
@inlinable
@_disfavoredOverload
public init<S>(uncheckedUniqueElements elements: S) where S: Sequence, S.Element == Element {
public init(uncheckedUniqueElements elements: some Sequence<Element>) {
self.init(
id: \.id,
_id: { $0.id },
Expand All @@ -166,7 +164,7 @@ extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
/// implements high-quality hashing.
@inlinable
public init<S>(uniqueElements elements: S) where S: Sequence, S.Element == Element {
public init<S: Sequence<Element>>(uniqueElements elements: S) {
if S.self == Self.self {
self = elements as! Self
return
Expand Down Expand Up @@ -198,10 +196,10 @@ extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
/// - Complexity: Expected O(*n*) on average, where *n* is the count of elements, if `ID`
/// implements high-quality hashing.
@inlinable
public init<S: Sequence>(
_ elements: S,
public init(
_ elements: some Sequence<Element>,
uniquingIDsWith combine: (Element, Element) throws -> Element
) rethrows where S.Element == Element {
) rethrows {
try self.init(
id: \.id,
_id: { $0.id },
Expand All @@ -217,15 +215,15 @@ extension IdentifiedArray where Element: Identifiable, ID == Element.ID {

extension IdentifiedArray {
@available(*, deprecated, renamed: "init(uniqueElements:id:)")
public init<S>(_ elements: S, id: KeyPath<Element, ID>) where S: Sequence, S.Element == Element {
public init(_ elements: some Sequence<Element>, id: KeyPath<Element, ID>) {
self.init(uniqueElements: elements, id: id)
}
}

@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
@available(*, deprecated, renamed: "init(uniqueElements:)")
public init<S>(_ elements: S) where S: Sequence, S.Element == Element {
public init(_ elements: some Sequence<Element>) {
self.init(uniqueElements: elements)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ extension IdentifiedArray {
/// - Complexity: The operation is expected to perform amortized O(1) copy, hash, and compare
/// operations on the `Element` type, if it implements high-quality hashing.
@inlinable
public mutating func append<S>(contentsOf newElements: S)
where Element == S.Element, S: Sequence {
public mutating func append(contentsOf newElements: some Sequence<Element>) {
self.reserveCapacity(self.count + newElements.underestimatedCount)
for element in newElements {
self.append(element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ where Element: Identifiable, ID == Element.ID {
}

@inlinable
public mutating func replaceSubrange<C: Collection>(_ subrange: Range<Int>, with newElements: C)
where C.Element == Element {
public mutating func replaceSubrange(
_ subrange: Range<Int>, with newElements: some Collection<Element>
) {
self._dictionary.removeSubrange(subrange)
self._dictionary.reserveCapacity(self.count + newElements.count)
for element in newElements.reversed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ import OrderedCollections
/// should not be mutated in place, as it will drift from its associated dictionary key. Identified
/// array is designed to avoid this invariant, with the exception of its *id-based* subscript.
/// Mutating an element's id will result in a runtime error.
public struct IdentifiedArray<ID, Element> where ID: Hashable {
public struct IdentifiedArray<ID: Hashable, Element> {
public let id: KeyPath<Element, ID>

// NB: Captures identity access. Direct access to `Identifiable`'s `.id` property is faster than
Expand Down

0 comments on commit 189031b

Please sign in to comment.