Files
swift-mirror/stdlib/public/core/WriteBackMutableSlice.swift
Ben Cohen ea2f64cad2 [stdlib] Add Sequence.Element, change ExpressibleByArrayLiteral.Element to ArrayLiteralElement (#8990)
* Give Sequence a top-level Element, constrain Iterator to match

* Remove many instances of Iterator.

* Fixed various hard-coded tests

* XFAIL a few tests that need further investigation

* Change assoc type for arrayLiteralConvertible

* Mop up remaining "better expressed as a where clause" warnings

* Fix UnicodeDecoders prototype test

* Fix UIntBuffer

* Fix hard-coded Element identifier in CSDiag

* Fix up more tests

* Account for flatMap changes
2017-05-14 06:33:25 -07:00

50 lines
1.7 KiB
Swift

//===--- 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
//
//===----------------------------------------------------------------------===//
@_inlineable
@_versioned
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")
}