More doc updates

This commit is contained in:
Karoy Lorentey committed 2025-09-28 22:56:02 -07:00
1 parent 9cf2cf254a
commit 8df8ae3537
14 files changed
+713 -57

No files matched your search

+131
View File
@@ -63,11 +63,13 @@ public struct UniqueArray<Element: ~Copyable>: ~Copyable {
@usableFromInline
internal var _storage: RigidArray<Element>
/// Initializes a new unique array with the specified capacity and no elements.
@inlinable
public init(capacity: Int) {
_storage = .init(capacity: capacity)
}
/// Initializes a new unique array with no elements.
@inlinable
public init() {
_storage = .init(capacity: 0)
@@ -81,10 +83,16 @@ extension UniqueArray: Sendable where Element: Sendable & ~Copyable {}
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// The maximum number of elements this array can hold without reallocating
/// its storage.
@inlinable
@inline(__always)
public var capacity: Int { _assumeNonNegative(_storage.capacity) }
/// The number of additional elements that can be added to this array without
/// reallocating its storage.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var freeCapacity: Int {
@@ -96,6 +104,9 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// A span over the elements of this array, providing direct read-only access.
///
/// - Complexity: O(1)
public var span: Span<Element> {
@_lifetime(borrow self)
@inlinable
@@ -104,6 +115,10 @@ extension UniqueArray where Element: ~Copyable {
}
}
/// A mutable span over the elements of this array, providing direct
/// mutating access.
///
/// - Complexity: O(1)
@available(SwiftStdlib 5.0, *)
public var mutableSpan: MutableSpan<Element> {
@_lifetime(&self)
@@ -145,10 +160,16 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// A Boolean value indicating whether this array contains no elements.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var isEmpty: Bool { _storage.isEmpty }
/// The number of elements in this array.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var count: Int { _storage.count }
@@ -169,25 +190,49 @@ extension UniqueArray: Container where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// A type that represents a position in the array: an integer offset from the
/// start.
///
/// Valid indices consist of the position of every element and a "past the
/// end” position that’s not valid for use as a subscript argument.
public typealias Index = Int
/// The position of the first element in a nonempty array. This is always zero.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var startIndex: Int { _storage.startIndex }
/// The array’s "past the end” position—that is, the position one greater than
/// the last valid subscript argument. This is always equal to array's count.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var endIndex: Int { _storage.count }
/// The range of indices that are valid for subscripting the array.
///
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var indices: Range<Int> { _storage.indices }
/// Accesses the element at the specified position.
///
/// - Parameter position: The position of the element to access.
/// The position must be a valid index of the array that is not equal
/// to the `endIndex` property.
///
/// - Complexity: O(1)
@inlinable
public subscript(position: Int) -> Element {
@inline(__always)
unsafeAddress {
_storage._ptr(to: position)
}
@inline(__always)
unsafeMutableAddress {
_storage._mutablePtr(to: position)
}
@@ -196,6 +241,15 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Exchanges the values at the specified indices of the array.
///
/// Both parameters must be valid indices of the array and not equal to
/// endIndex. Passing the same index as both `i` and `j` has no effect.
///
/// - Parameter i: The index of the first value to swap.
/// - Parameter j: The index of the second valud to swap.
///
/// - Complexity: O(1)
@inlinable
public mutating func swapAt(_ i: Int, _ j: Int) {
_storage.swapAt(i, j)
@@ -204,12 +258,26 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Return a borrowing span over the maximal storage chunk following the
/// specified position in the array. The span provides direct read-only access
/// to all array elements in the range `index ..< count`.
///
/// - Parameter index: A valid index in the array, including the end index.
///
/// - Complexity: O(1)
@inlinable
@_lifetime(borrow self)
public func span(after index: inout Int) -> Span<Element> {
_storage.span(after: &index)
}
/// Return a borrowing span over the maximal storage chunk preceding the
/// specified position in the array. The span provides direct read-only access
/// to all array elements in the range `0 ..< index`.
///
/// - Parameter index: A valid index in the array, including the end index.
///
/// - Complexity: O(1)
@inlinable
@_lifetime(borrow self)
public func span(before index: inout Int) -> Span<Element> {
@@ -219,6 +287,13 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Return a mutable span over the maximal storage chunk following the
/// specified position in the array. The span provides direct mutating access
/// to all array elements in the range `index ..< count`.
///
/// - Parameter index: A valid index in the array, including the end index.
///
/// - Complexity: O(1)
@_lifetime(&self)
public mutating func mutableSpan(
after index: inout Int
@@ -226,6 +301,13 @@ extension UniqueArray where Element: ~Copyable {
_storage.mutableSpan(after: &index)
}
/// Return a mutable span over the maximal storage chunk preceding the specified
/// position in the array. The span provides direct mutating access to all
/// array elements in the range `0 ..< index`.
///
/// - Parameter index: A valid index in the array, including the end index.
///
/// - Complexity: O(1)
@_lifetime(&self)
public mutating func mutableSpan(
before index: inout Int
@@ -247,11 +329,30 @@ internal func _growDynamicArrayCapacity(_ capacity: Int) -> Int {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Grow or shrink the capacity of a unique array instance without discarding
/// its contents.
///
/// This operation replaces the array's storage buffer with a newly allocated
/// buffer of the specified capacity, moving all existing elements
/// to its new storage. The old storage is then deallocated.
///
/// - Parameter newCapacity: The desired new capacity. `newCapacity` must be
/// greater than or equal to the current count.
///
/// - Complexity: O(`count`)
@inlinable @inline(never)
public mutating func reallocate(capacity: Int) {
_storage.reallocate(capacity: capacity)
}
/// Ensure that the array has capacity to store the specified number of
/// elements, by growing its storage buffer if necessary.
///
/// If `capacity < n`, then this operation reallocates the unique array's
/// storage to grow it; on return, the array's capacity becomes `n`.
/// Otherwise the array is left as is.
///
/// - Complexity: O(`count`)
@inlinable @inline(never)
public mutating func reserveCapacity(_ n: Int) {
_storage.reserveCapacity(n)
@@ -279,4 +380,34 @@ extension UniqueArray where Element: ~Copyable {
}
}
//MARK: - Copying helpers
@available(SwiftStdlib 5.0, *)
extension UniqueArray {
/// Copy the contents of this array into a newly allocated unique array
/// instance with just enough capacity to hold all its elements.
///
/// - Complexity: O(`count`)
@inlinable
@inline(__always)
public func copy() -> Self {
UniqueArray(consuming: _storage.copy())
}
/// Copy the contents of this array into a newly allocated unique array
/// instance with the specified capacity.
///
/// - Parameter capacity: The desired capacity of the resulting unique array.
/// `capacity` must be greater than or equal to `count`.
///
/// - Complexity: O(`count`)
@inlinable
@inline(__always)
public func copy(capacity: Int) -> Self {
UniqueArray(consuming: _storage.copy(capacity: capacity))
}
}
#endif