All files / engine/Source/Scene ImplicitSubtreeCache.js

95.23% Statements 40/42
88.88% Branches 16/18
100% Functions 5/5
95.12% Lines 39/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                            48x           48x             48x           48x               1x 23x       23x 23x     23x 23x 4x 4x     4x 1x         22x 4x 1x 1x         1x                 1x 42x 42x 42x   42x 9x 9x 9x 9x 5x     37x               1x 6x 6x 6x     6x 3x   3x                         23x 23x        
import Frozen from "../Core/Frozen.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 = options ?? Frozen.EMPTY_OBJECT;
 
  /**
   * @type {number}
   * @private
   */
  this._maximumSubtreeCount = 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();
      Iif (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;
  Iif (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;