mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
Merge pull request #406 from glessard/just-span
Round out Span initializers
This commit is contained in:
4 files changed
+181
-18
No files matched your search
@@ -58,6 +58,23 @@ extension RawSpan {
|
||||
)
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
///
|
||||
/// The memory in `buffer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - buffer: an `UnsafeMutableRawBufferPointer` to initialized memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeMutableRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
///
|
||||
/// The memory over `count` bytes starting at
|
||||
@@ -115,7 +132,7 @@ extension RawSpan {
|
||||
/// order.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
@_alwaysEmitIntoClient
|
||||
public var _byteOffsets: Range<Int> {
|
||||
.init(uncheckedBounds: (0, byteCount))
|
||||
}
|
||||
@@ -425,6 +442,12 @@ extension RawSpan {
|
||||
|
||||
extension RawSpan {
|
||||
|
||||
/// Returns true if the memory represented by `span` is a subrange of
|
||||
/// the memory represented by `self`
|
||||
///
|
||||
/// Parameters:
|
||||
/// - span: a span of the same type as `self`
|
||||
/// Returns: whether `span` is a subrange of `self`
|
||||
@inlinable @inline(__always)
|
||||
public func contains(_ span: borrowing Self) -> Bool {
|
||||
if span._count > _count { return false }
|
||||
@@ -433,6 +456,14 @@ extension RawSpan {
|
||||
return span._start.advanced(by: span._count) <= _start.advanced(by: _count)
|
||||
}
|
||||
|
||||
/// Returns the offsets where the memory of `span` is located within
|
||||
/// the memory represented by `self`
|
||||
///
|
||||
/// Note: `span` must be a subrange of `self`
|
||||
///
|
||||
/// Parameters:
|
||||
/// - span: a subrange of `self`
|
||||
/// Returns: A range of offsets within `self`
|
||||
@inlinable @inline(__always)
|
||||
public func offsets(of span: borrowing Self) -> Range<Int> {
|
||||
precondition(contains(span))
|
||||
@@ -529,7 +560,7 @@ extension RawSpan {
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable
|
||||
public func extracting(droppingFirst k: Int = 1) -> Self {
|
||||
public func extracting(droppingFirst k: Int) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let dc = min(k, byteCount)
|
||||
let newStart = _pointer?.advanced(by: dc)
|
||||
|
||||
@@ -79,6 +79,23 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
self.init(_unchecked: buffer, owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory in `buffer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - buffer: an `UnsafeMutableBufferPointer` to initialized elements.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeMutableBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory representing `count` instances starting at
|
||||
@@ -124,6 +141,23 @@ extension Span where Element: BitwiseCopyable {
|
||||
self.init(_unchecked: buffer, owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory in `buffer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - buffer: an `UnsafeMutableBufferPointer` to initialized elements.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeMutableBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory representing `count` instances starting at
|
||||
@@ -175,6 +209,28 @@ extension Span where Element: BitwiseCopyable {
|
||||
)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory in `unsafeBytes` must be owned by the instance `owner`
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// `unsafeBytes` must be correctly aligned for accessing
|
||||
/// an element of type `Element`, and must contain a number of bytes
|
||||
/// that is an exact multiple of `Element`'s stride.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - unsafeBytes: a buffer to initialized elements.
|
||||
/// - type: the type to use when interpreting the bytes in memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeMutableRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
///
|
||||
/// The memory representing `count` instances starting at
|
||||
@@ -293,7 +349,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
/// order.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
@_alwaysEmitIntoClient
|
||||
public var _indices: Range<Int> {
|
||||
.init(uncheckedBounds: (0, _count))
|
||||
}
|
||||
@@ -597,6 +653,12 @@ extension Span where Element: Copyable {
|
||||
|
||||
extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
|
||||
/// Returns true if the memory represented by `span` is a subrange of
|
||||
/// the memory represented by `self`
|
||||
///
|
||||
/// Parameters:
|
||||
/// - span: a span of the same type as `self`
|
||||
/// Returns: whether `span` is a subrange of `self`
|
||||
@inlinable @inline(__always)
|
||||
public func contains(_ span: borrowing Self) -> Bool {
|
||||
if span._count > _count { return false }
|
||||
@@ -605,6 +667,14 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
return span._start.advanced(by: span._count) <= _start.advanced(by: _count)
|
||||
}
|
||||
|
||||
/// Returns the offsets where the memory of `span` is located within
|
||||
/// the memory represented by `self`
|
||||
///
|
||||
/// Note: `span` must be a subrange of `self`
|
||||
///
|
||||
/// Parameters:
|
||||
/// - span: a subrange of `self`
|
||||
/// Returns: A range of offsets within `self`
|
||||
@inlinable @inline(__always)
|
||||
public func offsets(of span: borrowing Self) -> Range<Int> {
|
||||
precondition(contains(span))
|
||||
@@ -701,7 +771,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable
|
||||
public func extracting(droppingFirst k: Int = 1) -> Self {
|
||||
public func extracting(droppingFirst k: Int) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let droppedCount = min(k, count)
|
||||
let newStart = _pointer?.advanced(by: droppedCount)
|
||||
|
||||
@@ -43,16 +43,21 @@ final class RawSpanTests: XCTestCase {
|
||||
|
||||
func testInitWithRawBytes() {
|
||||
let capacity = 4
|
||||
let a = Array(0..<capacity)
|
||||
var a = Array(0..<capacity)
|
||||
a.withUnsafeBytes {
|
||||
let span = RawSpan(unsafeBytes: $0, owner: a)
|
||||
XCTAssertEqual(span.byteCount, capacity*MemoryLayout<Int>.stride)
|
||||
}
|
||||
|
||||
a.withUnsafeMutableBytes {
|
||||
let span = RawSpan(unsafeBytes: $0, owner: $0)
|
||||
XCTAssertEqual(span.byteCount, capacity*MemoryLayout<Int>.stride)
|
||||
}
|
||||
}
|
||||
|
||||
func testWithRawPointer() {
|
||||
let capacity = 4
|
||||
let a = Array(0..<capacity)
|
||||
var a = Array(0..<capacity)
|
||||
a.withUnsafeBytes {
|
||||
let pointer = $0.baseAddress!
|
||||
let span = RawSpan(
|
||||
@@ -60,7 +65,17 @@ final class RawSpanTests: XCTestCase {
|
||||
byteCount: capacity*MemoryLayout<Int>.stride,
|
||||
owner: a
|
||||
)
|
||||
XCTAssertEqual(span.byteCount, capacity*MemoryLayout<Int>.stride)
|
||||
XCTAssertEqual(span.byteCount, $0.count)
|
||||
}
|
||||
|
||||
a.withUnsafeMutableBytes {
|
||||
let pointer = $0.baseAddress!
|
||||
let span = RawSpan(
|
||||
unsafeStart: pointer,
|
||||
byteCount: capacity*MemoryLayout<Int>.stride,
|
||||
owner: $0
|
||||
)
|
||||
XCTAssertEqual(span.byteCount, $0.count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,13 +248,15 @@ final class RawSpanTests: XCTestCase {
|
||||
let b = UnsafeMutableRawBufferPointer.allocate(byteCount: 8, alignment: 8)
|
||||
defer { b.deallocate() }
|
||||
|
||||
let span = RawSpan(unsafeBytes: .init(b), owner: b)
|
||||
let span = RawSpan(unsafeBytes: b, owner: b)
|
||||
let subSpan = span.extracting(last: 2)
|
||||
let emptySpan = span.extracting(first: 0)
|
||||
let fakeSpan = RawSpan(
|
||||
unsafeStart: b.baseAddress!.advanced(by: 8), byteCount: 8, owner: b
|
||||
)
|
||||
let nilSpan = RawSpan(unsafeBytes: .init(start: nil, count: 0), owner: b)
|
||||
let nilSpan = RawSpan(
|
||||
unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0), owner: b
|
||||
)
|
||||
|
||||
XCTAssertTrue(span.contains(subSpan))
|
||||
XCTAssertFalse(subSpan.contains(span))
|
||||
@@ -256,10 +273,12 @@ final class RawSpanTests: XCTestCase {
|
||||
let b = UnsafeMutableRawBufferPointer.allocate(byteCount: 8, alignment: 8)
|
||||
defer { b.deallocate() }
|
||||
|
||||
let span = RawSpan(unsafeBytes: .init(b), owner: b)
|
||||
let span = RawSpan(unsafeBytes: b, owner: b)
|
||||
let subSpan = span.extracting(last: 2)
|
||||
let emptySpan = span.extracting(first: 0)
|
||||
let nilSpan = RawSpan(unsafeBytes: .init(start: nil, count: 0), owner: b)
|
||||
let nilSpan = RawSpan(
|
||||
unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0), owner: b
|
||||
)
|
||||
|
||||
var bounds: Range<Int>
|
||||
bounds = span.offsets(of: subSpan)
|
||||
|
||||
@@ -29,18 +29,42 @@ final class SpanTests: XCTestCase {
|
||||
|
||||
func testInitWithOrdinaryElement() {
|
||||
let capacity = 4
|
||||
let s = (0..<capacity).map({ "\(#file)+\(#function)--\($0)" })
|
||||
var s = (0..<capacity).map({ "\(#file)+\(#function)--\($0)" })
|
||||
s.withUnsafeBufferPointer {
|
||||
let span = Span(unsafeElements: $0, owner: $0)
|
||||
_ = span
|
||||
var span = Span(unsafeElements: $0, owner: s)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
|
||||
span = Span(unsafeStart: $0.baseAddress!, count: $0.count, owner: s)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
}
|
||||
|
||||
s.withUnsafeMutableBufferPointer {
|
||||
var span = Span(unsafeElements: $0, owner: $0)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
|
||||
let pointer: UnsafeMutablePointer = $0.baseAddress!
|
||||
span = Span(unsafeStart: pointer, count: $0.count, owner: $0)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
}
|
||||
}
|
||||
|
||||
func testInitWithBitwiseCopyableElement() {
|
||||
let capacity = 4
|
||||
let a = Array(0..<capacity)
|
||||
var a = Array(0..<capacity)
|
||||
a.withUnsafeBufferPointer {
|
||||
let span = Span(unsafeElements: $0, owner: $0)
|
||||
var span = Span(unsafeElements: $0, owner: a)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
|
||||
span = Span(unsafeStart: $0.baseAddress!, count: $0.count, owner: a)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
}
|
||||
|
||||
a.withUnsafeMutableBufferPointer {
|
||||
var span = Span(unsafeElements: $0, owner: $0)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
|
||||
let pointer: UnsafeMutablePointer = $0.baseAddress!
|
||||
span = Span(unsafeStart: pointer, count: $0.count, owner: $0)
|
||||
XCTAssertEqual(span.count, capacity)
|
||||
}
|
||||
|
||||
@@ -50,6 +74,25 @@ final class SpanTests: XCTestCase {
|
||||
|
||||
let r = Span<Int8>(unsafeBytes: $0, owner: $0)
|
||||
XCTAssertEqual(r.count, capacity*MemoryLayout<Int>.stride)
|
||||
|
||||
let p = Span<Int>(
|
||||
unsafeStart: $0.baseAddress!,
|
||||
byteCount: capacity*MemoryLayout<Int>.stride,
|
||||
owner: $0
|
||||
)
|
||||
XCTAssertEqual(p.count, capacity)
|
||||
}
|
||||
|
||||
a.withUnsafeMutableBytes {
|
||||
let b = Span<UInt>(unsafeBytes: $0, owner: $0)
|
||||
XCTAssertEqual(b.count, capacity)
|
||||
|
||||
let p = Span<Int>(
|
||||
unsafeStart: $0.baseAddress!,
|
||||
byteCount: capacity*MemoryLayout<Int>.stride,
|
||||
owner: $0
|
||||
)
|
||||
XCTAssertEqual(p.count, capacity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +371,7 @@ final class SpanTests: XCTestCase {
|
||||
unsafeStart: b.baseAddress!.advanced(by: 8), count: 8, owner: b
|
||||
)
|
||||
let nilSpan = Span<Int>(
|
||||
unsafeElements: .init(start: nil, count: 0), owner: b
|
||||
unsafeElements: UnsafeBufferPointer(start: nil, count: 0), owner: b
|
||||
)
|
||||
|
||||
XCTAssertTrue(span.contains(subSpan))
|
||||
@@ -351,7 +394,7 @@ final class SpanTests: XCTestCase {
|
||||
let subSpan = span.extracting(last: 2)
|
||||
let emptySpan = span.extracting(first: 0)
|
||||
let nilSpan = Span<Int>(
|
||||
unsafeElements: .init(start: nil, count: 0), owner: b
|
||||
unsafeElements: UnsafeBufferPointer(start: nil, count: 0), owner: b
|
||||
)
|
||||
|
||||
var bounds: Range<Int>
|
||||
|
||||
Reference in new issue
Block a user