mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
202 lines
6.4 KiB
Plaintext
202 lines
6.4 KiB
Plaintext
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2022 - 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
%{
|
|
from gyb_utils import *
|
|
}%
|
|
${autogenerated_warning()}
|
|
|
|
% for modifier in visibility_levels:
|
|
${visibility_boilerplate(modifier)}
|
|
extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
|
@inlinable
|
|
@inline(__always)
|
|
${modifier} static var _empty: Self {
|
|
.init(start: nil, count: 0)
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
${modifier} func _ptr(at index: Int) -> UnsafeMutablePointer<Element> {
|
|
assert(index >= 0 && index < count)
|
|
return baseAddress.unsafelyUnwrapped + index
|
|
}
|
|
|
|
@_alwaysEmitIntoClient
|
|
${modifier} func _extracting(unchecked bounds: Range<Int>) -> Self {
|
|
assert(bounds.lowerBound >= 0 && bounds.upperBound <= count,
|
|
"Index out of range")
|
|
guard let start = self.baseAddress else {
|
|
return Self(start: nil, count: 0)
|
|
}
|
|
return Self(start: start + bounds.lowerBound, count: bounds.count)
|
|
}
|
|
|
|
@inlinable
|
|
${modifier} func _extracting(first n: Int) -> Self {
|
|
assert(n >= 0)
|
|
if n >= count { return self }
|
|
return extracting(Range(uncheckedBounds: (0, n)))
|
|
}
|
|
|
|
@inlinable
|
|
${modifier} func _extracting(last n: Int) -> Self {
|
|
assert(n >= 0)
|
|
if n >= count { return self }
|
|
return extracting(Range(uncheckedBounds: (count - n, count)))
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableBufferPointer {
|
|
@inlinable
|
|
${modifier} func initialize(fromContentsOf source: Self) -> Index {
|
|
guard source.count > 0 else { return 0 }
|
|
precondition(
|
|
source.count <= self.count,
|
|
"buffer cannot contain every element from source.")
|
|
baseAddress.unsafelyUnwrapped.initialize(
|
|
from: source.baseAddress.unsafelyUnwrapped,
|
|
count: source.count)
|
|
return source.count
|
|
}
|
|
|
|
@inlinable
|
|
${modifier} func initialize(fromContentsOf source: Slice<Self>) -> Index {
|
|
let sourceCount = source.count
|
|
guard sourceCount > 0 else { return 0 }
|
|
precondition(
|
|
sourceCount <= self.count,
|
|
"buffer cannot contain every element from source.")
|
|
baseAddress.unsafelyUnwrapped.initialize(
|
|
from: source.base.baseAddress.unsafelyUnwrapped + source.startIndex,
|
|
count: sourceCount)
|
|
return sourceCount
|
|
}
|
|
}
|
|
|
|
extension Slice {
|
|
@inlinable @inline(__always)
|
|
${modifier} func initialize<Element>(
|
|
fromContentsOf source: UnsafeMutableBufferPointer<Element>
|
|
) -> Index
|
|
where Base == UnsafeMutableBufferPointer<Element>
|
|
{
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
let i = target.initialize(fromContentsOf: source)
|
|
return self.startIndex + i
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initialize<Element>(
|
|
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
|
|
) -> Index
|
|
where Base == UnsafeMutableBufferPointer<Element>
|
|
{
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
let i = target.initialize(fromContentsOf: source)
|
|
return self.startIndex + i
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableBufferPointer {
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll<C: Collection>(
|
|
fromContentsOf source: C
|
|
) where C.Element == Element {
|
|
let i = self.initialize(fromContentsOf: source)
|
|
assert(i == self.endIndex)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll(fromContentsOf source: UnsafeBufferPointer<Element>) {
|
|
assert(self.count == source.count)
|
|
guard source.count > 0 else { return }
|
|
self.baseAddress.unsafelyUnwrapped.initialize(
|
|
from: source.baseAddress.unsafelyUnwrapped,
|
|
count: source.count)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll(fromContentsOf source: Slice<UnsafeBufferPointer<Element>>) {
|
|
self.initializeAll(fromContentsOf: .init(rebasing: source))
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll(fromContentsOf source: Self) {
|
|
self.initializeAll(fromContentsOf: UnsafeBufferPointer(source))
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll(fromContentsOf source: Slice<Self>) {
|
|
self.initializeAll(fromContentsOf: UnsafeMutableBufferPointer(rebasing: source))
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
|
@inlinable @inline(__always)
|
|
${modifier} func moveInitializeAll(fromContentsOf source: Self) {
|
|
let i = self.moveInitialize(fromContentsOf: source)
|
|
assert(i == self.endIndex)
|
|
}
|
|
}
|
|
|
|
extension UnsafeMutableBufferPointer {
|
|
@inlinable @inline(__always)
|
|
${modifier} func moveInitializeAll(fromContentsOf source: Slice<Self>) {
|
|
let i = self.moveInitialize(fromContentsOf: source)
|
|
assert(i == self.endIndex)
|
|
}
|
|
}
|
|
|
|
extension Slice {
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll<C: Collection>(
|
|
fromContentsOf source: C
|
|
) where Base == UnsafeMutableBufferPointer<C.Element> {
|
|
let i = self.initialize(fromContentsOf: source)
|
|
assert(i == self.endIndex)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll<Element>(
|
|
fromContentsOf source: UnsafeMutableBufferPointer<Element>
|
|
) where Base == UnsafeMutableBufferPointer<Element> {
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
target.initializeAll(fromContentsOf: source)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func initializeAll<Element>(
|
|
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
|
|
) where Base == UnsafeMutableBufferPointer<Element> {
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
target.initializeAll(fromContentsOf: source)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func moveInitializeAll<Element>(
|
|
fromContentsOf source: UnsafeMutableBufferPointer<Element>
|
|
) where Base == UnsafeMutableBufferPointer<Element> {
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
target.moveInitializeAll(fromContentsOf: source)
|
|
}
|
|
|
|
@inlinable @inline(__always)
|
|
${modifier} func moveInitializeAll<Element>(
|
|
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
|
|
) where Base == UnsafeMutableBufferPointer<Element> {
|
|
let target = UnsafeMutableBufferPointer(rebasing: self)
|
|
target.moveInitializeAll(fromContentsOf: source)
|
|
}
|
|
}
|
|
% end
|
|
${visibility_boilerplate("end")}
|