You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-16Lines changed: 13 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,7 @@ Or you can specify neither. By default a `PriorityQueue` is descending and empty
56
56
### Methods
57
57
`PriorityQueue` has all of the standard methods you'd expect a priority queue data structure to have.
58
58
*`push(element: T)` - Puts an element into the priority queue. O(lg n)
59
-
*`push(element: T, maxHeap: Int)` - Attempts to put an element into an ‘inverted’ priority queue, with limitation on heap size. O(lg n)
59
+
*`push(element: T, maxCount: Int) -> T?` - Adds an element while limiting the size of the priority queue to `maxCount`. If more than `maxCount` elements are in the priority queue, `maxCount - count` elements will be popped. Only the lowest priority items will be removed. The first element popped will be returned or `nil` if nothing is popped. O(n lg n)
60
60
*`pop() -> T?` - Returns and removes the element with the highest (or lowest if ascending) priority or `nil` if the priority queue is empty. O(lg n)
61
61
*`peek() -> T?` - Returns the element with the highest (or lowest if ascending) priority or `nil` if the priority queue is empty. O(1)
62
62
*`clear()` - Removes all elements from the priority queue.
@@ -82,29 +82,26 @@ Note: `PriorityQueue` is *not* thread-safe (do not manipulate it from multiple t
82
82
83
83
### Limited Heap Size Example
84
84
85
-
Two use cases in which you'll want to limit the heap size of a Priority Queue:
85
+
Suppose you want to only keep the `maxCount` highest priority items in the priority queue.
86
86
87
-
1. Bulk insertion of data where memory is constrained.
88
-
2. Only the ‘head’ of the Priority Queue is relevant and the ‘tail’ can be ignored.
89
-
90
-
In such cases, create an ‘inverted’ Priority Queue (where the sort is reversed) and use the `push(element: T, maxHeap: Int)` method to add items to the queue. For example, where you want to prioritize the first four(4) elements in ascending order:
87
+
For example, say you only want the priority queue to ever have 4 elements:
91
88
92
89
```
93
-
var pq: PriorityQueue<Int> = PriorityQueue<Int>(ascending: false) // inverted!
90
+
var pq: PriorityQueue<Int> = PriorityQueue<Int>()
94
91
let maxHeap = 4
95
-
// pq.reversed() output shown
96
-
pq.push(4, maxHeap) // [4]
97
-
pq.push(5, maxHeap) // [4, 5]
98
-
pq.push(0, maxHeap) // [0, 4, 5]
99
-
pq.push(3, maxHeap) // [0, 3, 4, 5]
100
-
pq.push(6, maxHeap) // [0, 3, 4, 5] (push of 6 is ignored)
101
-
pq.push(1, maxHeap) // [0, 1, 3, 4] (5 is discarded)
92
+
93
+
pq.push(4, maxHeap)
94
+
pq.push(5, maxHeap)
95
+
pq.push(0, maxHeap)
96
+
pq.push(3, maxHeap)
97
+
pq.push(6, maxHeap)
98
+
pq.push(1, maxHeap)
102
99
```
103
100
104
-
Note the use of `pq.reversed()` to show the values in ascending order.
101
+
In this case, after 4 elements were pushed, only the largest elements were kept (because the order was `ascending`). So, the final priority queue has the elements 0, 1, 3, 4 in it.
105
102
106
103
### Just for Fun - A* (`astar.swift`)
107
104
A* is a pathfinding algorithm that uses a priority queue. The sample program that comes with SwiftPriorityQueue is a maze solver that uses A*. You can find some in-source documentation if you want to reuse this algorithm inside `astar.swift`.
108
105
109
106
## Authorship & License
110
-
SwiftPriorityQueue is written by David Kopec and released under the MIT License (see `LICENSE`). You can find my email address on my GitHub profile page. I encourage you to submit pull requests and open issues here on GitHub.
107
+
SwiftPriorityQueue is written by David Kopec (@davecom on GitHub) and released under the MIT License (see `LICENSE`). You can find my contact information on my GitHub profile page. I encourage you to submit pull requests and open issues here on GitHub. Thank you to all of the contributors over the years.
0 commit comments