Skip to content

Commit 38dc9ee

Browse files
author
Joe
committed
update Array2D, Deque to swift3.0
update Array2D, Deque to swift3.0
1 parent c783351 commit 38dc9ee

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

Array2D/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ let myCookie = cookies[3][6]
3030
Actually, you could create the array in a single line of code, like so:
3131

3232
```swift
33-
var cookies = [[Int]](count: 9, repeatedValue: [Int](count: 7, repeatedValue: 0))
33+
var cookies = [[Int]](repeating: [Int](repeating: 0, count: 7), count: 9)
3434
```
3535

3636
but that's just ugly. To be fair, you can hide the ugliness in a helper function:
3737

3838
```swift
3939
func dim<T>(count: Int, _ value: T) -> [T] {
40-
return [T](count: count, repeatedValue: value)
40+
return [T](repeating: value, count: count)
4141
}
4242
```
4343

@@ -72,7 +72,7 @@ public struct Array2D<T> {
7272
public init(columns: Int, rows: Int, initialValue: T) {
7373
self.columns = columns
7474
self.rows = rows
75-
array = .init(count: rows*columns, repeatedValue: initialValue)
75+
array = .init(repeating: initialValue, count: rows*columns)
7676
}
7777

7878
public subscript(column: Int, row: Int) -> T {

Deque/Deque-Optimized.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public struct Deque<T> {
1010

1111
public init(_ capacity: Int = 10) {
1212
self.capacity = max(capacity, 1)
13-
array = .init(count: capacity, repeatedValue: nil)
13+
array = [T?](repeating: nil, count: capacity)
1414
head = capacity
1515
}
1616

@@ -29,7 +29,7 @@ public struct Deque<T> {
2929
public mutating func enqueueFront(_ element: T) {
3030
if head == 0 {
3131
capacity *= 2
32-
let emptySpace = [T?](count: capacity, repeatedValue: nil)
32+
let emptySpace = [T?](repeating: nil, count: capacity)
3333
array.insertContentsOf(emptySpace, at: 0)
3434
head = capacity
3535
}

Deque/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public struct Deque<T> {
122122

123123
public init(_ capacity: Int = 10) {
124124
self.capacity = max(capacity, 1)
125-
array = .init(count: capacity, repeatedValue: nil)
125+
array = [T?](repeating: nil, count: capacity)
126126
head = capacity
127127
}
128128

@@ -238,8 +238,8 @@ There is one tiny problem... If you enqueue a lot of objects at the front, you'r
238238
public mutating func enqueueFront(element: T) {
239239
if head == 0 {
240240
capacity *= 2
241-
let emptySpace = [T?](count: capacity, repeatedValue: nil)
242-
array.insertContentsOf(emptySpace, at: 0)
241+
let emptySpace = [T?](repeating: nil, count: capacity)
242+
array.insert(contentsOf: emptySpace, at: 0)
243243
head = capacity
244244
}
245245

0 commit comments

Comments
 (0)