mxDictionary.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxDictionary
  7. *
  8. * A wrapper class for an associative array with object keys. Note: This
  9. * implementation uses <mxObjectIdentitiy> to turn object keys into strings.
  10. *
  11. * Constructor: mxEventSource
  12. *
  13. * Constructs a new dictionary which allows object to be used as keys.
  14. */
  15. function mxDictionary()
  16. {
  17. this.clear();
  18. };
  19. /**
  20. * Function: map
  21. *
  22. * Stores the (key, value) pairs in this dictionary.
  23. */
  24. mxDictionary.prototype.map = null;
  25. /**
  26. * Function: clear
  27. *
  28. * Clears the dictionary.
  29. */
  30. mxDictionary.prototype.clear = function()
  31. {
  32. this.map = {};
  33. };
  34. /**
  35. * Function: get
  36. *
  37. * Returns the value for the given key.
  38. */
  39. mxDictionary.prototype.get = function(key)
  40. {
  41. var id = mxObjectIdentity.get(key);
  42. return this.map[id];
  43. };
  44. /**
  45. * Function: put
  46. *
  47. * Stores the value under the given key and returns the previous
  48. * value for that key.
  49. */
  50. mxDictionary.prototype.put = function(key, value)
  51. {
  52. var id = mxObjectIdentity.get(key);
  53. var previous = this.map[id];
  54. this.map[id] = value;
  55. return previous;
  56. };
  57. /**
  58. * Function: remove
  59. *
  60. * Removes the value for the given key and returns the value that
  61. * has been removed.
  62. */
  63. mxDictionary.prototype.remove = function(key)
  64. {
  65. var id = mxObjectIdentity.get(key);
  66. var previous = this.map[id];
  67. delete this.map[id];
  68. return previous;
  69. };
  70. /**
  71. * Function: getKeys
  72. *
  73. * Returns all keys as an array.
  74. */
  75. mxDictionary.prototype.getKeys = function()
  76. {
  77. var result = [];
  78. for (var key in this.map)
  79. {
  80. result.push(key);
  81. }
  82. return result;
  83. };
  84. /**
  85. * Function: getValues
  86. *
  87. * Returns all values as an array.
  88. */
  89. mxDictionary.prototype.getValues = function()
  90. {
  91. var result = [];
  92. for (var key in this.map)
  93. {
  94. result.push(this.map[key]);
  95. }
  96. return result;
  97. };
  98. /**
  99. * Function: visit
  100. *
  101. * Visits all entries in the dictionary using the given function with the
  102. * following signature: function(key, value) where key is a string and
  103. * value is an object.
  104. *
  105. * Parameters:
  106. *
  107. * visitor - A function that takes the key and value as arguments.
  108. */
  109. mxDictionary.prototype.visit = function(visitor)
  110. {
  111. for (var key in this.map)
  112. {
  113. visitor(key, this.map[key]);
  114. }
  115. };
  116. __mxOutput.mxDictionary = typeof mxDictionary !== 'undefined' ? mxDictionary : undefined;