All files / engine/Source/Core ManagedArray.js

100% Statements 45/45
100% Branches 14/14
100% Functions 12/12
100% Lines 44/44

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                        696x 696x 696x     1x                   88955x       46513x   46512x 46512x 46512x   20266x 20288x   26246x 63x   46512x                         70x                   1x   31x     30x                 1x   30x     29x 2x   29x               1x 260x               1x 20265x 20265x               1x 20247x 1x   20246x 20246x 20246x               1x   6x     5x 4x                 1x   6x     5x               1x 17894x 17894x      
import Check from "./Check.js";
 
/**
 * A wrapper around arrays so that the internal length of the array can be manually managed.
 *
 * @alias ManagedArray
 * @constructor
 * @private
 *
 * @param {number} [length=0] The initial length of the array.
 */
function ManagedArray(length) {
  length = length ?? 0;
  this._array = new Array(length);
  this._length = length;
}
 
Object.defineProperties(ManagedArray.prototype, {
  /**
   * Gets or sets the length of the array.
   * If the set length is greater than the length of the internal array, the internal array is resized.
   *
   * @memberof ManagedArray.prototype
   * @type {number}
   */
  length: {
    get: function () {
      return this._length;
    },
    set: function (length) {
      //>>includeStart('debug', pragmas.debug);
      Check.typeOf.number.greaterThanOrEquals("length", length, 0);
      //>>includeEnd('debug');
      const array = this._array;
      const originalLength = this._length;
      if (length < originalLength) {
        // Remove trailing references
        for (let i = length; i < originalLength; ++i) {
          array[i] = undefined;
        }
      } else if (length > array.length) {
        array.length = length;
      }
      this._length = length;
    },
  },
 
  /**
   * Gets the internal array.
   *
   * @memberof ManagedArray.prototype
   * @type {Array}
   * @readonly
   */
  values: {
    get: function () {
      return this._array;
    },
  },
});
 
/**
 * Gets the element at an index.
 *
 * @param {number} index The index to get.
 */
ManagedArray.prototype.get = function (index) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number.lessThan("index", index, this._array.length);
  //>>includeEnd('debug');
 
  return this._array[index];
};
 
/**
 * Sets the element at an index. Resizes the array if index is greater than the length of the array.
 *
 * @param {number} index The index to set.
 * @param {*} element The element to set at index.
 */
ManagedArray.prototype.set = function (index, element) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("index", index);
  //>>includeEnd('debug');
 
  if (index >= this._length) {
    this.length = index + 1;
  }
  this._array[index] = element;
};
 
/**
 * Returns the last element in the array without modifying the array.
 *
 * @returns {*} The last element in the array.
 */
ManagedArray.prototype.peek = function () {
  return this._array[this._length - 1];
};
 
/**
 * Push an element into the array.
 *
 * @param {*} element The element to push.
 */
ManagedArray.prototype.push = function (element) {
  const index = this.length++;
  this._array[index] = element;
};
 
/**
 * Pop an element from the array.
 *
 * @returns {*} The last element in the array.
 */
ManagedArray.prototype.pop = function () {
  if (this._length === 0) {
    return undefined;
  }
  const element = this._array[this._length - 1];
  --this.length;
  return element;
};
 
/**
 * Resize the internal array if length > _array.length.
 *
 * @param {number} length The length.
 */
ManagedArray.prototype.reserve = function (length) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number.greaterThanOrEquals("length", length, 0);
  //>>includeEnd('debug');
 
  if (length > this._array.length) {
    this._array.length = length;
  }
};
 
/**
 * Resize the array.
 *
 * @param {number} length The length.
 */
ManagedArray.prototype.resize = function (length) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number.greaterThanOrEquals("length", length, 0);
  //>>includeEnd('debug');
 
  this.length = length;
};
 
/**
 * Trim the internal array to the specified length. Defaults to the current length.
 *
 * @param {number} [length] The length.
 */
ManagedArray.prototype.trim = function (length) {
  length = length ?? this._length;
  this._array.length = length;
};
export default ManagedArray;