ImplicitSubtreeCache.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import defaultValue from "../Core/defaultValue.js";
  2. import DeveloperError from "../Core/DeveloperError.js";
  3. import DoubleEndedPriorityQueue from "../Core/DoubleEndedPriorityQueue.js";
  4. /**
  5. * @alias ImplicitSubtreeCache
  6. * @constructor
  7. *
  8. * @param {object} [options] Object with the following properties
  9. * @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.
  10. *
  11. * @private
  12. */
  13. function ImplicitSubtreeCache(options) {
  14. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  15. /**
  16. * @type {number}
  17. * @private
  18. */
  19. this._maximumSubtreeCount = defaultValue(options.maximumSubtreeCount, 0);
  20. /**
  21. * A counter that goes up whenever a subtree is added. Used to sort subtrees by recency.
  22. * @type {number}
  23. * @private
  24. */
  25. this._subtreeRequestCounter = 0;
  26. /**
  27. * @type {DoubleEndedPriorityQueue}
  28. * @private
  29. */
  30. this._queue = new DoubleEndedPriorityQueue({
  31. comparator: ImplicitSubtreeCache.comparator,
  32. });
  33. }
  34. /**
  35. * @param {ImplicitSubtree} subtree
  36. */
  37. ImplicitSubtreeCache.prototype.addSubtree = function (subtree) {
  38. const cacheNode = new ImplicitSubtreeCacheNode(
  39. subtree,
  40. this._subtreeRequestCounter
  41. );
  42. this._subtreeRequestCounter++;
  43. this._queue.insert(cacheNode);
  44. // Make sure the parent subtree exists in the cache
  45. const subtreeCoord = subtree.implicitCoordinates;
  46. if (subtreeCoord.level > 0) {
  47. const parentCoord = subtreeCoord.getParentSubtreeCoordinates();
  48. const parentNode = this.find(parentCoord);
  49. //>>includeStart('debug', pragmas.debug)
  50. if (parentNode === undefined) {
  51. throw new DeveloperError("parent node needs to exist");
  52. }
  53. //>>includeEnd('debug');
  54. }
  55. if (this._maximumSubtreeCount > 0) {
  56. while (this._queue.length > this._maximumSubtreeCount) {
  57. const lowestPriorityNode = this._queue.getMinimum();
  58. if (lowestPriorityNode === cacheNode) {
  59. // Don't remove itself
  60. break;
  61. }
  62. this._queue.removeMinimum();
  63. }
  64. }
  65. };
  66. /**
  67. * @param {ImplicitTileCoordinates} subtreeCoord
  68. * @returns {ImplicitSubtree|undefined}
  69. */
  70. ImplicitSubtreeCache.prototype.find = function (subtreeCoord) {
  71. const queue = this._queue;
  72. const array = queue.internalArray;
  73. const arrayLength = queue.length;
  74. for (let i = 0; i < arrayLength; i++) {
  75. const other = array[i];
  76. const otherSubtree = other.subtree;
  77. const otherCoord = otherSubtree.implicitCoordinates;
  78. if (subtreeCoord.isEqual(otherCoord)) {
  79. return other.subtree;
  80. }
  81. }
  82. return undefined;
  83. };
  84. /**
  85. * @param {ImplicitSubtreeCacheNode} a
  86. * @param {ImplicitSubtreeCacheNode} b
  87. * @returns {number}
  88. */
  89. ImplicitSubtreeCache.comparator = function (a, b) {
  90. const aCoord = a.subtree.implicitCoordinates;
  91. const bCoord = b.subtree.implicitCoordinates;
  92. if (aCoord.isAncestor(bCoord)) {
  93. // Technically this shouldn't happen because the ancestor subtree was supposed to be added to the cache first.
  94. return +1.0;
  95. } else if (bCoord.isAncestor(aCoord)) {
  96. return -1.0;
  97. }
  98. return a.stamp - b.stamp;
  99. };
  100. /**
  101. * @alias ImplicitSubtreeCacheNode
  102. * @constructor
  103. *
  104. * @param {ImplicitSubtree} subtree
  105. * @param {number} stamp
  106. *
  107. * @private
  108. */
  109. function ImplicitSubtreeCacheNode(subtree, stamp) {
  110. this.subtree = subtree;
  111. this.stamp = stamp;
  112. }
  113. export default ImplicitSubtreeCache;