babylon.smartArray.ts 6.1 KB

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