New tile queue implementation (heapify-powered)

This commit is contained in:
Éric Lemoine
2013-01-17 16:15:03 +01:00
parent 11938d2264
commit c0c9cdef15
4 changed files with 235 additions and 18 deletions

View File

@@ -4,6 +4,12 @@ beforeEach(function() {
toBeA: function(type) {
return this.actual instanceof type;
},
toBeGreaterThanOrEqualTo: function(other) {
return this.actual >= other;
},
toBeLessThanOrEqualTo: function(other) {
return this.actual <= other;
},
toRoughlyEqual: function(other, tol) {
return Math.abs(this.actual - other) <= tol;
}

View File

@@ -84,6 +84,7 @@
<script type="text/javascript" src="spec/ol/source/xyz.test.js"></script>
<script type="text/javascript" src="spec/ol/tilecoord.test.js"></script>
<script type="text/javascript" src="spec/ol/tilegrid.test.js"></script>
<script type="text/javascript" src="spec/ol/tilequeue.test.js"></script>
<script type="text/javascript" src="spec/ol/tilerange.test.js"></script>
<script type="text/javascript" src="spec/ol/tileurlfunction.test.js"></script>
<script type="text/javascript" src="spec/ol/control/control.test.js"></script>

View File

@@ -0,0 +1,68 @@
describe('ol.TileQueue', function() {
// is the tile queue's array a heap?
function isHeap(tq) {
var heap = tq.heap_;
var i;
var key;
var leftKey;
var rightKey;
for (i = 0; i < (heap.length >> 1) - 1; i++) {
key = heap[i][0];
leftKey = heap[tq.getLeftChildIndex_(i)][0];
rightKey = heap[tq.getRightChildIndex_(i)][0];
if (leftKey < key || rightKey < key) {
return false;
}
}
return true;
}
function addRandomPriorityTiles(tq, num) {
var i, tile, priority;
for (i = 0; i < num; i++) {
tile = new ol.Tile();
priority = Math.floor(Math.random() * 100);
tq.heap_.push([priority, tile, new ol.Coordinate(0, 0), 1]);
tq.queuedTileKeys_[tile.getKey()] = true;
}
}
describe('heapify', function() {
it('does convert an arbitrary array into a heap', function() {
var tq = new ol.TileQueue(function() {});
addRandomPriorityTiles(tq, 100);
tq.heapify_();
expect(isHeap(tq)).toBeTruthy();
});
});
describe('reprioritize', function() {
it('does reprioritize the array', function() {
var tq = new ol.TileQueue(function() {});
addRandomPriorityTiles(tq, 100);
tq.heapify_();
// now reprioritize, changing the priority of 50 tiles and removing the
// rest
var i = 0;
tq.tilePriorityFunction_ = function() {
if ((i++) % 2 === 0) {
return undefined;
}
return Math.floor(Math.random() * 100);
};
tq.reprioritize();
expect(tq.heap_.length).toEqual(50);
expect(isHeap(tq)).toBeTruthy();
});
});
});