Commit 940c963
Add a min-max heap implementation that can be used to back a priority queue (apple#61)
* Add PriorityQueue implementation built on top of a MinMaxHeap
* Merge MinMaxHeap into PriorityQueue
* Add `unordered` read-only view into underlying heap
* Start filling in PriorityQueue benchmarks
* Implement our own swapAt()
storage.swapAt exhibits excessive allocations
* Rename _delete(at:) to _remove(at:)
* Rename removeMin/removeMax -> popMin/popMax
* Use magic values 1, 2 to refer to the items in the first max level
Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
* Specify logarithmic complexities as "O(log `count`) / 2" instead of "O(log n)"
Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
* Clarify logic in popMax
* Simplify logic in _remove(at:)
* Move the bounds checking into the various index computation methods
They will all now return `nil` for invalid indices.
* Floyd's heap construction algorithm should start from count/2 - 1
* Add ObjC wrapper around CFBinaryHeap to benchmarks
* Fix benchmarks that broke because of renames
* Add removeMin/removeMax
* Make _minMaxHeapIsMinLevel an instance method
* Split _indexOfChildOrGrandchild(of:sortedUsing:) into two separate functions
There is a large performance cost with passing a predicate function.
* Defer comparing children when determining largest/smallest descendant
If the given index has 4 grandchildren, we can skip comparing the children.
* Added an iterator of the min and max views to the priority queue
* seperated iterator implementation into a new file
* Use renamed popMin/Max in Iterator instead of removeMin/Max
* Fix code formatting
Indentation of 2 spaces and 80 char column width
* Add sequence initializer
* Fix code formatting in benchmarks
* Fix benchmark names
* Remove init from Collection
This should already be handled by the init from Sequence
* Add conformance to ExpressibleByArrayLiteral
* Move ExpressibleByArrayLiteral conformance to separate file
* Update PriorityQueue's CMakeLists.txt
* Make ascending and descending iterators public
* Inline ALL THE THINGS!
* Iterative instead of recursive implementation, @inline(__always) a couple more critical functions.
* Address PR feedback, thanks!
* Check invariants on insertion and deletion
* Minor code formatting cleanup
* Add copyright header to test file
* Fix missing empty line
* Add naïve implementation of insert(contentsOf:)
* Add documentation on complexity of init<S:Sequence>(_:)
* Cite source paper in documentation
* Rename PriorityQueue -> MinMaxHeap
We'll be reintroducing the PriorityQueue type as a wrapper.
* Mark insert(contentsOf:) as inlinable
This results in ~10x speedup in my initial tests.
* Rename argument label "startingAt" -> "elementAt"
These functions used to be recursive, so "startingAt" made sense.
Now that they're iterative, we should fix the label.
* Remove coefficients from complexity docs
* Make `_minMaxHeapIsMinLevel` take an index instead of a count
* Rename MinMaxHeap -> Heap
* Add documentation for Heap
* Make Heap.Iterator init and direction internal
* Fix reference to queue in documentation
Co-authored-by: Dante Broggi <34220985+Dante-Broggi@users.noreply.github.com>
* Don't wrap integers in CFBinaryHeap benchmark in NSNumber
* Avoid heap allocation altogether
* Add table with performance of operations
* Add heap performance graph
* Apply suggestions from code review
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
* Make _checkInvariants comments a doc comment
* Split Heap.Iterator into two separate views
Adapted from Daryle Walker's (github.com/CTMacUser) suggestions
* Prefix heap storage variable with an underscore
* Update benchmark image
* Fix Sources/PriorityQueueModule/CMakeLists.txt
Heap+Iterator.swift was renamed to Heap+OrderedViews.swift
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
* CFBinaryHeap is only available on Darwin
* CFBinaryHeap is only available on Darwin
* CFBinaryHeap is only available on Darwin
Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
Co-authored-by: Amanuel Ephem <amanuelephrem776@gmail.com>
Co-authored-by: Joakim Hassila <joj@mac.com>
Co-authored-by: Dante Broggi <34220985+Dante-Broggi@users.noreply.github.com>
Co-authored-by: Karoy Lorentey <klorentey@apple.com>1 parent 0959ba7 commit 940c963
File tree
22 files changed
+1721
-3
lines changed- Benchmarks
- Benchmarks
- CppBenchmarks
- include
- src
- swift-collections-benchmark
- Documentation
- Images
- Sources
- Collections
- PriorityQueueModule
- Tests/PriorityQueueTests
22 files changed
+1721
-3
lines changedLines changed: 77 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
135 | 149 | | |
136 | 150 | | |
137 | 151 | | |
| |||
170 | 184 | | |
171 | 185 | | |
172 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
173 | 197 | | |
174 | 198 | | |
175 | 199 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
51 | 61 | | |
52 | 62 | | |
53 | 63 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
764 | 764 | | |
765 | 765 | | |
766 | 766 | | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
767 | 807 | | |
768 | 808 | | |
769 | 809 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | | - | |
0 commit comments