Skip to content

Commit 0c0463c

Browse files
authored
Update README.md
1 parent 970a35b commit 0c0463c

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Or you can specify neither. By default a `PriorityQueue` is descending and empty
5656
### Methods
5757
`PriorityQueue` has all of the standard methods you'd expect a priority queue data structure to have.
5858
* `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)
6060
* `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)
6161
* `peek() -> T?` - Returns the element with the highest (or lowest if ascending) priority or `nil` if the priority queue is empty. O(1)
6262
* `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
8282

8383
### Limited Heap Size Example
8484

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.
8686

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:
9188

9289
```
93-
var pq: PriorityQueue<Int> = PriorityQueue<Int>(ascending: false) // inverted!
90+
var pq: PriorityQueue<Int> = PriorityQueue<Int>()
9491
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)
10299
```
103100

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.
105102

106103
### Just for Fun - A* (`astar.swift`)
107104
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`.
108105

109106
## 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

Comments
 (0)