mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
Invert dependencies to ContainersPreview, moving all container APIs there
- Move `InputSpan` into its own module, `SpanPreview`. Add dependencies to it. - Invert dependencies to `ContainersPreview`, so that no production target imports it, but it now depends on its previous dependents. - Move all generic APIs and conformances using `Producer`/`Drain`/`Container` to the `ContainersPreview` module. - Remove `UnstableContainersPreview` conditions around `Iterable` APIs In summary, importing `BasicContainers` and `DequeModule` continue to get us the basic definitions for `RigidArray`, `UniqueSet`, `RigidDeque` etc., without most of their unstable APIs. (With the exception of core APIs that require `InputSpan`.) To get any API that uses an unstable protocol like `Container` or `Drain`, we’ll now need to import `ContainersPreview` in addition to the type-defining module. This will provide a slightly less horrible migration path when (if) the container/producer protocol family gets adopted/adapted into the stdlib.
This commit is contained in:
131 files changed
+2675
-2613
No files matched your search
+19
-7
@@ -220,6 +220,7 @@ let targets: [CustomTarget] = [
|
||||
"InternalCollectionsUtilities",
|
||||
"ContainersPreview",
|
||||
"BasicContainers",
|
||||
"SpanPreview",
|
||||
]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -235,10 +236,7 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "BasicContainers",
|
||||
dependencies: [
|
||||
"InternalCollectionsUtilities",
|
||||
"ContainersPreview",
|
||||
],
|
||||
dependencies: ["InternalCollectionsUtilities", "SpanPreview"],
|
||||
exclude: ["CMakeLists.txt"]
|
||||
),
|
||||
.target(
|
||||
@@ -263,7 +261,12 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "ContainersPreview",
|
||||
dependencies: ["InternalCollectionsUtilities"],
|
||||
dependencies: [
|
||||
"InternalCollectionsUtilities",
|
||||
"BasicContainers",
|
||||
"DequeModule",
|
||||
"SpanPreview",
|
||||
],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -275,7 +278,7 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "DequeModule",
|
||||
dependencies: ["ContainersPreview", "InternalCollectionsUtilities"],
|
||||
dependencies: ["InternalCollectionsUtilities", "SpanPreview"],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -325,6 +328,16 @@ let targets: [CustomTarget] = [
|
||||
name: "RopeModuleTests",
|
||||
dependencies: ["_RopeModule", "_CollectionsTestSupport"]),
|
||||
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "SpanPreview",
|
||||
dependencies: ["InternalCollectionsUtilities"],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
name: "SpanPreviewTests",
|
||||
dependencies: ["SpanPreview", "_CollectionsTestSupport"]),
|
||||
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "TrailingElementsModule",
|
||||
@@ -334,7 +347,6 @@ let targets: [CustomTarget] = [
|
||||
name: "TrailingElementsTests",
|
||||
dependencies: ["TrailingElementsModule"]),
|
||||
|
||||
// These aren't ready for production use yet.
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "SortedCollections",
|
||||
|
||||
+32
-82
@@ -68,7 +68,7 @@ let availabilityMacros: KeyValuePairs<String, String> = [
|
||||
"SwiftStdlib 6.1": "macOS 15.4, iOS 18.4, watchOS 11.4, tvOS 18.4, visionOS 2.4",
|
||||
"SwiftStdlib 6.2": "macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0",
|
||||
"SwiftStdlib 6.3": "macOS 26.4, iOS 26.4, watchOS 26.4, tvOS 26.4, visionOS 26.4",
|
||||
"SwiftStdlib 6.4": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999",
|
||||
"SwiftStdlib 6.4": "macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0",
|
||||
"SwiftStdlib 6.5": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999",
|
||||
// Note: if you touch these, please make sure to also update the similar lists in
|
||||
// CMakeLists.txt and Xcode/Shared.xcconfig.
|
||||
@@ -171,53 +171,6 @@ extension CustomTarget {
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == CustomTarget {
|
||||
func toMonolithicTarget(
|
||||
name: String,
|
||||
linkerSettings: [LinkerSetting] = []
|
||||
) -> Target {
|
||||
let targets = self.filter { !$0.kind.isTest }
|
||||
return Target.target(
|
||||
name: name,
|
||||
path: "Sources",
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
"BitCollections/BitCollections.docc",
|
||||
"Collections/Collections.docc",
|
||||
"DequeModule/DequeModule.docc",
|
||||
"HashTreeCollections/HashTreeCollections.docc",
|
||||
"HeapModule/HeapModule.docc",
|
||||
"OrderedCollections/OrderedCollections.docc",
|
||||
] + targets.flatMap { t in
|
||||
t.exclude.map { "\(t.name)/\($0)" }
|
||||
},
|
||||
sources: targets.map { "\($0.directory)" },
|
||||
swiftSettings: _settings,
|
||||
linkerSettings: linkerSettings)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func toMonolithicTestTarget(
|
||||
name: String,
|
||||
dependencies: [Target.Dependency] = [],
|
||||
linkerSettings: [LinkerSetting] = []
|
||||
) -> Target {
|
||||
let targets = self.filter { $0.kind.isTest }
|
||||
return Target.testTarget(
|
||||
name: name,
|
||||
dependencies: dependencies,
|
||||
path: "Tests",
|
||||
exclude: [
|
||||
"README.md",
|
||||
] + targets.flatMap { t in
|
||||
t.exclude.map { "\(t.name)/\($0)" }
|
||||
},
|
||||
sources: targets.map { "\($0.name)" },
|
||||
swiftSettings: _testSettings,
|
||||
linkerSettings: linkerSettings)
|
||||
}
|
||||
}
|
||||
|
||||
let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .testSupport,
|
||||
@@ -226,6 +179,7 @@ let targets: [CustomTarget] = [
|
||||
"InternalCollectionsUtilities",
|
||||
"ContainersPreview",
|
||||
"BasicContainers",
|
||||
"SpanPreview",
|
||||
]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -241,10 +195,7 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "BasicContainers",
|
||||
dependencies: [
|
||||
"InternalCollectionsUtilities",
|
||||
"ContainersPreview",
|
||||
],
|
||||
dependencies: ["InternalCollectionsUtilities", "SpanPreview"],
|
||||
exclude: ["CMakeLists.txt"]
|
||||
),
|
||||
.target(
|
||||
@@ -269,7 +220,12 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "ContainersPreview",
|
||||
dependencies: ["InternalCollectionsUtilities"],
|
||||
dependencies: [
|
||||
"InternalCollectionsUtilities",
|
||||
"BasicContainers",
|
||||
"DequeModule",
|
||||
"SpanPreview",
|
||||
],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -281,7 +237,7 @@ let targets: [CustomTarget] = [
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "DequeModule",
|
||||
dependencies: ["ContainersPreview", "InternalCollectionsUtilities"],
|
||||
dependencies: ["InternalCollectionsUtilities", "SpanPreview"],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
@@ -331,16 +287,24 @@ let targets: [CustomTarget] = [
|
||||
name: "RopeModuleTests",
|
||||
dependencies: ["_RopeModule", "_CollectionsTestSupport"]),
|
||||
|
||||
// These aren't ready for production use yet.
|
||||
// .target(
|
||||
// kind: .exported,
|
||||
// name: "SortedCollections",
|
||||
// dependencies: ["InternalCollectionsUtilities"],
|
||||
// directory: "SortedCollections"),
|
||||
// .target(
|
||||
// kind: .test,
|
||||
// name: "SortedCollectionsTests",
|
||||
// dependencies: ["SortedCollections", "_CollectionsTestSupport"]),
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "SpanPreview",
|
||||
dependencies: ["InternalCollectionsUtilities"],
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
name: "SpanPreviewTests",
|
||||
dependencies: ["SpanPreview", "_CollectionsTestSupport"]),
|
||||
|
||||
.target(
|
||||
kind: .exported,
|
||||
name: "TrailingElementsModule",
|
||||
exclude: ["CMakeLists.txt"]),
|
||||
.target(
|
||||
kind: .test,
|
||||
name: "TrailingElementsTests",
|
||||
dependencies: ["TrailingElementsModule"]),
|
||||
|
||||
.target(
|
||||
kind: .exported,
|
||||
@@ -362,25 +326,11 @@ let targets: [CustomTarget] = [
|
||||
settings: _baseSettings),
|
||||
]
|
||||
|
||||
var _products: [Product] = []
|
||||
var _targets: [Target] = []
|
||||
if defines.contains("COLLECTIONS_SINGLE_MODULE") {
|
||||
_products = [
|
||||
.library(name: "Collections", targets: ["Collections"]),
|
||||
]
|
||||
_targets = [
|
||||
targets.toMonolithicTarget(name: "Collections"),
|
||||
targets.toMonolithicTestTarget(
|
||||
name: "CollectionsTests",
|
||||
dependencies: ["Collections"]),
|
||||
]
|
||||
} else {
|
||||
_products = targets.compactMap { t in
|
||||
guard t.kind == .exported else { return nil }
|
||||
return .library(name: t.name, targets: [t.name])
|
||||
}
|
||||
_targets = targets.map { $0.toTarget() }
|
||||
let _products: [Product] = targets.compactMap { t in
|
||||
guard t.kind == .exported else { return nil }
|
||||
return .library(name: t.name, targets: [t.name])
|
||||
}
|
||||
let _targets: [Target] = targets.map { $0.toTarget() }
|
||||
|
||||
let package = Package(
|
||||
name: "swift-collections",
|
||||
|
||||
@@ -16,7 +16,7 @@ else()
|
||||
add_library(BasicContainers
|
||||
${COLLECTIONS_ARRAY_SOURCES})
|
||||
target_link_libraries(BasicContainers PRIVATE
|
||||
InternalCollectionsUtilities ContainersPreview)
|
||||
InternalCollectionsUtilities SpanPreview)
|
||||
set_target_properties(BasicContainers PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
|
||||
@@ -28,7 +28,7 @@ target_sources(${module_name} PRIVATE
|
||||
"RigidArray/RigidArray.swift"
|
||||
"RigidArray/RigidArray+Append.swift"
|
||||
"RigidArray/RigidArray+Consumption.swift"
|
||||
"RigidArray/RigidArray+Container.swift"
|
||||
"RigidArray/RigidArray+ContainerOperations.swift"
|
||||
"RigidArray/RigidArray+Deprecated.swift"
|
||||
"RigidArray/RigidArray+Descriptions.swift"
|
||||
"RigidArray/RigidArray+Equatable.swift"
|
||||
@@ -37,12 +37,13 @@ target_sources(${module_name} PRIVATE
|
||||
"RigidArray/RigidArray+Hashable.swift"
|
||||
"RigidArray/RigidArray+Initializers.swift"
|
||||
"RigidArray/RigidArray+Insertions.swift"
|
||||
"RigidArray/RigidArray+Iterable.swift"
|
||||
"RigidArray/RigidArray+Removals.swift"
|
||||
"RigidArray/RigidArray+Replacements.swift"
|
||||
"UniqueArray/UniqueArray.swift"
|
||||
"UniqueArray/UniqueArray+Append.swift"
|
||||
"UniqueArray/UniqueArray+Consumption.swift"
|
||||
"UniqueArray/UniqueArray+Container.swift"
|
||||
"UniqueArray/UniqueArray+ContainerOperations.swift"
|
||||
"UniqueArray/UniqueArray+Deprecated.swift"
|
||||
"UniqueArray/UniqueArray+Descriptions.swift"
|
||||
"UniqueArray/UniqueArray+Equatable.swift"
|
||||
@@ -50,6 +51,7 @@ target_sources(${module_name} PRIVATE
|
||||
"UniqueArray/UniqueArray+Hashable.swift"
|
||||
"UniqueArray/UniqueArray+Initializers.swift"
|
||||
"UniqueArray/UniqueArray+Insertions.swift"
|
||||
"UniqueArray/UniqueArray+Iterable.swift"
|
||||
"UniqueArray/UniqueArray+Removals.swift"
|
||||
"UniqueArray/UniqueArray+Replacements.swift"
|
||||
"HashTable/_HTable.swift"
|
||||
@@ -65,26 +67,28 @@ target_sources(${module_name} PRIVATE
|
||||
"HashTable/_HTable+Resizing.swift"
|
||||
"HashTable/_HTable+Sizing.swift"
|
||||
"RigidSet/RigidSet.swift"
|
||||
"RigidSet/RigidSet+BorrowingSequence.swift"
|
||||
"RigidSet/RigidSet+Consumption.swift"
|
||||
"RigidSet/RigidSet+Debugging.swift"
|
||||
"RigidSet/RigidSet+Deprecated.swift"
|
||||
"RigidSet/RigidSet+Equatable.swift"
|
||||
"RigidSet/RigidSet+Hashable.swift"
|
||||
"RigidSet/RigidSet+Index.swift"
|
||||
"RigidSet/RigidSet+Initializers.swift"
|
||||
"RigidSet/RigidSet+Insertions.swift"
|
||||
"RigidSet/RigidSet+Iterable.swift"
|
||||
"RigidSet/RigidSet+Lookup.swift"
|
||||
"RigidSet/RigidSet+Removals.swift"
|
||||
"RigidSet/RigidSet+Resizing.swift"
|
||||
"UniqueSet/UniqueSet.swift"
|
||||
"UniqueSet/UniqueSet+BorrowingSequence.swift"
|
||||
"UniqueSet/UniqueSet+Consumption.swift"
|
||||
"UniqueSet/UniqueSet+Debugging.swift"
|
||||
"UniqueSet/UniqueSet+Deprecated.swift"
|
||||
"UniqueSet/UniqueSet+Equatable.swift"
|
||||
"UniqueSet/UniqueSet+Hashable.swift"
|
||||
"UniqueSet/UniqueSet+Index.swift"
|
||||
"UniqueSet/UniqueSet+Initializers.swift"
|
||||
"UniqueSet/UniqueSet+Insertions.swift"
|
||||
"UniqueSet/UniqueSet+Iterable.swift"
|
||||
"UniqueSet/UniqueSet+Lookup.swift"
|
||||
"UniqueSet/UniqueSet+Removals.swift"
|
||||
"UniqueSet/UniqueSet+Resizing.swift"
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -176,7 +176,6 @@ extension RigidArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
#if !UnstableContainersPreview
|
||||
/// Appends the elements of a given array to the end of this array by moving
|
||||
/// them between the containers. On return, the input array becomes empty, but
|
||||
/// it is not destroyed, and it preserves its original storage capacity.
|
||||
@@ -192,11 +191,11 @@ extension RigidArray where Element: ~Copyable {
|
||||
public mutating func append(
|
||||
moving items: inout RigidArray<Element>
|
||||
) {
|
||||
// FIXME: Remove this; RangeReplaceableContainer provides a more general algorithm
|
||||
items.edit { span in
|
||||
self.append(moving: &span)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -275,8 +274,8 @@ extension RigidArray {
|
||||
_count += c
|
||||
return it
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
@@ -290,7 +289,7 @@ extension RigidArray {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the end of this array.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold all items in the
|
||||
@@ -334,7 +333,7 @@ extension RigidArray {
|
||||
precondition(it.next() == nil, "RigidArray capacity overflow")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the end of this array.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold all items in the
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -190,11 +190,6 @@ extension RigidArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray.SubrangeConsumer: Drain where Element: ~Copyable {
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray.SubrangeConsumer where Element: ~Copyable {
|
||||
@inlinable
|
||||
|
||||
+7
-56
@@ -2,7 +2,7 @@
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 - 2026 Apple Inc. and the Swift project authors
|
||||
// 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
|
||||
@@ -13,62 +13,12 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: Iterable where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func makeBorrowingIterator() -> BorrowingIterator {
|
||||
self.span._makeBorrowingIterator(from: 0, to: count)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
// FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self
|
||||
self.span._makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
self.span.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
|
||||
/// A Boolean value indicating whether this array contains no elements.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@@ -144,20 +94,20 @@ extension RigidArray where Element: ~Copyable {
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable @inline(__always)
|
||||
internal func _ptr(to index: Int) -> UnsafePointer<Element> {
|
||||
package func _ptr(to index: Int) -> UnsafePointer<Element> {
|
||||
_checkItemIndex(index)
|
||||
let p = _storage.baseAddress.unsafelyUnwrapped.advanced(by: index)
|
||||
return UnsafePointer(p)
|
||||
}
|
||||
|
||||
|
||||
@inlinable @inline(__always)
|
||||
internal mutating func _mutablePtr(
|
||||
package mutating func _mutablePtr(
|
||||
to index: Int
|
||||
) -> UnsafeMutablePointer<Element> {
|
||||
_checkItemIndex(index)
|
||||
return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index)
|
||||
}
|
||||
|
||||
|
||||
#if compiler(>=6.4)
|
||||
/// Accesses the element at the specified position.
|
||||
///
|
||||
@@ -219,6 +169,7 @@ extension RigidArray where Element: ~Copyable {
|
||||
unsafe _items.swapAt(i, j)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
/// Returns the position immediately after the given index.
|
||||
@@ -474,7 +425,7 @@ extension RigidArray where Element: ~Copyable {
|
||||
public func spanBoundary(
|
||||
before index: Index, maxDistance: Int, limitedBy limit: Index
|
||||
) -> (index: Index, distance: Int) {
|
||||
self.span.spanBoundary(
|
||||
self.span._spanBoundary(
|
||||
before: index, maxDistance: maxDistance, limitedBy: limit)
|
||||
}
|
||||
|
||||
@@ -13,41 +13,10 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2) && UnstableContainersPreview
|
||||
|
||||
#if false // FIXME
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable
|
||||
@_lifetime(borrow self)
|
||||
public func borrowElement(at index: Int) -> Ref<Element> {
|
||||
_checkItemIndex(index)
|
||||
return unsafe Ref(
|
||||
unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index),
|
||||
borrowing: self
|
||||
)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if false // FIXME
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable
|
||||
@_lifetime(&self)
|
||||
public mutating func mutateElement(at index: Int) -> Mut<Element> {
|
||||
_checkItemIndex(index)
|
||||
return unsafe Mut(
|
||||
unsafeAddress: _storage.baseAddress.unsafelyUnwrapped.advanced(by: index),
|
||||
mutating: &self
|
||||
)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if false // FIXME
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -116,7 +115,7 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new array with the specified capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
///
|
||||
@@ -139,7 +138,7 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new array with the specified capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
///
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -208,7 +208,6 @@ extension RigidArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
#if !UnstableContainersPreview
|
||||
/// Inserts the elements of a given array into the given position in this
|
||||
/// array by moving them between the containers. On return, the input array
|
||||
/// becomes empty, but it is not destroyed, and it preserves its original
|
||||
@@ -237,7 +236,6 @@ extension RigidArray where Element: ~Copyable {
|
||||
self.insert(moving: &source, at: index)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 - 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
}
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: Iterable where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func makeBorrowingIterator() -> BorrowingIterator {
|
||||
self.span._makeBorrowingIterator(from: 0, to: count)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -369,7 +369,6 @@ extension RigidArray where Element: ~Copyable {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
#if UnstableContainersPreview
|
||||
/// Replaces the specified range of elements by moving the elements of a
|
||||
/// given array into their place, consuming it in the process.
|
||||
///
|
||||
@@ -401,73 +400,9 @@ extension RigidArray where Element: ~Copyable {
|
||||
removing subrange: Range<Int>,
|
||||
consuming newElements: consuming RigidArray<Element>,
|
||||
) {
|
||||
// FIXME: Remove this in favor of a generic algorithm over consumable containers
|
||||
// FIXME: Remove this in favor of the generic algorithm over drainable containers
|
||||
replace(removing: subrange, moving: &newElements)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in array storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newItemCount: The maximum number of items to insert into the array.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(
|
||||
removing: subrange,
|
||||
addingCount: newItemCount
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -581,32 +516,6 @@ extension RigidArray {
|
||||
unsafe self.replace(removing: subrange, copying: buffer)
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
while !target.isFull {
|
||||
let source = it.nextSpan(maxCount: target.freeCapacity)
|
||||
precondition(
|
||||
!source.isEmpty,
|
||||
"Broken Container: count doesn't match contents")
|
||||
target._append(copying: source)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
internal mutating func _replace(
|
||||
@@ -635,49 +544,6 @@ extension RigidArray {
|
||||
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newElements.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given collection.
|
||||
///
|
||||
@@ -715,50 +581,6 @@ extension RigidArray {
|
||||
copyingCollection: newElements,
|
||||
newCount: newElements.count)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
|
||||
/// *m* is the count of `newElements`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(<6.2)
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,11 +11,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
extension RigidDictionary where Key: ~Copyable, Value: ~Copyable {
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if UnstableHashedContainers
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -39,7 +37,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
|
||||
guard left.count == right.count else { return false }
|
||||
|
||||
#if UnstableContainersPreview
|
||||
var lit = left.makeBorrowingIterator()
|
||||
while true {
|
||||
let l = lit.nextSpan(maxCount: .max)
|
||||
@@ -50,13 +47,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
i &+= 1
|
||||
}
|
||||
}
|
||||
#else
|
||||
var i = left.startIndex
|
||||
while i != left.endIndex {
|
||||
guard right.contains(left[i]) else { return false }
|
||||
i = left.index(after: i)
|
||||
}
|
||||
#endif
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidSet: Hashable where Element: ~Copyable {}
|
||||
@@ -35,7 +34,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
|
||||
@inlinable
|
||||
public func _rawHashValue(seed: Int) -> Int {
|
||||
#if UnstableContainersPreview
|
||||
var hash = 0
|
||||
var it = self.makeBorrowingIterator()
|
||||
while true {
|
||||
@@ -47,12 +45,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
return hash
|
||||
#else
|
||||
var hasher = Hasher()
|
||||
hasher.combine(seed)
|
||||
self.hash(into: &hasher)
|
||||
return hasher.finalize()
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -48,40 +44,10 @@ extension RigidSet where Element: ~Copyable {
|
||||
self.init(capacity: capacity)
|
||||
try self.insert(addingCount: capacity, initializingWith: initializer)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(capacity: capacity)
|
||||
try self.insert(addingCount: capacity, from: &producer)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from drain: inout D
|
||||
) {
|
||||
self.init(capacity: capacity)
|
||||
self.insert(addingCount: capacity, from: &drain)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet /* where Element: Copyable */ {
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
@@ -95,8 +61,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
self.init(capacity: capacity)
|
||||
try self._insert(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init(
|
||||
@@ -117,13 +82,10 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
self.insert(copying: contents)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable & Sequence<Element>
|
||||
>(
|
||||
public init<S: Iterable & Sequence<Element>>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing S
|
||||
) throws(S.Failure)
|
||||
@@ -135,9 +97,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<
|
||||
S: Iterable & Collection<Element>
|
||||
>(
|
||||
public init<S: Iterable & Collection<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
) throws(S.Failure)
|
||||
@@ -145,7 +105,6 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
try self._insert(copying: contents)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
@@ -166,60 +166,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int? = nil,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.insert(
|
||||
addingCount: newItemCount ?? freeCapacity
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: CountedProducer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: consuming P
|
||||
) throws(E) {
|
||||
try self.insert(addingCount: producer.count, from: &producer)
|
||||
try producer._expectEnd("Invalid CountedProducer")
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from drain: consuming D
|
||||
) {
|
||||
var remainder = drain.count
|
||||
while remainder > 0 {
|
||||
var span = drain.drainNext(maxCount: remainder)
|
||||
guard !span.isEmpty else { break }
|
||||
remainder &-= span.count
|
||||
while let next = span.popFirst() {
|
||||
self.insert(next)
|
||||
}
|
||||
}
|
||||
drain._expectEnd("Invalid Drain")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -235,7 +181,6 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
}
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
package mutating func _insert<
|
||||
@@ -251,9 +196,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
self.insert(copying: span)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
@@ -265,8 +208,7 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
where S.Element == Element {
|
||||
try _insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert(copying items: some Sequence<Element>) {
|
||||
@@ -276,7 +218,6 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
}
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
@@ -288,7 +229,6 @@ extension RigidSet /* where Element: Copyable */ {
|
||||
where S.Element == Element {
|
||||
try _insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
+1
-5
@@ -11,11 +11,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet: Iterable where Element: ~Copyable {
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -40,7 +36,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
_find(item).bucket != nil
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
@@ -48,7 +43,6 @@ extension RigidSet where Element: ~Copyable {
|
||||
assert(self._table.isOccupied(bucket))
|
||||
return Ref(unsafeAddress: self._memberPtr(at: bucket), borrowing: self)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if UnstableHashedContainers
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -136,7 +136,6 @@ extension UniqueArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
#if !UnstableContainersPreview
|
||||
/// Appends the elements of a given array to the end of this array by moving
|
||||
/// them between the containers. On return, the input array becomes empty, but
|
||||
/// it is not destroyed, and it preserves its original storage capacity.
|
||||
@@ -154,6 +153,7 @@ extension UniqueArray where Element: ~Copyable {
|
||||
public mutating func append(
|
||||
moving items: inout RigidArray<Element>
|
||||
) {
|
||||
// FIXME: Remove in favor of the generic RRC algorithm
|
||||
_ensureFreeCapacity(items.count)
|
||||
items.edit { source in
|
||||
source.withUnsafeMutableBufferPointer { buffer, count in
|
||||
@@ -162,45 +162,6 @@ extension UniqueArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Appends all the items generated by a producer to the end of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate all the
|
||||
/// elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similarly-sized invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
_ensureFreeCapacity(c)
|
||||
try self.append(addingCount: freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -263,10 +224,10 @@ extension UniqueArray {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
package mutating func _append<
|
||||
Source: Iterable & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
@@ -286,8 +247,8 @@ extension UniqueArray {
|
||||
// FIXME: Add _append(copyingContainer:), forwarding to the same method on RigidArray
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Copies the elements of a borrowing sequence to the end of this array.
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of an iterable to the end of this array.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold enough elements,
|
||||
/// then this reallocates the array's storage to extend its capacity, using
|
||||
@@ -343,7 +304,7 @@ extension UniqueArray {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a container to the end of this array.
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold enough elements,
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
-52
@@ -13,62 +13,10 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: Iterable where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
|
||||
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func makeBorrowingIterator() -> BorrowingIterator {
|
||||
self._storage.makeBorrowingIterator()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
_storage.makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
_storage.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: DynamicContainer where Element: ~Copyable {}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
/// A Boolean value indicating whether this array contains no elements.
|
||||
@@ -11,10 +11,8 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2) && UnstableContainersPreview
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -85,7 +84,7 @@ extension UniqueArray where Element: ~Copyable {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new array with the specified initial capacity, holding a copy
|
||||
/// of the contents of a given borrowing sequence.
|
||||
///
|
||||
@@ -124,7 +123,7 @@ extension UniqueArray /*where Element: Copyable*/ {
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new array with the specified initial capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
///
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -177,7 +177,6 @@ extension UniqueArray where Element: ~Copyable {
|
||||
_storage.insert(moving: &items, at: index)
|
||||
}
|
||||
|
||||
#if !UnstableContainersPreview
|
||||
/// Inserts the elements of a given array into the given position in this
|
||||
/// array by moving them between the containers. On return, the input array
|
||||
/// becomes empty, but it is not destroyed, and it preserves its original
|
||||
@@ -201,7 +200,6 @@ extension UniqueArray where Element: ~Copyable {
|
||||
_ensureFreeCapacity(items.count)
|
||||
_storage.insert(moving: &items, at: index)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
+8
-6
@@ -11,20 +11,22 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension InputSpan: Iterable where Element: ~Copyable {
|
||||
extension UniqueArray: Iterable where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
|
||||
|
||||
@inlinable
|
||||
public var underestimatedCount: Int { count }
|
||||
|
||||
@_lifetime(borrow self)
|
||||
@inlinable
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func makeBorrowingIterator() -> BorrowingIterator {
|
||||
BorrowingIterator(self.span)
|
||||
self._storage.makeBorrowingIterator()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -343,67 +343,6 @@ extension UniqueArray where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified range of elements by at most `maxCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the array doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the array's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `maxCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `maxCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in array storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(removing: subrange, addingCount: newItemCount) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray {
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
@@ -522,54 +461,6 @@ extension UniqueArray {
|
||||
_storage.replace(removing: subrange, copying: newElements)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same location.
|
||||
/// The number of new elements need not match the number of elements being
|
||||
/// removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to perform the replacement,
|
||||
/// then this reallocates the array's storage to extend its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
|
||||
/// the `insert(copying:at:)` method instead is preferred in this case.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. Calling the `removeSubrange(_:)` method instead is
|
||||
/// preferred in this case.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
|
||||
/// *m* is the count of `newElements`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c - subrange.count)
|
||||
_storage._replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: c)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given collection.
|
||||
///
|
||||
@@ -612,53 +503,5 @@ extension UniqueArray {
|
||||
copyingCollection: newElements,
|
||||
newCount: c)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same location.
|
||||
/// The number of new elements need not match the number of elements being
|
||||
/// removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to perform the replacement,
|
||||
/// then this reallocates the array's storage to extend its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. Calling
|
||||
/// the `insert(copying:at:)` method instead is preferred in this case.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. Calling the `removeSubrange(_:)` method instead is
|
||||
/// preferred in this case.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
|
||||
/// *m* is the count of `newElements`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c - subrange.count)
|
||||
_storage._replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: c)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(<6.2)
|
||||
@@ -24,7 +23,7 @@ import ContainersPreview
|
||||
@available(*, unavailable, message: "UniqueArray requires a Swift 6.2 toolchain")
|
||||
public struct UniqueArray<Element: ~Copyable>: ~Copyable {
|
||||
@usableFromInline
|
||||
internal var _storage: RigidArray<Element>
|
||||
package var _storage: RigidArray<Element>
|
||||
|
||||
@inlinable
|
||||
public init() {
|
||||
@@ -65,7 +64,7 @@ public struct UniqueArray<Element: ~Copyable>: ~Copyable {
|
||||
@frozen
|
||||
public struct UniqueArray<Element: ~Copyable>: ~Copyable {
|
||||
@usableFromInline
|
||||
internal var _storage: RigidArray<Element>
|
||||
package var _storage: RigidArray<Element>
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
package init(_storage: consuming RigidArray<Element>) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable {
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,15 +11,14 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueSet where Element: ~Copyable {
|
||||
#if UnstableContainersPreview
|
||||
@inlinable
|
||||
public mutating func consumeAll(
|
||||
consumingWith consumer: (inout InputSpan<Element>) -> Void
|
||||
@@ -30,7 +29,6 @@ extension UniqueSet where Element: ~Copyable {
|
||||
_ = consume span
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
public mutating func _consumeAll(
|
||||
|
||||
File renamed without changes.
@@ -13,11 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueSet where Element: ~Copyable {
|
||||
|
||||
@@ -13,10 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueSet: Hashable where Element: ~Copyable {}
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -49,32 +45,10 @@ extension UniqueSet where Element: ~Copyable {
|
||||
addingCount: minimumCapacity,
|
||||
initializingWith: initializer)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
minimumCapacity: Int? = nil, from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
let c = producer.underestimatedCount
|
||||
if let minimumCapacity {
|
||||
self.init(minimumCapacity: Swift.min(minimumCapacity, c))
|
||||
} else {
|
||||
self.init(minimumCapacity: c)
|
||||
}
|
||||
try self.insert(from: &producer)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueSet where Element: Copyable {
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public init<S: Iterable & ~Copyable & ~Escapable>(
|
||||
@@ -84,15 +58,13 @@ extension UniqueSet where Element: Copyable {
|
||||
self.init()
|
||||
try self.insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public init(copying items: some Sequence<Element>) {
|
||||
self.init()
|
||||
self.insert(copying: items)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public init<S: Iterable & Sequence<Element>>(
|
||||
@@ -102,7 +74,6 @@ extension UniqueSet where Element: Copyable {
|
||||
self.init()
|
||||
try self._insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,10 +11,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -79,45 +75,6 @@ extension UniqueSet where Element: ~Copyable {
|
||||
try _storage.insert(
|
||||
addingCount: newItemCount, initializingWith: initializer)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
_ensureFreeCapacity(Swift.max(producer.underestimatedCount, 1))
|
||||
try self.insert(addingCount: self.freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from drain: inout D
|
||||
) {
|
||||
while true {
|
||||
var span = drain.drainNext()
|
||||
guard !span.isEmpty else { break }
|
||||
while let next = span.popFirst() {
|
||||
self.insert(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -130,7 +87,6 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
_storage.insert(copying: items)
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
package mutating func _insert<
|
||||
@@ -147,9 +103,7 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
self.insert(copying: span)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
@@ -160,8 +114,7 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
) throws(S.Failure) where S.Element == Element {
|
||||
try _insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert(copying items: some Sequence<Element>) {
|
||||
@@ -172,7 +125,6 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
}
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
@@ -184,7 +136,6 @@ extension UniqueSet /* where Element: Copyable */ {
|
||||
where S.Element == Element {
|
||||
try _insert(copying: items)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
+1
-2
@@ -13,10 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueSet: Iterable where Element: ~Copyable {
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if UnstableHashedContainers
|
||||
|
||||
@@ -57,6 +57,7 @@ endif()
|
||||
|
||||
|
||||
add_subdirectory(InternalCollectionsUtilities)
|
||||
add_subdirectory(SpanPreview)
|
||||
add_subdirectory(ContainersPreview)
|
||||
if(NOT COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE)
|
||||
add_subdirectory(BasicContainers)
|
||||
|
||||
@@ -16,7 +16,7 @@ else()
|
||||
add_library(ContainersPreview
|
||||
${COLLECTIONS_CONTAINERS_SOURCES})
|
||||
target_link_libraries(ContainersPreview PRIVATE
|
||||
InternalCollectionsUtilities)
|
||||
InternalCollectionsUtilities SpanPreview BasicContainers DequeModule)
|
||||
set_target_properties(ContainersPreview PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
|
||||
@@ -28,11 +28,16 @@ target_sources(${module_name} PRIVATE
|
||||
"Conformances/Array+Iterable.swift"
|
||||
"Conformances/ClosedRange+Iterable.swift"
|
||||
"Conformances/InputSpan+Container.swift"
|
||||
"Conformances/InputSpan+Iterable.swift"
|
||||
"Conformances/MutableSpan+Container.swift"
|
||||
"Conformances/OutputSpan+Container.swift"
|
||||
"Conformances/Range+Iterable.swift"
|
||||
"Conformances/RigidArray+Container.swift"
|
||||
"Conformances/RigidDeque+Container.swift"
|
||||
"Conformances/RigidSet+Container.swift"
|
||||
"Conformances/Span+Container.swift"
|
||||
"Conformances/UniqueArray+Container.swift"
|
||||
"Conformances/UniqueSet+Container.swift"
|
||||
"Conformances/UniqueDeque+Container.swift"
|
||||
"Protocols/Container/BidirectionalContainer.swift"
|
||||
"Protocols/Container/Container.swift"
|
||||
"Protocols/Container/Container+Filter.swift"
|
||||
@@ -50,7 +55,9 @@ target_sources(${module_name} PRIVATE
|
||||
"Protocols/Container/RangeExpression2.swift"
|
||||
"Protocols/Container/RangeReplaceableContainer.swift"
|
||||
"Protocols/Container/Strideable+Limits.swift"
|
||||
"Protocols/Iterable/BorrowingIteratorProtocol.swift"
|
||||
"Protocols/Drain/Drain.swift"
|
||||
"Protocols/Drain/Drain+Map.swift"
|
||||
"Protocols/Drain/Drain+Reduce.swift"
|
||||
"Protocols/Iterable/BorrowingIteratorProtocol+Copy.swift"
|
||||
"Protocols/Iterable/BorrowingIteratorProtocol+ElementsEqual.swift"
|
||||
"Protocols/Iterable/BorrowingIteratorProtocol+Filter.swift"
|
||||
@@ -60,10 +67,6 @@ target_sources(${module_name} PRIVATE
|
||||
"Protocols/Iterable/BorrowingIteratorProtocol+SpanwiseZip.swift"
|
||||
"Protocols/Iterable/Iterable+ElementsEqual.swift"
|
||||
"Protocols/Iterable/Iterable+Reduce.swift"
|
||||
"Protocols/Iterable/Iterable+Utilities.swift"
|
||||
"Protocols/Drain/Drain.swift"
|
||||
"Protocols/Drain/Drain+Map.swift"
|
||||
"Protocols/Drain/Drain+Reduce.swift"
|
||||
"Protocols/Producer/CountedProducer.swift"
|
||||
"Protocols/Producer/Producer.swift"
|
||||
"Protocols/Producer/Producer+Collect.swift"
|
||||
@@ -71,9 +74,6 @@ target_sources(${module_name} PRIVATE
|
||||
"Protocols/Producer/Producer+Map.swift"
|
||||
"Protocols/Producer/Producer+Reduce.swift"
|
||||
"Protocols/Producer/Producer+Unfold.swift"
|
||||
"Types/InputSpan.swift"
|
||||
"Types/MutableRef.swift"
|
||||
"Types/OutputSpan+Helpers.swift"
|
||||
"Types/Shared.swift"
|
||||
"Types/UniqueBox.swift"
|
||||
)
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import BasicContainers
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
// FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self
|
||||
self.span._makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
self.span.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: DrainableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension RigidArray: RangeReplaceableContainer where Element: ~Copyable {
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
while !target.isFull {
|
||||
let source = it.nextSpan(maxCount: target.freeCapacity)
|
||||
precondition(
|
||||
!source.isEmpty,
|
||||
"Broken Container: count doesn't match contents")
|
||||
target._append(copying: source)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newElements.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: newElements.count)
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this array and
|
||||
/// *m* is the count of `newElements`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying newElements: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange,
|
||||
copyingContainer: newElements,
|
||||
newCount: newElements.count)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray.SubrangeConsumer: Drain where Element: ~Copyable {
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,539 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 - 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import DequeModule
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self, from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
// FIXME: This should validate that the iterator belongs to this deque.
|
||||
return iterator._position
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque.SubrangeConsumer: Drain where Element: ~Copyable {
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque where Element: ~Copyable {
|
||||
/// Creates a new rigid deque of the specified capacity, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// If the capacity is exhausted before the producer reaches its end, then
|
||||
/// this triggers a runtime error.
|
||||
///
|
||||
/// If the producer throws an error during this initializer, then all
|
||||
/// previously produced items are discarded and the initializer rethrows the
|
||||
/// error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(capacity: capacity)
|
||||
try self.append(from: &producer)
|
||||
}
|
||||
|
||||
/// Appends at most `newItemCount` items generated by a producer to the end of
|
||||
/// this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation appends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in the
|
||||
/// producer; if the producer has more, then they remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to append to the deque, or
|
||||
/// nil to use all available capacity.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the number of items
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.append(addingCount: newItemCount) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends all the items generated by a producer to the end of this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold all the
|
||||
/// new items, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the number of items produced.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
try self.append(
|
||||
addingCount: freeCapacity
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else {
|
||||
done = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !done {
|
||||
done = (try producer.next() == nil)
|
||||
}
|
||||
precondition(done, "RigidDeque capacity overflow")
|
||||
}
|
||||
|
||||
/// Prepends at most `newItemCount` items generated by a producer to the
|
||||
/// front of this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation prepends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in
|
||||
/// the producer; if the producer has more, then they remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// Note: Partial prepends create a gap in ring buffer storage that needs to
|
||||
/// be closed by moving newly prepended items to their correct positions given
|
||||
/// the adjusted count. This adds some overhead compared to adding exactly as
|
||||
/// many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to prepend to the deque, or
|
||||
/// nil to use all available capacity.
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(`newItemCount ?? self.freeCapacity`)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int? = nil,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.prepend(
|
||||
addingCount: newItemCount ?? freeCapacity
|
||||
) { target throws(E) in
|
||||
try producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the deque.
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `maxCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(
|
||||
removing: subrange,
|
||||
addingCount: newItemCount
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts at most `newItemCount` items generated by a producer into this
|
||||
/// deque, starting at the given index.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items
|
||||
/// left in its underlying sequence, then extra items remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results
|
||||
/// in a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - index: The position at which to insert the new items.
|
||||
/// `index` must be a valid index in the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P,
|
||||
at index: Int
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try insert(addingCount: newItemCount, at: index) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque /*where Element: Copyable*/ {
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// container, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(`items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the front of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the front of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
package mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && self.count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`).
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this deque and
|
||||
/// *m* is the count of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import BasicContainers
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet where Element: ~Copyable {
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -11,40 +11,14 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_frozen
|
||||
@usableFromInline
|
||||
package struct _ShamSpanIterator {
|
||||
// FIXME: Remove this when `Span.BorrowingIterator` becomes actually usable.
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _basePointer: UnsafeRawPointer?
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _baseCount: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _start: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _count: Int
|
||||
}
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension Span: RandomAccessContainer where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(copy self)
|
||||
package func _makeBorrowingIterator(from start: Int, to end: Int) -> BorrowingIterator {
|
||||
// Note: This is declared `copy self` so that types can forward to it without having to override lifetimes.
|
||||
#if false // FIXME: Span's iterator needs to provide a public "slicing" initializer
|
||||
return BorrowingIterator(self, from: start, to: end)
|
||||
#else
|
||||
var it = BorrowingIterator(self.extracting(first: end))
|
||||
let c = it.skip(by: start)
|
||||
precondition(c == start)
|
||||
return it
|
||||
#endif
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(from start: Int, to end: Int) -> BorrowingIterator {
|
||||
@@ -59,15 +33,13 @@ extension Span: RandomAccessContainer where Element: ~Copyable {
|
||||
"Iterator does not belong to this container")
|
||||
return iterator._start
|
||||
#else
|
||||
precondition(MemoryLayout<BorrowingIterator>.stride == MemoryLayout<_ShamSpanIterator>.stride)
|
||||
return self.withUnsafeBufferPointer { ourBuffer in
|
||||
Swift.withUnsafeBytes(of: &iterator) { buffer in
|
||||
buffer.withMemoryRebound(to: _ShamSpanIterator.self) { shamBuffer in
|
||||
precondition(
|
||||
shamBuffer[0]._basePointer == UnsafeRawPointer(ourBuffer.baseAddress) && shamBuffer[0]._baseCount <= ourBuffer.count,
|
||||
"Iterator does not belong to this container")
|
||||
return shamBuffer[0]._start
|
||||
}
|
||||
iterator._withShamIterator { sham in
|
||||
precondition(
|
||||
sham._basePointer == UnsafeRawPointer(ourBuffer.baseAddress)
|
||||
&& sham._baseCount <= ourBuffer.count,
|
||||
"Iterator does not belong to this container")
|
||||
return sham._start
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -79,48 +51,23 @@ extension Span: RandomAccessContainer where Element: ~Copyable {
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension Span where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
public var startIndex: Int {
|
||||
0
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_transparent
|
||||
public var endIndex: Int {
|
||||
count
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(copy self)
|
||||
public func _nextSpan(after index: inout Int) -> Self {
|
||||
precondition(index >= 0 && index <= count, "Index out of bounds")
|
||||
let result = self.extracting(last: count - index)
|
||||
index = count
|
||||
return result
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func nextSpan(after index: inout Int) -> Self {
|
||||
_nextSpan(after: &index)
|
||||
}
|
||||
|
||||
// FIXME: This has the proper lifetime declaration but can't fulfill the Container requirement.
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(copy self)
|
||||
public func _nextSpan(
|
||||
after index: inout Int, maxCount: Int, limitedBy limit: Int
|
||||
) -> Self {
|
||||
precondition(index >= 0 && index <= count, "Index out of bounds")
|
||||
precondition(maxCount > 0, "maxCount must be positive")
|
||||
precondition(limit >= 0 && limit <= count, "Index out of bounds")
|
||||
var end = index &+ Swift.min(maxCount, count &- index)
|
||||
if limit >= index, limit < end {
|
||||
end = limit
|
||||
}
|
||||
let r = self.extracting(unchecked: Range(uncheckedBounds: (index, end)))
|
||||
index = end
|
||||
return r
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func nextSpan(
|
||||
@@ -141,11 +88,7 @@ extension Span where Element: ~Copyable {
|
||||
public func spanBoundary(
|
||||
before index: Index, maxDistance: Int, limitedBy limit: Index
|
||||
) -> (index: Index, distance: Int) {
|
||||
precondition(index >= 0 && index <= count, "Index out of bounds")
|
||||
precondition(limit >= 0 && limit <= count, "Index out of bounds")
|
||||
precondition(maxDistance > 0, "maxDistance must be positive")
|
||||
let p = index._clampedDown(towards: 0, maxDistance: maxDistance, limitedBy: limit)
|
||||
return (p, index &- p)
|
||||
_spanBoundary(before: index, maxDistance: maxDistance, limitedBy: limit)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import BasicContainers
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
_storage.makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
_storage.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension UniqueArray: DynamicContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray where Element: Copyable {
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,588 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import DequeModule
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
_storage.makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
_storage.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: DynamicContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
/// Appends at most `newItemCount` items generated by a producer to the end of
|
||||
/// this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the specified
|
||||
/// number of new elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// This operation appends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in the
|
||||
/// producer; if the producer has more, then they remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to append to the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage.append(addingCount: newItemCount, from: &producer)
|
||||
}
|
||||
|
||||
/// Appends all the items generated by a producer to the end of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate all the
|
||||
/// elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similarly-sized invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
try self.append(addingCount: c) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepends at most `newItemCount` items generated by a producer to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the specified
|
||||
/// number of new elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// This operation prepends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in
|
||||
/// the producer; if the producer has more, then they remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// Note: Partial prepends create a gap in ring buffer storage that needs to
|
||||
/// be closed by moving newly prepended items to their correct positions given
|
||||
/// the adjusted count. This adds some overhead compared to adding exactly as
|
||||
/// many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to prepend to the deque.
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage._handle.uncheckedPrepend(
|
||||
addingCount: newItemCount
|
||||
) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prepends all items generated by a producer to the front of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate all the
|
||||
/// items, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similarly-sized invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
_ensureFreeCapacity(c)
|
||||
try self.prepend(addingCount: freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new unique deque of the specified capacity, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(minimumCapacity: capacity)
|
||||
try self.append(addingCount: capacity, from: &producer)
|
||||
}
|
||||
|
||||
/// Creates a new unique deque, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(minimumCapacity: producer.underestimatedCount)
|
||||
try self.append(from: &producer)
|
||||
}
|
||||
|
||||
/// Inserts at most `newItemCount` items generated by a producer into this
|
||||
/// deque, starting at the given index.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items
|
||||
/// left in its underlying sequence, then extra items remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results
|
||||
/// in a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - index: The position at which to insert the new items.
|
||||
/// `index` must be a valid index in the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P,
|
||||
at index: Int
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try insert(addingCount: newItemCount, at: index) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the deque.
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(removing: subrange, addingCount: newItemCount) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// container, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(`items.count`) when amortized over many similar
|
||||
/// invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
||||
/// over many similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
||||
/// over many similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this deque and
|
||||
/// *m* is the count of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import BasicContainers
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview && UnstableHashedContainers
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueSet where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
minimumCapacity: Int? = nil, from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
let c = producer.underestimatedCount
|
||||
if let minimumCapacity {
|
||||
self.init(minimumCapacity: Swift.min(minimumCapacity, c))
|
||||
} else {
|
||||
self.init(minimumCapacity: c)
|
||||
}
|
||||
try self.insert(from: &producer)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
_ensureFreeCapacity(Swift.max(producer.underestimatedCount, 1))
|
||||
try self.insert(addingCount: self.freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from drain: inout D
|
||||
) {
|
||||
while true {
|
||||
var span = drain.drainNext()
|
||||
guard !span.isEmpty else { break }
|
||||
while let next = span.popFirst() {
|
||||
self.insert(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,143 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import BasicContainers
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview && UnstableHashedContainers
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(capacity: capacity)
|
||||
try self.insert(addingCount: capacity, from: &producer)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
E: Error,
|
||||
P: CountedProducer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int?,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
let c = capacity ?? producer.count
|
||||
self.init(capacity: c)
|
||||
try self.insert(addingCount: c, from: &producer)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public init<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int? = nil,
|
||||
from drain: inout D
|
||||
) {
|
||||
let c = capacity ?? drain.count
|
||||
self.init(capacity: c)
|
||||
self.insert(addingCount: c, from: &drain)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet /* where Element: Copyable */ {
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<S: Container<Element> & Collection<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
)
|
||||
where S.Element == Element {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
self._insert(copying: contents)
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<S: Container<Element> & Sequence<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: S
|
||||
)
|
||||
where S.Element == Element {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
self._insert(copying: contents)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidSet where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int? = nil,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.insert(
|
||||
addingCount: newItemCount ?? freeCapacity
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: CountedProducer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: consuming P
|
||||
) throws(E) {
|
||||
try self.insert(addingCount: producer.count, from: &producer)
|
||||
try producer._expectEnd("Invalid CountedProducer")
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
D: Drain<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from drain: consuming D
|
||||
) {
|
||||
var remainder = drain.count
|
||||
while remainder > 0 {
|
||||
var span = drain.drainNext(maxCount: remainder)
|
||||
guard !span.isEmpty else { break }
|
||||
remainder &-= span.count
|
||||
while let next = span.popFirst() {
|
||||
self.insert(next)
|
||||
}
|
||||
}
|
||||
drain._expectEnd("Invalid Drain")
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@@ -107,13 +108,50 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
extension RangeReplaceableContainer
|
||||
where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
{
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the container and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the container cannot accommodate the specified number of new elements,
|
||||
/// then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation ends up inserting fewer than `newItemCount` items, then
|
||||
/// it may have to relocate elements to restore invariants after the partial
|
||||
/// insertion. This will come with some performance overhead when compared to
|
||||
/// adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the container to replace. The bounds of
|
||||
/// the range must be valid indices in the container.
|
||||
/// - newItemCount: The maximum number of items to insert into the container.
|
||||
/// - producer: A producer that generates the items to insert.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: some RangeExpression2<Index>,
|
||||
addingCount: Int,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(P.Failure)
|
||||
where P.Element: ~Copyable
|
||||
@@ -121,7 +159,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
try replace(
|
||||
removing: subrange.relative(to: self),
|
||||
consumingWith: { _ in },
|
||||
addingCount: count,
|
||||
addingCount: newItemCount,
|
||||
initializingWith: { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
}
|
||||
@@ -198,7 +236,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
C: RangeReplaceableContainer<Element> & ~Copyable & ~Escapable
|
||||
C: DrainableContainer<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing targetSubrange: some RangeExpression2<Index>,
|
||||
moving sourceSubrange: some RangeExpression2<C.Index>,
|
||||
@@ -212,9 +250,35 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
producer._expectEnd("Invalid Container")
|
||||
}
|
||||
|
||||
/// Replaces the specified range of elements by moving the elements of a
|
||||
/// given array into their place, consuming it in the process.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the array and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the array isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `newElements` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(consuming:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length buffer as the `newElements`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the array to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - newElements: An array whose contents to move into `self`.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newElements.count`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
C: RangeReplaceableContainer<Element> & ~Copyable & ~Escapable
|
||||
C: DrainableContainer<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing targetSubrange: some RangeExpression2<Index>,
|
||||
consuming source: consuming C
|
||||
@@ -454,7 +518,7 @@ where
|
||||
Element: Copyable
|
||||
{
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
public mutating func _replaceContainer<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
removing subrange: Range<Index>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
@@ -468,6 +532,101 @@ where
|
||||
precondition(c == 0, "Invalid RangeReplaceableContainer")
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given source container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the target container and inserting the new elements starting at the
|
||||
/// same location. The number of new elements need not match the number of
|
||||
/// elements being removed.
|
||||
///
|
||||
/// If the target container cannot accommodate the newly inserted items, then
|
||||
/// this operation results in a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. Calling
|
||||
/// the `insert(copying:at:)` method instead is preferred in this case.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. Calling the `removeSubrange(_:)` method instead is
|
||||
/// preferred in this case.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the container to replace. The bounds of
|
||||
/// the range must be valid indices in the container.
|
||||
/// - items: The new elements to copy into the container.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
removing subrange: Range<Index>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replaceContainer(removing: subrange, copying: items)
|
||||
}
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given source collection.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the target container and inserting the new elements starting at the
|
||||
/// same location. The number of new elements need not match the number of
|
||||
/// elements being removed.
|
||||
///
|
||||
/// If the target container cannot accommodate the newly inserted items, then
|
||||
/// this operation results in a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. Calling
|
||||
/// the `insert(copying:at:)` method instead is preferred in this case.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. Calling the `removeSubrange(_:)` method instead is
|
||||
/// preferred in this case.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the container to replace. The bounds of
|
||||
/// the range must be valid indices in the container.
|
||||
/// - items: The new elements to copy into the container.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<C: Collection<Element>>(
|
||||
removing subrange: Range<Index>,
|
||||
copying items: C
|
||||
) {
|
||||
var c = items.count
|
||||
|
||||
let done: Void? = items.withContiguousStorageIfAvailable { buffer in
|
||||
precondition(buffer.count == c, "Broken Collection: mismatching count")
|
||||
var buffer = buffer
|
||||
self.replace(removing: subrange, addingCount: c) { target in
|
||||
target._append(copying: buffer._trim(first: target.freeCapacity))
|
||||
}
|
||||
precondition(buffer.count == 0, "Invalid RangeReplaceableContainer")
|
||||
}
|
||||
if done != nil { return }
|
||||
|
||||
var it = items.makeIterator()
|
||||
self.replace(removing: subrange, addingCount: c) { target in
|
||||
while !target.isFull {
|
||||
guard let item = it.next() else {
|
||||
preconditionFailure("Broken Collection: mismatching count")
|
||||
}
|
||||
target.append(item)
|
||||
c -= 1
|
||||
}
|
||||
}
|
||||
precondition(it.next() == nil, "Broken Collection")
|
||||
precondition(c == 0, "Invalid RangeReplaceableContainer")
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<C: Container<Element> & Collection<Element>>(
|
||||
removing subrange: Range<Index>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replaceContainer(removing: subrange, copying: items)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
internal mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
|
||||
@@ -11,66 +11,17 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Strideable {
|
||||
@_alwaysEmitIntoClient
|
||||
package func _clampedUp(
|
||||
towards boundary: Self, maxDistance: Stride, limitedBy limit: Self
|
||||
) -> Self {
|
||||
assert(self <= boundary)
|
||||
assert(maxDistance >= 0)
|
||||
let limit = (limit >= self ? Swift.min(limit, boundary) : boundary)
|
||||
if limit.distance(to: self) <= maxDistance {
|
||||
return limit
|
||||
}
|
||||
return self.advanced(by: maxDistance)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
package func _clampedDown(
|
||||
towards boundary: Self, maxDistance: Stride, limitedBy limit: Self
|
||||
) -> Self {
|
||||
assert(self >= boundary)
|
||||
assert(maxDistance >= 0)
|
||||
let limit = (limit <= self ? Swift.max(limit, boundary) : boundary)
|
||||
if self.distance(to: limit) <= maxDistance {
|
||||
return limit
|
||||
}
|
||||
return self.advanced(by: -maxDistance)
|
||||
}
|
||||
}
|
||||
|
||||
extension Strideable {
|
||||
@inlinable
|
||||
package mutating func _advance(
|
||||
by distance: inout Stride, limitedBy limit: Self
|
||||
) {
|
||||
if distance >= 0 {
|
||||
guard limit >= self else {
|
||||
self = self.advanced(by: distance)
|
||||
distance = 0
|
||||
return
|
||||
}
|
||||
let d = Swift.min(distance, self.distance(to: limit))
|
||||
self = self.advanced(by: d)
|
||||
distance -= d
|
||||
} else {
|
||||
guard limit <= self else {
|
||||
self = self.advanced(by: distance)
|
||||
distance = 0
|
||||
return
|
||||
}
|
||||
let d = Swift.max(distance, self.distance(to: limit))
|
||||
self = self.advanced(by: d)
|
||||
distance -= d
|
||||
}
|
||||
}
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if UnstableContainersPreview
|
||||
extension Strideable {
|
||||
@inlinable
|
||||
public mutating func advance(
|
||||
by distance: inout Stride, limitedBy limit: Self
|
||||
) {
|
||||
_advance(by: &distance, limitedBy: limit)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -11,6 +11,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
/// A type that supplies the values of an in-place consumable sequence through
|
||||
|
||||
+7
-160
@@ -17,159 +17,21 @@ import InternalCollectionsUtilities
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension Iterable
|
||||
where
|
||||
Self: ~Copyable & ~Escapable, Element: Equatable,
|
||||
Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
package func _elementsEqual<
|
||||
Other: Iterable<Element, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: borrowing Other,
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element: ~Copyable
|
||||
{
|
||||
let it1 = self.makeBorrowingIterator()
|
||||
let it2 = other.makeBorrowingIterator()
|
||||
return try it1.elementsEqual(it2)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension Iterable
|
||||
where
|
||||
Self: ~Copyable & ~Escapable,
|
||||
Element: ~Copyable
|
||||
{
|
||||
/// Returns a Boolean value indicating whether two borrowing sequences contain
|
||||
/// equivalent elements in the same order, using the given predicate as the
|
||||
/// equivalence test.
|
||||
///
|
||||
/// The predicate must form an *equivalence relation* over the elements. That
|
||||
/// is, for any elements `a`, `b`, and `c`, the following conditions must
|
||||
/// hold:
|
||||
///
|
||||
/// - `areEquivalent(a, a)` is always `true`. (Reflexivity)
|
||||
/// - `areEquivalent(a, b)` implies `areEquivalent(b, a)`. (Symmetry)
|
||||
/// - If `areEquivalent(a, b)` and `areEquivalent(b, c)` are both `true`, then
|
||||
/// `areEquivalent(a, c)` is also `true`. (Transitivity)
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - other: A BorrowingSequence to compare to this BorrowingSequence.
|
||||
/// - areEquivalent: A predicate that returns `true` if its two arguments
|
||||
/// are equivalent; otherwise, `false`.
|
||||
/// - Returns: `true` if this BorrowingSequence and `other` contain equivalent items,
|
||||
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the count of the longer of the input sequences.
|
||||
@inlinable
|
||||
package func _elementsEqual<
|
||||
Other: Iterable & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: borrowing Other,
|
||||
by areEquivalent: (borrowing Element, borrowing Other.Element) throws(Failure) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element: ~Copyable, Other.Failure == Failure
|
||||
{
|
||||
let it1 = self.makeBorrowingIterator()
|
||||
let it2 = other.makeBorrowingIterator()
|
||||
return try it1.elementsEqual(it2, by: areEquivalent)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension BorrowingIteratorProtocol
|
||||
where
|
||||
Self: ~Copyable & ~Escapable,
|
||||
Element: ~Copyable & Equatable
|
||||
{
|
||||
@inlinable
|
||||
package consuming func elementsEqual<
|
||||
@_alwaysEmitIntoClient
|
||||
public consuming func elementsEqual<
|
||||
Other: BorrowingIteratorProtocol<Element, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element: ~Copyable
|
||||
where Other.Element: ~Copyable
|
||||
{
|
||||
var result = true
|
||||
try _spanwiseZip(state: &result, with: other) { state, a, b in
|
||||
if a.isEmpty || b.isEmpty {
|
||||
state = false
|
||||
return false
|
||||
}
|
||||
precondition(a.count == b.count)
|
||||
for i in 0 ..< a.count {
|
||||
guard a[unchecked: i] == b[unchecked: i] else {
|
||||
state = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@inlinable
|
||||
package consuming func _directElementsEqual<
|
||||
Other: BorrowingIteratorProtocol<Element, Failure> & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element: ~Copyable
|
||||
{
|
||||
#if true // FIXME: rdar://150228920 Exclusive access scopes aren't expanded enough
|
||||
// Note: This is the less efficient implementation of elementsEqual. The
|
||||
// variant in the #else branch would be preferable, but it doesn't work yet.
|
||||
// (It lets the two iterators run at their native speeds, with no artificial
|
||||
// maxCounts.)
|
||||
while true {
|
||||
let a = try self.nextSpan()
|
||||
var i = 0
|
||||
if a.isEmpty {
|
||||
return try other.nextSpan().isEmpty
|
||||
}
|
||||
while i < a.count {
|
||||
let b = try other.nextSpan(maxCount: a.count - i)
|
||||
if b.isEmpty {
|
||||
return false
|
||||
}
|
||||
precondition(b.count <= a.count - i)
|
||||
|
||||
var j = 0
|
||||
while j < b.count {
|
||||
guard a[unchecked: i] == b[unchecked: j] else { return false }
|
||||
i &+= 1
|
||||
j &+= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
var a = Span<Element>()
|
||||
var b = Span<Element>()
|
||||
loop:
|
||||
while true {
|
||||
if a.isEmpty {
|
||||
a = self.nextSpan()
|
||||
}
|
||||
if b.isEmpty {
|
||||
b = other.nextSpan()
|
||||
}
|
||||
if a.isEmpty || b.isEmpty {
|
||||
return a.isEmpty && b.isEmpty
|
||||
}
|
||||
|
||||
let c = Swift.min(a.count, b.count)
|
||||
var i = 0
|
||||
while i < c {
|
||||
guard a[unchecked: i] == b[unchecked: i] else { return false }
|
||||
i &+= 1
|
||||
}
|
||||
a = a.extracting(droppingFirst: c)
|
||||
b = b.extracting(droppingFirst: c)
|
||||
}
|
||||
#endif
|
||||
try self._elementsEqual(other)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,8 +41,8 @@ where
|
||||
Self: ~Copyable & ~Escapable,
|
||||
Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
package consuming func elementsEqual<
|
||||
@_alwaysEmitIntoClient
|
||||
public consuming func elementsEqual<
|
||||
Other: BorrowingIteratorProtocol & ~Copyable & ~Escapable
|
||||
>(
|
||||
_ other: consuming Other,
|
||||
@@ -188,22 +50,7 @@ where
|
||||
) throws(Failure) -> Bool
|
||||
where Other.Element: ~Copyable, Other.Failure == Failure
|
||||
{
|
||||
var result = true
|
||||
try _spanwiseZip(state: &result, with: other) { state, a, b throws(Failure) in
|
||||
assert(a.count == b.count || a.isEmpty || b.isEmpty)
|
||||
if a.isEmpty || b.isEmpty {
|
||||
state = false
|
||||
return false
|
||||
}
|
||||
for i in 0 ..< a.count {
|
||||
guard try areEquivalent(a[i], b[i]) else {
|
||||
state = false
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return result
|
||||
try self._elementsEqual(other, by: areEquivalent)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,55 +11,50 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension Iterable
|
||||
where Self: ~Copyable & ~Escapable, Element: ~Copyable
|
||||
{
|
||||
@inlinable
|
||||
public func elementsEqual<OtherSequence: Iterable>(
|
||||
_ other: borrowing OtherSequence,
|
||||
by areEquivalent: (borrowing Element, borrowing OtherSequence.Element) throws(Failure) -> Bool
|
||||
@_alwaysEmitIntoClient
|
||||
public borrowing func elementsEqual<Other: Iterable>(
|
||||
_ other: borrowing Other,
|
||||
// FIXME: The predicate should be able to throw an arbitrary error type,
|
||||
// but we cannot throw an error union yet.
|
||||
by areEquivalent: (borrowing Element, borrowing Other.Element) throws(Failure) -> Bool
|
||||
) throws(Failure) -> Bool
|
||||
where OtherSequence: ~Copyable & ~Escapable, OtherSequence.Element: ~Copyable, OtherSequence.Failure == Failure
|
||||
where
|
||||
Other: ~Copyable & ~Escapable,
|
||||
Other.Element: ~Copyable,
|
||||
// FIXME: This should not require Other.Failure == Failure; but we cannot
|
||||
// throw an error union yet.
|
||||
Other.Failure == Failure
|
||||
{
|
||||
// FIXME: Forward to the iterator's implementation of same
|
||||
var iter1 = makeBorrowingIterator()
|
||||
var iter2 = other.makeBorrowingIterator()
|
||||
while true {
|
||||
var el1 = try iter1.nextSpan()
|
||||
|
||||
if el1.isEmpty {
|
||||
// LHS is empty - sequences are equal iff RHS is also empty
|
||||
let el2 = try iter2.nextSpan(maxCount: 1)
|
||||
return el2.isEmpty
|
||||
}
|
||||
|
||||
while el1.count > 0 {
|
||||
let el2 = try iter2.nextSpan(maxCount: el1.count)
|
||||
if el2.isEmpty { return false }
|
||||
for i in 0..<el2.count {
|
||||
if try !areEquivalent(el1[i], el2[i]) { return false }
|
||||
}
|
||||
el1 = el1.extracting(droppingFirst: el2.count)
|
||||
}
|
||||
}
|
||||
try _elementsEqual(other, by: areEquivalent)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
extension Iterable
|
||||
where Self: ~Copyable & ~Escapable, Element: ~Copyable & Equatable {
|
||||
@inlinable
|
||||
public func elementsEqual<OtherSequence: Iterable<Element, Failure>>(
|
||||
_ other: borrowing OtherSequence
|
||||
@_alwaysEmitIntoClient
|
||||
public borrowing func elementsEqual<Other: Iterable>(
|
||||
_ other: borrowing Other
|
||||
) throws(Failure) -> Bool
|
||||
where
|
||||
OtherSequence: ~Copyable & ~Escapable,
|
||||
OtherSequence.Element: ~Copyable
|
||||
Other: ~Copyable & ~Escapable,
|
||||
Other.Element: ~Copyable,
|
||||
Other.Element == Element,
|
||||
// FIXME: This should not require Other.Failure == Failure; but we cannot
|
||||
// throw an error union yet.
|
||||
Other.Failure == Failure
|
||||
{
|
||||
return try self.elementsEqual(other, by: ==)
|
||||
try _elementsEqual(other)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
|
||||
@@ -17,7 +17,7 @@ else()
|
||||
${COLLECTIONS_DEQUE_SOURCES})
|
||||
target_link_libraries(DequeModule PRIVATE
|
||||
InternalCollectionsUtilities
|
||||
ContainersPreview)
|
||||
SpanPreview)
|
||||
set_target_properties(DequeModule PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
|
||||
|
||||
@@ -26,27 +26,41 @@ else()
|
||||
endif()
|
||||
|
||||
target_sources(${module_name} PRIVATE
|
||||
"_DequeSlot.swift"
|
||||
"_UnsafeDequeSegments.swift"
|
||||
"_UnsafeDequeHandle.swift"
|
||||
"_DequeSlot.swift"
|
||||
"RigidDeque/RigidDeque.swift"
|
||||
"Deque/Deque+Codable.swift"
|
||||
"Deque/Deque+Collection.swift"
|
||||
"Deque/Deque+CustomReflectable.swift"
|
||||
"Deque/Deque+Descriptions.swift"
|
||||
"Deque/Deque+Equatable.swift"
|
||||
"Deque/Deque+ExpressibleByArrayLiteral.swift"
|
||||
"Deque/Deque+Extras.swift"
|
||||
"Deque/Deque+Hashable.swift"
|
||||
"Deque/Deque+Testing.swift"
|
||||
"Deque/Deque._Storage.swift"
|
||||
"Deque/Deque._UnsafeHandle.swift"
|
||||
"Deque/Deque.swift"
|
||||
"Deque/_DequeBuffer.swift"
|
||||
"Deque/_DequeBufferHeader.swift"
|
||||
"RigidDeque/RigidDeque+Append.swift"
|
||||
"RigidDeque/RigidDeque+Consumption.swift"
|
||||
"RigidDeque/RigidDeque+Container.swift"
|
||||
"RigidDeque/RigidDeque+ContainerOperations.swift"
|
||||
"RigidDeque/RigidDeque+Descriptions.swift"
|
||||
"RigidDeque/RigidDeque+Equatable.swift"
|
||||
"RigidDeque/RigidDeque+Experimental.swift"
|
||||
"RigidDeque/RigidDeque+Hashable.swift"
|
||||
"RigidDeque/RigidDeque+Initializers.swift"
|
||||
"RigidDeque/RigidDeque+Insertions.swift"
|
||||
"RigidDeque/RigidDeque+Iterable.swift"
|
||||
"RigidDeque/RigidDeque+Prepend.swift"
|
||||
"RigidDeque/RigidDeque+Removals.swift"
|
||||
"RigidDeque/RigidDeque+Replacements.swift"
|
||||
"RigidDeque/RigidDeque+Testing.swift"
|
||||
"UniqueDeque/UniqueDeque.swift"
|
||||
"RigidDeque/RigidDeque.swift"
|
||||
"UniqueDeque/UniqueDeque+Append.swift"
|
||||
"UniqueDeque/UniqueDeque+Consumption.swift"
|
||||
"UniqueDeque/UniqueDeque+Container.swift"
|
||||
"UniqueDeque/UniqueDeque+ContainerOperations.swift"
|
||||
"UniqueDeque/UniqueDeque+Deprecated.swift"
|
||||
"UniqueDeque/UniqueDeque+Descriptions.swift"
|
||||
"UniqueDeque/UniqueDeque+Equatable.swift"
|
||||
@@ -54,21 +68,9 @@ target_sources(${module_name} PRIVATE
|
||||
"UniqueDeque/UniqueDeque+Hashable.swift"
|
||||
"UniqueDeque/UniqueDeque+Initializers.swift"
|
||||
"UniqueDeque/UniqueDeque+Insertions.swift"
|
||||
"UniqueDeque/UniqueDeque+Iterable.swift"
|
||||
"UniqueDeque/UniqueDeque+Prepend.swift"
|
||||
"UniqueDeque/UniqueDeque+Removals.swift"
|
||||
"UniqueDeque/UniqueDeque+Replacements.swift"
|
||||
"Deque/_DequeBuffer.swift"
|
||||
"Deque/Deque+Collection.swift"
|
||||
"Deque/Deque+CustomReflectable.swift"
|
||||
"Deque/Deque.swift"
|
||||
"Deque/Deque+Hashable.swift"
|
||||
"Deque/_DequeBufferHeader.swift"
|
||||
"Deque/Deque+Codable.swift"
|
||||
"Deque/Deque+Testing.swift"
|
||||
"Deque/Deque._UnsafeHandle.swift"
|
||||
"Deque/Deque+Descriptions.swift"
|
||||
"Deque/Deque+Extras.swift"
|
||||
"Deque/Deque+ExpressibleByArrayLiteral.swift"
|
||||
"Deque/Deque+Equatable.swift"
|
||||
"Deque/Deque._Storage.swift"
|
||||
"UniqueDeque/UniqueDeque.swift"
|
||||
)
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -177,80 +177,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Appends at most `newItemCount` items generated by a producer to the end of
|
||||
/// this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation appends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in the
|
||||
/// producer; if the producer has more, then they remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to append to the deque, or
|
||||
/// nil to use all available capacity.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the number of items
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.append(addingCount: newItemCount) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Appends all the items generated by a producer to the end of this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold all the
|
||||
/// new items, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*), where *n* is the number of items produced.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
try self.append(
|
||||
addingCount: freeCapacity
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else {
|
||||
done = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !done {
|
||||
done = (try producer.next() == nil)
|
||||
}
|
||||
precondition(done, "RigidDeque capacity overflow")
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -310,7 +236,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
internal mutating func _append<
|
||||
@@ -367,7 +293,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
precondition(it.next() == nil, "RigidDeque capacity overflow")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -221,10 +221,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque.SubrangeConsumer: Drain where Element: ~Copyable {
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque.SubrangeConsumer where Element: ~Copyable {
|
||||
@inlinable
|
||||
|
||||
-106
@@ -13,116 +13,10 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@frozen
|
||||
public struct BorrowingIterator: ~Escapable, BorrowingIteratorProtocol {
|
||||
@usableFromInline
|
||||
internal var _currentSegment: Span<Element>
|
||||
|
||||
@usableFromInline
|
||||
internal var _nextSegment: Span<Element>
|
||||
|
||||
@usableFromInline
|
||||
internal var _position: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow _deque)
|
||||
internal init(_deque: borrowing RigidDeque<Element>) {
|
||||
let segments = _deque._handle.segments()
|
||||
self._currentSegment = _overrideLifetime(
|
||||
Span(_unsafeElements: segments.first),
|
||||
borrowing: _deque)
|
||||
self._nextSegment = _overrideLifetime(
|
||||
Span(
|
||||
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
|
||||
borrowing: _deque)
|
||||
self._position = 0
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow _deque)
|
||||
internal init(
|
||||
_deque: borrowing RigidDeque<Element>,
|
||||
from start: Int,
|
||||
to end: Int
|
||||
) {
|
||||
precondition(start >= 0 && start <= _deque.count, "Index out of bounds")
|
||||
precondition(end >= 0 && end <= _deque.count, "Index out of bounds")
|
||||
precondition(start <= end, "The start must not be greater than the end")
|
||||
let segments = _deque._handle.segments(forOffsets: start ..< end)
|
||||
self._currentSegment = _overrideLifetime(
|
||||
Span(_unsafeElements: segments.first),
|
||||
borrowing: _deque)
|
||||
self._nextSegment = _overrideLifetime(
|
||||
Span(
|
||||
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
|
||||
borrowing: _deque)
|
||||
self._position = start
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(&self)
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func nextSpan(maxCount: Int) -> Span<Element> {
|
||||
let result = _currentSegment._trim(first: maxCount)
|
||||
if _currentSegment.isEmpty {
|
||||
_currentSegment = _nextSegment
|
||||
_nextSegment = Span()
|
||||
}
|
||||
_position &+= result.count
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self, from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
// FIXME: This should validate that the iterator belongs to this deque.
|
||||
return iterator._position
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -39,11 +38,9 @@ extension RigidDeque where Element: Equatable & ~Copyable {
|
||||
guard left.count == right.count else { return false }
|
||||
guard !left.isTriviallyIdentical(to: right) else { return true }
|
||||
|
||||
#if UnstableContainersPreview
|
||||
if #available(anyAppleOS 27, *) {
|
||||
return left._elementsEqual(right)
|
||||
}
|
||||
#endif
|
||||
for i in 0 ..< left.count {
|
||||
guard left[i] == right[i] else { return false }
|
||||
}
|
||||
|
||||
@@ -13,10 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2) && UnstableContainersPreview
|
||||
#if compiler(>=6.2)
|
||||
|
||||
#if false // FIXME
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -66,39 +65,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Creates a new rigid deque of the specified capacity, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// If the capacity is exhausted before the producer reaches its end, then
|
||||
/// this triggers a runtime error.
|
||||
///
|
||||
/// If the producer throws an error during this initializer, then all
|
||||
/// previously produced items are discarded and the initializer rethrows the
|
||||
/// error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(capacity: capacity)
|
||||
try self.append(from: &producer)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque /*where Element: Copyable*/ {
|
||||
/// Creates a new deque containing the specified number of a single,
|
||||
@@ -121,7 +87,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new deque with the specified capacity, holding a copy
|
||||
/// of the contents of a given borrowing sequence.
|
||||
///
|
||||
@@ -176,7 +142,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new deque with the specified capacity, holding a copy
|
||||
/// of the contents of a given sequence.
|
||||
///
|
||||
@@ -197,7 +163,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new deque with the specified capacity, holding a copy
|
||||
/// of the contents of a given collection.
|
||||
///
|
||||
@@ -219,7 +185,6 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
try self._append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -207,57 +207,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Inserts at most `newItemCount` items generated by a producer into this
|
||||
/// deque, starting at the given index.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items
|
||||
/// left in its underlying sequence, then extra items remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results
|
||||
/// in a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - index: The position at which to insert the new items.
|
||||
/// `index` must be a valid index in the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P,
|
||||
at index: Int
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try insert(addingCount: newItemCount, at: index) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque /* where Element: Copyable */ {
|
||||
/// Copies the elements of a fully initialized buffer pointer into this
|
||||
@@ -355,29 +304,8 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && self.count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
internal mutating func _insertCollection(
|
||||
package mutating func _insertCollection(
|
||||
at index: Int,
|
||||
copying items: some Collection<Element>,
|
||||
newCount: Int
|
||||
@@ -398,40 +326,6 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
"Broken Collection: count doesn't match contents")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`).
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Copies the elements of a collection into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -460,40 +354,6 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
_insertCollection(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,88 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift Collections open source project
|
||||
//
|
||||
// Copyright (c) 2025 - 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 !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4)
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidDeque: Iterable where Element: ~Copyable {
|
||||
@frozen
|
||||
public struct BorrowingIterator: ~Escapable, BorrowingIteratorProtocol {
|
||||
@usableFromInline
|
||||
package var _currentSegment: Span<Element>
|
||||
|
||||
@usableFromInline
|
||||
package var _nextSegment: Span<Element>
|
||||
|
||||
@usableFromInline
|
||||
package var _position: Int
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow _deque)
|
||||
package init(_deque: borrowing RigidDeque<Element>) {
|
||||
let segments = _deque._handle.segments()
|
||||
self._currentSegment = _overrideLifetime(
|
||||
Span(_unsafeElements: segments.first),
|
||||
borrowing: _deque)
|
||||
self._nextSegment = _overrideLifetime(
|
||||
Span(
|
||||
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
|
||||
borrowing: _deque)
|
||||
self._position = 0
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow _deque)
|
||||
package init(
|
||||
_deque: borrowing RigidDeque<Element>,
|
||||
from start: Int,
|
||||
to end: Int
|
||||
) {
|
||||
precondition(start >= 0 && start <= _deque.count, "Index out of bounds")
|
||||
precondition(end >= 0 && end <= _deque.count, "Index out of bounds")
|
||||
precondition(start <= end, "The start must not be greater than the end")
|
||||
let segments = _deque._handle.segments(forOffsets: start ..< end)
|
||||
self._currentSegment = _overrideLifetime(
|
||||
Span(_unsafeElements: segments.first),
|
||||
borrowing: _deque)
|
||||
self._nextSegment = _overrideLifetime(
|
||||
Span(
|
||||
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
|
||||
borrowing: _deque)
|
||||
self._position = start
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(&self)
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func nextSpan(maxCount: Int) -> Span<Element> {
|
||||
let result = _currentSegment._trim(first: maxCount)
|
||||
if _currentSegment.isEmpty {
|
||||
_currentSegment = _nextSegment
|
||||
_nextSegment = Span()
|
||||
}
|
||||
_position &+= result.count
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -182,49 +182,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Prepends at most `newItemCount` items generated by a producer to the
|
||||
/// front of this deque.
|
||||
///
|
||||
/// If the target deque does not have sufficient capacity to hold the
|
||||
/// specified number of new items, then this triggers a runtime error.
|
||||
///
|
||||
/// This operation prepends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in
|
||||
/// the producer; if the producer has more, then they remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// Note: Partial prepends create a gap in ring buffer storage that needs to
|
||||
/// be closed by moving newly prepended items to their correct positions given
|
||||
/// the adjusted count. This adds some overhead compared to adding exactly as
|
||||
/// many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to prepend to the deque, or
|
||||
/// nil to use all available capacity.
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(`newItemCount ?? self.freeCapacity`)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int? = nil,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try self.prepend(
|
||||
addingCount: newItemCount ?? freeCapacity
|
||||
) { target throws(E) in
|
||||
try producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -287,7 +244,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
@@ -310,7 +267,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _prepend<
|
||||
package mutating func _prepend<
|
||||
S: Iterable & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing S,
|
||||
@@ -346,24 +303,6 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
where S.Element == Element {
|
||||
try self._prepend(copying: items)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// container, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(`items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Prepend the elements of a sequence to the front of this deque by copying
|
||||
@@ -435,7 +374,7 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
_handle.uncheckedPrepend(copying: items, exactCount: c)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the front of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
@@ -471,40 +410,6 @@ extension RigidDeque /*where Element: Copyable*/ {
|
||||
where S.Element == Element {
|
||||
try self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the front of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the front of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -278,66 +278,6 @@ extension RigidDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the deque.
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `maxCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(
|
||||
removing: subrange,
|
||||
addingCount: newItemCount
|
||||
) { target throws(E) in
|
||||
while !target.isFull {
|
||||
guard try producer.generate(into: &target) else { break }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -454,28 +394,6 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
unsafe self.replace(removing: subrange, copying: buffer)
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
internal mutating func _replace(
|
||||
@@ -503,47 +421,6 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
"Broken Collection: count doesn't match contents")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given collection.
|
||||
///
|
||||
@@ -579,48 +456,6 @@ extension RigidDeque /* where Element: Copyable */ {
|
||||
_replace(
|
||||
removing: subrange, copyingCollection: items, newCount: items.count)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the capacity of the deque isn't sufficient to accommodate the new
|
||||
/// elements, then this method triggers a runtime error.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this deque and
|
||||
/// *m* is the count of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
// This file contains exported but non-public entry points to support clear box
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(<6.2)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -167,74 +167,6 @@ extension UniqueDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Appends at most `newItemCount` items generated by a producer to the end of
|
||||
/// this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the specified
|
||||
/// number of new elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// This operation appends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in the
|
||||
/// producer; if the producer has more, then they remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to append to the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage.append(addingCount: newItemCount, from: &producer)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Appends all the items generated by a producer to the end of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate all the
|
||||
/// elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similarly-sized invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
try self.append(addingCount: c) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -297,7 +229,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
_storage.append(copying: items)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
internal mutating func _append<S: Iterable & ~Copyable & ~Escapable>(
|
||||
@@ -364,7 +296,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the end of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
@@ -385,7 +317,6 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
try self._append(copying: items)
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
-50
@@ -13,60 +13,10 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
public typealias BorrowingIterator = RigidDeque<Element>.BorrowingIterator
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self._storage)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: Container where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public func makeBorrowingIterator(
|
||||
from start: Index, to end: Index
|
||||
) -> BorrowingIterator {
|
||||
_storage.makeBorrowingIterator(from: start, to: end)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
|
||||
_storage.currentIndex(of: &iterator)
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: BidirectionalContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: RandomAccessContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: MutableContainer where Element: ~Copyable {}
|
||||
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: RangeReplaceableContainer where Element: ~Copyable {}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque: DynamicContainer where Element: ~Copyable {}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -13,10 +13,9 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2) && UnstableContainersPreview
|
||||
#if compiler(>=6.2)
|
||||
|
||||
#if false // FIXME
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -67,54 +66,6 @@ extension UniqueDeque where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Creates a new unique deque of the specified capacity, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
capacity: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(minimumCapacity: capacity)
|
||||
try self.append(addingCount: capacity, from: &producer)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Creates a new unique deque, populating it with
|
||||
/// all items generated by a given producer.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to append to the new
|
||||
/// deque.
|
||||
///
|
||||
/// - Complexity: O(`capacity`)
|
||||
public init<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
self.init(minimumCapacity: producer.underestimatedCount)
|
||||
try self.append(from: &producer)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque /*where Element: Copyable*/ {
|
||||
/// Creates a new deque containing the specified number of a single,
|
||||
@@ -134,7 +85,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque /*where Element: Copyable*/ {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new deque with the specified capacity, holding a copy
|
||||
/// of the contents of a given borrowing sequence.
|
||||
///
|
||||
@@ -172,7 +123,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Creates a new deque with the specified capacity, holding a copy
|
||||
/// of the contents of a given sequence.
|
||||
///
|
||||
@@ -212,7 +163,6 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
try self._append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -217,59 +217,6 @@ extension UniqueDeque where Element: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Inserts at most `newItemCount` items generated by a producer into this
|
||||
/// deque, starting at the given index.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items
|
||||
/// left in its underlying sequence, then extra items remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results
|
||||
/// in a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - index: The position at which to insert the new items.
|
||||
/// `index` must be a valid index in the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func insert<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P,
|
||||
at index: Int
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try insert(addingCount: newItemCount, at: index) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque /* where Element: Copyable */ {
|
||||
/// Copies the elements of a fully initialized buffer pointer into this
|
||||
@@ -373,29 +320,8 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
at index: Int,
|
||||
copying items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
let expectedCount = self.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
insert(addingCount: newCount, at: index) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
internal mutating func _insertCollection(
|
||||
package mutating func _insertCollection(
|
||||
at index: Int,
|
||||
copying items: some Collection<Element>,
|
||||
newCount: Int
|
||||
@@ -417,42 +343,6 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
"Broken Collection: count doesn't match contents")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Copies the elements of a collection into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -483,42 +373,6 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
_insertCollection(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Copies the elements of a container into this deque at the specified
|
||||
/// position.
|
||||
///
|
||||
/// The new elements are inserted before the element currently at the
|
||||
/// specified index. If you pass the deque's `endIndex` as the `index`
|
||||
/// parameter, then the new elements are appended to the end of the deque.
|
||||
///
|
||||
/// Existing elements in the deque's storage are moved as needed to make room
|
||||
/// for the new items. (The direction of the move depends on the location of
|
||||
/// the insertion, minimizing the cost.)
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold enough items,
|
||||
/// then this reallocates the deque's storage to grow its capacity, using a
|
||||
/// geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to insert into the deque.
|
||||
/// - index: The position at which to insert the new elements. It must be
|
||||
/// a valid index of the deque.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`) when amortized over many
|
||||
/// similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func insert<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
copying items: borrowing C, at index: Int
|
||||
) {
|
||||
_insertContainer(
|
||||
at: index, copying: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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.4)
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueDeque where Element: ~Copyable {
|
||||
public typealias BorrowingIterator = RigidDeque<Element>.BorrowingIterator
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@_lifetime(borrow self)
|
||||
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
|
||||
BorrowingIterator(_deque: self._storage)
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -172,88 +172,6 @@ extension UniqueDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Prepends at most `newItemCount` items generated by a producer to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the specified
|
||||
/// number of new elements, then this method reallocates the deque's storage
|
||||
/// to grow it, using a geometric growth rate.
|
||||
///
|
||||
/// This operation prepends as many items as the producer can generate before
|
||||
/// either reaching its end (or throwing an error), or filling the specified
|
||||
/// capacity. This operation only consumes the first `newItemCount` items in
|
||||
/// the producer; if the producer has more, then they remain available after
|
||||
/// this method returns.
|
||||
///
|
||||
/// Note: Partial prepends create a gap in ring buffer storage that needs to
|
||||
/// be closed by moving newly prepended items to their correct positions given
|
||||
/// the adjusted count. This adds some overhead compared to adding exactly as
|
||||
/// many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - newItemCount: The maximum number of items to prepend to the deque.
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similar invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
_ensureFreeCapacity(newItemCount)
|
||||
try _storage._handle.uncheckedPrepend(
|
||||
addingCount: newItemCount
|
||||
) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Prepends all items generated by a producer to the front of this deque.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate all the
|
||||
/// items, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - producer: A producer that generates the items to prepend.
|
||||
///
|
||||
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
||||
/// amortized over many similarly-sized invocations on the same deque.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func prepend<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
var done = false
|
||||
while !done {
|
||||
let c = Swift.max(producer.underestimatedCount, 1)
|
||||
_ensureFreeCapacity(c)
|
||||
try self.prepend(addingCount: freeCapacity) { target throws(E) in
|
||||
while !target.isFull, !done {
|
||||
done = try !producer.generate(into: &target)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -319,7 +237,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _prepend<S: Iterable & ~Copyable & ~Escapable>(
|
||||
@@ -341,7 +259,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _prepend<S: Iterable & ~Copyable & ~Escapable>(
|
||||
package mutating func _prepend<S: Iterable & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing S,
|
||||
exactCount: Int
|
||||
) throws(S.Failure)
|
||||
@@ -376,25 +294,6 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
where S.Element == Element {
|
||||
try self._prepend(copying: items)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container and prepends them to the front
|
||||
/// of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// container, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(`items.count`) when amortized over many similar
|
||||
/// invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Prepend the elements of a sequence to the front of this deque by copying
|
||||
@@ -470,7 +369,7 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
_storage._handle.uncheckedPrepend(copying: items, exactCount: c)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
#if compiler(>=6.4)
|
||||
/// Copies the elements of a borrowing sequence to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
@@ -508,42 +407,6 @@ extension UniqueDeque /*where Element: Copyable*/ {
|
||||
where S.Element == Element {
|
||||
try self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
||||
/// over many similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
||||
copying items: borrowing C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
|
||||
/// Copies the elements of a container to the end of this deque.
|
||||
///
|
||||
/// If the deque does not have sufficient capacity to hold all items in the
|
||||
/// sequence, then this triggers a runtime error.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - items: The new elements to copy into the deque.
|
||||
///
|
||||
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
||||
/// over many similar invocations on the same deque.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
||||
copying items: C
|
||||
) {
|
||||
self._prepend(copying: items, exactCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -13,7 +13,6 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
#if !COLLECTIONS_SINGLE_MODULE
|
||||
import InternalCollectionsUtilities
|
||||
import ContainersPreview
|
||||
import SpanPreview
|
||||
#endif
|
||||
|
||||
#if compiler(>=6.2)
|
||||
@@ -286,64 +286,6 @@ extension UniqueDeque where Element: ~Copyable {
|
||||
count = 0
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified range of elements by at most `newItemCount` items
|
||||
/// generated by a producer.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the items generated by `producer` at `subrange.lowerBound`. This
|
||||
/// case is more directly expressed by calling `insert(moving:at:)`.
|
||||
///
|
||||
/// Likewise, if the given producer is empty, then this method removes the
|
||||
/// elements in the given subrange without replacement. This case is more
|
||||
/// directly expressed by calling `removeSubrange`.
|
||||
///
|
||||
/// This operation inserts as many items as the producer can generate before
|
||||
/// either reaching `newItemCount`, or the producer hitting its end, or
|
||||
/// throwing an error. If the producer has more than `newItemCount` items left in
|
||||
/// its underlying sequence, then extra items remain available after this
|
||||
/// method returns.
|
||||
///
|
||||
/// If the operation inserts fewer than `newItemCount` items, then it results in
|
||||
/// a gap in ring buffer storage that needs to be closed by moving some
|
||||
/// items to their correct positions given the adjusted count. This adds some
|
||||
/// overhead compared to adding exactly as many items as promised.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the deque.
|
||||
/// - newItemCount: The maximum number of items to insert into the deque.
|
||||
/// - producer: A producer that generates the items to append.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `newItemCount`)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func replace<
|
||||
E: Error,
|
||||
P: Producer<Element, E> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
addingCount newItemCount: Int,
|
||||
from producer: inout P
|
||||
) throws(E)
|
||||
where P.Element: ~Copyable
|
||||
{
|
||||
try replace(removing: subrange, addingCount: newItemCount) { target throws(E) in
|
||||
while !target.isFull, try producer.generate(into: &target) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
@@ -463,28 +405,6 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
unsafe self.replace(removing: subrange, copying: buffer)
|
||||
}
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
internal mutating func _replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copyingContainer items: borrowing C,
|
||||
newCount: Int
|
||||
) {
|
||||
|
||||
let expectedCount = self.count - subrange.count + newCount
|
||||
var it = items.makeBorrowingIterator()
|
||||
self.replace(removing: subrange, addingCount: newCount) { target in
|
||||
it._copyContents(into: &target)
|
||||
}
|
||||
precondition(
|
||||
it.nextSpan().isEmpty && count == expectedCount,
|
||||
"Broken Container: count doesn't match contents")
|
||||
}
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
internal mutating func _replace(
|
||||
@@ -512,48 +432,6 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
"Broken Collection: count doesn't match contents")
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(`self.count` + `items.count`)
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: borrowing C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given collection.
|
||||
///
|
||||
@@ -590,49 +468,6 @@ extension UniqueDeque /* where Element: Copyable */ {
|
||||
_replace(
|
||||
removing: subrange, copyingCollection: items, newCount: items.count)
|
||||
}
|
||||
|
||||
#if compiler(>=6.4) && UnstableContainersPreview
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
/// This method has the effect of removing the specified range of elements
|
||||
/// from the deque and inserting the new elements starting at the same
|
||||
/// location. The number of new elements need not match the number of elements
|
||||
/// being removed.
|
||||
///
|
||||
/// If the deque doesn't have sufficient capacity to accommodate the new
|
||||
/// elements, then this method reallocates the deque's storage to grow it,
|
||||
/// using a geometric growth rate.
|
||||
///
|
||||
/// If you pass a zero-length range as the `subrange` parameter, this method
|
||||
/// inserts the elements of `items` at `subrange.lowerBound`. This case
|
||||
/// is more directly expressed by calling `insert(copying:at:)`.
|
||||
///
|
||||
/// Likewise, if you pass a zero-length container as the `items`
|
||||
/// parameter, this method removes the elements in the given subrange
|
||||
/// without replacement. This case is more directly expressed by calling
|
||||
/// `removeSubrange`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - subrange: The subrange of the deque to replace. The bounds of
|
||||
/// the range must be valid indices in the array.
|
||||
/// - items: The new elements to copy into the collection.
|
||||
///
|
||||
/// - Complexity: O(*n* + *m*), where *n* is count of this deque and
|
||||
/// *m* is the count of `items`.
|
||||
@available(SwiftStdlib 6.4, *)
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func replace<
|
||||
C: Container<Element> & Collection<Element>
|
||||
>(
|
||||
removing subrange: Range<Int>,
|
||||
copying items: C
|
||||
) {
|
||||
_replace(
|
||||
removing: subrange, copyingContainer: items, newCount: items.count)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
Loaded 100 of 131 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user