1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 - 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
//
//===----------------------------------------------------------------------===//
extension Deque {
@frozen
@usableFromInline
struct _Storage {
@usableFromInline
internal typealias _Buffer = ManagedBufferPointer<_DequeBufferHeader, Element>
@usableFromInline
internal var _buffer: _Buffer
@inlinable
@inline(__always)
internal init(_buffer: _Buffer) {
self._buffer = _buffer
}
}
}
extension Deque._Storage: CustomStringConvertible {
@usableFromInline
internal var description: String {
"Deque<\(Element.self)>._Storage\(_buffer.header)"
}
}
extension Deque._Storage {
@inlinable
internal init() {
self.init(_buffer: _Buffer(unsafeBufferObject: _emptyDequeStorage))
}
@inlinable
internal init(_ object: _DequeBuffer<Element>) {
self.init(_buffer: _Buffer(unsafeBufferObject: object))
}
@inlinable
internal init(minimumCapacity: Int) {
let object = _DequeBuffer<Element>.create(
minimumCapacity: minimumCapacity,
makingHeaderWith: {
#if os(OpenBSD)
let capacity = minimumCapacity
#else
let capacity = $0.capacity
#endif
return _DequeBufferHeader(capacity: capacity, count: 0, startSlot: .zero)
})
self.init(_buffer: _Buffer(unsafeBufferObject: object))
}
}
extension Deque._Storage {
#if COLLECTIONS_INTERNAL_CHECKS
@usableFromInline @inline(never) @_effects(releasenone)
internal func _checkInvariants() {
_buffer.withUnsafeMutablePointerToHeader { $0.pointee._checkInvariants() }
}
#else
@inlinable @inline(__always)
internal func _checkInvariants() {}
#endif // COLLECTIONS_INTERNAL_CHECKS
}
extension Deque._Storage {
@inlinable
@inline(__always)
internal var identity: AnyObject { _buffer.buffer }
@inlinable
@inline(__always)
internal var capacity: Int {
_buffer.withUnsafeMutablePointerToHeader { $0.pointee.capacity }
}
@inlinable
@inline(__always)
internal var count: Int {
_buffer.withUnsafeMutablePointerToHeader { $0.pointee.count }
}
@inlinable
@inline(__always)
internal var startSlot: _DequeSlot {
_buffer.withUnsafeMutablePointerToHeader { $0.pointee.startSlot
}
}
}
extension Deque._Storage {
@usableFromInline
internal typealias Index = Int
@usableFromInline
internal typealias _UnsafeHandle = Deque._UnsafeHandle
@inlinable
@inline(__always)
internal func read<R>(_ body: (_UnsafeHandle) throws -> R) rethrows -> R {
try _buffer.withUnsafeMutablePointers { header, elements in
let handle = _UnsafeHandle(header: header,
elements: elements,
isMutable: false)
return try body(handle)
}
}
@inlinable
@inline(__always)
internal func update<R>(_ body: (_UnsafeHandle) throws -> R) rethrows -> R {
try _buffer.withUnsafeMutablePointers { header, elements in
let handle = _UnsafeHandle(header: header,
elements: elements,
isMutable: true)
return try body(handle)
}
}
}
extension Deque._Storage {
/// Return a boolean indicating whether this storage instance is known to have
/// a single unique reference. If this method returns true, then it is safe to
/// perform in-place mutations on the deque.
@inlinable
@inline(__always)
internal mutating func isUnique() -> Bool {
_buffer.isUniqueReference()
}
/// Ensure that this storage refers to a uniquely held buffer by copying
/// elements if necessary.
@inlinable
@inline(__always)
internal mutating func ensureUnique() {
if isUnique() { return }
self._makeUniqueCopy()
}
@inlinable
@inline(never)
internal mutating func _makeUniqueCopy() {
self = self.read { $0.copyElements() }
}
/// The growth factor to use to increase storage size to make place for an
/// insertion.
@inlinable
@inline(__always)
internal static var growthFactor: Double { 1.5 }
@usableFromInline
internal func _growCapacity(
to minimumCapacity: Int,
linearly: Bool
) -> Int {
if linearly { return Swift.max(capacity, minimumCapacity) }
return Swift.max(Int((Self.growthFactor * Double(capacity)).rounded(.up)),
minimumCapacity)
}
/// Ensure that we have a uniquely referenced buffer with enough space to
/// store at least `minimumCapacity` elements.
///
/// - Parameter minimumCapacity: The minimum number of elements the buffer
/// needs to be able to hold on return.
///
/// - Parameter linearGrowth: If true, then don't use an exponential growth
/// factor when reallocating the buffer -- just allocate space for the
/// requested number of elements
@inlinable
@inline(__always)
internal mutating func ensureUnique(
minimumCapacity: Int,
linearGrowth: Bool = false
) {
let unique = isUnique()
if _slowPath(capacity < minimumCapacity || !unique) {
_ensureUnique(isUnique: unique, minimumCapacity: minimumCapacity, linearGrowth: linearGrowth)
}
}
@inlinable
@inline(never)
internal mutating func _ensureUnique(
isUnique: Bool,
minimumCapacity: Int,
linearGrowth: Bool
) {
if capacity >= minimumCapacity {
assert(!isUnique)
self = self.read { $0.copyElements() }
return
}
let minimumCapacity = _growCapacity(to: minimumCapacity, linearly: linearGrowth)
if isUnique {
self = self.update { source in
source.moveElements(minimumCapacity: minimumCapacity)
}
} else {
self = self.read { source in
source.copyElements(minimumCapacity: minimumCapacity)
}
}
}
}
extension Deque._Storage {
@inlinable
@inline(__always)
internal func isIdentical(to other: Self) -> Bool {
self._buffer.buffer === other._buffer.buffer
}
}
|