-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathWriteBackMutableSlice.swift
48 lines (40 loc) · 1.69 KB
/
WriteBackMutableSlice.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//===--- WriteBackMutableSlice.swift --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@inlinable
internal func _writeBackMutableSlice<C, Slice_>(
_ self_: inout C, bounds: Range<C.Index>, slice: Slice_
) where
C: MutableCollection,
Slice_: Collection,
C.Element == Slice_.Element,
C.Index == Slice_.Index {
self_._failEarlyRangeCheck(bounds, bounds: self_.startIndex..<self_.endIndex)
// FIXME(performance): can we use
// _withUnsafeMutableBufferPointerIfSupported? Would that create inout
// aliasing violations if the newValue points to the same buffer?
var selfElementIndex = bounds.lowerBound
let selfElementsEndIndex = bounds.upperBound
var newElementIndex = slice.startIndex
let newElementsEndIndex = slice.endIndex
while selfElementIndex != selfElementsEndIndex &&
newElementIndex != newElementsEndIndex {
self_[selfElementIndex] = slice[newElementIndex]
self_.formIndex(after: &selfElementIndex)
slice.formIndex(after: &newElementIndex)
}
_precondition(
selfElementIndex == selfElementsEndIndex,
"Cannot replace a slice of a MutableCollection with a slice of a smaller size")
_precondition(
newElementIndex == newElementsEndIndex,
"Cannot replace a slice of a MutableCollection with a slice of a larger size")
}