-
-
Notifications
You must be signed in to change notification settings - Fork 7
Optimize PossibleIntersection and StablePriorityQueue #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace PolygonClipper; | ||
|
|
||
|
|
@@ -16,14 +18,18 @@ namespace PolygonClipper; | |
| internal sealed class StablePriorityQueue<T, TComparer> | ||
| where TComparer : IComparer<T> | ||
| { | ||
| private readonly List<T> heap = []; | ||
| private readonly List<T> heap; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="StablePriorityQueue{T, TComparer}"/> class with a specified comparer. | ||
| /// </summary> | ||
| /// <param name="comparer">The comparer to determine the priority of the elements.</param> | ||
| public StablePriorityQueue(TComparer comparer) | ||
| => this.Comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); | ||
| /// <param name="capacity">The initial capacity of the priority queue.</param> | ||
| public StablePriorityQueue(TComparer comparer, int capacity) | ||
| { | ||
| this.Comparer = comparer ?? throw new ArgumentNullException(nameof(comparer)); | ||
| this.heap = new List<T>(capacity > 0 ? capacity : 16); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the number of elements in the priority queue. | ||
|
|
@@ -42,7 +48,7 @@ public StablePriorityQueue(TComparer comparer) | |
| public void Enqueue(T item) | ||
| { | ||
| this.heap.Add(item); | ||
| this.Up(this.heap.Count - 1); | ||
| this.Up((uint)this.heap.Count - 1); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -86,61 +92,66 @@ public T Peek() | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Restores the heap property by moving the item at the specified index upward. | ||
| /// Restores the min-heap property by moving the item at the specified index upward | ||
| /// through the heap until it is in the correct position. This is called after insertion. | ||
| /// </summary> | ||
| /// <param name="index">The index of the item to move upward.</param> | ||
| private void Up(int index) | ||
| /// <param name="index">The index of the newly added item to sift upward.</param> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo sift --> shift
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, that's intentional, sift is standard terminology in heap operations |
||
| private void Up(uint index) | ||
| { | ||
| List<T> data = this.heap; | ||
| T item = data[index]; | ||
| ref T dRef = ref MemoryMarshal.GetReference(CollectionsMarshal.AsSpan(this.heap)); | ||
| T item = Unsafe.Add(ref dRef, index); | ||
| TComparer comparer = this.Comparer; | ||
|
|
||
| while (index > 0) | ||
| { | ||
| int parent = (index - 1) >> 1; | ||
| T current = data[parent]; | ||
| uint parent = (index - 1u) >> 1; | ||
| T current = Unsafe.Add(ref dRef, parent); | ||
| if (comparer.Compare(item, current) >= 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| data[index] = current; | ||
| Unsafe.Add(ref dRef, index) = current; | ||
| index = parent; | ||
| } | ||
|
|
||
| data[index] = item; | ||
| Unsafe.Add(ref dRef, index) = item; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Restores the heap property by moving the item at the specified index downward. | ||
| /// Restores the min-heap property by moving the item at the specified index downward | ||
| /// through the heap until it is in the correct position. This is called after removal of the root. | ||
| /// </summary> | ||
| /// <param name="index">The index of the item to move downward.</param> | ||
| private void Down(int index) | ||
| /// <param name="index">The index of the item to sift downward (typically the root).</param> | ||
| private void Down(uint index) | ||
| { | ||
| List<T> data = this.heap; | ||
| int halfLength = data.Count >> 1; | ||
| T item = data[index]; | ||
| Span<T> data = CollectionsMarshal.AsSpan(this.heap); | ||
| ref T dRef = ref MemoryMarshal.GetReference(data); | ||
|
|
||
| uint length = (uint)data.Length; | ||
| uint halfLength = length >> 1; | ||
| T item = Unsafe.Add(ref dRef, index); | ||
| TComparer comparer = this.Comparer; | ||
|
|
||
| while (index < halfLength) | ||
| { | ||
| int bestChild = (index << 1) + 1; // Initially left child | ||
| int right = bestChild + 1; | ||
| uint bestChild = (index << 1) + 1; // Initially left child | ||
| uint right = bestChild + 1u; | ||
|
|
||
| if (right < data.Count && comparer.Compare(data[right], data[bestChild]) < 0) | ||
| if (right < length && comparer.Compare(Unsafe.Add(ref dRef, right), Unsafe.Add(ref dRef, bestChild)) < 0) | ||
| { | ||
| bestChild = right; | ||
| } | ||
|
|
||
| if (comparer.Compare(data[bestChild], item) >= 0) | ||
| if (comparer.Compare(Unsafe.Add(ref dRef, bestChild), item) >= 0) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| data[index] = data[bestChild]; | ||
| Unsafe.Add(ref dRef, index) = Unsafe.Add(ref dRef, bestChild); | ||
| index = bestChild; | ||
| } | ||
|
|
||
| data[index] = item; | ||
| Unsafe.Add(ref dRef, index) = item; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.