mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
Replace Container with a far less powerful (but more universal) Iterable construct
I’m hoping Container will return later.
This commit is contained in:
26 files changed
+1071
-413
No files matched your search
@@ -274,9 +274,9 @@ extension RigidArray {
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
Source: Container<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copyingContainer newElements: borrowing Source
|
||||
copyingIterable newElements: borrowing Source
|
||||
) {
|
||||
let target = _freeSpace
|
||||
_count += newElements._copyContents(intoPrefixOf: target)
|
||||
@@ -296,11 +296,11 @@ extension RigidArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Container<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
_append(copyingContainer: newElements)
|
||||
_append(copyingIterable: newElements)
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -338,9 +338,9 @@ extension RigidArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Container<Element> & Sequence<Element>
|
||||
Source: Iterable<Element> & Sequence<Element>
|
||||
>(copying newElements: Source) {
|
||||
_append(copyingContainer: newElements)
|
||||
_append(copyingIterable: newElements)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -98,15 +98,38 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Container<Element> & ~Copyable & ~Escapable>(
|
||||
capacity: Int? = nil,
|
||||
public init<Source: Iterable<Element> & ~Copyable & ~Escapable>(
|
||||
capacity: Int,
|
||||
copying contents: borrowing Source
|
||||
) {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
self.init(capacity: capacity)
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// Creates a new array with the specified capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - capacity: The storage capacity of the new array, or nil to allocate
|
||||
/// just enough capacity to store the contents.
|
||||
/// - contents: The container whose contents to copy into the new array.
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Iterable<Element> & Sequence<Element>>(
|
||||
capacity: Int,
|
||||
copying contents: Source
|
||||
) {
|
||||
self.init(capacity: capacity)
|
||||
self.append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// FIXME: Add a version that's generic over `Container`, with an optional capacity
|
||||
|
||||
/// Creates a new array with the specified capacity, holding a copy
|
||||
/// of the contents of a given collection.
|
||||
///
|
||||
@@ -124,26 +147,6 @@ extension RigidArray /*where Element: Copyable*/ {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
self.append(copying: contents)
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// Creates a new array with the specified capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - capacity: The storage capacity of the new array, or nil to allocate
|
||||
/// just enough capacity to store the contents.
|
||||
/// - contents: The container whose contents to copy into the new array.
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Container<Element> & Sequence<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: Source
|
||||
) {
|
||||
self.init(capacity: capacity ?? contents.count)
|
||||
self.append(copying: contents)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// FIXME: Add init(moving:), init(consuming:)
|
||||
|
||||
@@ -341,6 +341,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
@inlinable
|
||||
internal mutating func _insertContainer<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
@@ -359,6 +360,7 @@ extension RigidArray {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
@@ -388,6 +390,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Copies the elements of a container into this array at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -417,6 +420,7 @@ extension RigidArray {
|
||||
_insertContainer(
|
||||
at: index, copying: newElements, newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Copies the elements of a collection into this array at the specified
|
||||
@@ -448,6 +452,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Copies the elements of a container into this array at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -478,6 +483,7 @@ extension RigidArray {
|
||||
at: index, copying: newElements, newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -382,6 +382,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
@inlinable
|
||||
internal mutating func _replaceSubrange<
|
||||
C: Container<Element> & ~Copyable & ~Escapable
|
||||
@@ -399,6 +400,7 @@ extension RigidArray {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@inlinable
|
||||
@@ -429,6 +431,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
@@ -466,6 +469,7 @@ extension RigidArray {
|
||||
_replaceSubrange(
|
||||
subrange, copyingContainer: newElements, newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
@@ -505,6 +509,7 @@ extension RigidArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
@@ -544,6 +549,7 @@ extension RigidArray {
|
||||
subrange, copyingContainer: newElements, newCount: newElements.count)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -286,13 +286,49 @@ extension RigidArray where Element: ~Copyable {
|
||||
|
||||
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension RigidArray: Container where Element: ~Copyable {
|
||||
public typealias BorrowIterator = Span<Element>
|
||||
|
||||
extension RigidArray: Iterable where Element: ~Copyable {
|
||||
@frozen
|
||||
public struct BorrowIterator: ~Copyable, ~Escapable, BorrowIteratorProtocol {
|
||||
@usableFromInline
|
||||
internal let _span: Span<Element>
|
||||
|
||||
@usableFromInline
|
||||
internal var _offset: Int
|
||||
|
||||
@inlinable
|
||||
@_lifetime(copy span)
|
||||
internal init(_span span: Span<Element>, offset: Int) {
|
||||
self._span = span
|
||||
self._offset = offset
|
||||
}
|
||||
|
||||
@_lifetime(&self)
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func nextSpan(maximumCount: Int) -> Span<Element> {
|
||||
let c = Swift.min(maximumCount, _span.count - _offset)
|
||||
let end = _offset &+ c
|
||||
let result = _span.extracting(Range(uncheckedBounds: (_offset, end)))
|
||||
_offset = end
|
||||
return result
|
||||
}
|
||||
|
||||
@_lifetime(self: copy self)
|
||||
public mutating func skip(by offset: Int) -> Int {
|
||||
let c = Swift.min(offset, _span.count &- _offset)
|
||||
_offset += offset
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var estimatedCount: EstimatedCount {
|
||||
.exactly(count)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func startBorrowIteration() -> Span<Element> {
|
||||
self.span
|
||||
public func startBorrowIteration() -> BorrowIterator {
|
||||
.init(_span: self.span, offset: 0)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -231,11 +231,32 @@ extension UniqueArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// Copies the elements of a container to the end of this array.
|
||||
@inlinable
|
||||
internal mutating func _append<
|
||||
Source: Iterable<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copyingIterable newElements: borrowing Source
|
||||
) {
|
||||
_ensureFreeCapacity(newElements.underestimatedCount)
|
||||
var it = newElements.startBorrowIteration()
|
||||
while true {
|
||||
let span = it.nextSpan()
|
||||
if span.isEmpty { break }
|
||||
_ensureFreeCapacity(span.count)
|
||||
_storage.append(copying: span)
|
||||
}
|
||||
}
|
||||
// FIXME: Add _append(copyingContainer:), forwarding to the same method on RigidArray
|
||||
#endif
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// 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
|
||||
/// a geometric growth rate.
|
||||
/// a geometric growth rate. If the input sequence does not provide a precise
|
||||
/// estimate of its count, then the array's storage may need to be resized
|
||||
/// more than once.
|
||||
///
|
||||
/// - Parameters
|
||||
/// - newElements: A container whose contents to copy into the array.
|
||||
@@ -245,12 +266,11 @@ extension UniqueArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Container<Element> & ~Copyable & ~Escapable
|
||||
Source: Iterable<Element> & ~Copyable & ~Escapable
|
||||
>(
|
||||
copying newElements: borrowing Source
|
||||
) {
|
||||
_ensureFreeCapacity(newElements.count)
|
||||
_storage._append(copyingContainer: newElements)
|
||||
self._append(copyingIterable: newElements)
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -259,7 +279,7 @@ extension UniqueArray {
|
||||
///
|
||||
/// If the array does not have sufficient capacity to hold enough elements,
|
||||
/// then this reallocates the array's storage to extend its capacity, using
|
||||
/// a geometric growth rate. If the input sequence does not provide a correct
|
||||
/// a geometric growth rate. If the input sequence does not provide a precise
|
||||
/// estimate of its count, then the array's storage may need to be resized
|
||||
/// more than once.
|
||||
///
|
||||
@@ -300,10 +320,9 @@ extension UniqueArray {
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public mutating func append<
|
||||
Source: Container<Element> & Sequence<Element>
|
||||
Source: Iterable<Element> & Sequence<Element>
|
||||
>(copying newElements: Source) {
|
||||
_ensureFreeCapacity(newElements.count)
|
||||
_storage._append(copyingContainer: newElements)
|
||||
self._append(copyingIterable: newElements)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -70,16 +70,16 @@ extension UniqueArray where Element: ~Copyable {
|
||||
extension UniqueArray /*where Element: Copyable*/ {
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
/// Creates a new array with the specified initial capacity, holding a copy
|
||||
/// of the contents of a given container.
|
||||
/// of the contents of a given iterable.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - capacity: The storage capacity of the new array, or nil to allocate
|
||||
/// just enough capacity to store the contents.
|
||||
/// - contents: The container whose contents to copy into the new array.
|
||||
/// The container must not contain more than `capacity` elements.
|
||||
/// - contents: An iterable whose contents to copy into the new array.
|
||||
/// The iterable must not contain more than `capacity` elements.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Container<Element> & ~Copyable & ~Escapable>(
|
||||
public init<Source: Iterable<Element> & ~Copyable & ~Escapable>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: borrowing Source
|
||||
) {
|
||||
@@ -115,7 +115,7 @@ extension UniqueArray /*where Element: Copyable*/ {
|
||||
/// - contents: The container whose contents to copy into the new array.
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public init<Source: Container<Element> & Sequence<Element>>(
|
||||
public init<Source: Iterable<Element> & Sequence<Element>>(
|
||||
capacity: Int? = nil,
|
||||
copying contents: Source
|
||||
) {
|
||||
|
||||
@@ -301,6 +301,7 @@ extension UniqueArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Copies the elements of a container into this array at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -334,6 +335,7 @@ extension UniqueArray {
|
||||
_ensureFreeCapacity(c)
|
||||
_storage._insertContainer(at: index, copying: newElements, newCount: c)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Copies the elements of a collection into this array at the specified
|
||||
@@ -368,6 +370,7 @@ extension UniqueArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Copies the elements of a container into this array at the specified
|
||||
/// position.
|
||||
///
|
||||
@@ -402,6 +405,7 @@ extension UniqueArray {
|
||||
_storage._insertContainer(at: index, copying: newElements, newCount: c)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -378,6 +378,7 @@ extension UniqueArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
@@ -420,6 +421,7 @@ extension UniqueArray {
|
||||
_storage._replaceSubrange(
|
||||
subrange, copyingContainer: newElements, newCount: c)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
@@ -464,6 +466,7 @@ extension UniqueArray {
|
||||
}
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
#if false // FIXME: This needs a container with an exact count.
|
||||
/// Replaces the specified subrange of elements by copying the elements of
|
||||
/// the given container.
|
||||
///
|
||||
@@ -507,5 +510,6 @@ extension UniqueArray {
|
||||
subrange, copyingContainer: newElements, newCount: c)
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -177,12 +177,16 @@ extension UniqueArray where Element: ~Copyable {
|
||||
|
||||
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
|
||||
@available(SwiftStdlib 5.0, *)
|
||||
extension UniqueArray: Container where Element: ~Copyable {
|
||||
extension UniqueArray: Iterable where Element: ~Copyable {
|
||||
public typealias BorrowIterator = RigidArray<Element>.BorrowIterator
|
||||
|
||||
public var estimatedCount: ContainersPreview.EstimatedCount {
|
||||
.exactly(count)
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@inline(__always)
|
||||
public func startBorrowIteration() -> Span<Element> {
|
||||
public func startBorrowIteration() -> BorrowIterator {
|
||||
self._storage.startBorrowIteration()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user