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 227 228 229 230 231 232 233 234 235 236 237 238 239
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
@frozen
@usableFromInline
internal struct _UnsafeWrappedBuffer<Element> {
@usableFromInline
internal let first: UnsafeBufferPointer<Element>
@usableFromInline
internal let second: UnsafeBufferPointer<Element>?
@inlinable
@inline(__always)
internal init(
_ first: UnsafeBufferPointer<Element>,
_ second: UnsafeBufferPointer<Element>? = nil
) {
self.first = first
self.second = second
assert(first.count > 0 || second == nil)
}
@inlinable
internal init(
start: UnsafePointer<Element>,
count: Int
) {
self.init(UnsafeBufferPointer(start: start, count: count))
}
@inlinable
internal init(
first start1: UnsafePointer<Element>,
count count1: Int,
second start2: UnsafePointer<Element>,
count count2: Int
) {
self.init(UnsafeBufferPointer(start: start1, count: count1),
UnsafeBufferPointer(start: start2, count: count2))
}
@inlinable
internal var count: Int { first.count + (second?.count ?? 0) }
}
@frozen
@usableFromInline
internal struct _UnsafeMutableWrappedBuffer<Element> {
@usableFromInline
internal let first: UnsafeMutableBufferPointer<Element>
@usableFromInline
internal let second: UnsafeMutableBufferPointer<Element>?
@inlinable
@inline(__always)
internal init(
_ first: UnsafeMutableBufferPointer<Element>,
_ second: UnsafeMutableBufferPointer<Element>? = nil
) {
self.first = first
self.second = second?.count == 0 ? nil : second
assert(first.count > 0 || second == nil)
}
@inlinable
@inline(__always)
internal init(
_ first: UnsafeMutableBufferPointer<Element>.SubSequence,
_ second: UnsafeMutableBufferPointer<Element>? = nil
) {
self.init(UnsafeMutableBufferPointer(rebasing: first), second)
}
@inlinable
@inline(__always)
internal init(
_ first: UnsafeMutableBufferPointer<Element>,
_ second: UnsafeMutableBufferPointer<Element>.SubSequence
) {
self.init(first, UnsafeMutableBufferPointer(rebasing: second))
}
@inlinable
@inline(__always)
internal init(
start: UnsafeMutablePointer<Element>,
count: Int
) {
self.init(UnsafeMutableBufferPointer(start: start, count: count))
}
@inlinable
@inline(__always)
internal init(
first start1: UnsafeMutablePointer<Element>,
count count1: Int,
second start2: UnsafeMutablePointer<Element>,
count count2: Int
) {
self.init(UnsafeMutableBufferPointer(start: start1, count: count1),
UnsafeMutableBufferPointer(start: start2, count: count2))
}
@inlinable
@inline(__always)
internal init(mutating buffer: _UnsafeWrappedBuffer<Element>) {
self.init(.init(mutating: buffer.first),
buffer.second.map { .init(mutating: $0) })
}
}
extension _UnsafeMutableWrappedBuffer {
@inlinable
@inline(__always)
internal var count: Int { first.count + (second?.count ?? 0) }
@inlinable
internal func prefix(_ n: Int) -> Self {
assert(n >= 0)
if n >= self.count {
return self
}
if n <= first.count {
return Self(first.prefix(n))
}
return Self(first, second!.prefix(n - first.count))
}
@inlinable
internal func suffix(_ n: Int) -> Self {
assert(n >= 0)
if n >= self.count {
return self
}
guard let second = second else {
return Self(first.suffix(n))
}
if n <= second.count {
return Self(second.suffix(n))
}
return Self(first.suffix(n - second.count), second)
}
}
extension _UnsafeMutableWrappedBuffer {
@inlinable
internal func deinitialize() {
first.deinitialize()
second?.deinitialize()
}
@inlinable
@discardableResult
internal func initialize<I: IteratorProtocol>(
fromPrefixOf iterator: inout I
) -> Int
where I.Element == Element {
var copied = 0
var gap = first
var wrapped = false
while true {
if copied == gap.count {
guard !wrapped, let second = second, second.count > 0 else { break }
gap = second
copied = 0
wrapped = true
}
guard let next = iterator.next() else { break }
(gap.baseAddress! + copied).initialize(to: next)
copied += 1
}
return wrapped ? first.count + copied : copied
}
@inlinable
internal func initialize<S: Sequence>(
fromSequencePrefix elements: __owned S
) -> (iterator: S.Iterator, count: Int)
where S.Element == Element {
guard second == nil || first.count >= elements.underestimatedCount else {
var it = elements.makeIterator()
let copied = initialize(fromPrefixOf: &it)
return (it, copied)
}
// Note: Array._copyContents traps when not given enough space, so we
// need to check if we have enough contiguous space available above.
//
// FIXME: Add support for segmented (a.k.a. piecewise contiguous)
// collections to the stdlib.
var (it, copied) = elements._copyContents(initializing: first)
if copied == first.count, let second = second {
var i = 0
while i < second.count {
guard let next = it.next() else { break }
(second.baseAddress! + i).initialize(to: next)
i += 1
}
copied += i
}
return (it, copied)
}
@inlinable
internal func initialize<C: Collection>(
from elements: __owned C
) where C.Element == Element {
assert(self.count == elements.count)
if let second = second {
let wrap = elements.index(elements.startIndex, offsetBy: first.count)
first.initializeAll(fromContentsOf: elements[..<wrap])
second.initializeAll(fromContentsOf: elements[wrap...])
} else {
first.initializeAll(fromContentsOf: elements)
}
}
@inlinable
internal func assign<C: Collection>(
from elements: C
) where C.Element == Element {
assert(elements.count == self.count)
deinitialize()
initialize(from: elements)
}
}
|