Skip to content

Commit 9782a65

Browse files
stackotterctreffs
andauthored
Fix RangeReplaceableCollection conformance for latest Swift version (#9)
* Fix RangeReplaceableCollection conformance for latest Swift version * Add documentation to new replaceSubrange implementations and refactor from fatalError to precondition * Fix incorrect preconditions Co-authored-by: Christian Treffs <[email protected]>
1 parent 5d51572 commit 9782a65

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Sources/FirebladeMath/Matrix/MatrixStorage.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ extension Storage4x4Protocol where Element == Value, Index == Int {
2727
public func index(after i: Int) -> Int { i + 1 }
2828
}
2929

30+
extension Storage4x4Protocol {
31+
/// Replaces a subrange of the matrix's underlying storage.
32+
/// - Precondition: `subrange` and `newElements` must be the same length because if they don't
33+
/// that doesn't make any sense in relation to matrices.
34+
public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, Value == C.Element {
35+
precondition(subrange.count == newElements.count, "newElements must be the same length as subrange")
36+
37+
for (i, element) in zip(subrange, newElements) {
38+
self[i] = element
39+
}
40+
}
41+
}
42+
3043
public protocol Storage3x3Protocol: RandomAccessCollection, MutableCollection, RangeReplaceableCollection, Equatable {
3144
associatedtype Value: StorageScalar
3245
// associatedtype _Storage4x4: Storage4x4Protocol where _Storage4x4.Value == Value
@@ -47,3 +60,16 @@ extension Storage3x3Protocol where Element == Value, Index == Int {
4760

4861
public func index(after i: Int) -> Int { i + 1 }
4962
}
63+
64+
extension Storage3x3Protocol {
65+
/// Replaces a subrange of the matrix's underlying storage.
66+
/// - Precondition: `subrange` and `newElements` must be the same length because if they don't
67+
/// that doesn't make any sense in relation to matrices.
68+
public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, Value == C.Element {
69+
precondition(subrange.count == newElements.count, "newElements must be the same length as subrange")
70+
71+
for (i, element) in zip(subrange, newElements) {
72+
self[i] = element
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)