babylon.smartCollection.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. while (index < this.count) {
  33. this._keys[index] = this._keys[index + 1];
  34. index++;
  35. }
  36. }
  37. else {
  38. return -1;
  39. }
  40. return --this.count;
  41. };
  42. SmartCollection.prototype.indexOf = function (key) {
  43. for (var i = 0; i !== this.count; i++) {
  44. if (this._keys[i] === key) {
  45. return i;
  46. }
  47. }
  48. return -1;
  49. };
  50. SmartCollection.prototype.item = function (key) {
  51. return this.items[key];
  52. };
  53. SmartCollection.prototype.getAllKeys = function () {
  54. if (this.count > 0) {
  55. var keys = new Array(this.count);
  56. for (var i = 0; i < this.count; i++) {
  57. keys[i] = this._keys[i];
  58. }
  59. return keys;
  60. }
  61. else {
  62. return undefined;
  63. }
  64. };
  65. SmartCollection.prototype.getKeyByIndex = function (index) {
  66. if (index < this.count && index > -1) {
  67. return this._keys[index];
  68. }
  69. else {
  70. return undefined;
  71. }
  72. };
  73. SmartCollection.prototype.getItemByIndex = function (index) {
  74. if (index < this.count && index > -1) {
  75. return this.items[this._keys[index]];
  76. }
  77. else {
  78. return undefined;
  79. }
  80. };
  81. SmartCollection.prototype.empty = function () {
  82. if (this.count > 0) {
  83. this.count = 0;
  84. this.items = {};
  85. this._keys = new Array(this._initialCapacity);
  86. }
  87. };
  88. SmartCollection.prototype.forEach = function (block) {
  89. var key;
  90. for (key in this.items) {
  91. if (this.items.hasOwnProperty(key)) {
  92. block(this.items[key]);
  93. }
  94. }
  95. };
  96. return SmartCollection;
  97. })();
  98. BABYLON.SmartCollection = SmartCollection;
  99. })(BABYLON || (BABYLON = {}));
  100. //# sourceMappingURL=babylon.smartCollection.js.map