ShapedArraySlice

@frozen
public struct ShapedArraySlice<Scalar> : _ShapedArrayProtocol
extension ShapedArraySlice: RandomAccessCollection, MutableCollection
extension ShapedArraySlice: CustomStringConvertible
extension ShapedArraySlice: CustomPlaygroundDisplayConvertible
extension ShapedArraySlice: CustomReflectable
extension ShapedArraySlice: ExpressibleByArrayLiteral where Scalar: TensorFlowScalar
extension ShapedArraySlice: Equatable where Scalar: Equatable
extension ShapedArraySlice: Hashable where Scalar: Hashable
extension ShapedArraySlice: Codable where Scalar: Codable

A contiguous slice of a ShapedArray or ShapedArraySlice instance.

ShapedArraySlice enables fast, efficient operations on contiguous slices of ShapedArray instances. ShapedArraySlice instances do not have their own storage. Instead, they provides a view onto the storage of their base ShapedArray. ShapedArraySlice can represent two different kinds of slices: element arrays and subarrays.

Element arrays are subdimensional elements of a ShapedArray: their rank is one less than that of their base. Element array slices are obtained by indexing a ShapedArray instance with a singular Int32 index.

For example:

    var matrix = ShapedArray(shape: [2, 2], scalars: [0, 1, 2, 3])
    // `matrix` represents [[0, 1], [2, 3]].

    let element = matrix[0]
    // `element` is a `ShapedArraySlice` with shape [2]. It is an element
    // array, specifically the first element in `matrix`: [0, 1].

    matrix[1] = ShapedArraySlice(shape: [2], scalars: [4, 8])
    // The second element in `matrix` has been mutated.
    // `matrix` now represents [[0, 1, 4, 8]].

Subarrays are a contiguous range of the elements in a ShapedArray. The rank of a subarray is the same as that of its base, but its leading dimension is the count of the slice range. Subarray slices are obtained by indexing a ShapedArray with a Range<Int32> that represents a range of elements (in the leading dimension). Methods like prefix(:) and suffix(:) that internally index with a range also produce subarray.

For example:

    let zeros = ShapedArray(repeating: 0, shape: [3, 2])
    var matrix = ShapedArray(shape: [3, 2], scalars: Array(0..<6))
    // `zeros` represents [[0, 0], [0, 0], [0, 0]].
    // `matrix` represents [[0, 1], [2, 3], [4, 5]].

    let subarray = matrix.prefix(2)
    // `subarray` is a `ShapedArraySlice` with shape [2, 2]. It is a slice
    // of the first 2 elements in `matrix` and represents [[0, 1], [2, 3]].

    matrix[0..<2] = zeros.prefix(2)
    // The first 2 elements in `matrix` have been mutated.
    // `matrix` now represents [[0, 0], [0, 0], [4, 5]].
  • The number of dimensions of the array.

    Declaration

    public var rank: Int { get }
  • The shape of the array.

    Declaration

    public var shape: [Int] { get }
  • The total number of scalars in the array.

    Declaration

    public var scalarCount: Int { get }
  • Creates a ShapedArraySlice with the specified shape and contiguous scalars in row-major order.

    Precondition

    The number of scalars must equal the product of the dimensions of the shape.

    Declaration

    public init(shape: [Int], scalars: [Scalar])
  • Creates an ShapedArraySlice with the specified shape and sequence of scalars in row-major order.

    Precondition

    The number of scalars must equal the product of the dimensions of the shape.

    Declaration

    public init<S>(shape: [Int], scalars: S) where Scalar == S.Element, S : Sequence
  • Creates a ShapedArraySlice from a scalar value.

    Declaration

    public init(_ scalar: Scalar)
  • Creates a ShapedArraySlice with the specified shape and a single, repeated scalar value.

    Declaration

    @available(*, deprecated, renamed: "init(repeating:shape:﹚")
    public init(shape: [Int], repeating repeatedValue: Scalar)

    Parameters

    repeatedValue

    The scalar value to repeat.

    shape

    The shape of the ShapedArraySlice.

  • Creates a ShapedArraySlice with the specified shape and a single, repeated scalar value.

    Declaration

    public init(repeating repeatedValue: Scalar, shape: [Int])

    Parameters

    repeatedValue

    The scalar value to repeat.

    shape

    The shape of the ShapedArraySlice.

  • The range of scalars from the base ShapedArray represented by a ShapedArraySlice.

    Declaration

    var scalarRange: Range<Int> { get }
  • Calls a closure with a pointer to the ShapedArraySlice’s contiguous storage.

    Declaration

    public func withUnsafeBufferPointer<Result>(
      _ body: (UnsafeBufferPointer<Scalar>) throws -> Result
    ) rethrows -> Result

    Parameters

    body

    A closure with an UnsafeBufferPointer parameter that points to the contiguous storage for the ShapedArraySlice. If no such storage exists, it is created. If body has a return value, that value is also used as the return value for the withUnsafeBufferPointer(_:) method. The pointer argument is valid only for the duration of the method’s execution.

  • Calls the given closure with a pointer to the ShapedArraySlice‘s mutable contiguous storage.

    Declaration

    public mutating func withUnsafeMutableBufferPointer<Result>(
      _ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> Result
    ) rethrows -> Result

    Parameters

    body

    A closure with an UnsafeMutableBufferPointer parameter that points to the contiguous storage for the ShapedArraySlice. If no such storage exists, it is created. If body has a return value, that value is also used as the return value for the withUnsafeMutableBufferPointer(_:) method. The pointer argument is valid only for the duration of the method’s execution.

  • Declaration

    public typealias Index = Int
  • Declaration

    public typealias Element = ShapedArraySlice
  • Declaration

    public typealias SubSequence = ShapedArraySlice
  • Declaration

    public var indices: Range<Int> { get }
  • Declaration

    public var startIndex: Int { get }
  • Declaration

    public var endIndex: Int { get }
  • Access the element array specified by an index in the leading dimension.

    Declaration

    public subscript(index: Int) -> Element { get set }

    Parameters

    index

    Index of the element array.

  • Access the subarray specified by a contiguous range of indices.

    Declaration

    public subscript(bounds: Range<Int>) -> SubSequence { get set }

    Parameters

    bounds

    Contiguous range of indices.

  • A textual representation of this ShapedArraySlice.

    Note

    use fullDescription for a non-pretty-printed representation showing all scalars.

    Declaration

    public var description: String { get }
  • Declaration

    public var playgroundDescription: Any { get }
  • Declaration

    public var customMirror: Mirror { get }
  • Declaration

    public init(_ tensor: Tensor<Scalar>)
  • Declaration

    public typealias ArrayLiteralElement = _TensorElementLiteral<Scalar>
  • Declaration

    public init(arrayLiteral elements: _TensorElementLiteral<Scalar>...)
  • Declaration

    public static func == (lhs: ShapedArraySlice, rhs: ShapedArraySlice) -> Bool
  • Declaration

    public func hash(into hasher: inout Hasher)
  • Declaration

    public func encode(to encoder: Encoder) throws
  • Declaration

    public init(from decoder: Decoder) throws