mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
This is currently conditional on the UnstableContainersPreview trait. (Swift 6.4 has not shipped yet, so we cannot unconditionally enable this without risking breakage.)
97 lines
2.9 KiB
Swift
97 lines
2.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 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.2)
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension Span where Element: ~Copyable {
|
|
@_lifetime(copy self)
|
|
@_alwaysEmitIntoClient
|
|
package mutating func _trim(first maxLength: Int) -> Self {
|
|
precondition(maxLength >= 0, "Cannot have a prefix of negative length")
|
|
let cut = Swift.min(maxLength, count)
|
|
guard cut > 0 else { return .init() }
|
|
let result = self.extracting(first: cut)
|
|
self = self.extracting(droppingFirst: cut)
|
|
return result
|
|
}
|
|
|
|
@_lifetime(copy self)
|
|
@_alwaysEmitIntoClient
|
|
package mutating func _trim(last maxLength: Int) -> Self {
|
|
precondition(maxLength >= 0, "Cannot have a suffix of negative length")
|
|
let cut = Swift.min(maxLength, count)
|
|
guard cut > 0 else { return .init() }
|
|
let result = self.extracting(last: cut)
|
|
self = self.extracting(droppingLast: cut)
|
|
return result
|
|
}
|
|
}
|
|
|
|
#if compiler(>=6.4) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension Span where Element: Equatable & ~Copyable {
|
|
@_alwaysEmitIntoClient
|
|
package func _elementsEqual(to other: borrowing Self) -> Bool {
|
|
return self.withUnsafeBufferPointer { a in
|
|
other.withUnsafeBufferPointer { b in
|
|
guard a.count == b.count else { return false }
|
|
guard a.baseAddress != b.baseAddress else { return true }
|
|
var i = 0
|
|
while i < self.count {
|
|
guard a[i] == b[i] else { return false }
|
|
i &+= 1
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#else
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension Span where Element: Equatable /* & ~Copyable */ {
|
|
@_alwaysEmitIntoClient
|
|
package func _elementsEqual(to other: borrowing Self) -> Bool {
|
|
return self.withUnsafeBufferPointer { a in
|
|
other.withUnsafeBufferPointer { b in
|
|
guard a.count == b.count else { return false }
|
|
guard a.baseAddress != b.baseAddress else { return true }
|
|
var i = 0
|
|
while i < self.count {
|
|
guard a[i] == b[i] else { return false }
|
|
i &+= 1
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension Span where Element: Hashable /* & ~Copyable */ {
|
|
@_alwaysEmitIntoClient
|
|
package func _hashContents(into hasher: inout Hasher) {
|
|
// Note: no discriminating combine call -- caller is expected to do that
|
|
// separately when needed.
|
|
var i = 0
|
|
while i < self.count {
|
|
hasher.combine(self[unchecked: i])
|
|
i &+= 1
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endif
|