Skip to content

[AutoDiff][stdlib] Conform Array.DifferentiableView to several collection protocols #59878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions stdlib/public/Differentiation/ArrayDifferentiation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,48 @@ where Element: AdditiveArithmetic & Differentiable {
}
}

extension Array.DifferentiableView:
BidirectionalCollection,
Collection,
MutableCollection,
RandomAccessCollection,
RangeReplaceableCollection,
Sequence
where Element: Differentiable {
public typealias Element = Array<Element>.Element
public typealias Index = Array<Element>.Index
public typealias Indices = Array<Element>.Indices
public typealias SubSequence = Array<Element>.SubSequence

@inlinable
public subscript(position: Array<Element>.Index) -> Element {
_read { yield base[position] }
set { base[position] = newValue }
}

@inlinable
public subscript(bounds: Range<Array<Element>.Index>) -> Self.SubSequence {
_read { yield base[bounds] }
set { base[bounds] = newValue }
}

@inlinable
public mutating func replaceSubrange<C>(
_ subrange: Range<Self.Index>, with newElements: C
) where C : Collection, Self.Element == C.Element {
self[subrange] = Self.SubSequence(newElements)
}

@inlinable
public var startIndex: Index { base.startIndex }

@inlinable
public var endIndex: Index { base.endIndex }

@inlinable
public init() { self.init(.init()) }
}

/// Makes `Array` differentiable as the product manifold of `Element`
/// multiplied with itself `count` times.
extension Array: Differentiable where Element: Differentiable {
Expand Down