babylon.stringDictionary.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * This class implement a typical dictionary using a string as key and the generic type T as value.
  5. * The underlying implemetation relies on an associative array to ensure the best performances.
  6. * The value can be anything including 'null' but except 'undefined'
  7. */
  8. var StringDictionary = (function () {
  9. function StringDictionary() {
  10. this._count = 0;
  11. this._data = {};
  12. }
  13. /**
  14. * Get a value based from its key
  15. * @param key the given key to get the matching value from
  16. * @return the value if found, otherwise undefined is returned
  17. */
  18. StringDictionary.prototype.get = function (key) {
  19. var val = this._data[key];
  20. if (val !== undefined) {
  21. return val;
  22. }
  23. return undefined;
  24. };
  25. /**
  26. * Get a value from its key or add it if it doesn't exist.
  27. * This method will ensure you that a given key/data will be present in the dictionary.
  28. * @param key the given key to get the matchin value from
  29. * @param factory the factory that will create the value if the key is not present in the dictionary.
  30. * The factory will only be invoked if there's no data for the given key.
  31. * @return the value corresponding to the key.
  32. */
  33. StringDictionary.prototype.getOrAddWithFactory = function (key, factory) {
  34. var val = this.get(key);
  35. if (val !== undefined) {
  36. return val;
  37. }
  38. val = factory(key);
  39. if (val) {
  40. this.add(key, val);
  41. }
  42. return val;
  43. };
  44. /**
  45. * Get a value from its key if present in the dictionary otherwise add it
  46. * @param key the key to get the value from
  47. * @param val if there's no such key/value pair in the dictionary add it with this value
  48. * @return the value corresponding to the key
  49. */
  50. StringDictionary.prototype.getOrAdd = function (key, val) {
  51. var curVal = this.get(key);
  52. if (curVal !== undefined) {
  53. return curVal;
  54. }
  55. this.add(key, val);
  56. return val;
  57. };
  58. /**
  59. * Check if there's a given key in the dictionary
  60. * @param key the key to check for
  61. * @return true if the key is present, false otherwise
  62. */
  63. StringDictionary.prototype.contains = function (key) {
  64. return this._data[key] !== undefined;
  65. };
  66. /**
  67. * Add a new key and its corresponding value
  68. * @param key the key to add
  69. * @param value the value corresponding to the key
  70. * @return true if the operation completed successfully, false if we couldn't insert the key/value because there was already this key in the dictionary
  71. */
  72. StringDictionary.prototype.add = function (key, value) {
  73. if (this._data[key] !== undefined) {
  74. return false;
  75. }
  76. this._data[key] = value;
  77. ++this._count;
  78. return true;
  79. };
  80. StringDictionary.prototype.set = function (key, value) {
  81. if (this._data[key] === undefined) {
  82. return false;
  83. }
  84. this._data[key] = value;
  85. return true;
  86. };
  87. /**
  88. * Remove a key/value from the dictionary.
  89. * @param key the key to remove
  90. * @return true if the item was successfully deleted, false if no item with such key exist in the dictionary
  91. */
  92. StringDictionary.prototype.remove = function (key) {
  93. if (this.contains(key)) {
  94. delete this._data[key];
  95. --this._count;
  96. return true;
  97. }
  98. return false;
  99. };
  100. /**
  101. * Clear the whole content of the dictionary
  102. */
  103. StringDictionary.prototype.clear = function () {
  104. this._data = {};
  105. this._count = 0;
  106. };
  107. Object.defineProperty(StringDictionary.prototype, "count", {
  108. get: function () {
  109. return this._count;
  110. },
  111. enumerable: true,
  112. configurable: true
  113. });
  114. /**
  115. * Execute a callback on each key/val of the dictionary.
  116. * Note that you can remove any element in this dictionary in the callback implementation
  117. * @param callback the callback to execute on a given key/value pair
  118. */
  119. StringDictionary.prototype.forEach = function (callback) {
  120. for (var cur in this._data) {
  121. var val = this._data[cur];
  122. callback(cur, val);
  123. }
  124. };
  125. /**
  126. * Execute a callback on every occurence of the dictionary until it returns a valid TRes object.
  127. * If the callback returns null or undefined the method will iterate to the next key/value pair
  128. * Note that you can remove any element in this dictionary in the callback implementation
  129. * @param callback the callback to execute, if it return a valid T instanced object the enumeration will stop and the object will be returned
  130. */
  131. StringDictionary.prototype.first = function (callback) {
  132. for (var cur in this._data) {
  133. var val = this._data[cur];
  134. var res = callback(cur, val);
  135. if (res) {
  136. return res;
  137. }
  138. }
  139. return null;
  140. };
  141. return StringDictionary;
  142. })();
  143. BABYLON.StringDictionary = StringDictionary;
  144. })(BABYLON || (BABYLON = {}));