From 249608a6ae99748b93bfcefb88893d97a5e222d7 Mon Sep 17 00:00:00 2001 From: Philip Turner Date: Mon, 4 Jul 2022 18:48:49 -0400 Subject: [PATCH] Update ArrayDifferentiation.swift --- .../ArrayDifferentiation.swift | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/stdlib/public/Differentiation/ArrayDifferentiation.swift b/stdlib/public/Differentiation/ArrayDifferentiation.swift index 42664b81eb3ea..2b98328caf3a7 100644 --- a/stdlib/public/Differentiation/ArrayDifferentiation.swift +++ b/stdlib/public/Differentiation/ArrayDifferentiation.swift @@ -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 + public typealias Index = Array.Index + public typealias Indices = Array.Indices + public typealias SubSequence = Array.SubSequence + + @inlinable + public subscript(position: Array.Index) -> Element { + _read { yield base[position] } + set { base[position] = newValue } + } + + @inlinable + public subscript(bounds: Range.Index>) -> Self.SubSequence { + _read { yield base[bounds] } + set { base[bounds] = newValue } + } + + @inlinable + public mutating func replaceSubrange( + _ subrange: Range, 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 {