smartArray.ts 5.4 KB

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