Skip to content

Commit

Permalink
Added proper priorityqueue (#519)
Browse files Browse the repository at this point in the history
Co-authored-by: Björn Harrtell <bjornharrtell@users.noreply.github.com>
  • Loading branch information
doskabouter and bjornharrtell committed May 9, 2024
1 parent 8788a0f commit 70ff1b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 55 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"build-jsdoc": "jsdoc -c doc/jsdoc.json",
"build": "rollup -c rollup.config.js -o dist/jsts.min.js"
},
"dependencies": {
"fastpriorityqueue": "^0.7.5"
}
}
68 changes: 13 additions & 55 deletions src/java/util/PriorityQueue.js
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)
}
}
12 changes: 12 additions & 0 deletions test/manual/issues/516.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70ff1b6

Please sign in to comment.