Skip to content

Commit 9187839

Browse files
committed
Merge pull request #1607 from apple/swift-3-api-guidelines
Applying API guidelines to The Standard Library
2 parents 94e999f + 885b564 commit 9187839

File tree

1,378 files changed

+19862
-18504
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,378 files changed

+19862
-18504
lines changed

benchmark/single-source/AngryPhonebook.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public func run_AngryPhonebook(N: Int) {
3636
for _ in 1...N {
3737
for firstname in words {
3838
for lastname in words {
39-
(firstname.uppercaseString, lastname.lowercaseString)
39+
(firstname.uppercased, lastname.lowercased)
4040
}
4141
}
4242
}

benchmark/single-source/ArrayInClass.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ArrayContainer {
1414
final var arr : [Int]
1515

1616
init() {
17-
arr = [Int] (count: 100_000, repeatedValue: 0)
17+
arr = [Int] (repeating: 0, count: 100_000)
1818
}
1919

2020
func runLoop(N: Int) {

benchmark/single-source/ArrayOfGenericPOD.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RefArray<T> {
2222
var array : [T]
2323

2424
init(_ i:T) {
25-
array = [T](count: 100000, repeatedValue: i)
25+
array = [T](repeating: i, count: 100000)
2626
}
2727
}
2828

benchmark/single-source/ArrayOfGenericRef.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RefArray<T> {
5959
var array : [T]
6060

6161
init(_ i:T, count:Int = 10_000) {
62-
array = [T](count: count, repeatedValue: i)
62+
array = [T](repeating: i, count: count)
6363
}
6464
}
6565

benchmark/single-source/ArrayOfPOD.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RefArray<T> {
2020
var array : [T]
2121

2222
init(_ i:T, count:Int = 100_000) {
23-
array = [T](count: count, repeatedValue: i)
23+
array = [T](repeating: i, count: count)
2424
}
2525
}
2626

benchmark/single-source/ArrayOfRef.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class RefArray<T> {
7171
var array : [T]
7272

7373
init(_ i:T, count:Int = 10_000) {
74-
array = [T](count: count, repeatedValue: i)
74+
array = [T](repeating: i, count: count)
7575
}
7676
}
7777

benchmark/single-source/ArraySubscript.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public func run_ArraySubscript(N: Int) {
2222

2323
func bound(x: Int) -> Int { return min(x, numArrayElements-1) }
2424

25-
var arrays = [[Int]](count: numArrays, repeatedValue: [])
25+
var arrays = [[Int]](repeating: [], count: numArrays)
2626
for i in 0..<numArrays {
2727
for _ in 0..<numArrayElements {
2828
arrays[i].append(Int(truncatingBitPattern: Random()))

benchmark/single-source/CaptureProp.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func sum(x:Int, y:Int) -> Int {
1515
}
1616

1717
@inline(never)
18-
func benchCaptureProp<S : SequenceType
18+
func benchCaptureProp<S : Sequence
1919
>(
20-
s:S, _ f:(S.Generator.Element, S.Generator.Element)->S.Generator.Element) -> S.Generator.Element {
20+
s:S, _ f:(S.Iterator.Element, S.Iterator.Element)->S.Iterator.Element) -> S.Iterator.Element {
2121

22-
var g = s.generate()
23-
let initial = g.next()!
24-
return GeneratorSequence(g).reduce(initial, combine: f)
22+
var it = s.makeIterator()
23+
let initial = it.next()!
24+
return IteratorSequence(it).reduce(initial, combine: f)
2525
}
2626

2727
public func run_CaptureProp(N: Int) {

benchmark/single-source/DictionaryRemove.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public func run_DictionaryRemove(N: Int) {
3131
tmpDict = dict
3232
// Empty dictionary
3333
for i in 1...size {
34-
tmpDict.removeValueForKey(i)
34+
tmpDict.removeValue(forKey: i)
3535
}
3636
if !tmpDict.isEmpty {
3737
break

benchmark/single-source/ErrorHandling.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import TestsUtils
1414

15-
enum PizzaError : ErrorType {
15+
enum PizzaError : ErrorProtocol {
1616
case Pepperoni, Olives, Anchovy
1717
}
1818

benchmark/single-source/Hash.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Hash {
6464
// Hash state:
6565
final var messageLength : Int = 0
6666
final var dataLength : Int = 0
67-
final var data = [UInt8](count: 64, repeatedValue: 0)
67+
final var data = [UInt8](repeating: 0, count: 64)
6868
final var blocksize : Int
6969

7070
/// \brief Hash the internal data.
@@ -223,7 +223,7 @@ class MD5 : Hash {
223223
return first | second | third | fourth
224224
}
225225

226-
var w = [UInt32](count: 16, repeatedValue: 0)
226+
var w = [UInt32](repeating: 0, count: 16)
227227
override
228228
func hash() {
229229
assert(dataLength == blocksize, "Invalid block size")
@@ -362,7 +362,7 @@ class SHA1 : Hash {
362362
assert(dataLength == blocksize, "Invalid block size")
363363

364364
// Init the "W" buffer.
365-
var w = [UInt32](count: 80, repeatedValue: 0)
365+
var w = [UInt32](repeating: 0, count: 80)
366366

367367
// Convert the Byte array to UInt32 array.
368368
var word : UInt32 = 0
@@ -497,7 +497,7 @@ class SHA256 : Hash {
497497
assert(dataLength == blocksize, "Invalid block size")
498498

499499
// Init the "W" buffer.
500-
var w = [UInt32](count: 64, repeatedValue: 0)
500+
var w = [UInt32](repeating: 0, count: 64)
501501

502502
// Convert the Byte array to UInt32 array.
503503
var word : UInt32 = 0

benchmark/single-source/Histogram.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import TestsUtils
1616

1717
typealias rrggbb_t = UInt32
1818

19-
func output_sorted_sparse_rgb_histogram<S: SequenceType where S.Generator.Element == rrggbb_t>(samples: S, _ N: Int) {
19+
func output_sorted_sparse_rgb_histogram<S: Sequence where S.Iterator.Element == rrggbb_t>(samples: S, _ N: Int) {
2020
var histogram = Dictionary<rrggbb_t, Int>()
2121
for _ in 1...50*N {
2222
for sample in samples { // This part is really awful, I agree
23-
let i = histogram.indexForKey(sample)
23+
let i = histogram.index(forKey: sample)
2424
histogram[sample] = (i != nil ? histogram[i!].1 : 0) + 1
2525
}
2626
}

benchmark/single-source/Join.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public func run_Join(N: Int) {
1919
for x in 0..<1000 * N {
2020
array.append(String(x))
2121
}
22-
_ = array.joinWithSeparator("")
23-
_ = array.joinWithSeparator(" ")
22+
_ = array.joined(separator: "")
23+
_ = array.joined(separator: " ")
2424
}

benchmark/single-source/Memset.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func memset(a: inout [Int], _ c: Int) {
2121

2222
@inline(never)
2323
public func run_Memset(N: Int) {
24-
var a = [Int](count: 10_000, repeatedValue: 0)
24+
var a = [Int](repeating: 0, count: 10_000)
2525
for _ in 1...50*N {
2626
memset(&a, 1)
2727
memset(&a, 0)

benchmark/single-source/MonteCarloE.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import TestsUtils
2121

2222
public func run_MonteCarloE(scale: Int) {
2323
let N = 200000*scale
24-
var intervals = [Bool](count: N, repeatedValue: false)
24+
var intervals = [Bool](repeating: false, count: N)
2525
for _ in 1...N {
2626
let pos = Int(UInt(truncatingBitPattern: Random())%UInt(N))
2727
intervals[pos] = true

benchmark/single-source/NSStringConversion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TestsUtils
1515
import Foundation
1616

1717
public func run_NSStringConversion(N: Int) {
18-
let test:NSString = NSString(CString: "test", encoding: NSASCIIStringEncoding)!
18+
let test:NSString = NSString(cString: "test", encoding: NSASCIIStringEncoding)!
1919
for _ in 1...N * 10000 {
2020
test as String
2121
}

benchmark/single-source/Phonebook.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ public func run_Phonebook(N: Int) {
6969
}
7070
for _ in 1...N {
7171
var t = Names;
72-
t.sortInPlace()
72+
t.sort()
7373
}
7474
}

benchmark/single-source/PopFront.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ let arrayCount = 1024
1717

1818
@inline(never)
1919
public func run_PopFrontArray(N: Int) {
20-
let orig = Array(count: arrayCount, repeatedValue: 1)
20+
let orig = Array(repeating: 1, count: arrayCount)
2121
var a = [Int]()
2222
for _ in 1...20*N {
2323
for _ in 1...reps {
2424
var result = 0
25-
a.appendContentsOf(orig)
25+
a.append(contentsOf: orig)
2626
while a.count != 0 {
2727
result += a[0]
28-
a.removeAtIndex(0)
28+
a.remove(at: 0)
2929
}
3030
CheckResults(result == arrayCount, "IncorrectResults in StringInterpolation: \(result) != \(arrayCount)")
3131
}
@@ -34,8 +34,8 @@ public func run_PopFrontArray(N: Int) {
3434

3535
@inline(never)
3636
public func run_PopFrontUnsafePointer(N: Int) {
37-
var orig = Array(count: arrayCount, repeatedValue: 1)
38-
let a = UnsafeMutablePointer<Int>.alloc(arrayCount)
37+
var orig = Array(repeating: 1, count: arrayCount)
38+
let a = UnsafeMutablePointer<Int>(allocatingCapacity: arrayCount)
3939
for _ in 1...100*N {
4040
for _ in 1...reps {
4141
for i in 0..<arrayCount {
@@ -51,6 +51,6 @@ public func run_PopFrontUnsafePointer(N: Int) {
5151
CheckResults(result == arrayCount, "IncorrectResults in StringInterpolation: \(result) != \(arrayCount)")
5252
}
5353
}
54-
a.dealloc(arrayCount)
54+
a.deallocateCapacity(arrayCount)
5555
}
5656

benchmark/single-source/PopFrontGeneric.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ let arrayCount = 1024
1818
// This test case exposes rdar://17440222 which caused rdar://17974483 (popFront
1919
// being really slow).
2020

21-
func _arrayReplace<B: _ArrayBufferType, C: CollectionType
22-
where C.Generator.Element == B.Element, B.Index == Int
21+
func _arrayReplace<B: _ArrayBufferProtocol, C: Collection
22+
where C.Iterator.Element == B.Element, B.Index == Int
2323
>(
2424
target: inout B, _ subRange: Range<Int>, _ newValues: C
2525
) {
@@ -36,7 +36,7 @@ func _arrayReplace<B: _ArrayBufferType, C: CollectionType
3636
let insertCount = numericCast(newValues.count) as Int
3737
let growth = insertCount - eraseCount
3838

39-
if target.requestUniqueMutableBackingBuffer(oldCount + growth) != nil {
39+
if target.requestUniqueMutableBackingBuffer(minimumCapacity: oldCount + growth) != nil {
4040
target.replace(subRange: subRange, with: insertCount, elementsOf: newValues)
4141
}
4242
else {
@@ -47,12 +47,12 @@ func _arrayReplace<B: _ArrayBufferType, C: CollectionType
4747

4848
@inline(never)
4949
public func run_PopFrontArrayGeneric(N: Int) {
50-
let orig = Array(count: arrayCount, repeatedValue: 1)
50+
let orig = Array(repeating: 1, count: arrayCount)
5151
var a = [Int]()
5252
for _ in 1...20*N {
5353
for _ in 1...reps {
5454
var result = 0
55-
a.appendContentsOf(orig)
55+
a.append(contentsOf: orig)
5656
while a.count != 0 {
5757
result += a[0]
5858
_arrayReplace(&a._buffer, 0..<1, EmptyCollection())

benchmark/single-source/Prims.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PriorityQueue {
2929
// Create heap for graph with NUM nodes.
3030
init(Num: Int) {
3131
heap = Array<EdgeCost>()
32-
graphIndexToHeapIndexMap = Array<Int?>(count: Num, repeatedValue:nil)
32+
graphIndexToHeapIndexMap = Array<Int?>(repeating:nil, count: Num)
3333
}
3434

3535
func isEmpty() -> Bool {
@@ -184,7 +184,7 @@ extension Edge : Hashable {
184184
}
185185

186186
func Prims(graph : Array<GraphNode>, _ fun : (Int,Int)->Double) -> Array<Int?> {
187-
var treeEdges = Array<Int?>(count:graph.count, repeatedValue:nil)
187+
var treeEdges = Array<Int?>(repeating:nil, count:graph.count)
188188

189189
let queue = PriorityQueue(Num:graph.count)
190190
// Make the minimum spanning tree root its own parent for simplicity.

benchmark/single-source/RC4.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct RC4 {
2020
var J : UInt8 = 0
2121

2222
init() {
23-
State = [UInt8](count: 256, repeatedValue: 0)
23+
State = [UInt8](repeating: 0, count: 256)
2424
}
2525

2626
mutating
@@ -84,7 +84,7 @@ public func run_RC4(N: Int) {
8484
let SecretData : [UInt8] = Array(Secret.utf8)
8585
let KeyData : [UInt8] = Array(Key.utf8)
8686

87-
var LongData : [UInt8] = [UInt8](count: messageLen, repeatedValue: 0)
87+
var LongData : [UInt8] = [UInt8](repeating: 0, count: messageLen)
8888

8989
for _ in 1...N {
9090
// Generate a long message.

benchmark/single-source/RGBHistogram.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import TestsUtils
2020

2121
@inline(never)
2222
public func run_RGBHistogram(N: Int) {
23-
var histogram = [(rrggbb_t, Int)]()
23+
var histogram = [(key: rrggbb_t, value: Int)]()
2424
for _ in 1...100*N {
2525
histogram = createSortedSparseRGBHistogram(samples)
2626
if !isCorrectHistogram(histogram) {
@@ -86,23 +86,23 @@ let samples: [rrggbb_t] = [
8686
0x607F4D9F, 0x9733E55E, 0xA360439D, 0x1DB568FD, 0xB7A5C3A1, 0xBE84492D
8787
]
8888

89-
func isCorrectHistogram(histogram: [(rrggbb_t, Int)]) -> Bool {
89+
func isCorrectHistogram(histogram: [(key: rrggbb_t, value: Int)]) -> Bool {
9090
return histogram.count == 157 &&
9191
histogram[0].0 == 0x00808080 && histogram[0].1 == 54 &&
9292
histogram[156].0 == 0x003B8D96 && histogram[156].1 == 1
9393
}
9494

9595
func createSortedSparseRGBHistogram
96-
<S: SequenceType where S.Generator.Element == rrggbb_t>
97-
(samples: S) -> [(rrggbb_t, Int)] {
96+
<S: Sequence where S.Iterator.Element == rrggbb_t>
97+
(samples: S) -> [(key: rrggbb_t, value: Int)] {
9898
var histogram = Dictionary<rrggbb_t, Int>()
9999

100100
for sample in samples {
101-
let i = histogram.indexForKey(sample)
101+
let i = histogram.index(forKey: sample)
102102
histogram[sample] = ((i != nil) ? histogram[i!].1 : 0) + 1
103103
}
104104

105-
return histogram.sort() {
105+
return histogram.sorted() {
106106
if $0.1 == $1.1 {
107107
return $0.0 > $1.0
108108
} else {

benchmark/single-source/RangeAssignment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TestsUtils
1515
@inline(never)
1616
public func run_RangeAssignment(scale: Int) {
1717
let range = 100..<200
18-
var vector = [Double](count: 5000, repeatedValue: 0.0 )
18+
var vector = [Double](repeating: 0.0 , count: 5000)
1919
let alfa = 1.0
2020
let N = 500*scale
2121
for _ in 1...N {

benchmark/single-source/RecursiveOwnedParameter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ArrayWrapper {
2424
var data: [Int]
2525

2626
init(_ count: Int, _ initialValue: Int) {
27-
data = [Int](count: count, repeatedValue: initialValue)
27+
data = [Int](repeating: initialValue, count: count)
2828
}
2929

3030
func compare(b: ArrayWrapper, _ iteration: Int, _ stopIteration: Int) -> Bool {

benchmark/single-source/Sim2DArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Array2D {
1616
let cols: Int
1717

1818
init(numRows: Int, numCols: Int) {
19-
storage = [Int](count: numRows * numCols, repeatedValue: 0)
19+
storage = [Int](repeating: 0, count: numRows * numCols)
2020
rows = numRows
2121
cols = numCols
2222
}

benchmark/single-source/SortLettersInPlace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public func run_SortLettersInPlace(N: Int) {
3434
]
3535

3636
// Sort the letters in place.
37-
letters.sortInPlace {
37+
letters.sort {
3838
return $0.value < $1.value
3939
}
4040

benchmark/single-source/SortStrings.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ func benchSortStrings(words: [String]) {
10211021
// Notice that we _copy_ the array of words before we sort it.
10221022
// Pass an explicit '<' predicate to benchmark reabstraction thunks.
10231023
var tempwords = words
1024-
tempwords.sortInPlace(<)
1024+
tempwords.sort(isOrderedBefore: <)
10251025
}
10261026

10271027
public func run_SortStrings(N: Int) {

0 commit comments

Comments
 (0)