ManagedArray.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import Check from './Check.js';
  2. import defaultValue from './defaultValue.js';
  3. import defineProperties from './defineProperties.js';
  4. /**
  5. * A wrapper around arrays so that the internal length of the array can be manually managed.
  6. *
  7. * @alias ManagedArray
  8. * @constructor
  9. * @private
  10. *
  11. * @param {Number} [length=0] The initial length of the array.
  12. */
  13. function ManagedArray(length) {
  14. length = defaultValue(length, 0);
  15. this._array = new Array(length);
  16. this._length = length;
  17. }
  18. defineProperties(ManagedArray.prototype, {
  19. /**
  20. * Gets or sets the length of the array.
  21. * If the set length is greater than the length of the internal array, the internal array is resized.
  22. *
  23. * @memberof ManagedArray.prototype
  24. * @type Number
  25. */
  26. length : {
  27. get : function() {
  28. return this._length;
  29. },
  30. set : function(length) {
  31. this._length = length;
  32. if (length > this._array.length) {
  33. this._array.length = length;
  34. }
  35. }
  36. },
  37. /**
  38. * Gets the internal array.
  39. *
  40. * @memberof ManagedArray.prototype
  41. * @type Array
  42. * @readonly
  43. */
  44. values : {
  45. get : function() {
  46. return this._array;
  47. }
  48. }
  49. });
  50. /**
  51. * Gets the element at an index.
  52. *
  53. * @param {Number} index The index to get.
  54. */
  55. ManagedArray.prototype.get = function(index) {
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.typeOf.number.lessThan('index', index, this._array.length);
  58. //>>includeEnd('debug');
  59. return this._array[index];
  60. };
  61. /**
  62. * Sets the element at an index. Resizes the array if index is greater than the length of the array.
  63. *
  64. * @param {Number} index The index to set.
  65. * @param {*} element The element to set at index.
  66. */
  67. ManagedArray.prototype.set = function(index, element) {
  68. //>>includeStart('debug', pragmas.debug);
  69. Check.typeOf.number('index', index);
  70. //>>includeEnd('debug');
  71. if (index >= this.length) {
  72. this.length = index + 1;
  73. }
  74. this._array[index] = element;
  75. };
  76. /**
  77. * Returns the last element in the array without modifying the array.
  78. *
  79. * @returns {*} The last element in the array.
  80. */
  81. ManagedArray.prototype.peek = function() {
  82. return this._array[this._length - 1];
  83. };
  84. /**
  85. * Push an element into the array.
  86. *
  87. * @param {*} element The element to push.
  88. */
  89. ManagedArray.prototype.push = function(element) {
  90. var index = this.length++;
  91. this._array[index] = element;
  92. };
  93. /**
  94. * Pop an element from the array.
  95. *
  96. * @returns {*} The last element in the array.
  97. */
  98. ManagedArray.prototype.pop = function() {
  99. return this._array[--this.length];
  100. };
  101. /**
  102. * Resize the internal array if length > _array.length.
  103. *
  104. * @param {Number} length The length.
  105. */
  106. ManagedArray.prototype.reserve = function(length) {
  107. //>>includeStart('debug', pragmas.debug);
  108. Check.typeOf.number.greaterThanOrEquals('length', length, 0);
  109. //>>includeEnd('debug');
  110. if (length > this._array.length) {
  111. this._array.length = length;
  112. }
  113. };
  114. /**
  115. * Resize the array.
  116. *
  117. * @param {Number} length The length.
  118. */
  119. ManagedArray.prototype.resize = function(length) {
  120. //>>includeStart('debug', pragmas.debug);
  121. Check.typeOf.number.greaterThanOrEquals('length', length, 0);
  122. //>>includeEnd('debug');
  123. this.length = length;
  124. };
  125. /**
  126. * Trim the internal array to the specified length. Defaults to the current length.
  127. *
  128. * @param {Number} [length] The length.
  129. */
  130. ManagedArray.prototype.trim = function(length) {
  131. length = defaultValue(length, this.length);
  132. this._array.length = length;
  133. };
  134. export default ManagedArray;