mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
211 lines
7.5 KiB
Swift
211 lines
7.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2026 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE
|
|
import _FoundationCollections
|
|
#elseif COLLECTIONS_SINGLE_MODULE
|
|
import Collections
|
|
#else
|
|
import InternalCollectionsUtilities
|
|
import DequeModule
|
|
#endif
|
|
|
|
#if compiler(>=6.4) && UnstableContainersPreview
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
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 6.4, *)
|
|
extension UniqueDeque: BidirectionalContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque: RandomAccessContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque: MutableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque: DrainableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque: RangeReplaceableContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque: DynamicContainer where Element: ~Copyable {}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
extension UniqueDeque where Element: ~Copyable {
|
|
/// 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.
|
|
/// - Returns: A valid index range addressing the newly inserted items.
|
|
/// - Complexity: O(*n*) where *n* is the number of new items, when
|
|
/// amortized over many similar invocations on the same deque.
|
|
@_alwaysEmitIntoClient
|
|
@inline(__always)
|
|
@discardableResult
|
|
public mutating func prepend<
|
|
E: Error,
|
|
P: Producer<Element, E> & ~Copyable & ~Escapable
|
|
>(
|
|
addingCount newItemCount: Int,
|
|
from producer: inout P
|
|
) throws(E) -> Range<Int>
|
|
where P.Element: ~Copyable
|
|
{
|
|
_ensureFreeCapacity(newItemCount)
|
|
return unsafe try _storage._handle.uncheckedPrepend(
|
|
addingCount: newItemCount
|
|
) { target throws(E) in
|
|
try producer.generate(into: &target)
|
|
}
|
|
}
|
|
|
|
/// 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 counted producer that generates the items to prepend.
|
|
/// - Returns: A valid index range addressing the newly inserted items.
|
|
/// - 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)
|
|
@discardableResult
|
|
public mutating func prepend<
|
|
E: Error,
|
|
P: Producer<Element, E> & ~Copyable & ~Escapable
|
|
>(
|
|
from producer: consuming P
|
|
) throws(E) -> Range<Int>
|
|
where P.Element: ~Copyable
|
|
{
|
|
// FIXME: This is slower than it should be because it is moving elements
|
|
// unnecessarily. Forward to append then rotate instead; additionally,
|
|
// add a CountedProducer overload that avoids having to move items at all.
|
|
var done = false
|
|
var added = 0
|
|
while !done {
|
|
let c = Swift.max(producer.underestimatedCount, 1)
|
|
_ensureFreeCapacity(c)
|
|
_ = unsafe try _storage._handle.uncheckedInsert(
|
|
addingCount: freeCapacity,
|
|
at: added
|
|
) { target throws(E) in
|
|
let origCount = target.count
|
|
defer {
|
|
let d = target.count - origCount
|
|
precondition(d >= 0, "Invalid Producer")
|
|
added &+= d
|
|
}
|
|
done = try producer.generate(into: &target) == 0
|
|
}
|
|
}
|
|
return unsafe Range(uncheckedBounds: (0, added))
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.4, *)
|
|
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.
|
|
/// - Returns: A valid index range addressing the newly inserted items.
|
|
/// - Complexity: O(`items.count`) when amortized over many similar
|
|
/// invocations on the same deque.
|
|
@_alwaysEmitIntoClient
|
|
@discardableResult
|
|
public mutating func prepend<C: Container<Element> & ~Copyable & ~Escapable>(
|
|
copying items: borrowing C
|
|
) -> Range<Index> {
|
|
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.
|
|
/// - Returns: A valid index range addressing the newly inserted items.
|
|
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
|
/// over many similar invocations on the same deque.
|
|
@_alwaysEmitIntoClient
|
|
@discardableResult
|
|
public mutating func prepend<C: Container<Element> & Sequence<Element>>(
|
|
copying items: borrowing C
|
|
) -> Range<Index> {
|
|
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.
|
|
/// - Returns: A valid index range addressing the newly inserted items.
|
|
/// - Complexity: O(*m*), where *m* is the length of `items` when amortized
|
|
/// over many similar invocations on the same deque.
|
|
@_alwaysEmitIntoClient
|
|
@discardableResult
|
|
public mutating func prepend<C: Container<Element> & Collection<Element>>(
|
|
copying items: C
|
|
) -> Range<Index> {
|
|
self._prepend(copying: items, exactCount: items.count)
|
|
}
|
|
}
|
|
|
|
#endif
|