AssociativeArray.js 3.5 KB

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