123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import defaultValue from "../Core/defaultValue.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import DoubleEndedPriorityQueue from "../Core/DoubleEndedPriorityQueue.js";
- /**
- * @alias ImplicitSubtreeCache
- * @constructor
- *
- * @param {object} [options] Object with the following properties
- * @param {number} [options.maximumSubtreeCount=0] The total number of subtrees this cache can store. If adding a new subtree would exceed this limit, the lowest priority subtrees will be removed until there is room, unless the subtree that is going to be removed is the parent of the new subtree, in which case it will not be removed and the new subtree will still be added, exceeding the memory limit.
- *
- * @private
- */
- function ImplicitSubtreeCache(options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- /**
- * @type {number}
- * @private
- */
- this._maximumSubtreeCount = defaultValue(options.maximumSubtreeCount, 0);
- /**
- * A counter that goes up whenever a subtree is added. Used to sort subtrees by recency.
- * @type {number}
- * @private
- */
- this._subtreeRequestCounter = 0;
- /**
- * @type {DoubleEndedPriorityQueue}
- * @private
- */
- this._queue = new DoubleEndedPriorityQueue({
- comparator: ImplicitSubtreeCache.comparator,
- });
- }
- /**
- * @param {ImplicitSubtree} subtree
- */
- ImplicitSubtreeCache.prototype.addSubtree = function (subtree) {
- const cacheNode = new ImplicitSubtreeCacheNode(
- subtree,
- this._subtreeRequestCounter
- );
- this._subtreeRequestCounter++;
- this._queue.insert(cacheNode);
- // Make sure the parent subtree exists in the cache
- const subtreeCoord = subtree.implicitCoordinates;
- if (subtreeCoord.level > 0) {
- const parentCoord = subtreeCoord.getParentSubtreeCoordinates();
- const parentNode = this.find(parentCoord);
- //>>includeStart('debug', pragmas.debug)
- if (parentNode === undefined) {
- throw new DeveloperError("parent node needs to exist");
- }
- //>>includeEnd('debug');
- }
- if (this._maximumSubtreeCount > 0) {
- while (this._queue.length > this._maximumSubtreeCount) {
- const lowestPriorityNode = this._queue.getMinimum();
- if (lowestPriorityNode === cacheNode) {
- // Don't remove itself
- break;
- }
- this._queue.removeMinimum();
- }
- }
- };
- /**
- * @param {ImplicitTileCoordinates} subtreeCoord
- * @returns {ImplicitSubtree|undefined}
- */
- ImplicitSubtreeCache.prototype.find = function (subtreeCoord) {
- const queue = this._queue;
- const array = queue.internalArray;
- const arrayLength = queue.length;
- for (let i = 0; i < arrayLength; i++) {
- const other = array[i];
- const otherSubtree = other.subtree;
- const otherCoord = otherSubtree.implicitCoordinates;
- if (subtreeCoord.isEqual(otherCoord)) {
- return other.subtree;
- }
- }
- return undefined;
- };
- /**
- * @param {ImplicitSubtreeCacheNode} a
- * @param {ImplicitSubtreeCacheNode} b
- * @returns {number}
- */
- ImplicitSubtreeCache.comparator = function (a, b) {
- const aCoord = a.subtree.implicitCoordinates;
- const bCoord = b.subtree.implicitCoordinates;
- if (aCoord.isAncestor(bCoord)) {
- // Technically this shouldn't happen because the ancestor subtree was supposed to be added to the cache first.
- return +1.0;
- } else if (bCoord.isAncestor(aCoord)) {
- return -1.0;
- }
- return a.stamp - b.stamp;
- };
- /**
- * @alias ImplicitSubtreeCacheNode
- * @constructor
- *
- * @param {ImplicitSubtree} subtree
- * @param {number} stamp
- *
- * @private
- */
- function ImplicitSubtreeCacheNode(subtree, stamp) {
- this.subtree = subtree;
- this.stamp = stamp;
- }
- export default ImplicitSubtreeCache;
|