mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
renamings and doc-comments in RawSpan
This commit is contained in:
1 parent
79ec3505da
commit
3af133efec
1 file changed
+183
-75
+183
-75
@@ -12,6 +12,8 @@
|
||||
|
||||
import Builtin
|
||||
|
||||
// A RawSpan represents a span of initialized memory
|
||||
// of unspecified type.
|
||||
@frozen
|
||||
public struct RawSpan: Copyable, ~Escapable {
|
||||
@usableFromInline let _start: UnsafeRawPointer
|
||||
@@ -67,12 +69,12 @@ extension RawSpan {
|
||||
/// the returned `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeRawPointer pointer: UnsafeRawPointer,
|
||||
count: Int,
|
||||
unsafeStart pointer: UnsafeRawPointer,
|
||||
byteCount: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
precondition(count >= 0, "Count must not be negative")
|
||||
self.init(_unchecked: pointer, count: count, owner: owner)
|
||||
precondition(byteCount >= 0, "Count must not be negative")
|
||||
self.init(_unchecked: pointer, count: byteCount, owner: owner)
|
||||
}
|
||||
|
||||
/// Create a `RawSpan` over the memory represented by a `Span<T>`
|
||||
@@ -92,8 +94,36 @@ extension RawSpan {
|
||||
}
|
||||
}
|
||||
|
||||
extension RawSpan {
|
||||
|
||||
/// The number of bytes in the span.
|
||||
///
|
||||
/// To check whether the span is empty, use its `isEmpty` property
|
||||
/// instead of comparing `count` to zero.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
public var count: Int { _count }
|
||||
|
||||
/// A Boolean value indicating whether the span is empty.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
public var isEmpty: Bool { count == 0 }
|
||||
|
||||
/// The indices that are valid for subscripting the span, in ascending
|
||||
/// order.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
public var indices: Range<Int> {
|
||||
.init(uncheckedBounds: (0, count))
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: Bounds Checking
|
||||
extension RawSpan {
|
||||
|
||||
/// Traps if `offset` is not a valid offset into this `RawSpan`
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -119,69 +149,124 @@ extension RawSpan {
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: Offset Manipulation
|
||||
//MARK: extracting sub-spans
|
||||
extension RawSpan {
|
||||
|
||||
/// Constructs a new span over the bytes within the supplied range of
|
||||
/// positions within this span.
|
||||
///
|
||||
/// The returned span's first byte is always at offset 0; unlike buffer
|
||||
/// slices, extracted spans do not generally share their indices with the
|
||||
/// span from which they are extracted.
|
||||
///
|
||||
/// - Parameter bounds: A valid range of positions. Every position in
|
||||
/// this range must be within the bounds of this `RawSpan`.
|
||||
///
|
||||
/// - Returns: A `Span` over the bytes within `bounds`
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
public var count: Int {
|
||||
borrowing get { self._count }
|
||||
public func extracting(_ bounds: Range<Int>) -> Self {
|
||||
boundsCheckPrecondition(bounds)
|
||||
return extracting(uncheckedBounds: bounds)
|
||||
}
|
||||
|
||||
/// Constructs a new span over the bytes within the supplied range of
|
||||
/// positions within this span.
|
||||
///
|
||||
/// The returned span's first byte is always at offset 0; unlike buffer
|
||||
/// slices, extracted spans do not generally share their indices with the
|
||||
/// span from which they are extracted.
|
||||
///
|
||||
/// This function does not validate `bounds`; this is an unsafe operation.
|
||||
///
|
||||
/// - Parameter bounds: A valid range of positions. Every position in
|
||||
/// this range must be within the bounds of this `RawSpan`.
|
||||
///
|
||||
/// - Returns: A `Span` over the bytes within `bounds`
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@inlinable @inline(__always)
|
||||
public var isEmpty: Bool { count == 0 }
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public var indices: Range<Int> {
|
||||
.init(uncheckedBounds: (0, count))
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: integer offset subscripts
|
||||
extension RawSpan {
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public subscript(offsets offsets: Range<Int>) -> Self {
|
||||
borrowing get {
|
||||
boundsCheckPrecondition(offsets)
|
||||
return self[uncheckedOffsets: offsets]
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public subscript(uncheckedOffsets offsets: Range<Int>) -> Self {
|
||||
borrowing get {
|
||||
RawSpan(
|
||||
_unchecked: _start.advanced(by: offsets.lowerBound),
|
||||
count: offsets.count,
|
||||
owner: self
|
||||
)
|
||||
}
|
||||
public func extracting(uncheckedBounds bounds: Range<Int>) -> Self {
|
||||
RawSpan(
|
||||
_unchecked: _start.advanced(by: bounds.lowerBound),
|
||||
count: bounds.count,
|
||||
owner: self
|
||||
)
|
||||
}
|
||||
|
||||
/// Constructs a new span over the bytes within the supplied range of
|
||||
/// positions within this span.
|
||||
///
|
||||
/// The returned span's first byte is always at offset 0; unlike buffer
|
||||
/// slices, extracted spans do not generally share their indices with the
|
||||
/// span from which they are extracted.
|
||||
///
|
||||
/// - Parameter bounds: A valid range of positions. Every position in
|
||||
/// this range must be within the bounds of this `RawSpan`.
|
||||
///
|
||||
/// - Returns: A `Span` over the bytes within `bounds`
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@_alwaysEmitIntoClient
|
||||
public subscript(offsets offsets: some RangeExpression<Int>) -> Self {
|
||||
borrowing get {
|
||||
self[offsets: offsets.relative(to: indices)]
|
||||
}
|
||||
public func extracting(_ bounds: some RangeExpression<Int>) -> Self {
|
||||
extracting(bounds.relative(to: indices))
|
||||
}
|
||||
|
||||
/// Constructs a new span over the bytes within the supplied range of
|
||||
/// positions within this span.
|
||||
///
|
||||
/// The returned span's first byte is always at offset 0; unlike buffer
|
||||
/// slices, extracted spans do not generally share their indices with the
|
||||
/// span from which they are extracted.
|
||||
///
|
||||
/// This function does not validate `bounds`; this is an unsafe operation.
|
||||
///
|
||||
/// - Parameter bounds: A valid range of positions. Every position in
|
||||
/// this range must be within the bounds of this `RawSpan`.
|
||||
///
|
||||
/// - Returns: A `Span` over the bytes within `bounds`
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@_alwaysEmitIntoClient
|
||||
public subscript(uncheckedOffsets offsets: some RangeExpression<Int>) -> Self {
|
||||
borrowing get {
|
||||
self[uncheckedOffsets: offsets.relative(to: indices)]
|
||||
}
|
||||
public func extracting(
|
||||
uncheckedBounds bounds: some RangeExpression<Int>
|
||||
) -> Self {
|
||||
extracting(uncheckedBounds: bounds.relative(to: indices))
|
||||
}
|
||||
|
||||
/// Constructs a new span over all the bytes of this span.
|
||||
///
|
||||
/// The returned span's first byte is always at offset 0; unlike buffer
|
||||
/// slices, extracted spans do not generally share their indices with the
|
||||
/// span from which they are extracted.
|
||||
///
|
||||
/// - Returns: A `RawSpan` over all the items of this span.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
@_alwaysEmitIntoClient
|
||||
public subscript(offsets _: UnboundedRange) -> Self {
|
||||
borrowing get { copy self }
|
||||
public func extracting(_: UnboundedRange) -> Self {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension RawSpan {
|
||||
|
||||
//FIXME: mark closure parameter as non-escaping
|
||||
@_alwaysEmitIntoClient
|
||||
/// Calls the given closure with a pointer to the underlying bytes of
|
||||
/// the viewed contiguous storage.
|
||||
///
|
||||
/// The buffer pointer passed as an argument to `body` is valid only
|
||||
/// during the execution of `withUnsafeBytes(_:)`.
|
||||
/// Do not store or return the pointer for later use.
|
||||
///
|
||||
/// - Parameter body: A closure with an `UnsafeRawBufferPointer`
|
||||
/// parameter that points to the viewed contiguous storage.
|
||||
/// If `body` has a return value, that value is also
|
||||
/// used as the return value for the `withUnsafeBytes(_:)` method.
|
||||
/// The closure's parameter is valid only for the duration of
|
||||
/// its execution.
|
||||
/// - Returns: The return value of the `body` closure parameter.
|
||||
borrowing public func withUnsafeBytes<
|
||||
E: Error, Result: ~Copyable & ~Escapable
|
||||
>(
|
||||
@@ -192,10 +277,14 @@ extension RawSpan {
|
||||
}
|
||||
|
||||
extension RawSpan {
|
||||
/// View the bytes of this span as a given type
|
||||
///
|
||||
/// - Parameter type: The type as which we should view <reword>
|
||||
/// - Returns: A typed span viewing these bytes as T
|
||||
borrowing public func view<T: BitwiseCopyable>(
|
||||
as: T.Type
|
||||
as type: T.Type
|
||||
) -> dependsOn(self) Span<T> {
|
||||
Span(unsafeRawPointer: _start, as: T.self, byteCount: count, owner: self)
|
||||
Span(unsafeStart: _start, byteCount: count, owner: self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,49 +325,68 @@ extension RawSpan {
|
||||
//MARK: one-sided slicing operations
|
||||
extension RawSpan {
|
||||
|
||||
borrowing public func prefix(upTo offset: Int) -> dependsOn(self) Self {
|
||||
if offset != 0 {
|
||||
boundsCheckPrecondition(offset &- 1)
|
||||
}
|
||||
return Self(_unchecked: _start, count: offset, owner: self)
|
||||
}
|
||||
|
||||
borrowing public func prefix(through offset: Int) -> dependsOn(self) Self {
|
||||
boundsCheckPrecondition(offset)
|
||||
return Self(_unchecked: _start, count: offset &+ 1, owner: self)
|
||||
}
|
||||
|
||||
borrowing public func prefix(_ maxLength: Int) -> dependsOn(self) Self {
|
||||
/// Returns a span containing the initial bytes of this span,
|
||||
/// up to the specified maximum byte count.
|
||||
///
|
||||
/// If the maximum length exceeds the length of this span,
|
||||
/// the result contains all the bytes.
|
||||
///
|
||||
/// - Parameter maxLength: The maximum number of bytes to return.
|
||||
/// `maxLength` must be greater than or equal to zero.
|
||||
/// - Returns: A span with at most `maxLength` bytes.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
borrowing public func extracting(first maxLength: Int) -> Self {
|
||||
precondition(maxLength >= 0, "Can't have a prefix of negative length.")
|
||||
let nc = maxLength < count ? maxLength : count
|
||||
return Self(_unchecked: _start, count: nc, owner: self)
|
||||
}
|
||||
|
||||
borrowing public func dropLast(_ k: Int = 1) -> dependsOn(self) Self {
|
||||
/// Returns a span over all but the given number of trailing bytes.
|
||||
///
|
||||
/// If the number of elements to drop exceeds the number of elements in
|
||||
/// the span, the result is an empty span.
|
||||
///
|
||||
/// - Parameter k: The number of bytes to drop off the end of
|
||||
/// the span. `k` must be greater than or equal to zero.
|
||||
/// - Returns: A span leaving off the specified number of bytes at the end.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
borrowing public func extracting(droppingLast k: Int) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let nc = k < count ? count&-k : 0
|
||||
return Self(_unchecked: _start, count: nc, owner: self)
|
||||
}
|
||||
|
||||
borrowing public func suffix(from offset: Int) -> dependsOn(self) Self {
|
||||
if offset != count {
|
||||
boundsCheckPrecondition(offset)
|
||||
}
|
||||
return Self(
|
||||
_unchecked: _start.advanced(by: offset),
|
||||
count: count &- offset,
|
||||
owner: self
|
||||
)
|
||||
}
|
||||
|
||||
borrowing public func suffix(_ maxLength: Int) -> dependsOn(self) Self {
|
||||
/// Returns a span containing the trailing bytes of the span,
|
||||
/// up to the given maximum length.
|
||||
///
|
||||
/// If the maximum length exceeds the length of this span,
|
||||
/// the result contains all the bytes.
|
||||
///
|
||||
/// - Parameter maxLength: The maximum number of bytes to return.
|
||||
/// `maxLength` must be greater than or equal to zero.
|
||||
/// - Returns: A span with at most `maxLength` bytes.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
borrowing public func extracting(last maxLength: Int) -> Self {
|
||||
precondition(maxLength >= 0, "Can't have a suffix of negative length.")
|
||||
let nc = maxLength < count ? maxLength : count
|
||||
let newStart = _start.advanced(by: count&-nc)
|
||||
return Self(_unchecked: newStart, count: nc, owner: self)
|
||||
}
|
||||
|
||||
borrowing public func dropFirst(_ k: Int = 1) -> dependsOn(self) Self {
|
||||
/// Returns a span over all but the given number of initial bytes.
|
||||
///
|
||||
/// If the number of elements to drop exceeds the number of bytes in
|
||||
/// the span, the result is an empty span.
|
||||
///
|
||||
/// - Parameter k: The number of bytes to drop from the beginning of
|
||||
/// the span. `k` must be greater than or equal to zero.
|
||||
/// - Returns: A span starting after the specified number of bytes.
|
||||
///
|
||||
/// - Complexity: O(1)
|
||||
borrowing public func extracting(droppingFirst k: Int = 1) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let dc = k < count ? k : count
|
||||
let newStart = _start.advanced(by: dc)
|
||||
|
||||
Reference in new issue
Block a user