mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
I have a linked list prototype that can support comparable indices (and an O(1) `count`); accordingly, reinstate the requirement for index comparability. This eliminates a potential performance footgun for custom containers that define their own Index, as an incomparable index would make `func distance` 2x slower.
53 lines
1.3 KiB
Swift
53 lines
1.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension PermutableContainer
|
|
where Self: BidirectionalContainer, Element: ~Copyable
|
|
{
|
|
@inlinable
|
|
public mutating func reverse() {
|
|
var i = self.startIndex
|
|
var j = self.endIndex
|
|
while i != j {
|
|
self.formIndex(before: &j)
|
|
guard i != j else { break }
|
|
self.swapAt(i, j)
|
|
self.formIndex(after: &i)
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension PermutableContainer
|
|
where
|
|
Self: BidirectionalContainer,
|
|
Element: ~Copyable
|
|
{
|
|
@inlinable
|
|
public mutating func reverseSubrange(_ range: Range<Index>) {
|
|
var i = range.lowerBound
|
|
var j = range.upperBound
|
|
while i != j {
|
|
self.formIndex(before: &j)
|
|
guard i != j else { break }
|
|
self.swapAt(i, j)
|
|
self.formIndex(after: &i)
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|