From 681141cb4e31c99d7942c421274b5ab92e46076a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Fri, 3 May 2024 20:19:09 +0200 Subject: [PATCH] Testcase for 516 and fix (#517) --- src/java/util/PriorityQueue.js | 64 +++++++++++++++------------------- test/manual/issues/516.js | 17 +++++++++ 2 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 test/manual/issues/516.js diff --git a/src/java/util/PriorityQueue.js b/src/java/util/PriorityQueue.js index 97c06a77..7f305bb9 100644 --- a/src/java/util/PriorityQueue.js +++ b/src/java/util/PriorityQueue.js @@ -1,78 +1,72 @@ -import ArrayList from '../../../../java/util/ArrayList.js' +import ArrayList from './ArrayList.js' export default class PriorityQueue { constructor() { PriorityQueue.constructor_.apply(this, arguments) } - - poll() { + 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(1) + const minItem = this._items.get(i) this._items.set(1, this._items.get(this._size)) this._size -= 1 - this.reorder(1) + this.reorder(i) return minItem } - + poll() { + return this.remove_(1) + } 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 + 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) } - clear() { this._size = 0 this._items.clear() } - peek() { if (this.isEmpty()) return null const minItem = this._items.get(1) return minItem } - remove(o) { - return this._items.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 + } } - isEmpty() { return this._size === 0 } - 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) + 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) } - - getClass() { - return PriorityQueue - } - - get interfaces_() { - return [] - } -} -PriorityQueue.constructor_ = function() { - this._size = null - this._items = null - this._size = 0 - this._items = new ArrayList() - this._items.add(null) } diff --git a/test/manual/issues/516.js b/test/manual/issues/516.js new file mode 100644 index 00000000..e6b0247a --- /dev/null +++ b/test/manual/issues/516.js @@ -0,0 +1,17 @@ +import expect from 'expect.js' + +import WKTReader from '../../../src/org/locationtech/jts/io/WKTReader.js' +import WKTWriter from '../../../src/org/locationtech/jts/io/WKTWriter.js' +import MaximumInscribedCircle from '../../../src/org/locationtech/jts/algorithm/construct/MaximumInscribedCircle.js' + +describe('Test (#516)', function() { + it('MaximumInscribedCircle basic test', function() { + const reader = new WKTReader() + const writer = new WKTWriter() + const input = 'POLYGON((10 10, 100 10, 100 100, 10 100, 10 10))' + const p = reader.read(input) + const result = MaximumInscribedCircle.getCenter(p, 1) + const wkt = writer.write(result) + expect(wkt).to.equal('POINT (55 55)') + }) +})