babylon.smartArray.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module BABYLON {
  2. export class SmartArray<T> {
  3. public data: Array<T>;
  4. public length: number = 0;
  5. private _id: number;
  6. private _duplicateId = 0;
  7. constructor(capacity: number) {
  8. this.data = new Array(capacity);
  9. this._id = SmartArray._GlobalId++;
  10. }
  11. public push(value): void {
  12. this.data[this.length++] = value;
  13. if (this.length > this.data.length) {
  14. this.data.length *= 2;
  15. }
  16. if (!value.__smartArrayFlags) {
  17. value.__smartArrayFlags = {};
  18. }
  19. value.__smartArrayFlags[this._id] = this._duplicateId;
  20. }
  21. public pushNoDuplicate(value): void {
  22. if (value.__smartArrayFlags && value.__smartArrayFlags[this._id] === this._duplicateId) {
  23. return;
  24. }
  25. this.push(value);
  26. }
  27. public sort(compareFn): void {
  28. this.data.sort(compareFn);
  29. }
  30. public reset(): void {
  31. this.length = 0;
  32. this._duplicateId++;
  33. }
  34. public concat(array: any): void {
  35. if (array.length === 0) {
  36. return;
  37. }
  38. if (this.length + array.length > this.data.length) {
  39. this.data.length = (this.length + array.length) * 2;
  40. }
  41. for (var index = 0; index < array.length; index++) {
  42. this.data[this.length++] = (array.data || array)[index];
  43. }
  44. }
  45. public concatWithNoDuplicate(array: any): void {
  46. if (array.length === 0) {
  47. return;
  48. }
  49. if (this.length + array.length > this.data.length) {
  50. this.data.length = (this.length + array.length) * 2;
  51. }
  52. for (var index = 0; index < array.length; index++) {
  53. var item = (array.data || array)[index];
  54. this.pushNoDuplicate(item);
  55. }
  56. }
  57. public indexOf(value): number {
  58. var position = this.data.indexOf(value);
  59. if (position >= this.length) {
  60. return -1;
  61. }
  62. return position;
  63. }
  64. // Statics
  65. private static _GlobalId = 0;
  66. }
  67. }