@@ -8,14 +8,14 @@ public struct Heap<T> {
8
8
var elements = [ T] ( )
9
9
10
10
/** Determines whether this is a max-heap (>) or min-heap (<). */
11
- private var isOrderedBefore : ( T , T ) -> Bool
11
+ fileprivate var isOrderedBefore : ( T , T ) -> Bool
12
12
13
13
/**
14
14
* Creates an empty heap.
15
15
* The sort function determines whether this is a min-heap or max-heap.
16
16
* For integers, > makes a max-heap, < makes a min-heap.
17
17
*/
18
- public init ( sort: ( T , T ) -> Bool ) {
18
+ public init ( sort: @escaping ( T , T ) -> Bool ) {
19
19
self . isOrderedBefore = sort
20
20
}
21
21
@@ -24,9 +24,9 @@ public struct Heap<T> {
24
24
* the elements are inserted into the heap in the order determined by the
25
25
* sort function.
26
26
*/
27
- public init ( array: [ T ] , sort: ( T , T ) -> Bool ) {
27
+ public init ( array: [ T ] , sort: @escaping ( T , T ) -> Bool ) {
28
28
self . isOrderedBefore = sort
29
- buildHeap ( array)
29
+ buildHeap ( fromArray : array)
30
30
}
31
31
32
32
/*
@@ -43,9 +43,9 @@ public struct Heap<T> {
43
43
* Converts an array to a max-heap or min-heap in a bottom-up manner.
44
44
* Performance: This runs pretty much in O(n).
45
45
*/
46
- private mutating func buildHeap( array: [ T ] ) {
46
+ fileprivate mutating func buildHeap( fromArray array: [ T ] ) {
47
47
elements = array
48
- for i in ( elements. count/ 2 - 1 ) . stride ( through: 0 , by: - 1 ) {
48
+ for i in stride ( from : ( elements. count/ 2 - 1 ) , through: 0 , by: - 1 ) {
49
49
shiftDown ( index: i, heapSize: elements. count)
50
50
}
51
51
}
@@ -62,7 +62,7 @@ public struct Heap<T> {
62
62
* Returns the index of the parent of the element at index i.
63
63
* The element at index 0 is the root of the tree and has no parent.
64
64
*/
65
- @inline ( __always) func indexOfParent ( i: Int ) -> Int {
65
+ @inline ( __always) func parentIndex ( ofIndex i: Int ) -> Int {
66
66
return ( i - 1 ) / 2
67
67
}
68
68
@@ -71,7 +71,7 @@ public struct Heap<T> {
71
71
* Note that this index can be greater than the heap size, in which case
72
72
* there is no left child.
73
73
*/
74
- @inline ( __always) func indexOfLeftChild ( i: Int ) -> Int {
74
+ @inline ( __always) func leftChildIndex ( ofIndex i: Int ) -> Int {
75
75
return 2 * i + 1
76
76
}
77
77
@@ -80,7 +80,7 @@ public struct Heap<T> {
80
80
* Note that this index can be greater than the heap size, in which case
81
81
* there is no right child.
82
82
*/
83
- @inline ( __always) func indexOfRightChild ( i: Int ) -> Int {
83
+ @inline ( __always) func rightChildIndex ( ofIndex i: Int ) -> Int {
84
84
return 2 * i + 2
85
85
}
86
86
@@ -96,12 +96,12 @@ public struct Heap<T> {
96
96
* Adds a new value to the heap. This reorders the heap so that the max-heap
97
97
* or min-heap property still holds. Performance: O(log n).
98
98
*/
99
- public mutating func insert( value: T ) {
99
+ public mutating func insert( _ value: T ) {
100
100
elements. append ( value)
101
101
shiftUp ( index: elements. count - 1 )
102
102
}
103
103
104
- public mutating func insert< S: SequenceType where S. Generator . Element == T > ( sequence : S ) {
104
+ public mutating func insert< S: Sequence > ( _ sequence : S ) where S. Iterator . Element == T {
105
105
for value in sequence {
106
106
insert ( value)
107
107
}
@@ -123,7 +123,7 @@ public struct Heap<T> {
123
123
* Removes the root node from the heap. For a max-heap, this is the maximum
124
124
* value; for a min-heap it is the minimum value. Performance: O(log n).
125
125
*/
126
- public mutating func remove( ) -> T ? {
126
+ @ discardableResult public mutating func remove( ) -> T ? {
127
127
if elements. isEmpty {
128
128
return nil
129
129
} else if elements. count == 1 {
@@ -142,14 +142,14 @@ public struct Heap<T> {
142
142
* Removes an arbitrary node from the heap. Performance: O(log n). You need
143
143
* to know the node's index, which may actually take O(n) steps to find.
144
144
*/
145
- public mutating func removeAtIndex ( i : Int ) -> T ? {
146
- guard i < elements. count else { return nil }
145
+ public mutating func removeAt ( index : Int ) -> T ? {
146
+ guard index < elements. count else { return nil }
147
147
148
148
let size = elements. count - 1
149
- if i != size {
150
- swap ( & elements[ i ] , & elements[ size] )
151
- shiftDown ( index: i , heapSize: size)
152
- shiftUp ( index: i )
149
+ if index != size {
150
+ swap ( & elements[ index ] , & elements[ size] )
151
+ shiftDown ( index: index , heapSize: size)
152
+ shiftUp ( index: index )
153
153
}
154
154
return elements. removeLast ( )
155
155
}
@@ -158,15 +158,15 @@ public struct Heap<T> {
158
158
* Takes a child node and looks at its parents; if a parent is not larger
159
159
* (max-heap) or not smaller (min-heap) than the child, we exchange them.
160
160
*/
161
- mutating func shiftUp( index index : Int ) {
161
+ mutating func shiftUp( index: Int ) {
162
162
var childIndex = index
163
163
let child = elements [ childIndex]
164
- var parentIndex = indexOfParent ( childIndex)
164
+ var parentIndex = self . parentIndex ( ofIndex : childIndex)
165
165
166
166
while childIndex > 0 && isOrderedBefore ( child, elements [ parentIndex] ) {
167
167
elements [ childIndex] = elements [ parentIndex]
168
168
childIndex = parentIndex
169
- parentIndex = indexOfParent ( childIndex)
169
+ parentIndex = self . parentIndex ( ofIndex : childIndex)
170
170
}
171
171
172
172
elements [ childIndex] = child
@@ -180,11 +180,11 @@ public struct Heap<T> {
180
180
* Looks at a parent node and makes sure it is still larger (max-heap) or
181
181
* smaller (min-heap) than its childeren.
182
182
*/
183
- mutating func shiftDown( index index : Int , heapSize: Int ) {
183
+ mutating func shiftDown( index: Int , heapSize: Int ) {
184
184
var parentIndex = index
185
185
186
186
while true {
187
- let leftChildIndex = indexOfLeftChild ( parentIndex)
187
+ let leftChildIndex = self . leftChildIndex ( ofIndex : parentIndex)
188
188
let rightChildIndex = leftChildIndex + 1
189
189
190
190
// Figure out which comes first if we order them by the sort function:
@@ -212,16 +212,16 @@ extension Heap where T: Equatable {
212
212
/**
213
213
* Searches the heap for the given element. Performance: O(n).
214
214
*/
215
- public func indexOf ( element: T ) -> Int ? {
216
- return indexOf ( element, 0 )
215
+ public func index ( of element: T ) -> Int ? {
216
+ return index ( of : element, 0 )
217
217
}
218
218
219
- private func indexOf ( element: T , _ i: Int ) -> Int ? {
219
+ fileprivate func index ( of element: T , _ i: Int ) -> Int ? {
220
220
if i >= count { return nil }
221
221
if isOrderedBefore ( element, elements [ i] ) { return nil }
222
222
if element == elements [ i] { return i }
223
- if let j = indexOf ( element, indexOfLeftChild ( i) ) { return j }
224
- if let j = indexOf ( element, indexOfRightChild ( i) ) { return j }
223
+ if let j = index ( of : element, self . leftChildIndex ( ofIndex : i) ) { return j }
224
+ if let j = index ( of : element, self . rightChildIndex ( ofIndex : i) ) { return j }
225
225
return nil
226
226
}
227
227
}
0 commit comments