babylon.smartArray.js 2.5 KB

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