babylon.smartArray.ts 2.0 KB

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