mirror of
https://github.com/apple/swift-collections
synced 2026-11-22 04:20:40 +00:00
162 lines
3.8 KiB
Swift
162 lines
3.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2024 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if !COLLECTIONS_SINGLE_MODULE
|
|
import InternalCollectionsUtilities
|
|
import Future
|
|
#endif
|
|
|
|
@frozen
|
|
public struct DynamicDeque<Element: ~Copyable>: ~Copyable {
|
|
@usableFromInline
|
|
internal var _storage: RigidDeque<Element>
|
|
|
|
@inlinable
|
|
public init() {
|
|
_storage = .init(capacity: 0)
|
|
}
|
|
|
|
@inlinable
|
|
public init(capacity: Int) {
|
|
_storage = .init(capacity: capacity)
|
|
}
|
|
}
|
|
|
|
extension DynamicDeque: @unchecked Sendable where Element: Sendable & ~Copyable {}
|
|
|
|
extension DynamicDeque: RandomAccessContainer where Element: ~Copyable {
|
|
@available(SwiftStdlib 6.2, *)
|
|
@lifetime(borrow self)
|
|
public func nextSpan(after index: inout Int) -> Span<Element> {
|
|
_storage.nextSpan(after: &index)
|
|
}
|
|
|
|
@available(SwiftStdlib 6.2, *)
|
|
@lifetime(borrow self)
|
|
public func previousSpan(before index: inout Int) -> Span<Element> {
|
|
_storage.previousSpan(before: &index)
|
|
}
|
|
}
|
|
|
|
extension DynamicDeque where Element: ~Copyable {
|
|
public typealias Index = Int
|
|
|
|
@inlinable
|
|
public var isEmpty: Bool { _storage.isEmpty }
|
|
|
|
@inlinable
|
|
public var count: Int { _storage.count }
|
|
|
|
@inlinable
|
|
public var startIndex: Int { _storage.startIndex }
|
|
|
|
@inlinable
|
|
public var endIndex: Int { _storage.endIndex }
|
|
|
|
@lifetime(borrow self)
|
|
public func borrowElement(at index: Int) -> Future.Borrow<Element> {
|
|
_storage.borrowElement(at: index)
|
|
}
|
|
}
|
|
|
|
extension DynamicDeque where Element: ~Copyable {
|
|
@inlinable
|
|
internal var _capacity: Int { _storage.capacity }
|
|
|
|
@inlinable
|
|
internal var _freeCapacity: Int { _storage.freeCapacity }
|
|
|
|
@inlinable
|
|
internal var _isFull: Bool { _storage.isFull }
|
|
|
|
@inlinable
|
|
internal mutating func _grow(to minimumCapacity: Int) {
|
|
guard minimumCapacity > _capacity else { return }
|
|
let c = Swift.max(minimumCapacity, 7 * _capacity / 4)
|
|
_storage.resize(to: c)
|
|
}
|
|
|
|
@inlinable
|
|
internal mutating func _ensureFreeCapacity(_ freeCapacity: Int) {
|
|
_grow(to: count + freeCapacity)
|
|
}
|
|
}
|
|
|
|
extension DynamicDeque where Element: ~Copyable {
|
|
@inlinable
|
|
public mutating func append(_ newElement: consuming Element) {
|
|
_ensureFreeCapacity(1)
|
|
_storage.append(newElement)
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func prepend(_ newElement: consuming Element) {
|
|
_ensureFreeCapacity(1)
|
|
_storage.prepend(newElement)
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func insert(_ newElement: consuming Element, at index: Int) {
|
|
_ensureFreeCapacity(1)
|
|
_storage.insert(newElement, at: index)
|
|
}
|
|
}
|
|
|
|
extension DynamicDeque where Element: ~Copyable {
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func remove(at index: Int) -> Element {
|
|
_storage.remove(at: index)
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func removeSubrange(_ bounds: Range<Int>) {
|
|
_storage.removeSubrange(bounds)
|
|
}
|
|
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func removeFirst() -> Element {
|
|
_storage.removeFirst()
|
|
}
|
|
|
|
@inlinable
|
|
@discardableResult
|
|
public mutating func removeLast() -> Element {
|
|
_storage.removeLast()
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func removeFirst(_ n: Int) {
|
|
_storage.removeFirst(n)
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func removeLast(_ n: Int) {
|
|
_storage.removeLast(n)
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func removeAll() {
|
|
_storage.removeAll()
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func popFirst() -> Element? {
|
|
_storage.popFirst()
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func popLast() -> Element? {
|
|
_storage.popLast()
|
|
}
|
|
}
|