-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
3D Tiles Traversal cleanup #6390
Changes from 34 commits
d683e4d
366a1d2
3f09e0c
15700e4
7500d7d
b285439
b44933d
b975f1e
5a36e0b
66f977d
e0a2a86
dbdd7ef
2894551
b9219db
5e7e764
63ad7c3
c804dd3
a6aad57
4bbacf8
248c443
db216c1
1f51e9b
978f9f6
582a51f
49c005b
4509017
7d21281
ca07240
fae93f7
72ec719
e558f2f
0cb8dcd
6d29c37
5c97c9b
15921c2
bc9c55a
ee36a46
059d2b0
7254492
9046570
593826f
8e58835
57627f4
ac0cf76
3f6714b
fc65396
5e57ca2
5856714
6bda10e
79e77c7
716c6a4
a0d5c5c
6a8fcfa
2705996
c8c981e
1dd5e68
8df3ddb
6e84390
e95a9a2
8d85e60
4fc6f7e
6991103
30c0459
b527503
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
define([ | ||
'./Check', | ||
'./defineProperties' | ||
], function( | ||
Check, | ||
defineProperties) { | ||
'use strict'; | ||
|
||
/** | ||
* Priority queue for the {@link RequestScheduler} implemented as a sorted array. | ||
* The request with the highest priority is placed at index 0 and the request | ||
* with lowest priority is placed at index <code>length - 1</code>. | ||
* <p> | ||
* A lower <code>request.priority</code> value indicates that the request has higher priority. See {@link Request#priority}. | ||
* </p> | ||
* | ||
* @alias RequestQueue | ||
* @constructor | ||
* @private | ||
* | ||
* @param {Number} maximumLength The maximum length of the queue. | ||
*/ | ||
function RequestQueue(maximumLength) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.typeOf.number('maximumLength', maximumLength); | ||
//>>includeEnd('debug'); | ||
|
||
this._array = new Array(maximumLength); | ||
this._length = 0; | ||
this._maximumLength = maximumLength; | ||
} | ||
|
||
defineProperties(RequestQueue.prototype, { | ||
/** | ||
* Gets the length of the queue. | ||
* | ||
* @memberof RequestQueue.prototype | ||
* | ||
* @type {Number} | ||
* @readonly | ||
*/ | ||
length : { | ||
get : function() { | ||
return this._length; | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Get the request at the given index. | ||
* | ||
* @param {Number} index The index of the request. | ||
* | ||
* @return {Request} The request at the given index. | ||
*/ | ||
RequestQueue.prototype.get = function(index) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.typeOf.number.greaterThanOrEquals('index', index, 0); | ||
Check.typeOf.number.lessThan('index', index, this._length); | ||
//>>includeEnd('debug'); | ||
return this._array[index]; | ||
}; | ||
|
||
/** | ||
* Insert a request into the queue. If the length would grow greater than the maximum length | ||
* of the queue, the lowest priority request is removed and returned. | ||
* | ||
* @param {Request} request The request to insert. | ||
* | ||
* @return {Request|undefined} The request that was removed from the queue if the queue is at full capacity. | ||
*/ | ||
RequestQueue.prototype.insert = function(request) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.defined('request', request); | ||
//>>includeEnd('debug'); | ||
|
||
var array = this._array; | ||
var previousLength = this._length; | ||
var length = this._length; | ||
var maximumLength = this._maximumLength; | ||
|
||
if (length < maximumLength) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this can also just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
++this._length; | ||
} | ||
|
||
if (previousLength === 0) | ||
{ | ||
array[0] = request; | ||
return; | ||
} | ||
|
||
var removedRequest; | ||
var lastIndex = previousLength - 1; | ||
|
||
if (previousLength === maximumLength) { | ||
var lastRequest = array[lastIndex]; | ||
if (request.priority >= lastRequest.priority) { | ||
// The array is full and the priority value of this request is too high to be inserted. | ||
return request; | ||
} | ||
// The array is full and the inserted request pushes off the last request | ||
removedRequest = lastRequest; | ||
--lastIndex; | ||
} | ||
|
||
while (lastIndex >= 0 && request.priority < array[lastIndex].priority) { | ||
array[lastIndex + 1] = array[lastIndex]; // Shift element to the right | ||
--lastIndex; | ||
} | ||
array[lastIndex + 1] = request; | ||
|
||
return removedRequest; | ||
}; | ||
|
||
/** | ||
* Call the given function for each request in the queue. | ||
* | ||
* @type {RequestQueue~ForEachCallback} The function to call. | ||
*/ | ||
RequestQueue.prototype.forEach = function(callback) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.typeOf.func('callback', callback); | ||
//>>includeEnd('debug'); | ||
|
||
var array = this._array; | ||
var length = this._length; | ||
for (var i = 0; i < length; ++i) { | ||
callback(array[i]); | ||
} | ||
}; | ||
|
||
/** | ||
* Sorts the queue. | ||
*/ | ||
RequestQueue.prototype.sort = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I asked this in another PR, but do you want this to delegate to a new generic function just like how mergeSort is separate? https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Core/mergeSort.js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replied really late to your comment in the other PR, but it is here: #6240 (comment). |
||
var array = this._array; | ||
var length = this._length; | ||
|
||
// Use insertion sort since our array is small and likely to be mostly sorted already. | ||
// Additionally length may be smaller than the array's actual length, so calling array.sort will lead to incorrect results for uninitialized values. | ||
for (var i = 1; i < length; ++i) { | ||
var j = i; | ||
while ((j > 0) && (array[j - 1].priority > array[j].priority)) { | ||
var temp = array[j - 1]; | ||
array[j - 1] = array[j]; | ||
array[j] = temp; | ||
--j; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Remove <code>length</code> number of requests from the top of the queue. | ||
* | ||
* @param {Number} length The number of requests to remove. | ||
*/ | ||
RequestQueue.prototype.remove = function(length) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.typeOf.number.greaterThanOrEquals('length', length, 0); | ||
Check.typeOf.number.lessThanOrEquals('length', length, this._length); | ||
//>>includeEnd('debug'); | ||
if (length === 0) { | ||
return; | ||
} | ||
if (length === this._length) { | ||
this._length = 0; | ||
return; | ||
} | ||
|
||
// Shift remaining requests back to the left | ||
var array = this._array; | ||
for (var i = length; i < this._length; ++i) { | ||
array[i - length] = array[i]; | ||
} | ||
this._length -= length; | ||
}; | ||
|
||
/** | ||
* The callback to use in forEach. | ||
* @callback RequestQueue~ForEachCallback | ||
* @param {Request} request The request. | ||
*/ | ||
|
||
return RequestQueue; | ||
}); |
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.
Nitpicky, but since we have it should this check
number.greaterThanOrEquals
?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.
Update in #6240: 9b9d8fd