AssociativeArray.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import defined from './defined.js';
  2. import defineProperties from './defineProperties.js';
  3. import DeveloperError from './DeveloperError.js';
  4. /**
  5. * A collection of key-value pairs that is stored as a hash for easy
  6. * lookup but also provides an array for fast iteration.
  7. * @alias AssociativeArray
  8. * @constructor
  9. */
  10. function AssociativeArray() {
  11. this._array = [];
  12. this._hash = {};
  13. }
  14. defineProperties(AssociativeArray.prototype, {
  15. /**
  16. * Gets the number of items in the collection.
  17. * @memberof AssociativeArray.prototype
  18. *
  19. * @type {Number}
  20. */
  21. length : {
  22. get : function() {
  23. return this._array.length;
  24. }
  25. },
  26. /**
  27. * Gets an unordered array of all values in the collection.
  28. * This is a live array that will automatically reflect the values in the collection,
  29. * it should not be modified directly.
  30. * @memberof AssociativeArray.prototype
  31. *
  32. * @type {Array}
  33. */
  34. values : {
  35. get : function() {
  36. return this._array;
  37. }
  38. }
  39. });
  40. /**
  41. * Determines if the provided key is in the array.
  42. *
  43. * @param {String|Number} key The key to check.
  44. * @returns {Boolean} <code>true</code> if the key is in the array, <code>false</code> otherwise.
  45. */
  46. AssociativeArray.prototype.contains = function(key) {
  47. //>>includeStart('debug', pragmas.debug);
  48. if (typeof key !== 'string' && typeof key !== 'number') {
  49. throw new DeveloperError('key is required to be a string or number.');
  50. }
  51. //>>includeEnd('debug');
  52. return defined(this._hash[key]);
  53. };
  54. /**
  55. * Associates the provided key with the provided value. If the key already
  56. * exists, it is overwritten with the new value.
  57. *
  58. * @param {String|Number} key A unique identifier.
  59. * @param {*} value The value to associate with the provided key.
  60. */
  61. AssociativeArray.prototype.set = function(key, value) {
  62. //>>includeStart('debug', pragmas.debug);
  63. if (typeof key !== 'string' && typeof key !== 'number') {
  64. throw new DeveloperError('key is required to be a string or number.');
  65. }
  66. //>>includeEnd('debug');
  67. var oldValue = this._hash[key];
  68. if (value !== oldValue) {
  69. this.remove(key);
  70. this._hash[key] = value;
  71. this._array.push(value);
  72. }
  73. };
  74. /**
  75. * Retrieves the value associated with the provided key.
  76. *
  77. * @param {String|Number} key The key whose value is to be retrieved.
  78. * @returns {*} The associated value, or undefined if the key does not exist in the collection.
  79. */
  80. AssociativeArray.prototype.get = function(key) {
  81. //>>includeStart('debug', pragmas.debug);
  82. if (typeof key !== 'string' && typeof key !== 'number') {
  83. throw new DeveloperError('key is required to be a string or number.');
  84. }
  85. //>>includeEnd('debug');
  86. return this._hash[key];
  87. };
  88. /**
  89. * Removes a key-value pair from the collection.
  90. *
  91. * @param {String|Number} key The key to be removed.
  92. * @returns {Boolean} True if it was removed, false if the key was not in the collection.
  93. */
  94. AssociativeArray.prototype.remove = function(key) {
  95. //>>includeStart('debug', pragmas.debug);
  96. if (defined(key) && typeof key !== 'string' && typeof key !== 'number') {
  97. throw new DeveloperError('key is required to be a string or number.');
  98. }
  99. //>>includeEnd('debug');
  100. var value = this._hash[key];
  101. var hasValue = defined(value);
  102. if (hasValue) {
  103. var array = this._array;
  104. array.splice(array.indexOf(value), 1);
  105. delete this._hash[key];
  106. }
  107. return hasValue;
  108. };
  109. /**
  110. * Clears the collection.
  111. */
  112. AssociativeArray.prototype.removeAll = function() {
  113. var array = this._array;
  114. if (array.length > 0) {
  115. this._hash = {};
  116. array.length = 0;
  117. }
  118. };
  119. export default AssociativeArray;