Skip to content

Commit d01e3b3

Browse files
Style fixes
1 parent 038347d commit d01e3b3

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Hashed Heap/HashedHeap.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public struct HashedHeap<T: Hashable> {
3131
/// - Complexity: O(n)
3232
public init(array: [T], sort: @escaping (T, T) -> Bool) {
3333
isOrderedBefore = sort
34-
buildHeap(fromArray: array)
34+
build(from: array)
3535
}
3636

3737
/// Converts an array to a max-heap or min-heap in a bottom-up manner.
3838
///
3939
/// - Complexity: O(n)
40-
private mutating func buildHeap(fromArray array: [T]) {
40+
private mutating func build(from array: [T]) {
4141
elements = array
4242
for index in elements.indices {
4343
indices[elements[index]] = index
@@ -96,12 +96,12 @@ public struct HashedHeap<T: Hashable> {
9696
/// Replaces an element in the hash.
9797
///
9898
/// 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 }
101101

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)
105105
}
106106

107107
/// Removes the root node from the heap.
@@ -130,7 +130,7 @@ public struct HashedHeap<T: Hashable> {
130130
/// You need to know the node's index, which may actually take O(n) steps to find.
131131
///
132132
/// - Complexity: O(log n).
133-
public mutating func removeAt(_ index: Int) -> T? {
133+
public mutating func remove(at index: Int) -> T? {
134134
guard index < elements.count else { return nil }
135135

136136
let size = elements.count - 1
@@ -158,12 +158,12 @@ public struct HashedHeap<T: Hashable> {
158158
mutating func shiftUp(_ index: Int) {
159159
var childIndex = index
160160
let child = elements[childIndex]
161-
var parentIndex = self.parentIndex(ofIndex: childIndex)
161+
var parentIndex = self.parentIndex(of: childIndex)
162162

163163
while childIndex > 0 && isOrderedBefore(child, elements[parentIndex]) {
164164
set(elements[parentIndex], at: childIndex)
165165
childIndex = parentIndex
166-
parentIndex = self.parentIndex(ofIndex: childIndex)
166+
parentIndex = self.parentIndex(of: childIndex)
167167
}
168168

169169
set(child, at: childIndex)
@@ -178,7 +178,7 @@ public struct HashedHeap<T: Hashable> {
178178
var parentIndex = index
179179

180180
while true {
181-
let leftChildIndex = self.leftChildIndex(ofIndex: parentIndex)
181+
let leftChildIndex = self.leftChildIndex(of: parentIndex)
182182
let rightChildIndex = leftChildIndex + 1
183183

184184
// Figure out which comes first if we order them by the sort function:
@@ -208,7 +208,7 @@ public struct HashedHeap<T: Hashable> {
208208

209209
/// Swap two elements in the heap and update the indices hash.
210210
private mutating func swapAt(_ i: Int, _ j: Int) {
211-
swap(&elements[i], &elements[j])
211+
elements.swapAt(i, j)
212212
indices[elements[i]] = i
213213
indices[elements[j]] = j
214214
}
@@ -217,23 +217,23 @@ public struct HashedHeap<T: Hashable> {
217217
///
218218
/// - Note: The element at index 0 is the root of the tree and has no parent.
219219
@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
222222
}
223223

224224
/// Returns the index of the left child of the element at index i.
225225
///
226226
/// - Note: this index can be greater than the heap size, in which case there is no left child.
227227
@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
230230
}
231231

232232
/// Returns the index of the right child of the element at index i.
233233
///
234234
/// - Note: this index can be greater than the heap size, in which case there is no right child.
235235
@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
238238
}
239239
}

0 commit comments

Comments
 (0)