babylon.smartCollection.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SmartCollection = (function () {
  4. function SmartCollection(capacity) {
  5. if (capacity === void 0) { capacity = 10; }
  6. this.count = 0;
  7. this._initialCapacity = capacity;
  8. this.items = {};
  9. this._keys = new Array(this._initialCapacity);
  10. }
  11. SmartCollection.prototype.add = function (key, item) {
  12. if (this.items[key] != undefined) {
  13. return -1;
  14. }
  15. this.items[key] = item;
  16. //literal keys are always strings, but we keep source type of key in _keys array
  17. this._keys[this.count++] = key;
  18. if (this.count > this._keys.length) {
  19. this._keys.length *= 2;
  20. }
  21. return this.count;
  22. };
  23. SmartCollection.prototype.remove = function (key) {
  24. if (this.items[key] == undefined) {
  25. return -1;
  26. }
  27. return this.removeItemOfIndex(this.indexOf(key));
  28. };
  29. SmartCollection.prototype.removeItemOfIndex = function (index) {
  30. if (index < this.count && index > -1) {
  31. delete this.items[this._keys[index]];
  32. //here, shifting by hand is better optimised than .splice
  33. while (index < this.count) {
  34. this._keys[index] = this._keys[index + 1];
  35. index++;
  36. }
  37. }
  38. else {
  39. return -1;
  40. }
  41. return --this.count;
  42. };
  43. SmartCollection.prototype.indexOf = function (key) {
  44. for (var i = 0; i !== this.count; i++) {
  45. if (this._keys[i] === key) {
  46. return i;
  47. }
  48. }
  49. return -1;
  50. };
  51. SmartCollection.prototype.item = function (key) {
  52. return this.items[key];
  53. };
  54. SmartCollection.prototype.getAllKeys = function () {
  55. if (this.count > 0) {
  56. var keys = new Array(this.count);
  57. for (var i = 0; i < this.count; i++) {
  58. keys[i] = this._keys[i];
  59. }
  60. return keys;
  61. }
  62. else {
  63. return undefined;
  64. }
  65. };
  66. SmartCollection.prototype.getKeyByIndex = function (index) {
  67. if (index < this.count && index > -1) {
  68. return this._keys[index];
  69. }
  70. else {
  71. return undefined;
  72. }
  73. };
  74. SmartCollection.prototype.getItemByIndex = function (index) {
  75. if (index < this.count && index > -1) {
  76. return this.items[this._keys[index]];
  77. }
  78. else {
  79. return undefined;
  80. }
  81. };
  82. SmartCollection.prototype.empty = function () {
  83. if (this.count > 0) {
  84. this.count = 0;
  85. this.items = {};
  86. this._keys = new Array(this._initialCapacity);
  87. }
  88. };
  89. SmartCollection.prototype.forEach = function (block) {
  90. var key;
  91. for (key in this.items) {
  92. if (this.items.hasOwnProperty(key)) {
  93. block(this.items[key]);
  94. }
  95. }
  96. };
  97. return SmartCollection;
  98. })();
  99. BABYLON.SmartCollection = SmartCollection;
  100. })(BABYLON || (BABYLON = {}));
  101. //# sourceMappingURL=babylon.smartCollection.js.map