6
6
public struct Heap < T> {
7
7
8
8
/** The array that stores the heap's nodes. */
9
- private ( set ) var nodes = [ T] ( )
9
+ internal var nodes = [ T] ( )
10
10
11
11
/**
12
12
* Determines how to compare two nodes in the heap.
@@ -33,14 +33,14 @@ public struct Heap<T> {
33
33
*/
34
34
public init ( array: [ T ] , sort: @escaping ( T , T ) -> Bool ) {
35
35
self . orderCriteria = sort
36
- buildHeap ( fromArray : array)
36
+ configureHeap ( from : array)
37
37
}
38
38
39
39
/**
40
- * Creates the max-heap or min-heap from an array, in a bottom-up manner.
40
+ * Configures the max-heap or min-heap from an array, in a bottom-up manner.
41
41
* Performance: This runs pretty much in O(n).
42
42
*/
43
- private mutating func buildHeap ( fromArray array: [ T ] ) {
43
+ private mutating func configureHeap ( from array: [ T ] ) {
44
44
nodes = array
45
45
for i in stride ( from: ( nodes. count/ 2 - 1 ) , through: 0 , by: - 1 ) {
46
46
shiftDown ( i)
@@ -59,7 +59,7 @@ public struct Heap<T> {
59
59
* Returns the index of the parent of the element at index i.
60
60
* The element at index 0 is the root of the tree and has no parent.
61
61
*/
62
- @inline ( __always) func parentIndex( ofIndex i: Int ) -> Int {
62
+ @inline ( __always) internal func parentIndex( ofIndex i: Int ) -> Int {
63
63
return ( i - 1 ) / 2
64
64
}
65
65
@@ -68,7 +68,7 @@ public struct Heap<T> {
68
68
* Note that this index can be greater than the heap size, in which case
69
69
* there is no left child.
70
70
*/
71
- @inline ( __always) func leftChildIndex( ofIndex i: Int ) -> Int {
71
+ @inline ( __always) internal func leftChildIndex( ofIndex i: Int ) -> Int {
72
72
return 2 * i + 1
73
73
}
74
74
@@ -77,7 +77,7 @@ public struct Heap<T> {
77
77
* Note that this index can be greater than the heap size, in which case
78
78
* there is no right child.
79
79
*/
80
- @inline ( __always) func rightChildIndex( ofIndex i: Int ) -> Int {
80
+ @inline ( __always) internal func rightChildIndex( ofIndex i: Int ) -> Int {
81
81
return 2 * i + 2
82
82
}
83
83
@@ -89,12 +89,6 @@ public struct Heap<T> {
89
89
return nodes. first
90
90
}
91
91
92
- /** Returns the node at given index */
93
- public func node( at i: Int ) -> T ? {
94
- guard i < nodes. count else { return nil }
95
- return nodes [ i]
96
- }
97
-
98
92
/**
99
93
* Adds a new value to the heap. This reorders the heap so that the max-heap
100
94
* or min-heap property still holds. Performance: O(log n).
@@ -121,7 +115,7 @@ public struct Heap<T> {
121
115
public mutating func replace( index i: Int , value: T ) {
122
116
guard i < nodes. count else { return }
123
117
124
- removeAt ( i)
118
+ remove ( at : i)
125
119
insert ( value)
126
120
}
127
121
@@ -130,26 +124,25 @@ public struct Heap<T> {
130
124
* value; for a min-heap it is the minimum value. Performance: O(log n).
131
125
*/
132
126
@discardableResult public mutating func remove( ) -> T ? {
133
- if !nodes. isEmpty {
134
- if nodes . count == 1 {
135
- return nodes . removeLast ( )
136
- } else {
137
- // Use the last node to replace the first one, then fix the heap by
138
- // shifting this new first node into its proper position.
139
- let value = nodes [ 0 ]
140
- nodes [ 0 ] = nodes . removeLast ( )
141
- shiftDown ( 0 )
142
- return value
143
- }
127
+ guard !nodes. isEmpty else { return nil }
128
+
129
+ if nodes . count == 1 {
130
+ return nodes . removeLast ( )
131
+ } else {
132
+ // Use the last node to replace the first one, then fix the heap by
133
+ // shifting this new first node into its proper position.
134
+ let value = nodes [ 0 ]
135
+ nodes [ 0 ] = nodes . removeLast ( )
136
+ shiftDown ( 0 )
137
+ return value
144
138
}
145
- return nil
146
139
}
147
140
148
141
/**
149
142
* Removes an arbitrary node from the heap. Performance: O(log n).
150
143
* Note that you need to know the node's index.
151
144
*/
152
- @discardableResult public mutating func removeAt ( _ index: Int ) -> T ? {
145
+ @discardableResult public mutating func remove ( at index: Int ) -> T ? {
153
146
guard index < nodes. count else { return nil }
154
147
155
148
let size = nodes. count - 1
@@ -165,7 +158,7 @@ public struct Heap<T> {
165
158
* Takes a child node and looks at its parents; if a parent is not larger
166
159
* (max-heap) or not smaller (min-heap) than the child, we exchange them.
167
160
*/
168
- mutating func shiftUp( _ index: Int ) {
161
+ internal mutating func shiftUp( _ index: Int ) {
169
162
var childIndex = index
170
163
let child = nodes [ childIndex]
171
164
var parentIndex = self . parentIndex ( ofIndex: childIndex)
@@ -183,7 +176,7 @@ public struct Heap<T> {
183
176
* Looks at a parent node and makes sure it is still larger (max-heap) or
184
177
* smaller (min-heap) than its childeren.
185
178
*/
186
- private mutating func shiftDown( from index: Int , until endIndex: Int ) {
179
+ internal mutating func shiftDown( from index: Int , until endIndex: Int ) {
187
180
let leftChildIndex = self . leftChildIndex ( ofIndex: index)
188
181
let rightChildIndex = leftChildIndex + 1
189
182
@@ -204,7 +197,7 @@ public struct Heap<T> {
204
197
shiftDown ( from: first, until: endIndex)
205
198
}
206
199
207
- private mutating func shiftDown( _ index: Int ) {
200
+ internal mutating func shiftDown( _ index: Int ) {
208
201
shiftDown ( from: index, until: nodes. count)
209
202
}
210
203
@@ -222,7 +215,7 @@ extension Heap where T: Equatable {
222
215
/** Removes the first occurrence of a node from the heap. Performance: O(n log n). */
223
216
@discardableResult public mutating func remove( node: T ) -> T ? {
224
217
if let index = index ( of: node) {
225
- return removeAt ( index)
218
+ return remove ( at : index)
226
219
}
227
220
return nil
228
221
}
0 commit comments