[stdlib] Restore Array value semantics

'nuff said.

Swift SVN r18923
This commit is contained in:
Dave Abrahams
2014-06-16 13:56:15 +00:00
parent acc2c3be40
commit 85533dd7d3
9 changed files with 69 additions and 46 deletions

View File

@@ -42,10 +42,25 @@ struct ${Self}<T> : MutableCollection, Sliceable {
_precondition(index >= 0, "Negative ${Self} index is out of range")
return _buffer[index]
}
nonmutating set {
set {
_precondition(index < count, "${Self} index out of range")
_precondition(index >= 0, "Negative array index is out of range")
_buffer[index] = newValue
if _buffer.isMutableAndUniquelyReferenced() {
_buffer[index] = newValue
}
else {
{
(inout buffer: _Buffer, e: Element)->() in
var newBuffer = ContiguousArrayBuffer<T>(
count: buffer.count, minimumCapacity: buffer.count)
let target = buffer._uninitializedCopy(
0..index, target: newBuffer.elementStorage)
target.initialize(e)
buffer._uninitializedCopy(
(index + 1)..buffer.count, target: target + 1)
buffer = _Buffer(newBuffer)
}(&_buffer, newValue)
}
}
}
@@ -147,15 +162,12 @@ extension ${Self} : ArrayType {
/// array.
/// Complexity: O(N)
func copy() -> ${Self} {
var result = self
result.reserveCapacity(0)
return result
return self
}
/// Ensure the uniqueness of the array.
/// Complexity: O(N)
mutating func unshare() {
reserveCapacity(0)
func unshare() {
}
/// Ensure the array has enough mutable contiguous storage to store