stdlib: rename RangeReplaceableCollectionType.extend() to appendContentsOf()

rdar://21972324

Swift SVN r30607
This commit is contained in:
Dmitri Hrybenko
2015-07-25 00:36:37 +00:00
parent 1a61bab17c
commit d97ac3e64c
22 changed files with 64 additions and 62 deletions

View File

@@ -396,10 +396,10 @@ self.test("\(testNamePrefix).append()/semantics") {
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// extend() // appendContentsOf()
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
self.test("\(testNamePrefix).extend()/semantics") { self.test("\(testNamePrefix).appendContentsOf()/semantics") {
let tests: [ExtendTest] = [ let tests: [ExtendTest] = [
ExtendTest( ExtendTest(
collection: [], collection: [],
@@ -441,7 +441,7 @@ self.test("\(testNamePrefix).extend()/semantics") {
var c = makeWrappedCollection(test.collection) var c = makeWrappedCollection(test.collection)
let newElements = let newElements =
MinimalForwardCollection(test.newElements.map(wrapValue)) MinimalForwardCollection(test.newElements.map(wrapValue))
c.extend(newElements) c.appendContentsOf(newElements)
expectEqualSequence( expectEqualSequence(
test.expected, test.expected,
c.map { extractValue($0).value }, c.map { extractValue($0).value },

View File

@@ -62,7 +62,7 @@ public struct LoggingGenerator<Base: GeneratorType>
/// Each static variable is a mapping of Type -> Number of calls. /// Each static variable is a mapping of Type -> Number of calls.
public class RangeReplaceableCollectionLog { public class RangeReplaceableCollectionLog {
public static var append = TypeIndexed(0) public static var append = TypeIndexed(0)
public static var extend = TypeIndexed(0) public static var appendContentsOf = TypeIndexed(0)
public static var insert = TypeIndexed(0) public static var insert = TypeIndexed(0)
public static var removeAll = TypeIndexed(0) public static var removeAll = TypeIndexed(0)
public static var removeAtIndex = TypeIndexed(0) public static var removeAtIndex = TypeIndexed(0)
@@ -140,11 +140,11 @@ public struct LoggingRangeReplaceableCollection<
base.append(newElement) base.append(newElement)
} }
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == Base.Generator.Element S : SequenceType where S.Generator.Element == Base.Generator.Element
>(newElements: S) { >(newElements: S) {
++Log.extend[selfType] ++Log.appendContentsOf[selfType]
base.extend(newElements) base.appendContentsOf(newElements)
} }
public mutating func insert( public mutating func insert(

View File

@@ -418,12 +418,12 @@ func _masterThreadOneTrial<RT : RaceTestWithPerTrialDataType>(
let rt = RT() let rt = RT()
sharedState.raceData.removeAll(keepCapacity: true) sharedState.raceData.removeAll(keepCapacity: true)
sharedState.raceData.extend( sharedState.raceData.appendContentsOf(
lazy(0..<raceDataCount).map { i in rt.makeRaceData() }) lazy(0..<raceDataCount).map { i in rt.makeRaceData() })
let identityShuffle = Array(0..<sharedState.raceData.count) let identityShuffle = Array(0..<sharedState.raceData.count)
sharedState.workerStates.removeAll(keepCapacity: true) sharedState.workerStates.removeAll(keepCapacity: true)
sharedState.workerStates.extend( sharedState.workerStates.appendContentsOf(
lazy(0..<racingThreadCount).map { lazy(0..<racingThreadCount).map {
i in i in
let workerState = _RaceTestWorkerState<RT>() let workerState = _RaceTestWorkerState<RT>()

View File

@@ -3131,11 +3131,11 @@ public struct ${Self}<T> : RangeReplaceableCollectionType {
elements.append(x) elements.append(x)
} }
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == T S : SequenceType where S.Generator.Element == T
>(newElements: S) { >(newElements: S) {
_willMutate() _willMutate()
elements.extend(newElements) elements.appendContentsOf(newElements)
} }
public mutating func replaceRange< public mutating func replaceRange<

View File

@@ -81,7 +81,7 @@ public func withArrayOfCStrings<R>(
var argsBuffer: [UInt8] = [] var argsBuffer: [UInt8] = []
argsBuffer.reserveCapacity(argsBufferSize) argsBuffer.reserveCapacity(argsBufferSize)
for arg in args { for arg in args {
argsBuffer.extend(arg.utf8) argsBuffer.appendContentsOf(arg.utf8)
argsBuffer.append(0) argsBuffer.append(0)
} }

View File

@@ -48,7 +48,7 @@ protocol _ArrayType
/// - Complexity: O(`count`). /// - Complexity: O(`count`).
mutating func reserveCapacity(minimumCapacity: Int) mutating func reserveCapacity(minimumCapacity: Int)
/// Operator form of `extend`. /// Operator form of `appendContentsOf`.
func += < func += <
S: SequenceType where S.Generator.Element == Generator.Element S: SequenceType where S.Generator.Element == Generator.Element
>(inout lhs: Self, rhs: S) >(inout lhs: Self, rhs: S)

View File

@@ -569,19 +569,19 @@ extension ${Self} : _ArrayType {
/// Append the elements of `newElements` to `self`. /// Append the elements of `newElements` to `self`.
/// ///
/// - Complexity: O(*length of result*). /// - Complexity: O(*length of result*).
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType S : SequenceType
where S.Generator.Element == Element where S.Generator.Element == Element
>(newElements: S) { >(newElements: S) {
self += newElements self += newElements
} }
// An overload of extend that uses the += that is optimized for // An overload of `appendContentsOf()` that uses the += that is optimized for
// collections. // collections.
/// Append the elements of `newElements` to `self`. /// Append the elements of `newElements` to `self`.
/// ///
/// - Complexity: O(*length of result*). /// - Complexity: O(*length of result*).
public mutating func extend< public mutating func appendContentsOf<
C : CollectionType C : CollectionType
where C.Generator.Element == Element where C.Generator.Element == Element
>(newElements: C) { >(newElements: C) {

View File

@@ -110,7 +110,7 @@ public struct Character :
return return
} }
var nativeString = "" var nativeString = ""
nativeString.extend(s) nativeString.appendContentsOf(s)
_representation = .Large(nativeString._core.nativeBuffer!._storage) _representation = .Large(nativeString._core.nativeBuffer!._storage)
} }
} }

View File

@@ -41,15 +41,15 @@ public func join<
if separatorSize != 0 { if separatorSize != 0 {
var gen = elements.generate() var gen = elements.generate()
if let first = gen.next() { if let first = gen.next() {
result.extend(first) result.appendContentsOf(first)
while let next = gen.next() { while let next = gen.next() {
result.extend(separator) result.appendContentsOf(separator)
result.extend(next) result.appendContentsOf(next)
} }
} }
} else { } else {
for x in elements { for x in elements {
result.extend(x) result.appendContentsOf(x)
} }
} }

View File

@@ -82,7 +82,7 @@ public protocol RangeReplaceableCollectionType : CollectionType {
mutating func append(x: Generator.Element) mutating func append(x: Generator.Element)
/* /*
The 'extend' requirement should be an operator, but the compiler crashes: The 'appendContentsOf' requirement should be an operator, but the compiler crashes:
<rdar://problem/16566712> Dependent type should have been substituted by Sema <rdar://problem/16566712> Dependent type should have been substituted by Sema
or SILGen or SILGen
@@ -96,7 +96,7 @@ public protocol RangeReplaceableCollectionType : CollectionType {
/// Append the elements of `newElements` to `self`. /// Append the elements of `newElements` to `self`.
/// ///
/// - Complexity: O(*length of result*). /// - Complexity: O(*length of result*).
mutating func extend< mutating func appendContentsOf<
S : SequenceType S : SequenceType
where S.Generator.Element == Generator.Element where S.Generator.Element == Generator.Element
>(newElements: S) >(newElements: S)
@@ -167,7 +167,7 @@ extension RangeReplaceableCollectionType {
insert(newElement, atIndex: endIndex) insert(newElement, atIndex: endIndex)
} }
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == Generator.Element S : SequenceType where S.Generator.Element == Generator.Element
>(newElements: S) { >(newElements: S) {
for element in newElements { for element in newElements {
@@ -311,7 +311,7 @@ public func removeAll<
/// Append elements from `newElements` to `x`. /// Append elements from `newElements` to `x`.
/// ///
/// - Complexity: O(N). /// - Complexity: O(N).
@available(*, unavailable, message="call the 'extend()' method on the collection") @available(*, unavailable, message="call the 'appendContentsOf()' method on the collection")
public func extend< public func extend<
C: RangeReplaceableCollectionType, C: RangeReplaceableCollectionType,
S : SequenceType where S.Generator.Element == C.Generator.Element S : SequenceType where S.Generator.Element == C.Generator.Element
@@ -335,7 +335,7 @@ public func +<
where S.Generator.Element == C.Generator.Element where S.Generator.Element == C.Generator.Element
>(var lhs: C, rhs: S) -> C { >(var lhs: C, rhs: S) -> C {
// FIXME: what if lhs is a reference type? This will mutate it. // FIXME: what if lhs is a reference type? This will mutate it.
lhs.extend(rhs) lhs.appendContentsOf(rhs)
return lhs return lhs
} }
@@ -346,8 +346,8 @@ public func +<
>(lhs: S, rhs: C) -> C { >(lhs: S, rhs: C) -> C {
var result = C() var result = C()
result.reserveCapacity(rhs.count + numericCast(rhs.underestimateCount())) result.reserveCapacity(rhs.count + numericCast(rhs.underestimateCount()))
result.extend(lhs) result.appendContentsOf(lhs)
result.extend(rhs) result.appendContentsOf(rhs)
return result return result
} }
@@ -358,7 +358,7 @@ public func +<
>(var lhs: C, rhs: S) -> C { >(var lhs: C, rhs: S) -> C {
// FIXME: what if lhs is a reference type? This will mutate it. // FIXME: what if lhs is a reference type? This will mutate it.
lhs.reserveCapacity(lhs.count + numericCast(rhs.count)) lhs.reserveCapacity(lhs.count + numericCast(rhs.count))
lhs.extend(rhs) lhs.appendContentsOf(rhs)
return lhs return lhs
} }
@@ -369,7 +369,7 @@ public func +<
>(var lhs: RRC1, rhs: RRC2) -> RRC1 { >(var lhs: RRC1, rhs: RRC2) -> RRC1 {
// FIXME: what if lhs is a reference type? This will mutate it. // FIXME: what if lhs is a reference type? This will mutate it.
lhs.reserveCapacity(lhs.count + numericCast(rhs.count)) lhs.reserveCapacity(lhs.count + numericCast(rhs.count))
lhs.extend(rhs) lhs.appendContentsOf(rhs)
return lhs return lhs
} }

View File

@@ -388,7 +388,7 @@ extension SequenceType {
) -> [S.${GElement}] { ) -> [S.${GElement}] {
var result: [S.${GElement}] = [] var result: [S.${GElement}] = []
for element in self { for element in self {
result.extend(transform(element)) result.appendContentsOf(transform(element))
} }
return result return result
} }

View File

@@ -50,7 +50,7 @@ import SwiftShims
/// ///
/// var a = "foo" /// var a = "foo"
/// var b = a /// var b = a
/// b.extend("bar") /// b.appendContentsOf("bar")
/// print("a=\(a), b=\(b)") // a=foo, b=foobar /// print("a=\(a), b=\(b)") // a=foo, b=foobar
/// ///
/// Strings use Copy-on-Write so that their data is only copied /// Strings use Copy-on-Write so that their data is only copied
@@ -414,7 +414,7 @@ public func <(lhs: String, rhs: String) -> Bool {
extension String { extension String {
/// Append the elements of `other` to `self`. /// Append the elements of `other` to `self`.
public mutating func extend(other: String) { public mutating func appendContentsOf(other: String) {
_core.append(other._core) _core.append(other._core)
} }
@@ -573,11 +573,13 @@ extension String {
} }
} }
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType S : SequenceType
where S.Generator.Element == Character where S.Generator.Element == Character
>(newElements: S) { >(newElements: S) {
withMutableCharacters { (inout v: CharacterView) in v.extend(newElements) } withMutableCharacters {
(inout v: CharacterView) in v.appendContentsOf(newElements)
}
} }
/// Create an instance containing `characters`. /// Create an instance containing `characters`.

View File

@@ -294,14 +294,14 @@ extension String.CharacterView : RangeReplaceableCollectionType {
switch c._representation { switch c._representation {
case .Small(let _63bits): case .Small(let _63bits):
let bytes = Character._smallValue(_63bits) let bytes = Character._smallValue(_63bits)
_core.extend(Character._SmallUTF16(bytes)) _core.appendContentsOf(Character._SmallUTF16(bytes))
case .Large(_): case .Large(_):
_core.append(String(c)._core) _core.append(String(c)._core)
} }
} }
/// Append the elements of `newElements` to `self`. /// Append the elements of `newElements` to `self`.
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType S : SequenceType
where S.Generator.Element == Character where S.Generator.Element == Character
>(newElements: S) { >(newElements: S) {
@@ -317,7 +317,7 @@ extension String.CharacterView : RangeReplaceableCollectionType {
where S.Generator.Element == Character where S.Generator.Element == Character
>(_ characters: S) { >(_ characters: S) {
self = String.CharacterView() self = String.CharacterView()
self.extend(characters) self.appendContentsOf(characters)
} }
} }

View File

@@ -638,9 +638,9 @@ extension _StringCore : RangeReplaceableCollectionType {
: representableAsASCII() && !newElements.contains { $0 > 0x7f } ? 1 : representableAsASCII() && !newElements.contains { $0 > 0x7f } ? 1
: 2 : 2
)) ))
r.extend(self[0..<subRange.startIndex]) r.appendContentsOf(self[0..<subRange.startIndex])
r.extend(newElements) r.appendContentsOf(newElements)
r.extend(self[subRange.endIndex..<count]) r.appendContentsOf(self[subRange.endIndex..<count])
self = r self = r
} }
} }
@@ -660,7 +660,7 @@ extension _StringCore : RangeReplaceableCollectionType {
_copyInPlace(newSize: count, newCapacity: max(count, n), minElementWidth: 1) _copyInPlace(newSize: count, newCapacity: max(count, n), minElementWidth: 1)
} }
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType S : SequenceType
where S.Generator.Element == UTF16.CodeUnit where S.Generator.Element == UTF16.CodeUnit
>(s: S) { >(s: S) {

View File

@@ -264,10 +264,10 @@ extension String.UnicodeScalarView : RangeReplaceableCollectionType {
/// Append the elements of `newElements` to `self`. /// Append the elements of `newElements` to `self`.
/// ///
/// - Complexity: O(*length of result*). /// - Complexity: O(*length of result*).
public mutating func extend< public mutating func appendContentsOf<
S : SequenceType where S.Generator.Element == UnicodeScalar S : SequenceType where S.Generator.Element == UnicodeScalar
>(newElements: S) { >(newElements: S) {
_core.extend(newElements._prext_lazy.flatMap { $0.utf16 }) _core.appendContentsOf(newElements._prext_lazy.flatMap { $0.utf16 })
} }
/// Replace the given `subRange` of elements with `newElements`. /// Replace the given `subRange` of elements with `newElements`.
/// ///

View File

@@ -15,10 +15,10 @@ RangeReplaceableTestSuite.test("append/dispatch") {
expectCustomizable(tester, tester.log.append) expectCustomizable(tester, tester.log.append)
} }
RangeReplaceableTestSuite.test("extend/dispatch") { RangeReplaceableTestSuite.test("appendContentsOf/dispatch") {
var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ]) var tester = RangeReplaceableCollectionLog.dispatchTester([ OpaqueValue(1) ])
tester.extend([ 2, 3 ].map(OpaqueValue.init)) tester.appendContentsOf([ 2, 3 ].map(OpaqueValue.init))
expectCustomizable(tester, tester.log.extend) expectCustomizable(tester, tester.log.appendContentsOf)
} }
RangeReplaceableTestSuite.test("insert/dispatch") { RangeReplaceableTestSuite.test("insert/dispatch") {

View File

@@ -44,7 +44,7 @@
// LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: startIndex[#Index#]; name=startIndex{{$}} // LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: startIndex[#Index#]; name=startIndex{{$}}
// LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: endIndex[#Index#]; name=endIndex{{$}} // LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: endIndex[#Index#]; name=endIndex{{$}}
// LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: append({#(c): Character#})[#Void#]; name=append(c: Character){{$}} // LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: append({#(c): Character#})[#Void#]; name=append(c: Character){{$}}
// LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: extend({#(newElements): S#})[#Void#]; name=extend(newElements: S){{$}} // LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: appendContentsOf({#(newElements): S#})[#Void#]; name=appendContentsOf(newElements: S){{$}}
// LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: splice({#(newElements): S#}, {#atIndex: Index#})[#Void#]; name=splice(newElements: S, atIndex: Index){{$}} // LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: splice({#(newElements): S#}, {#atIndex: Index#})[#Void#]; name=splice(newElements: S, atIndex: Index){{$}}
// LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: removeAtIndex({#(i): Index#})[#Character#]; name=removeAtIndex(i: Index){{$}} // LITERAL4-DAG: Decl[InstanceMethod]/CurrNominal: removeAtIndex({#(i): Index#})[#Character#]; name=removeAtIndex(i: Index){{$}}
// LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: lowercaseString[#String#]; name=lowercaseString{{$}} // LITERAL4-DAG: Decl[InstanceVar]/CurrNominal: lowercaseString[#String#]; name=lowercaseString{{$}}

View File

@@ -28,7 +28,7 @@ func flatten<Element, Sequence: SequenceType, InnerSequence: SequenceType
var result = [Element]() var result = [Element]()
for innerSequence in outerSequence { for innerSequence in outerSequence {
result.extend(innerSequence) result.appendContentsOf(innerSequence)
} }
return result return result

View File

@@ -423,7 +423,7 @@ func stringliterals() {
// <rdar://problem/17128913> // <rdar://problem/17128913>
var s = "" var s = ""
s.extend(["x"]) s.appendContentsOf(["x"])
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// InOut arguments // InOut arguments

View File

@@ -87,21 +87,21 @@ ${Self}TestSuite.test("append()/IndexInvalidation/DifferentMutationEpoch") {
} }
} }
${Self}TestSuite.test("extend()/IndexInvalidation/DifferentBuffer") { ${Self}TestSuite.test("appendContentsOf()/IndexInvalidation/DifferentBuffer") {
var c1 = ${Self}([ 1010, 2020, 3030 ].map(OpaqueValue.init)) var c1 = ${Self}([ 1010, 2020, 3030 ].map(OpaqueValue.init))
let c2 = c1 let c2 = c1
let i = c1.startIndex let i = c1.startIndex
c1.extend([ 4040, 5050 ].map(OpaqueValue.init)) c1.appendContentsOf([ 4040, 5050 ].map(OpaqueValue.init))
expectFailure { expectFailure {
_ = i == c1.startIndex _ = i == c1.startIndex
} }
_blackHole(c2) _blackHole(c2)
} }
${Self}TestSuite.test("extend()/IndexInvalidation/DifferentMutationEpoch") { ${Self}TestSuite.test("appendContentsOf()/IndexInvalidation/DifferentMutationEpoch") {
var c = ${Self}([ 1010, 2020, 3030 ].map(OpaqueValue.init)) var c = ${Self}([ 1010, 2020, 3030 ].map(OpaqueValue.init))
let i = c.startIndex let i = c.startIndex
c.extend([ 4040, 5050 ].map(OpaqueValue.init)) c.appendContentsOf([ 4040, 5050 ].map(OpaqueValue.init))
expectFailure { expectFailure {
_ = i == c.startIndex _ = i == c.startIndex
} }

View File

@@ -595,7 +595,7 @@ func asciiString<
S: SequenceType where S.Generator.Element == Character S: SequenceType where S.Generator.Element == Character
>(content: S) -> String { >(content: S) -> String {
var s = String() var s = String()
s.extend(content) s.appendContentsOf(content)
expectEqual(1, s._core.elementWidth) expectEqual(1, s._core.elementWidth)
return s return s
} }
@@ -617,11 +617,11 @@ StringTests.test("stringCoreExtensibility") {
if k == 0 { expectEqual(1, x.elementWidth) } if k == 0 { expectEqual(1, x.elementWidth) }
for i in 0..<length { for i in 0..<length {
x.extend( x.appendContentsOf(
Repeat(count: 3, repeatedValue: i < boundary ? ascii : nonAscii)) Repeat(count: 3, repeatedValue: i < boundary ? ascii : nonAscii))
} }
// Make sure we can extend wide storage with pure ASCII // Make sure we can append pure ASCII to wide storage
x.extend(Repeat(count: 2, repeatedValue: ascii)) x.appendContentsOf(Repeat(count: 2, repeatedValue: ascii))
expectEqualSequence( expectEqualSequence(
[UTF16.CodeUnit(UnicodeScalar("b").value)] [UTF16.CodeUnit(UnicodeScalar("b").value)]
@@ -696,7 +696,7 @@ func makeStringCore(base: String) -> _StringCore {
var x = _StringCore() var x = _StringCore()
// make sure some - but not all - replacements will have to grow the buffer // make sure some - but not all - replacements will have to grow the buffer
x.reserveCapacity(base._core.count * 3 / 2) x.reserveCapacity(base._core.count * 3 / 2)
x.extend(base._core) x.appendContentsOf(base._core)
// In case the core was widened and lost its capacity // In case the core was widened and lost its capacity
x.reserveCapacity(base._core.count * 3 / 2) x.reserveCapacity(base._core.count * 3 / 2)
return x return x

View File

@@ -44,7 +44,7 @@ func sliceConcurrentAppendThread(tid: ThreadID) {
if tid == .Master { if tid == .Master {
// Get a fresh buffer. // Get a fresh buffer.
sharedString = "" sharedString = ""
sharedString.extend("abc") sharedString.appendContentsOf("abc")
sharedString.reserveCapacity(16) sharedString.reserveCapacity(16)
expectLE(16, sharedString.capacityInBytes) expectLE(16, sharedString.capacityInBytes)
} }
@@ -58,9 +58,9 @@ func sliceConcurrentAppendThread(tid: ThreadID) {
// Append to the private string. // Append to the private string.
if tid == .Master { if tid == .Master {
privateString.extend("def") privateString.appendContentsOf("def")
} else { } else {
privateString.extend("ghi") privateString.appendContentsOf("ghi")
} }
barrier() barrier()