All files / engine/Source/Core DoubleEndedPriorityQueue.js

100% Statements 137/137
100% Branches 56/56
100% Functions 19/19
100% Lines 133/133

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406                                    1332x 1331x 1330x 1258x               1329x 1329x 1329x     1329x     1x                     93337x                                 9x     4x   3x       2x 5x       2x   3x                           58x                             3x                   1x 1203x 1203x 1203x 1203x   1203x         1203x 1203x 90702x     1203x           1x 83x     83x 83x   82x 140509283x       1x             1x 1x     1x 200x                       1x     954x 954x 700x 1x 699x     304x 304x     138x   166x       815x 815x 815x 815x   815x                 1x 45524x 45524x 1x     45523x     45523x   45523x 44920x 44920x       45523x   45523x                 1x 45438x 45438x 1x     45437x         45437x 1281x     44156x 44156x     44156x 44156x 43556x         45437x   45437x                   1x 18x 18x 4x       14x                 1x 16x 16x 4x         12x 8x       4x           162202x 162202x 162202x 162202x       924695x       179264x       1015x 120x   895x 895x 895x     895x 413x 413x             895x 1503x 1503x 328x   1175x 1175x         88476x 88476x       88476x   183824x 183824x 183824x 176516x 113211x   176516x 176516x       176516x 561957x 561957x 295231x           183824x 150678x 150678x 135104x 135104x 9936x         183824x                        
import CesiumMath from "./Math.js";
import Check from "./Check.js";
import defined from "./defined.js";
 
/**
 * Array-backed min-max heap implementation of a double-ended priority queue.
 * This data structure allows for efficient removal of minimum and maximum elements.
 *
 * @alias DoubleEndedPriorityQueue
 * @constructor
 * @private
 *
 * @param {object} options Object with the following properties:
 * @param {DoubleEndedPriorityQueue.ComparatorCallback} options.comparator The comparator to use for the queue. If comparator(a, b) is less than 0, a is lower priority than b.
 * @param {number} [options.maximumLength] The maximum length of the queue. If an element is inserted when the queue is at full capacity, the minimum element is removed. By default, the size of the queue is unlimited.
 */
function DoubleEndedPriorityQueue(options) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("options", options);
  Check.defined("options.comparator", options.comparator);
  if (defined(options.maximumLength)) {
    Check.typeOf.number.greaterThanOrEquals(
      "options.maximumLength",
      options.maximumLength,
      0,
    );
  }
  //>>includeEnd('debug');
 
  this._comparator = options.comparator;
  this._maximumLength = options.maximumLength;
  this._array = defined(options.maximumLength)
    ? new Array(options.maximumLength)
    : [];
  this._length = 0;
}
 
Object.defineProperties(DoubleEndedPriorityQueue.prototype, {
  /**
   * Gets the number of elements in the queue.
   *
   * @memberof DoubleEndedPriorityQueue.prototype
   *
   * @type {number}
   * @readonly
   */
  length: {
    get: function () {
      return this._length;
    },
  },
 
  /**
   * Gets or sets the maximum number of elements in the queue.
   * If set to a smaller value than the current length of the queue, the lowest priority elements are removed.
   * If an element is inserted when the queue is at full capacity, the minimum element is removed.
   * If set to undefined, the size of the queue is unlimited.
   *
   * @memberof DoubleEndedPriorityQueue.prototype
   *
   * @type {number}
   * @readonly
   */
  maximumLength: {
    get: function () {
      return this._maximumLength;
    },
    set: function (value) {
      if (defined(value)) {
        //>>includeStart('debug', pragmas.debug);
        Check.typeOf.number.greaterThanOrEquals("maximumLength", value, 0);
        //>>includeEnd('debug');
 
        // Remove elements until the maximum length is met.
        while (this._length > value) {
          this.removeMinimum();
        }
 
        // The array size is fixed to the maximum length
        this._array.length = value;
      }
      this._maximumLength = value;
    },
  },
 
  /**
   * Gets the internal array.
   *
   * @memberof DoubleEndedPriorityQueue.prototype
   *
   * @type {Array}
   * @readonly
   */
  internalArray: {
    get: function () {
      return this._array;
    },
  },
 
  /**
   * The comparator used by the queue.
   * If comparator(a, b) is less than 0, a is lower priority than b.
   *
   * @memberof DoubleEndedPriorityQueue.prototype
   *
   * @type {DoubleEndedPriorityQueue.ComparatorCallback}
   * @readonly
   */
  comparator: {
    get: function () {
      return this._comparator;
    },
  },
});
 
/**
 * Clones the double ended priority queue.
 *
 * @returns {DoubleEndedPriorityQueue} The cloned double ended priority queue.
 */
DoubleEndedPriorityQueue.prototype.clone = function () {
  const maximumLength = this._maximumLength;
  const comparator = this._comparator;
  const array = this._array;
  const length = this._length;
 
  const result = new DoubleEndedPriorityQueue({
    comparator: comparator,
    maximumLength: maximumLength,
  });
 
  result._length = length;
  for (let i = 0; i < length; i++) {
    result._array[i] = array[i];
  }
 
  return result;
};
 
/**
 * Removes all elements from the queue.
 */
DoubleEndedPriorityQueue.prototype.reset = function () {
  this._length = 0;
 
  // Dereference elements
  const maximumLength = this._maximumLength;
  if (defined(maximumLength)) {
    // Dereference all elements but keep the array the same size
    for (let i = 0; i < maximumLength; i++) {
      this._array[i] = undefined;
    }
  } else {
    // Dereference all elements by clearing the array
    this._array.length = 0;
  }
};
 
/**
 * Resort the queue.
 */
DoubleEndedPriorityQueue.prototype.resort = function () {
  const length = this._length;
 
  // Fix the queue from the top-down
  for (let i = 0; i < length; i++) {
    pushUp(this, i);
  }
};
 
/**
 * Inserts an element into the queue.
 * If the queue is at full capacity, the minimum element is removed.
 * The new element is returned (and not added) if it is less than or equal priority to the minimum element.
 *
 * @param {*} element
 * @returns {*|undefined} The minimum element if the queue is at full capacity. Returns undefined if there is no maximum length.
 */
DoubleEndedPriorityQueue.prototype.insert = function (element) {
  let removedElement;
 
  const maximumLength = this._maximumLength;
  if (defined(maximumLength)) {
    if (maximumLength === 0) {
      return undefined;
    } else if (this._length === maximumLength) {
      // It's faster to access the minimum directly instead of calling the getter
      // because it avoids the length === 0 check.
      const minimumElement = this._array[0];
      if (this._comparator(element, minimumElement) <= 0.0) {
        // The element that is being inserted is less than or equal to
        // the minimum element, so don't insert anything and exit early.
        return element;
      }
      removedElement = this.removeMinimum();
    }
  }
 
  const index = this._length;
  this._array[index] = element;
  this._length++;
  pushUp(this, index);
 
  return removedElement;
};
 
/**
 * Removes the minimum element from the queue and returns it.
 * If the queue is empty, the return value is undefined.
 *
 * @returns {*|undefined} The minimum element, or undefined if the queue is empty.
 */
DoubleEndedPriorityQueue.prototype.removeMinimum = function () {
  const length = this._length;
  if (length === 0) {
    return undefined;
  }
 
  this._length--;
 
  // The minimum element is always the root
  const minimumElement = this._array[0];
 
  if (length >= 2) {
    this._array[0] = this._array[length - 1];
    pushDown(this, 0);
  }
 
  // Dereference removed element
  this._array[length - 1] = undefined;
 
  return minimumElement;
};
 
/**
 * Removes the maximum element from the queue and returns it.
 * If the queue is empty, the return value is undefined.
 *
 * @returns {*|undefined} The maximum element, or undefined if the queue is empty.
 */
DoubleEndedPriorityQueue.prototype.removeMaximum = function () {
  const length = this._length;
  if (length === 0) {
    return undefined;
  }
 
  this._length--;
  let maximumElement;
 
  // If the root has no children, the maximum is the root.
  // If the root has one child, the maximum is the child.
  if (length <= 2) {
    maximumElement = this._array[length - 1];
  } else {
    // Otherwise, the maximum is the larger of the root's two children.
    const maximumElementIndex = greaterThan(this, 1, 2) ? 1 : 2;
    maximumElement = this._array[maximumElementIndex];
 
    // Re-balance the heap
    this._array[maximumElementIndex] = this._array[length - 1];
    if (length >= 4) {
      pushDown(this, maximumElementIndex);
    }
  }
 
  // Dereference removed element
  this._array[length - 1] = undefined;
 
  return maximumElement;
};
 
/**
 * Gets the minimum element in the queue.
 * If the queue is empty, the result is undefined.
 *
 * @returns {*|undefined} element
 */
 
DoubleEndedPriorityQueue.prototype.getMinimum = function () {
  const length = this._length;
  if (length === 0) {
    return undefined;
  }
 
  // The minimum element is always the root
  return this._array[0];
};
 
/**
 * Gets the maximum element in the queue.
 * If the queue is empty, the result is undefined.
 *
 * @returns {*|undefined} element
 */
DoubleEndedPriorityQueue.prototype.getMaximum = function () {
  const length = this._length;
  if (length === 0) {
    return undefined;
  }
 
  // If the root has no children, the maximum is the root.
  // If the root has one child, the maximum is the child.
  if (length <= 2) {
    return this._array[length - 1];
  }
 
  // Otherwise, the maximum is the larger of the root's two children.
  return this._array[greaterThan(this, 1, 2) ? 1 : 2];
};
 
// Helper functions
 
function swap(that, indexA, indexB) {
  const array = that._array;
  const temp = array[indexA];
  array[indexA] = array[indexB];
  array[indexB] = temp;
}
 
function lessThan(that, indexA, indexB) {
  return that._comparator(that._array[indexA], that._array[indexB]) < 0.0;
}
 
function greaterThan(that, indexA, indexB) {
  return that._comparator(that._array[indexA], that._array[indexB]) > 0.0;
}
 
function pushUp(that, index) {
  if (index === 0) {
    return;
  }
  const onMinLevel = Math.floor(CesiumMath.log2(index + 1)) % 2 === 0;
  const parentIndex = Math.floor((index - 1) / 2);
  const lessThanParent = lessThan(that, index, parentIndex);
 
  // Get the element onto the correct level if it's not already
  if (lessThanParent !== onMinLevel) {
    swap(that, index, parentIndex);
    index = parentIndex;
  }
 
  // Swap element with grandparent as long as it:
  // 1) has a grandparent
  // 2A) is less than the grandparent when on a min level
  // 2B) is greater than the grandparent when on a max level
  while (index >= 3) {
    const grandparentIndex = Math.floor((index - 3) / 4);
    if (lessThan(that, index, grandparentIndex) !== lessThanParent) {
      break;
    }
    swap(that, index, grandparentIndex);
    index = grandparentIndex;
  }
}
 
function pushDown(that, index) {
  const length = that._length;
  const onMinLevel = Math.floor(CesiumMath.log2(index + 1)) % 2 === 0;
 
  // Loop as long as there is a left child.
  let leftChildIndex;
  while ((leftChildIndex = 2 * index + 1) < length) {
    // Find the minimum (or maximum) child or grandchild
    let target = leftChildIndex;
    const rightChildIndex = leftChildIndex + 1;
    if (rightChildIndex < length) {
      if (lessThan(that, rightChildIndex, target) === onMinLevel) {
        target = rightChildIndex;
      }
      const grandChildStart = 2 * leftChildIndex + 1;
      const grandChildCount = Math.max(
        Math.min(length - grandChildStart, 4),
        0,
      );
      for (let i = 0; i < grandChildCount; i++) {
        const grandChildIndex = grandChildStart + i;
        if (lessThan(that, grandChildIndex, target) === onMinLevel) {
          target = grandChildIndex;
        }
      }
    }
 
    // Swap the element into the correct spot
    if (lessThan(that, target, index) === onMinLevel) {
      swap(that, target, index);
      if (target !== leftChildIndex && target !== rightChildIndex) {
        const parentOfGrandchildIndex = Math.floor((target - 1) / 2);
        if (greaterThan(that, target, parentOfGrandchildIndex) === onMinLevel) {
          swap(that, target, parentOfGrandchildIndex);
        }
      }
    }
 
    index = target;
  }
}
 
/**
 * The comparator to use for the queue.
 * @callback DoubleEndedPriorityQueue.ComparatorCallback
 * @param {*} a An element in the queue.
 * @param {*} b An element in the queue.
 * @returns {number} If the result of the comparison is less than 0, a is lower priority than b.
 */
export default DoubleEndedPriorityQueue;