mirror of
https://github.com/apple/swift-collections
synced 2026-11-23 04:24:23 +00:00
Package traits already pass defines with their trait name when building. Instead of adding our own conditional defines based on the traits we can use the trait provided defines directly. I observed that various tooling behaves better with the trait provided defines.
146 lines
3.9 KiB
Swift
146 lines
3.9 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 5.0, *)
|
|
public protocol RandomAccessContainer<Element>
|
|
: BidirectionalContainer, ~Copyable, ~Escapable
|
|
where Element: ~Copyable {}
|
|
|
|
extension RandomAccessCollection {
|
|
@inlinable
|
|
public func formIndex(
|
|
_ index: inout Index, offsetBy n: inout Int, limitedBy limit: Index
|
|
) {
|
|
let l = self.distance(from: index, to: limit)
|
|
if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
|
|
index = limit
|
|
n -= l
|
|
return
|
|
}
|
|
formIndex(&index, offsetBy: n)
|
|
n = 0
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RandomAccessContainer
|
|
where Self: ~Copyable & ~Escapable, Index: Strideable, Index.Stride == Int
|
|
{
|
|
@inlinable
|
|
public var isEmpty: Bool { startIndex == endIndex }
|
|
|
|
@inlinable
|
|
public var count: Int { startIndex.distance(to: endIndex) }
|
|
|
|
@inlinable
|
|
public func index(after index: Index) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: 1)
|
|
}
|
|
|
|
@inlinable
|
|
public func index(before index: Index) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: -1)
|
|
}
|
|
|
|
@inlinable
|
|
public func formIndex(after index: inout Index) {
|
|
// Note: Range checks are deferred until element access.
|
|
index = index.advanced(by: 1)
|
|
}
|
|
|
|
@inlinable
|
|
public func formIndex(before index: inout Index) {
|
|
// Note: Range checks are deferred until element access.
|
|
index = index.advanced(by: -1)
|
|
}
|
|
|
|
@inlinable
|
|
public func distance(from start: Index, to end: Index) -> Int {
|
|
// Note: Range checks are deferred until element access.
|
|
start.distance(to: end)
|
|
}
|
|
|
|
@inlinable
|
|
public func index(_ index: Index, offsetBy n: Int) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: n)
|
|
}
|
|
|
|
@inlinable
|
|
public func formIndex(
|
|
_ index: inout Index, offsetBy distance: inout Index.Stride, limitedBy limit: Index
|
|
) {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advance(by: &distance, limitedBy: limit)
|
|
}
|
|
}
|
|
|
|
// Disambiguate parallel extensions on RandomAccessContainer and RandomAccessCollection
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension RandomAccessCollection
|
|
where
|
|
Self: RandomAccessContainer,
|
|
Index: Strideable,
|
|
Index.Stride == Int,
|
|
Indices == Range<Int>
|
|
{
|
|
@inlinable
|
|
public var isEmpty: Bool { startIndex == endIndex }
|
|
|
|
@inlinable
|
|
public var count: Int { endIndex - startIndex }
|
|
|
|
@inlinable
|
|
public func index(after index: Index) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: 1)
|
|
}
|
|
|
|
@inlinable
|
|
public func index(before index: Index) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: -1)
|
|
}
|
|
|
|
@inlinable
|
|
public func formIndex(after index: inout Index) {
|
|
// Note: Range checks are deferred until element access.
|
|
index = index.advanced(by: 1)
|
|
}
|
|
|
|
@inlinable
|
|
public func formIndex(before index: inout Index) {
|
|
// Note: Range checks are deferred until element access.
|
|
index = index.advanced(by: -1)
|
|
}
|
|
|
|
@inlinable
|
|
public func distance(from start: Index, to end: Index) -> Int {
|
|
// Note: Range checks are deferred until element access.
|
|
start.distance(to: end)
|
|
}
|
|
|
|
@inlinable
|
|
public func index(_ index: Index, offsetBy n: Int) -> Index {
|
|
// Note: Range checks are deferred until element access.
|
|
index.advanced(by: n)
|
|
}
|
|
}
|
|
|
|
#endif
|