You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntHashMap.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * Note: originally released under the GNU LGPL v2.1,
  19. * but rereleased by the original author under the ASF license (above).
  20. */
  21. package com.dmdirc.addons.freedesktop_notifications.commons;
  22. /**
  23. * <p>A hash map that uses primitive ints for the key rather than objects.</p>
  24. *
  25. * <p>Note that this class is for internal optimization purposes only, and may
  26. * not be supported in future releases of Apache Commons Lang. Utilities of
  27. * this sort may be included in future releases of Apache Commons Collections.</p>
  28. *
  29. * @author Justin Couch
  30. * @author Alex Chaffee (alex@apache.org)
  31. * @author Stephen Colebourne
  32. * @since 2.0
  33. * @version $Revision$
  34. * @see java.util.HashMap
  35. */
  36. class IntHashMap {
  37. /**
  38. * The hash table data.
  39. */
  40. private transient Entry[] table;
  41. /**
  42. * The total number of entries in the hash table.
  43. */
  44. private transient int count;
  45. /**
  46. * The table is rehashed when its size exceeds this threshold. (The
  47. * value of this field is (int)(capacity * loadFactor).)
  48. *
  49. * @serial
  50. */
  51. private int threshold;
  52. /**
  53. * The load factor for the hashtable.
  54. *
  55. * @serial
  56. */
  57. private float loadFactor;
  58. /**
  59. * <p>Innerclass that acts as a datastructure to create a new entry in the
  60. * table.</p>
  61. */
  62. private static class Entry {
  63. int hash;
  64. // int key; // not used currently
  65. Object value;
  66. Entry next;
  67. /**
  68. * <p>Create a new entry with the given values.</p>
  69. *
  70. * @param hash The code used to hash the object with
  71. * @param key The key used to enter this in the table
  72. * @param value The value for this key
  73. * @param next A reference to the next entry in the table
  74. */
  75. protected Entry(int hash, int key, Object value, Entry next) {
  76. this.hash = hash;
  77. // this.key = key;
  78. this.value = value;
  79. this.next = next;
  80. }
  81. }
  82. /**
  83. * <p>Constructs a new, empty hashtable with a default capacity and load
  84. * factor, which is <code>20</code> and <code>0.75</code> respectively.</p>
  85. */
  86. public IntHashMap() {
  87. this(20, 0.75f);
  88. }
  89. /**
  90. * <p>Constructs a new, empty hashtable with the specified initial capacity
  91. * and default load factor, which is <code>0.75</code>.</p>
  92. *
  93. * @param initialCapacity the initial capacity of the hashtable.
  94. * @throws IllegalArgumentException if the initial capacity is less
  95. * than zero.
  96. */
  97. public IntHashMap(int initialCapacity) {
  98. this(initialCapacity, 0.75f);
  99. }
  100. /**
  101. * <p>Constructs a new, empty hashtable with the specified initial
  102. * capacity and the specified load factor.</p>
  103. *
  104. * @param initialCapacity the initial capacity of the hashtable.
  105. * @param loadFactor the load factor of the hashtable.
  106. * @throws IllegalArgumentException if the initial capacity is less
  107. * than zero, or if the load factor is nonpositive.
  108. */
  109. public IntHashMap(int initialCapacity, float loadFactor) {
  110. super();
  111. if (initialCapacity < 0) {
  112. throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
  113. }
  114. if (loadFactor <= 0) {
  115. throw new IllegalArgumentException("Illegal Load: " + loadFactor);
  116. }
  117. if (initialCapacity == 0) {
  118. initialCapacity = 1;
  119. }
  120. this.loadFactor = loadFactor;
  121. table = new Entry[initialCapacity];
  122. threshold = (int) (initialCapacity * loadFactor);
  123. }
  124. /**
  125. * <p>Returns the number of keys in this hashtable.</p>
  126. *
  127. * @return the number of keys in this hashtable.
  128. */
  129. public int size() {
  130. return count;
  131. }
  132. /**
  133. * <p>Tests if this hashtable maps no keys to values.</p>
  134. *
  135. * @return <code>true</code> if this hashtable maps no keys to values;
  136. * <code>false</code> otherwise.
  137. */
  138. public boolean isEmpty() {
  139. return count == 0;
  140. }
  141. /**
  142. * <p>Tests if some key maps into the specified value in this hashtable.
  143. * This operation is more expensive than the <code>containsKey</code>
  144. * method.</p>
  145. *
  146. * <p>Note that this method is identical in functionality to containsValue,
  147. * (which is part of the Map interface in the collections framework).</p>
  148. *
  149. * @param value a value to search for.
  150. * @return <code>true</code> if and only if some key maps to the
  151. * <code>value</code> argument in this hashtable as
  152. * determined by the <tt>equals</tt> method;
  153. * <code>false</code> otherwise.
  154. * @throws NullPointerException if the value is <code>null</code>.
  155. * @see #containsKey(int)
  156. * @see #containsValue(Object)
  157. * @see java.util.Map
  158. */
  159. public boolean contains(Object value) {
  160. if (value == null) {
  161. throw new NullPointerException();
  162. }
  163. Entry[] tab = table;
  164. for (int i = tab.length; i-- > 0;) {
  165. for (Entry e = tab[i]; e != null; e = e.next) {
  166. if (e.value.equals(value)) {
  167. return true;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * <p>Returns <code>true</code> if this HashMap maps one or more keys
  175. * to this value.</p>
  176. *
  177. * <p>Note that this method is identical in functionality to contains
  178. * (which predates the Map interface).</p>
  179. *
  180. * @param value value whose presence in this HashMap is to be tested.
  181. * @return boolean <code>true</code> if the value is contained
  182. * @see java.util.Map
  183. * @since JDK1.2
  184. */
  185. public boolean containsValue(Object value) {
  186. return contains(value);
  187. }
  188. /**
  189. * <p>Tests if the specified object is a key in this hashtable.</p>
  190. *
  191. * @param key possible key.
  192. * @return <code>true</code> if and only if the specified object is a
  193. * key in this hashtable, as determined by the <tt>equals</tt>
  194. * method; <code>false</code> otherwise.
  195. * @see #contains(Object)
  196. */
  197. public boolean containsKey(int key) {
  198. Entry[] tab = table;
  199. int hash = key;
  200. int index = (hash & 0x7FFFFFFF) % tab.length;
  201. for (Entry e = tab[index]; e != null; e = e.next) {
  202. if (e.hash == hash) {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. /**
  209. * <p>Returns the value to which the specified key is mapped in this map.</p>
  210. *
  211. * @param key a key in the hashtable.
  212. * @return the value to which the key is mapped in this hashtable;
  213. * <code>null</code> if the key is not mapped to any value in
  214. * this hashtable.
  215. * @see #put(int, Object)
  216. */
  217. public Object get(int key) {
  218. Entry[] tab = table;
  219. int hash = key;
  220. int index = (hash & 0x7FFFFFFF) % tab.length;
  221. for (Entry e = tab[index]; e != null; e = e.next) {
  222. if (e.hash == hash) {
  223. return e.value;
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * <p>Increases the capacity of and internally reorganizes this
  230. * hashtable, in order to accommodate and access its entries more
  231. * efficiently.</p>
  232. *
  233. * <p>This method is called automatically when the number of keys
  234. * in the hashtable exceeds this hashtable's capacity and load
  235. * factor.</p>
  236. */
  237. protected void rehash() {
  238. int oldCapacity = table.length;
  239. Entry[] oldMap = table;
  240. int newCapacity = oldCapacity * 2 + 1;
  241. Entry[] newMap = new Entry[newCapacity];
  242. threshold = (int) (newCapacity * loadFactor);
  243. table = newMap;
  244. for (int i = oldCapacity; i-- > 0;) {
  245. for (Entry old = oldMap[i]; old != null;) {
  246. Entry e = old;
  247. old = old.next;
  248. int index = (e.hash & 0x7FFFFFFF) % newCapacity;
  249. e.next = newMap[index];
  250. newMap[index] = e;
  251. }
  252. }
  253. }
  254. /**
  255. * <p>Maps the specified <code>key</code> to the specified
  256. * <code>value</code> in this hashtable. The key cannot be
  257. * <code>null</code>. </p>
  258. *
  259. * <p>The value can be retrieved by calling the <code>get</code> method
  260. * with a key that is equal to the original key.</p>
  261. *
  262. * @param key the hashtable key.
  263. * @param value the value.
  264. * @return the previous value of the specified key in this hashtable,
  265. * or <code>null</code> if it did not have one.
  266. * @throws NullPointerException if the key is <code>null</code>.
  267. * @see #get(int)
  268. */
  269. public Object put(int key, Object value) {
  270. // Makes sure the key is not already in the hashtable.
  271. Entry[] tab = table;
  272. int hash = key;
  273. int index = (hash & 0x7FFFFFFF) % tab.length;
  274. for (Entry e = tab[index]; e != null; e = e.next) {
  275. if (e.hash == hash) {
  276. Object old = e.value;
  277. e.value = value;
  278. return old;
  279. }
  280. }
  281. if (count >= threshold) {
  282. // Rehash the table if the threshold is exceeded
  283. rehash();
  284. tab = table;
  285. index = (hash & 0x7FFFFFFF) % tab.length;
  286. }
  287. // Creates the new entry.
  288. Entry e = new Entry(hash, key, value, tab[index]);
  289. tab[index] = e;
  290. count++;
  291. return null;
  292. }
  293. /**
  294. * <p>Removes the key (and its corresponding value) from this
  295. * hashtable.</p>
  296. *
  297. * <p>This method does nothing if the key is not present in the
  298. * hashtable.</p>
  299. *
  300. * @param key the key that needs to be removed.
  301. * @return the value to which the key had been mapped in this hashtable,
  302. * or <code>null</code> if the key did not have a mapping.
  303. */
  304. public Object remove(int key) {
  305. Entry[] tab = table;
  306. int hash = key;
  307. int index = (hash & 0x7FFFFFFF) % tab.length;
  308. for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) {
  309. if (e.hash == hash) {
  310. if (prev != null) {
  311. prev.next = e.next;
  312. } else {
  313. tab[index] = e.next;
  314. }
  315. count--;
  316. Object oldValue = e.value;
  317. e.value = null;
  318. return oldValue;
  319. }
  320. }
  321. return null;
  322. }
  323. /**
  324. * <p>Clears this hashtable so that it contains no keys.</p>
  325. */
  326. public synchronized void clear() {
  327. Entry[] tab = table;
  328. for (int index = tab.length; --index >= 0;) {
  329. tab[index] = null;
  330. }
  331. count = 0;
  332. }
  333. }