public struct ShapedArraySlice<Scalar> : _ShapedArrayProtocol

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:

let 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(shape: [3, 2], repeating: 0)
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]].
  • Declaration

    var rank: Int { get }
  • Declaration

    var shape: [Int] { get }
  • Declaration

    var scalarCount: Int { get }
  • Declaration

    init(shape: [Int], scalars: [Scalar])
  • Declaration

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

    Declaration

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

    Declaration

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

    Parameters

    shape

    The dimensions of the array.

    repeatedValue

    The scalar value to repeat.

  • Declaration

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

    mutating func withUnsafeMutableBufferPointer<Result>(
      _ body: (inout UnsafeMutableBufferPointer<Scalar>) throws -> Result
    ) rethrows -> Result
  • 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.

  • Declaration

    public static func == (lhs: ShapedArraySlice, rhs: ShapedArraySlice) -> Bool
  • A textual representation of this ShapedArraySlice.

    Declaration

    public var description: String { get }
  • Declaration

    public var customMirror: Mirror { get }