Skip to content

Commit

Permalink
Merge pull request #303 from Moonsownner/master
Browse files Browse the repository at this point in the history
update Array2D, Deque to swift3.0
  • Loading branch information
kelvinlauKL committed Dec 17, 2016
2 parents 4edbc60 + 38dc9ee commit 2a403da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Array2D/README.markdown
Expand Up @@ -30,14 +30,14 @@ let myCookie = cookies[3][6]
Actually, you could create the array in a single line of code, like so:

```swift
var cookies = [[Int]](count: 9, repeatedValue: [Int](count: 7, repeatedValue: 0))
var cookies = [[Int]](repeating: [Int](repeating: 0, count: 7), count: 9)
```

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

```swift
func dim<T>(count: Int, _ value: T) -> [T] {
return [T](count: count, repeatedValue: value)
return [T](repeating: value, count: count)
}
```

Expand Down Expand Up @@ -72,7 +72,7 @@ public struct Array2D<T> {
public init(columns: Int, rows: Int, initialValue: T) {
self.columns = columns
self.rows = rows
array = .init(count: rows*columns, repeatedValue: initialValue)
array = .init(repeating: initialValue, count: rows*columns)
}

public subscript(column: Int, row: Int) -> T {
Expand Down
4 changes: 2 additions & 2 deletions Deque/Deque-Optimized.swift
Expand Up @@ -10,7 +10,7 @@ public struct Deque<T> {

public init(_ capacity: Int = 10) {
self.capacity = max(capacity, 1)
array = .init(count: capacity, repeatedValue: nil)
array = [T?](repeating: nil, count: capacity)
head = capacity
}

Expand All @@ -29,7 +29,7 @@ public struct Deque<T> {
public mutating func enqueueFront(_ element: T) {
if head == 0 {
capacity *= 2
let emptySpace = [T?](count: capacity, repeatedValue: nil)
let emptySpace = [T?](repeating: nil, count: capacity)
array.insertContentsOf(emptySpace, at: 0)
head = capacity
}
Expand Down
6 changes: 3 additions & 3 deletions Deque/README.markdown
Expand Up @@ -122,7 +122,7 @@ public struct Deque<T> {

public init(_ capacity: Int = 10) {
self.capacity = max(capacity, 1)
array = .init(count: capacity, repeatedValue: nil)
array = [T?](repeating: nil, count: capacity)
head = capacity
}

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

Expand Down

0 comments on commit 2a403da

Please sign in to comment.