Return if the element is added to the ol.structs.PriorityQueue

This commit is contained in:
Frederic Junod
2015-12-09 13:30:54 +01:00
parent 3fef07f9e9
commit ad3c6713da
2 changed files with 15 additions and 1 deletions

View File

@@ -122,6 +122,7 @@ ol.structs.PriorityQueue.prototype.dequeue = function() {
/** /**
* Enqueue an element. O(log N). * Enqueue an element. O(log N).
* @param {T} element Element. * @param {T} element Element.
* @return {boolean} The element was added to the queue.
*/ */
ol.structs.PriorityQueue.prototype.enqueue = function(element) { ol.structs.PriorityQueue.prototype.enqueue = function(element) {
goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_), goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_),
@@ -132,7 +133,9 @@ ol.structs.PriorityQueue.prototype.enqueue = function(element) {
this.priorities_.push(priority); this.priorities_.push(priority);
this.queuedElements_[this.keyFunction_(element)] = true; this.queuedElements_[this.keyFunction_(element)] = true;
this.siftDown_(0, this.elements_.length - 1); this.siftDown_(0, this.elements_.length - 1);
return true;
} }
return false;
}; };

View File

@@ -28,14 +28,25 @@ describe('ol.structs.PriorityQueue', function() {
}); });
it('enqueue adds an element', function() { it('enqueue adds an element', function() {
pq.enqueue(0); var added = pq.enqueue(0);
expect(function() { expect(function() {
pq.assertValid(); pq.assertValid();
}).not.to.throwException(); }).not.to.throwException();
expect(added).to.be(true);
expect(pq.elements_).to.eql([0]); expect(pq.elements_).to.eql([0]);
expect(pq.priorities_).to.eql([0]); expect(pq.priorities_).to.eql([0]);
}); });
it('do not enqueue element with DROP priority', function() {
var added = pq.enqueue(Infinity);
expect(function() {
pq.assertValid();
}).not.to.throwException();
expect(added).to.be(false);
expect(pq.elements_).to.eql([]);
expect(pq.priorities_).to.eql([]);
});
it('maintains the pq property while elements are enqueued', function() { it('maintains the pq property while elements are enqueued', function() {
var i; var i;
for (i = 0; i < 32; ++i) { for (i = 0; i < 32; ++i) {