66public struct Heap < T> {
77 /** The array that stores the heap's nodes. */
88 var elements = [ T] ( )
9-
9+
1010 /** Determines whether this is a max-heap (>) or min-heap (<). */
1111 fileprivate var isOrderedBefore : ( T , T ) -> Bool
12-
12+
1313 /**
1414 * Creates an empty heap.
1515 * The sort function determines whether this is a min-heap or max-heap.
@@ -18,7 +18,7 @@ public struct Heap<T> {
1818 public init ( sort: @escaping ( T , T ) -> Bool ) {
1919 self . isOrderedBefore = sort
2020 }
21-
21+
2222 /**
2323 * Creates a heap from an array. The order of the array does not matter;
2424 * the elements are inserted into the heap in the order determined by the
@@ -28,17 +28,17 @@ public struct Heap<T> {
2828 self . isOrderedBefore = sort
2929 buildHeap ( fromArray: array)
3030 }
31-
31+
3232 /*
33- // This version has O(n log n) performance.
34- private mutating func buildHeap(array: [T]) {
35- elements.reserveCapacity(array.count)
36- for value in array {
37- insert(value)
38- }
39- }
40- */
41-
33+ // This version has O(n log n) performance.
34+ private mutating func buildHeap(array: [T]) {
35+ elements.reserveCapacity(array.count)
36+ for value in array {
37+ insert(value)
38+ }
39+ }
40+ */
41+
4242 /**
4343 * Converts an array to a max-heap or min-heap in a bottom-up manner.
4444 * Performance: This runs pretty much in O(n).
@@ -49,23 +49,23 @@ public struct Heap<T> {
4949 shiftDown ( i, heapSize: elements. count)
5050 }
5151 }
52-
52+
5353 public var isEmpty : Bool {
5454 return elements. isEmpty
5555 }
56-
56+
5757 public var count : Int {
5858 return elements. count
5959 }
60-
60+
6161 /**
6262 * Returns the index of the parent of the element at index i.
6363 * The element at index 0 is the root of the tree and has no parent.
6464 */
6565 @inline ( __always) func parentIndex( ofIndex i: Int ) -> Int {
6666 return ( i - 1 ) / 2
6767 }
68-
68+
6969 /**
7070 * Returns the index of the left child of the element at index i.
7171 * Note that this index can be greater than the heap size, in which case
@@ -74,7 +74,7 @@ public struct Heap<T> {
7474 @inline ( __always) func leftChildIndex( ofIndex i: Int ) -> Int {
7575 return 2 * i + 1
7676 }
77-
77+
7878 /**
7979 * Returns the index of the right child of the element at index i.
8080 * Note that this index can be greater than the heap size, in which case
@@ -83,15 +83,15 @@ public struct Heap<T> {
8383 @inline ( __always) func rightChildIndex( ofIndex i: Int ) -> Int {
8484 return 2 * i + 2
8585 }
86-
86+
8787 /**
8888 * Returns the maximum value in the heap (for a max-heap) or the minimum
8989 * value (for a min-heap).
9090 */
9191 public func peek( ) -> T ? {
9292 return elements. first
9393 }
94-
94+
9595 /**
9696 * Adds a new value to the heap. This reorders the heap so that the max-heap
9797 * or min-heap property still holds. Performance: O(log n).
@@ -100,25 +100,25 @@ public struct Heap<T> {
100100 elements. append ( value)
101101 shiftUp ( elements. count - 1 )
102102 }
103-
103+
104104 public mutating func insert< S: Sequence > ( _ sequence: S ) where S. Iterator. Element == T {
105105 for value in sequence {
106106 insert ( value)
107107 }
108108 }
109-
109+
110110 /**
111111 * Allows you to change an element. In a max-heap, the new element should be
112112 * larger than the old one; in a min-heap it should be smaller.
113113 */
114114 public mutating func replace( index i: Int , value: T ) {
115115 guard i < elements. count else { return }
116-
116+
117117 assert ( isOrderedBefore ( value, elements [ i] ) )
118118 elements [ i] = value
119119 shiftUp ( i)
120120 }
121-
121+
122122 /**
123123 * Removes the root node from the heap. For a max-heap, this is the maximum
124124 * value; for a min-heap it is the minimum value. Performance: O(log n).
@@ -137,23 +137,23 @@ public struct Heap<T> {
137137 return value
138138 }
139139 }
140-
140+
141141 /**
142142 * Removes an arbitrary node from the heap. Performance: O(log n). You need
143143 * to know the node's index, which may actually take O(n) steps to find.
144144 */
145145 public mutating func removeAt( _ index: Int ) -> T ? {
146146 guard index < elements. count else { return nil }
147-
147+
148148 let size = elements. count - 1
149149 if index != size {
150- swap ( & elements[ index] , & elements [ size] )
150+ elements. swapAt ( index, size)
151151 shiftDown ( index, heapSize: size)
152152 shiftUp ( index)
153153 }
154154 return elements. removeLast ( )
155155 }
156-
156+
157157 /**
158158 * Takes a child node and looks at its parents; if a parent is not larger
159159 * (max-heap) or not smaller (min-heap) than the child, we exchange them.
@@ -162,31 +162,31 @@ public struct Heap<T> {
162162 var childIndex = index
163163 let child = elements [ childIndex]
164164 var parentIndex = self . parentIndex ( ofIndex: childIndex)
165-
165+
166166 while childIndex > 0 && isOrderedBefore ( child, elements [ parentIndex] ) {
167167 elements [ childIndex] = elements [ parentIndex]
168168 childIndex = parentIndex
169169 parentIndex = self . parentIndex ( ofIndex: childIndex)
170170 }
171-
171+
172172 elements [ childIndex] = child
173173 }
174-
174+
175175 mutating func shiftDown( ) {
176176 shiftDown ( 0 , heapSize: elements. count)
177177 }
178-
178+
179179 /**
180180 * Looks at a parent node and makes sure it is still larger (max-heap) or
181181 * smaller (min-heap) than its childeren.
182182 */
183183 mutating func shiftDown( _ index: Int , heapSize: Int ) {
184184 var parentIndex = index
185-
185+
186186 while true {
187187 let leftChildIndex = self . leftChildIndex ( ofIndex: parentIndex)
188188 let rightChildIndex = leftChildIndex + 1
189-
189+
190190 // Figure out which comes first if we order them by the sort function:
191191 // the parent, the left child, or the right child. If the parent comes
192192 // first, we're done. If not, that element is out-of-place and we make
@@ -199,8 +199,8 @@ public struct Heap<T> {
199199 first = rightChildIndex
200200 }
201201 if first == parentIndex { return }
202-
203- swap ( & elements[ parentIndex] , & elements [ first] )
202+
203+ elements. swapAt ( parentIndex, first)
204204 parentIndex = first
205205 }
206206 }
@@ -215,7 +215,7 @@ extension Heap where T: Equatable {
215215 public func index( of element: T ) -> Int ? {
216216 return index ( of: element, 0 )
217217 }
218-
218+
219219 fileprivate func index( of element: T , _ i: Int ) -> Int ? {
220220 if i >= count { return nil }
221221 if isOrderedBefore ( element, elements [ i] ) { return nil }
0 commit comments