Skip to content

Commit 8dde602

Browse files
authored
Merge pull request kodecocodes#218 from raywenderlich/Swift3BoundedPriorityQueue
Swift3 bounded priority queue
2 parents 7e72285 + 6e2f5c1 commit 8dde602

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Bounded Priority Queue/BoundedPriorityQueue.playground/Sources/BoundedPriorityQueue.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BoundedPriorityQueue<T: Comparable> {
1212
private typealias Node = LinkedListNode<T>
1313

1414
private(set) public var count = 0
15-
private var head: Node?
15+
fileprivate var head: Node?
1616
private var tail: Node?
1717
private var maxElements: Int
1818

@@ -28,7 +28,7 @@ public class BoundedPriorityQueue<T: Comparable> {
2828
return head?.value
2929
}
3030

31-
public func enqueue(value: T) {
31+
public func enqueue(_ value: T) {
3232
if let node = insert(value, after: findInsertionPoint(value)) {
3333
// If the newly inserted node is the last one in the list, then update
3434
// the tail pointer.
@@ -44,7 +44,7 @@ public class BoundedPriorityQueue<T: Comparable> {
4444
}
4545
}
4646

47-
private func insert(value: T, after: Node?) -> Node? {
47+
private func insert(_ value: T, after: Node?) -> Node? {
4848
if let previous = after {
4949

5050
// If the queue is full and we have to insert at the end of the list,
@@ -78,11 +78,11 @@ public class BoundedPriorityQueue<T: Comparable> {
7878

7979
/* Find the node after which to insert the new value. If this returns nil,
8080
the new value should be inserted at the head of the list. */
81-
private func findInsertionPoint(value: T) -> Node? {
81+
private func findInsertionPoint(_ value: T) -> Node? {
8282
var node = head
8383
var prev: Node? = nil
8484

85-
while let current = node where value < current.value {
85+
while let current = node, value < current.value {
8686
prev = node
8787
node = current.next
8888
}

Bounded Priority Queue/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class BoundedPriorityQueue<T: Comparable> {
3535
private typealias Node = LinkedListNode<T>
3636

3737
private(set) public var count = 0
38-
private var head: Node?
38+
fileprivate var head: Node?
3939
private var tail: Node?
4040
private var maxElements: Int
4141

@@ -55,7 +55,7 @@ public class BoundedPriorityQueue<T: Comparable> {
5555
The `BoundedPriorityQueue` class contains a doubly linked list of `LinkedListNode` objects. Nothing special here yet. The fun stuff happens in the `enqueue()` method:
5656

5757
```swift
58-
public func enqueue(value: T) {
58+
public func enqueue(_ value: T) {
5959
if let node = insert(value, after: findInsertionPoint(value)) {
6060
// If the newly inserted node is the last one in the list, then update
6161
// the tail pointer.
@@ -71,7 +71,7 @@ public func enqueue(value: T) {
7171
}
7272
}
7373

74-
private func insert(value: T, after: Node?) -> Node? {
74+
private func insert(_ value: T, after: Node?) -> Node? {
7575
if let previous = after {
7676

7777
// If the queue is full and we have to insert at the end of the list,
@@ -105,7 +105,7 @@ private func insert(value: T, after: Node?) -> Node? {
105105

106106
/* Find the node after which to insert the new value. If this returns nil,
107107
the new value should be inserted at the head of the list. */
108-
private func findInsertionPoint(value: T) -> Node? {
108+
private func findInsertionPoint(_ value: T) -> Node? {
109109
var node = head
110110
var prev: Node? = nil
111111

0 commit comments

Comments
 (0)