Skip to content
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

[stdlib] Make various free functions mutating an _ArrayBufferProtocol into extensions #5483

Merged
merged 12 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions stdlib/public/core/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ extension _ArrayBuffer {
}
else {
var refCopy = self
refCopy.replace(
subRange: i..<(i + 1),
refCopy.replaceSubrange(
i..<(i + 1),
with: 1,
elementsOf: CollectionOfOne(newValue))
}
Expand Down
29 changes: 15 additions & 14 deletions stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ internal protocol _ArrayBufferProtocol
///
/// - Precondition: This buffer is backed by a uniquely-referenced
/// `_ContiguousArrayBuffer`.
mutating func replace<C>(
subRange: Range<Int>,
mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element
Expand Down Expand Up @@ -124,41 +124,42 @@ internal protocol _ArrayBufferProtocol
var identity: UnsafeRawPointer { get }

var startIndex: Int { get }
var endIndex: Int { get }
}

extension _ArrayBufferProtocol where Index == Int {
extension _ArrayBufferProtocol {

internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
return firstElementAddress
}

internal mutating func replace<C>(
subRange: Range<Int>,
internal mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element {
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
let oldCount = self.count
let eraseCount = subRange.count
let eraseCount = subrange.count

let growth = newCount - eraseCount
self.count = oldCount + growth

let elements = self.subscriptBaseAddress
let oldTailIndex = subRange.upperBound
let oldTailIndex = subrange.upperBound
let oldTailStart = elements + oldTailIndex
let newTailIndex = oldTailIndex + growth
let newTailStart = oldTailStart + growth
let tailCount = oldCount - subRange.upperBound
let tailCount = oldCount - subrange.upperBound

if growth > 0 {
// Slide the tail part of the buffer forwards, in reverse order
// so as not to self-clobber.
newTailStart.moveInitialize(from: oldTailStart, count: tailCount)

// Assign over the original subRange
// Assign over the original subrange
var i = newValues.startIndex
for j in CountableRange(subRange) {
for j in CountableRange(subrange) {
elements[j] = newValues[i]
newValues.formIndex(after: &i)
}
Expand All @@ -170,12 +171,12 @@ extension _ArrayBufferProtocol where Index == Int {
_expectEnd(i, newValues)
}
else { // We're not growing the buffer
// Assign all the new elements into the start of the subRange
var i = subRange.lowerBound
// Assign all the new elements into the start of the subrange
var i = subrange.lowerBound
var j = newValues.startIndex
for _ in 0..<newCount {
elements[i] = newValues[j]
formIndex(after: &i)
i += 1
newValues.formIndex(after: &j)
}
_expectEnd(j, newValues)
Expand All @@ -201,7 +202,7 @@ extension _ArrayBufferProtocol where Index == Int {
// Assign over the start of the replaced range with the tail
newTailStart.moveAssign(from: oldTailStart, count: tailCount)

// Destroy elements remaining after the tail in subRange
// Destroy elements remaining after the tail in subrange
(newTailStart + tailCount).deinitialize(
count: shrinkage - tailCount)
}
Expand Down
Loading