HashMap.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-HashMap'>/**
  19. </span> * @class Ext.util.HashMap
  20. * &lt;p&gt;
  21. * Represents a collection of a set of key and value pairs. Each key in the HashMap
  22. * must be unique, the same key cannot exist twice. Access to items is provided via
  23. * the key only. Sample usage:
  24. * &lt;pre&gt;&lt;code&gt;
  25. var map = new Ext.util.HashMap();
  26. map.add('key1', 1);
  27. map.add('key2', 2);
  28. map.add('key3', 3);
  29. map.each(function(key, value, length){
  30. console.log(key, value, length);
  31. });
  32. * &lt;/code&gt;&lt;/pre&gt;
  33. * &lt;/p&gt;
  34. *
  35. * &lt;p&gt;The HashMap is an unordered class,
  36. * there is no guarantee when iterating over the items that they will be in any particular
  37. * order. If this is required, then use a {@link Ext.util.MixedCollection}.
  38. * &lt;/p&gt;
  39. */
  40. Ext.define('Ext.util.HashMap', {
  41. mixins: {
  42. observable: 'Ext.util.Observable'
  43. },
  44. <span id='Ext-util-HashMap-cfg-keyFn'> /**
  45. </span> * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
  46. * A default is provided that returns the &lt;b&gt;id&lt;/b&gt; property on the object. This function is only used
  47. * if the add method is called with a single argument.
  48. */
  49. <span id='Ext-util-HashMap-method-constructor'> /**
  50. </span> * Creates new HashMap.
  51. * @param {Object} config (optional) Config object.
  52. */
  53. constructor: function(config) {
  54. config = config || {};
  55. var me = this,
  56. keyFn = config.keyFn;
  57. me.addEvents(
  58. <span id='Ext-util-HashMap-event-add'> /**
  59. </span> * @event add
  60. * Fires when a new item is added to the hash
  61. * @param {Ext.util.HashMap} this.
  62. * @param {String} key The key of the added item.
  63. * @param {Object} value The value of the added item.
  64. */
  65. 'add',
  66. <span id='Ext-util-HashMap-event-clear'> /**
  67. </span> * @event clear
  68. * Fires when the hash is cleared.
  69. * @param {Ext.util.HashMap} this.
  70. */
  71. 'clear',
  72. <span id='Ext-util-HashMap-event-remove'> /**
  73. </span> * @event remove
  74. * Fires when an item is removed from the hash.
  75. * @param {Ext.util.HashMap} this.
  76. * @param {String} key The key of the removed item.
  77. * @param {Object} value The value of the removed item.
  78. */
  79. 'remove',
  80. <span id='Ext-util-HashMap-event-replace'> /**
  81. </span> * @event replace
  82. * Fires when an item is replaced in the hash.
  83. * @param {Ext.util.HashMap} this.
  84. * @param {String} key The key of the replaced item.
  85. * @param {Object} value The new value for the item.
  86. * @param {Object} old The old value for the item.
  87. */
  88. 'replace'
  89. );
  90. me.mixins.observable.constructor.call(me, config);
  91. me.clear(true);
  92. if (keyFn) {
  93. me.getKey = keyFn;
  94. }
  95. },
  96. <span id='Ext-util-HashMap-method-getCount'> /**
  97. </span> * Gets the number of items in the hash.
  98. * @return {Number} The number of items in the hash.
  99. */
  100. getCount: function() {
  101. return this.length;
  102. },
  103. <span id='Ext-util-HashMap-method-getData'> /**
  104. </span> * Implementation for being able to extract the key from an object if only
  105. * a single argument is passed.
  106. * @private
  107. * @param {String} key The key
  108. * @param {Object} value The value
  109. * @return {Array} [key, value]
  110. */
  111. getData: function(key, value) {
  112. // if we have no value, it means we need to get the key from the object
  113. if (value === undefined) {
  114. value = key;
  115. key = this.getKey(value);
  116. }
  117. return [key, value];
  118. },
  119. <span id='Ext-util-HashMap-method-getKey'> /**
  120. </span> * Extracts the key from an object. This is a default implementation, it may be overridden
  121. * @param {Object} o The object to get the key from
  122. * @return {String} The key to use.
  123. */
  124. getKey: function(o) {
  125. return o.id;
  126. },
  127. <span id='Ext-util-HashMap-method-add'> /**
  128. </span> * Adds an item to the collection. Fires the {@link #event-add} event when complete.
  129. *
  130. * @param {String/Object} key The key to associate with the item, or the new item.
  131. *
  132. * If a {@link #getKey} implementation was specified for this HashMap,
  133. * or if the key of the stored items is in a property called `id`,
  134. * the HashMap will be able to *derive* the key for the new item.
  135. * In this case just pass the new item in this parameter.
  136. *
  137. * @param {Object} [o] The item to add.
  138. *
  139. * @return {Object} The item added.
  140. */
  141. add: function(key, value) {
  142. var me = this;
  143. if (value === undefined) {
  144. value = key;
  145. key = me.getKey(value);
  146. }
  147. if (me.containsKey(key)) {
  148. return me.replace(key, value);
  149. }
  150. me.map[key] = value;
  151. ++me.length;
  152. if (me.hasListeners.add) {
  153. me.fireEvent('add', me, key, value);
  154. }
  155. return value;
  156. },
  157. <span id='Ext-util-HashMap-method-replace'> /**
  158. </span> * Replaces an item in the hash. If the key doesn't exist, the
  159. * {@link #method-add} method will be used.
  160. * @param {String} key The key of the item.
  161. * @param {Object} value The new value for the item.
  162. * @return {Object} The new value of the item.
  163. */
  164. replace: function(key, value) {
  165. var me = this,
  166. map = me.map,
  167. old;
  168. if (value === undefined) {
  169. value = key;
  170. key = me.getKey(value);
  171. }
  172. if (!me.containsKey(key)) {
  173. me.add(key, value);
  174. }
  175. old = map[key];
  176. map[key] = value;
  177. if (me.hasListeners.replace) {
  178. me.fireEvent('replace', me, key, value, old);
  179. }
  180. return value;
  181. },
  182. <span id='Ext-util-HashMap-method-remove'> /**
  183. </span> * Remove an item from the hash.
  184. * @param {Object} o The value of the item to remove.
  185. * @return {Boolean} True if the item was successfully removed.
  186. */
  187. remove: function(o) {
  188. var key = this.findKey(o);
  189. if (key !== undefined) {
  190. return this.removeAtKey(key);
  191. }
  192. return false;
  193. },
  194. <span id='Ext-util-HashMap-method-removeAtKey'> /**
  195. </span> * Remove an item from the hash.
  196. * @param {String} key The key to remove.
  197. * @return {Boolean} True if the item was successfully removed.
  198. */
  199. removeAtKey: function(key) {
  200. var me = this,
  201. value;
  202. if (me.containsKey(key)) {
  203. value = me.map[key];
  204. delete me.map[key];
  205. --me.length;
  206. if (me.hasListeners.remove) {
  207. me.fireEvent('remove', me, key, value);
  208. }
  209. return true;
  210. }
  211. return false;
  212. },
  213. <span id='Ext-util-HashMap-method-get'> /**
  214. </span> * Retrieves an item with a particular key.
  215. * @param {String} key The key to lookup.
  216. * @return {Object} The value at that key. If it doesn't exist, &lt;tt&gt;undefined&lt;/tt&gt; is returned.
  217. */
  218. get: function(key) {
  219. return this.map[key];
  220. },
  221. <span id='Ext-util-HashMap-method-clear'> /**
  222. </span> * Removes all items from the hash.
  223. * @return {Ext.util.HashMap} this
  224. */
  225. clear: function(/* private */ initial) {
  226. var me = this;
  227. me.map = {};
  228. me.length = 0;
  229. if (initial !== true &amp;&amp; me.hasListeners.clear) {
  230. me.fireEvent('clear', me);
  231. }
  232. return me;
  233. },
  234. <span id='Ext-util-HashMap-method-containsKey'> /**
  235. </span> * Checks whether a key exists in the hash.
  236. * @param {String} key The key to check for.
  237. * @return {Boolean} True if they key exists in the hash.
  238. */
  239. containsKey: function(key) {
  240. return this.map[key] !== undefined;
  241. },
  242. <span id='Ext-util-HashMap-method-contains'> /**
  243. </span> * Checks whether a value exists in the hash.
  244. * @param {Object} value The value to check for.
  245. * @return {Boolean} True if the value exists in the dictionary.
  246. */
  247. contains: function(value) {
  248. return this.containsKey(this.findKey(value));
  249. },
  250. <span id='Ext-util-HashMap-method-getKeys'> /**
  251. </span> * Return all of the keys in the hash.
  252. * @return {Array} An array of keys.
  253. */
  254. getKeys: function() {
  255. return this.getArray(true);
  256. },
  257. <span id='Ext-util-HashMap-method-getValues'> /**
  258. </span> * Return all of the values in the hash.
  259. * @return {Array} An array of values.
  260. */
  261. getValues: function() {
  262. return this.getArray(false);
  263. },
  264. <span id='Ext-util-HashMap-method-getArray'> /**
  265. </span> * Gets either the keys/values in an array from the hash.
  266. * @private
  267. * @param {Boolean} isKey True to extract the keys, otherwise, the value
  268. * @return {Array} An array of either keys/values from the hash.
  269. */
  270. getArray: function(isKey) {
  271. var arr = [],
  272. key,
  273. map = this.map;
  274. for (key in map) {
  275. if (map.hasOwnProperty(key)) {
  276. arr.push(isKey ? key: map[key]);
  277. }
  278. }
  279. return arr;
  280. },
  281. <span id='Ext-util-HashMap-method-each'> /**
  282. </span> * Executes the specified function once for each item in the hash.
  283. * Returning false from the function will cease iteration.
  284. *
  285. * The paramaters passed to the function are:
  286. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  287. * &lt;li&gt;&lt;b&gt;key&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;The key of the item&lt;/p&gt;&lt;/li&gt;
  288. * &lt;li&gt;&lt;b&gt;value&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The value of the item&lt;/p&gt;&lt;/li&gt;
  289. * &lt;li&gt;&lt;b&gt;length&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The total number of items in the hash&lt;/p&gt;&lt;/li&gt;
  290. * &lt;/ul&gt;&lt;/div&gt;
  291. * @param {Function} fn The function to execute.
  292. * @param {Object} scope The scope to execute in. Defaults to &lt;tt&gt;this&lt;/tt&gt;.
  293. * @return {Ext.util.HashMap} this
  294. */
  295. each: function(fn, scope) {
  296. // copy items so they may be removed during iteration.
  297. var items = Ext.apply({}, this.map),
  298. key,
  299. length = this.length;
  300. scope = scope || this;
  301. for (key in items) {
  302. if (items.hasOwnProperty(key)) {
  303. if (fn.call(scope, key, items[key], length) === false) {
  304. break;
  305. }
  306. }
  307. }
  308. return this;
  309. },
  310. <span id='Ext-util-HashMap-method-clone'> /**
  311. </span> * Performs a shallow copy on this hash.
  312. * @return {Ext.util.HashMap} The new hash object.
  313. */
  314. clone: function() {
  315. var hash = new this.self(),
  316. map = this.map,
  317. key;
  318. hash.suspendEvents();
  319. for (key in map) {
  320. if (map.hasOwnProperty(key)) {
  321. hash.add(key, map[key]);
  322. }
  323. }
  324. hash.resumeEvents();
  325. return hash;
  326. },
  327. <span id='Ext-util-HashMap-method-findKey'> /**
  328. </span> * @private
  329. * Find the key for a value.
  330. * @param {Object} value The value to find.
  331. * @return {Object} The value of the item. Returns &lt;tt&gt;undefined&lt;/tt&gt; if not found.
  332. */
  333. findKey: function(value) {
  334. var key,
  335. map = this.map;
  336. for (key in map) {
  337. if (map.hasOwnProperty(key) &amp;&amp; map[key] === value) {
  338. return key;
  339. }
  340. }
  341. return undefined;
  342. }
  343. });
  344. </pre>
  345. </body>
  346. </html>