@@ -31,13 +31,13 @@ public struct HashedHeap<T: Hashable> {
31
31
/// - Complexity: O(n)
32
32
public init ( array: [ T ] , sort: @escaping ( T , T ) -> Bool ) {
33
33
isOrderedBefore = sort
34
- buildHeap ( fromArray : array)
34
+ build ( from : array)
35
35
}
36
36
37
37
/// Converts an array to a max-heap or min-heap in a bottom-up manner.
38
38
///
39
39
/// - Complexity: O(n)
40
- private mutating func buildHeap ( fromArray array: [ T ] ) {
40
+ private mutating func build ( from array: [ T ] ) {
41
41
elements = array
42
42
for index in elements. indices {
43
43
indices [ elements [ index] ] = index
@@ -96,12 +96,12 @@ public struct HashedHeap<T: Hashable> {
96
96
/// Replaces an element in the hash.
97
97
///
98
98
/// In a max-heap, the new element should be larger than the old one; in a min-heap it should be smaller.
99
- public mutating func replace( index i : Int , value : T ) {
100
- guard i < elements. count else { return }
99
+ public mutating func replace( _ value : T , at index : Int ) {
100
+ guard index < elements. count else { return }
101
101
102
- assert ( isOrderedBefore ( value, elements [ i ] ) )
103
- set ( value, at: i )
104
- shiftUp ( i )
102
+ assert ( isOrderedBefore ( value, elements [ index ] ) )
103
+ set ( value, at: index )
104
+ shiftUp ( index )
105
105
}
106
106
107
107
/// Removes the root node from the heap.
@@ -130,7 +130,7 @@ public struct HashedHeap<T: Hashable> {
130
130
/// You need to know the node's index, which may actually take O(n) steps to find.
131
131
///
132
132
/// - Complexity: O(log n).
133
- public mutating func removeAt ( _ index: Int ) -> T ? {
133
+ public mutating func remove ( at index: Int ) -> T ? {
134
134
guard index < elements. count else { return nil }
135
135
136
136
let size = elements. count - 1
@@ -158,12 +158,12 @@ public struct HashedHeap<T: Hashable> {
158
158
mutating func shiftUp( _ index: Int ) {
159
159
var childIndex = index
160
160
let child = elements [ childIndex]
161
- var parentIndex = self . parentIndex ( ofIndex : childIndex)
161
+ var parentIndex = self . parentIndex ( of : childIndex)
162
162
163
163
while childIndex > 0 && isOrderedBefore ( child, elements [ parentIndex] ) {
164
164
set ( elements [ parentIndex] , at: childIndex)
165
165
childIndex = parentIndex
166
- parentIndex = self . parentIndex ( ofIndex : childIndex)
166
+ parentIndex = self . parentIndex ( of : childIndex)
167
167
}
168
168
169
169
set ( child, at: childIndex)
@@ -178,7 +178,7 @@ public struct HashedHeap<T: Hashable> {
178
178
var parentIndex = index
179
179
180
180
while true {
181
- let leftChildIndex = self . leftChildIndex ( ofIndex : parentIndex)
181
+ let leftChildIndex = self . leftChildIndex ( of : parentIndex)
182
182
let rightChildIndex = leftChildIndex + 1
183
183
184
184
// Figure out which comes first if we order them by the sort function:
@@ -208,7 +208,7 @@ public struct HashedHeap<T: Hashable> {
208
208
209
209
/// Swap two elements in the heap and update the indices hash.
210
210
private mutating func swapAt( _ i: Int , _ j: Int ) {
211
- swap ( & elements[ i ] , & elements [ j ] )
211
+ elements. swapAt ( i , j )
212
212
indices [ elements [ i] ] = i
213
213
indices [ elements [ j] ] = j
214
214
}
@@ -217,23 +217,23 @@ public struct HashedHeap<T: Hashable> {
217
217
///
218
218
/// - Note: The element at index 0 is the root of the tree and has no parent.
219
219
@inline ( __always)
220
- func parentIndex( ofIndex i : Int ) -> Int {
221
- return ( i - 1 ) / 2
220
+ func parentIndex( of index : Int ) -> Int {
221
+ return ( index - 1 ) / 2
222
222
}
223
223
224
224
/// Returns the index of the left child of the element at index i.
225
225
///
226
226
/// - Note: this index can be greater than the heap size, in which case there is no left child.
227
227
@inline ( __always)
228
- func leftChildIndex( ofIndex i : Int ) -> Int {
229
- return 2 * i + 1
228
+ func leftChildIndex( of index : Int ) -> Int {
229
+ return 2 * index + 1
230
230
}
231
231
232
232
/// Returns the index of the right child of the element at index i.
233
233
///
234
234
/// - Note: this index can be greater than the heap size, in which case there is no right child.
235
235
@inline ( __always)
236
- func rightChildIndex( ofIndex i : Int ) -> Int {
237
- return 2 * i + 2
236
+ func rightChildIndex( of index : Int ) -> Int {
237
+ return 2 * index + 2
238
238
}
239
239
}
0 commit comments