-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
Added proper priorityqueue #519
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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 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,72 +1,30 @@ | ||
import ArrayList from './ArrayList.js' | ||
import FastPriorityQueue from 'fastpriorityqueue/FastPriorityQueue.js' | ||
|
||
export default class PriorityQueue { | ||
|
||
constructor() { | ||
PriorityQueue.constructor_.apply(this, arguments) | ||
} | ||
static constructor_() { | ||
this._size = null | ||
this._items = null | ||
this._size = 0 | ||
this._items = new ArrayList() | ||
this._items.add(null) | ||
} | ||
remove_(i) { | ||
if (this.isEmpty()) return null | ||
const minItem = this._items.get(i) | ||
this._items.set(1, this._items.get(this._size)) | ||
this._size -= 1 | ||
this.reorder(i) | ||
return minItem | ||
this._fpQueue = new FastPriorityQueue((a,b) => a.compareTo(b) < 0); | ||
} | ||
|
||
poll() { | ||
return this.remove_(1) | ||
return this._fpQueue.poll() | ||
} | ||
size() { | ||
return this._size | ||
} | ||
reorder(hole) { | ||
let child = null | ||
const tmp = this._items.get(hole) | ||
for (; hole * 2 <= this._size; hole = child) { | ||
child = hole * 2 | ||
if (child !== this._size && this._items.get(child + 1).compareTo(this._items.get(child)) < 0) child++ | ||
if (this._items.get(child).compareTo(tmp) < 0) this._items.set(hole, this._items.get(child)); else break | ||
} | ||
this._items.set(hole, tmp) | ||
return this._fpQueue.size | ||
} | ||
clear() { | ||
this._size = 0 | ||
this._items.clear() | ||
this._fpQueue = new FastPriorityQueue(); | ||
} | ||
peek() { | ||
if (this.isEmpty()) return null | ||
const minItem = this._items.get(1) | ||
return minItem | ||
return this._fpQueue.peek() | ||
} | ||
remove(o) { | ||
if (o === undefined) { | ||
o = this._items.get(1) | ||
this.remove_(1) | ||
return o | ||
} else { | ||
const i = this._items.array.indexOf(o) | ||
if (i === -1) | ||
return false | ||
this.remove_(i) | ||
return true | ||
} | ||
remove() { | ||
return this._fpQueue.poll() | ||
} | ||
isEmpty() { | ||
return this._size === 0 | ||
return this._fpQueue.isEmpty() | ||
} | ||
add(x) { | ||
this._items.add(null) | ||
this._size += 1 | ||
let hole = this._size | ||
this._items.set(0, x) | ||
for (; x.compareTo(this._items.get(Math.trunc(hole / 2))) < 0; hole /= 2) | ||
this._items.set(hole, this._items.get(Math.trunc(hole / 2))) | ||
|
||
this._items.set(hole, x) | ||
this._fpQueue.add(x) | ||
} | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should remain unchanged, the java PQ interface must be kept in place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current STRtree in jts also uses the import java.util.PriorityQueue... in fact you did that change yourself (locationtech/jts@ff638ee) :)
Or was that done after your last conversion from jts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's direct from conversion, that is why I don't want any change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well when STRtree uses the old (deprecated) priorityqueue my testcase gets in that infinite loop...
and if I check https://github.com/locationtech/jts/blob/7ef2b9d2e6f36ce5e7a787cff57bd18281e50826/modules/core/src/main/java/org/locationtech/jts/index/strtree/STRtree.java#L20 it clearly states that the priorityqueue should be the java one and not the jts one...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes I see now and sorry for misleading. The problem is, unfortunately, that the conversion process do not work for recent verisons of JTS so JSTS is lagging behind. There are several unfortunate reasons for this, one of them is lack of time on my side but another is lacking upstream interest (IMHO).
I'm not sure what the best way to move forward here. Possibly it is to also supply the alternative implementation, manually, over the generated one at https://github.com/bjornharrtell/jsts/blob/master/src/org/locationtech/jts/util/PriorityQueue.js and then I can except that file from the conversion process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, clear... I saw that you even committed a change there to use the internal PriorityQueue...(locationtech/jts@941fb5d)
Do you need something from me (time/effort/whatever) to get this going forward? I'd really like to get this fixed so I can continue using this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm completely forgot why I did that..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@doskabouter I've removed use of the internal PriorityQueue in the JTS 1.17 patched version and it seems to pass tests for that source, and I've regenerated to JSTS. You should try adapting this PR to that and see if it can work for your case. It would also be good with a test case that demonstrates that what is fixed by using a new PriorityQueue implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry I see you already have a good test case added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot!
Need anything else from me?