smartArray.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Defines an GC Friendly array where the backfield array do not shrink to prevent over allocations.
  3. */
  4. export class SmartArray {
  5. /**
  6. * Instantiates a Smart Array.
  7. * @param capacity defines the default capacity of the array.
  8. */
  9. constructor(capacity) {
  10. /**
  11. * The active length of the array.
  12. */
  13. this.length = 0;
  14. this.data = new Array(capacity);
  15. this._id = SmartArray._GlobalId++;
  16. }
  17. /**
  18. * Pushes a value at the end of the active data.
  19. * @param value defines the object to push in the array.
  20. */
  21. push(value) {
  22. this.data[this.length++] = value;
  23. if (this.length > this.data.length) {
  24. this.data.length *= 2;
  25. }
  26. }
  27. /**
  28. * Iterates over the active data and apply the lambda to them.
  29. * @param func defines the action to apply on each value.
  30. */
  31. forEach(func) {
  32. for (let index = 0; index < this.length; index++) {
  33. func(this.data[index]);
  34. }
  35. }
  36. /**
  37. * Sorts the full sets of data.
  38. * @param compareFn defines the comparison function to apply.
  39. */
  40. sort(compareFn) {
  41. this.data.sort(compareFn);
  42. }
  43. /**
  44. * Resets the active data to an empty array.
  45. */
  46. reset() {
  47. this.length = 0;
  48. }
  49. /**
  50. * Releases all the data from the array as well as the array.
  51. */
  52. dispose() {
  53. this.reset();
  54. if (this.data) {
  55. this.data.length = 0;
  56. }
  57. }
  58. /**
  59. * Concats the active data with a given array.
  60. * @param array defines the data to concatenate with.
  61. */
  62. concat(array) {
  63. if (array.length === 0) {
  64. return;
  65. }
  66. if (this.length + array.length > this.data.length) {
  67. this.data.length = (this.length + array.length) * 2;
  68. }
  69. for (let index = 0; index < array.length; index++) {
  70. this.data[this.length++] = (array.data || array)[index];
  71. }
  72. }
  73. /**
  74. * Returns the position of a value in the active data.
  75. * @param value defines the value to find the index for
  76. * @returns the index if found in the active data otherwise -1
  77. */
  78. indexOf(value) {
  79. const position = this.data.indexOf(value);
  80. if (position >= this.length) {
  81. return -1;
  82. }
  83. return position;
  84. }
  85. /**
  86. * Returns whether an element is part of the active data.
  87. * @param value defines the value to look for
  88. * @returns true if found in the active data otherwise false
  89. */
  90. contains(value) {
  91. return this.indexOf(value) !== -1;
  92. }
  93. }
  94. // Statics
  95. SmartArray._GlobalId = 0;
  96. /**
  97. * Defines an GC Friendly array where the backfield array do not shrink to prevent over allocations.
  98. * The data in this array can only be present once
  99. */
  100. export class SmartArrayNoDuplicate extends SmartArray {
  101. constructor() {
  102. super(...arguments);
  103. this._duplicateId = 0;
  104. }
  105. /**
  106. * Pushes a value at the end of the active data.
  107. * THIS DOES NOT PREVENT DUPPLICATE DATA
  108. * @param value defines the object to push in the array.
  109. */
  110. push(value) {
  111. super.push(value);
  112. if (!value.__smartArrayFlags) {
  113. value.__smartArrayFlags = {};
  114. }
  115. value.__smartArrayFlags[this._id] = this._duplicateId;
  116. }
  117. /**
  118. * Pushes a value at the end of the active data.
  119. * If the data is already present, it won t be added again
  120. * @param value defines the object to push in the array.
  121. * @returns true if added false if it was already present
  122. */
  123. pushNoDuplicate(value) {
  124. if (value.__smartArrayFlags && value.__smartArrayFlags[this._id] === this._duplicateId) {
  125. return false;
  126. }
  127. this.push(value);
  128. return true;
  129. }
  130. /**
  131. * Resets the active data to an empty array.
  132. */
  133. reset() {
  134. super.reset();
  135. this._duplicateId++;
  136. }
  137. /**
  138. * Concats the active data with a given array.
  139. * This ensures no duplicate will be present in the result.
  140. * @param array defines the data to concatenate with.
  141. */
  142. concatWithNoDuplicate(array) {
  143. if (array.length === 0) {
  144. return;
  145. }
  146. if (this.length + array.length > this.data.length) {
  147. this.data.length = (this.length + array.length) * 2;
  148. }
  149. for (let index = 0; index < array.length; index++) {
  150. const item = (array.data || array)[index];
  151. this.pushNoDuplicate(item);
  152. }
  153. }
  154. }
  155. //# sourceMappingURL=smartArray.js.map