L2ObjectHashMap.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.util;
  16. import java.util.Iterator;
  17. import java.util.NoSuchElementException;
  18. import net.sf.l2j.Config;
  19. import net.sf.l2j.gameserver.model.L2Object;
  20. /**
  21. * This class is a highly optimized hashtable, where
  22. * keys are integers. The main goal of this class is to allow
  23. * concurent read/iterate and write access to this table,
  24. * plus minimal used memory.
  25. *
  26. * This class uses plain array as the table of values, and
  27. * keys are used to get position in the table. If the position
  28. * is already busy, we iterate to the next position, unil we
  29. * find the needed element or null.
  30. *
  31. * To iterate over the table (read access) we may simply iterate
  32. * throgh table array.
  33. *
  34. * In case we remove an element from the table, we check - if
  35. * the next position is null, we reset table's slot to null,
  36. * otherwice we assign it to a dummy value
  37. *
  38. *
  39. * @author mkizub
  40. *
  41. * @param <T> type of values stored in this hashtable
  42. */
  43. public final class L2ObjectHashMap<T extends L2Object>
  44. extends L2ObjectMap<T>
  45. {
  46. private static final boolean TRACE = false;
  47. private static final boolean DEBUG = false;
  48. private final static int[] PRIMES = {
  49. 5, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293,
  50. 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333, 2801,
  51. 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 17519,
  52. 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523,
  53. 108631, 130363, 156437, 187751, 225307, 270371, 324449, 389357,
  54. 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319,
  55. 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369
  56. };
  57. private T[] _table;
  58. private int[] _keys;
  59. private int _count;
  60. private static int getPrime(int min)
  61. {
  62. for (int i=0; i < PRIMES.length; i++)
  63. {
  64. if (PRIMES[i] >= min)
  65. return PRIMES[i];
  66. }
  67. throw new OutOfMemoryError();
  68. }
  69. @SuppressWarnings("unchecked")
  70. public L2ObjectHashMap()
  71. {
  72. int size = PRIMES[0];
  73. _table = (T[])new L2Object[size];
  74. _keys = new int[size];
  75. if (DEBUG) check();
  76. }
  77. /* (non-Javadoc)
  78. * @see net.sf.l2j.util.L2ObjectMap#size()
  79. */
  80. @Override
  81. public int size()
  82. {
  83. return _count;
  84. }
  85. /* (non-Javadoc)
  86. * @see net.sf.l2j.util.L2ObjectMap#isEmpty()
  87. */
  88. @Override
  89. public boolean isEmpty()
  90. {
  91. return _count == 0;
  92. }
  93. /* (non-Javadoc)
  94. * @see net.sf.l2j.util.L2ObjectMap#clear()
  95. */
  96. @Override
  97. @SuppressWarnings("unchecked")
  98. public synchronized void clear()
  99. {
  100. int size = PRIMES[0];
  101. _table = (T[])new L2Object[size];
  102. _keys = new int[size];
  103. _count = 0;
  104. if (DEBUG) check();
  105. }
  106. private void check()
  107. {
  108. if (DEBUG)
  109. {
  110. int cnt = 0;
  111. for (int i=0; i < _table.length; i++)
  112. {
  113. L2Object obj = _table[i];
  114. if (obj == null) {
  115. assert _keys[i] == 0 || _keys[i] == 0x80000000;
  116. } else {
  117. cnt++;
  118. assert obj.getObjectId() == (_keys[i] & 0x7FFFFFFF);
  119. }
  120. }
  121. assert cnt == _count;
  122. }
  123. }
  124. /* (non-Javadoc)
  125. * @see net.sf.l2j.util.L2ObjectMap#put(T)
  126. */
  127. @Override
  128. public synchronized void put(T obj)
  129. {
  130. if (_count >= _table.length/2)
  131. expand();
  132. final int hashcode = obj.getObjectId();
  133. if (Config.ASSERT) assert hashcode > 0;
  134. int seed = hashcode;
  135. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  136. int ntry = 0;
  137. int slot = -1; // keep last found slot
  138. do
  139. {
  140. int pos = (seed % _table.length) & 0x7FFFFFFF;
  141. if (_table[pos] == null)
  142. {
  143. if (slot < 0)
  144. slot = pos;
  145. if (_keys[pos] >= 0) {
  146. // found an empty slot without previous collisions,
  147. // but use previously found slot
  148. _keys[slot] = hashcode;
  149. _table[slot] = obj;
  150. _count++;
  151. if (TRACE) System.err.println("ht: put obj id="+hashcode+" at slot="+slot);
  152. if (DEBUG) check();
  153. return;
  154. }
  155. }
  156. else
  157. {
  158. // check if we are adding the same object
  159. if (_table[pos] == obj)
  160. return;
  161. // this should never happen
  162. if (Config.ASSERT) assert obj.getObjectId() != _table[pos].getObjectId();
  163. // if there was no collisions at this slot, and we found a free
  164. // slot previously - use found slot
  165. if (slot >= 0 && _keys[pos] > 0)
  166. {
  167. _keys[slot] |= hashcode; // preserve collision bit
  168. _table[slot] = obj;
  169. _count++;
  170. if (TRACE) System.err.println("ht: put obj id="+hashcode+" at slot="+slot);
  171. if (DEBUG) check();
  172. return;
  173. }
  174. }
  175. // set collision bit
  176. _keys[pos] |= 0x80000000;
  177. // calculate next slot
  178. seed += incr;
  179. } while (++ntry < _table.length);
  180. if (DEBUG) check();
  181. throw new IllegalStateException();
  182. }
  183. /* (non-Javadoc)
  184. * @see net.sf.l2j.util.L2ObjectMap#remove(T)
  185. */
  186. @Override
  187. public synchronized void remove(T obj)
  188. {
  189. int hashcode = obj.getObjectId();
  190. if (Config.ASSERT) assert hashcode > 0;
  191. int seed = hashcode;
  192. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  193. int ntry = 0;
  194. do
  195. {
  196. int pos = (seed % _table.length) & 0x7FFFFFFF;
  197. if (_table[pos] == obj)
  198. {
  199. // found the object
  200. _keys[pos] &= 0x80000000; // preserve collision bit
  201. _table[pos] = null;
  202. _count--;
  203. if (TRACE) System.err.println("ht: remove obj id="+hashcode+" from slot="+pos);
  204. if (DEBUG) check();
  205. return;
  206. }
  207. // check for collision (if we previously deleted element)
  208. if (_table[pos] == null && _keys[pos] >= 0) {
  209. if (DEBUG) check();
  210. return; //throw new IllegalArgumentException();
  211. }
  212. // calculate next slot
  213. seed += incr;
  214. } while (++ntry < _table.length);
  215. if (DEBUG) check();
  216. throw new IllegalStateException();
  217. }
  218. /* (non-Javadoc)
  219. * @see net.sf.l2j.util.L2ObjectMap#get(int)
  220. */
  221. @Override
  222. public T get(int id)
  223. {
  224. final int size = _table.length;
  225. if (id <= 0)
  226. return null;
  227. if (size <= 11)
  228. {
  229. // for small tables linear check is fast
  230. for (int i=0; i < size; i++)
  231. {
  232. if ((_keys[i]&0x7FFFFFFF) == id)
  233. return _table[i];
  234. }
  235. return null;
  236. }
  237. int seed = id;
  238. int incr = 1 + (((seed >> 5) + 1) % (size - 1));
  239. int ntry = 0;
  240. do
  241. {
  242. int pos = (seed % size) & 0x7FFFFFFF;
  243. if ((_keys[pos]&0x7FFFFFFF) == id)
  244. return _table[pos];
  245. // check for collision (if we previously deleted element)
  246. if (_table[pos] == null && _keys[pos] >= 0) {
  247. return null;
  248. }
  249. // calculate next slot
  250. seed += incr;
  251. } while (++ntry < size);
  252. return null;
  253. }
  254. /* (non-Javadoc)
  255. * @see net.sf.l2j.util.L2ObjectMap#contains(T)
  256. */
  257. @Override
  258. public boolean contains(T obj)
  259. {
  260. return get(obj.getObjectId()) != null;
  261. }
  262. @SuppressWarnings("unchecked")
  263. private /*already synchronized in put()*/ void expand()
  264. {
  265. int newSize = getPrime(_table.length+1);
  266. L2Object[] newTable = new L2Object[newSize];
  267. int[] newKeys = new int[newSize];
  268. // over all old entries
  269. next_entry:
  270. for (int i=0; i < _table.length; i++)
  271. {
  272. L2Object obj = _table[i];
  273. if (obj == null)
  274. continue;
  275. final int hashcode = _keys[i] & 0x7FFFFFFF;
  276. if (Config.ASSERT) assert hashcode == obj.getObjectId();
  277. int seed = hashcode;
  278. int incr = 1 + (((seed >> 5) + 1) % (newSize - 1));
  279. int ntry = 0;
  280. do
  281. {
  282. int pos = (seed % newSize) & 0x7FFFFFFF;
  283. if (newTable[pos] == null)
  284. {
  285. if (Config.ASSERT) assert newKeys[pos] == 0 && hashcode != 0;
  286. // found an empty slot without previous collisions,
  287. // but use previously found slot
  288. newKeys[pos] = hashcode;
  289. newTable[pos] = obj;
  290. if (TRACE) System.err.println("ht: move obj id="+hashcode+" from slot="+i+" to slot="+pos);
  291. continue next_entry;
  292. }
  293. // set collision bit
  294. newKeys[pos] |= 0x80000000;
  295. // calculate next slot
  296. seed += incr;
  297. } while (++ntry < newSize);
  298. throw new IllegalStateException();
  299. }
  300. _table = (T[])newTable;
  301. _keys = newKeys;
  302. if (DEBUG) check();
  303. }
  304. /* (non-Javadoc)
  305. * @see net.sf.l2j.util.L2ObjectMap#iterator()
  306. */
  307. @Override
  308. public Iterator<T> iterator()
  309. {
  310. return new Itr(_table);
  311. }
  312. class Itr implements Iterator<T>
  313. {
  314. private final T[] _array;
  315. private int _nextIdx;
  316. private T _nextObj;
  317. private T _lastRet;
  318. Itr(T[] pArray)
  319. {
  320. this._array = pArray;
  321. for (; _nextIdx < _array.length; _nextIdx++)
  322. {
  323. _nextObj = _array[_nextIdx];
  324. if (_nextObj != null)
  325. return;
  326. }
  327. }
  328. public boolean hasNext()
  329. {
  330. return _nextObj != null;
  331. }
  332. public T next()
  333. {
  334. if (_nextObj == null)
  335. throw new NoSuchElementException();
  336. _lastRet = _nextObj;
  337. for (_nextIdx++; _nextIdx < _array.length; _nextIdx++)
  338. {
  339. _nextObj = _array[_nextIdx];
  340. if (_nextObj != null)
  341. break;
  342. }
  343. if (_nextIdx >= _array.length)
  344. _nextObj = null;
  345. return _lastRet;
  346. }
  347. public void remove()
  348. {
  349. if (_lastRet == null)
  350. throw new IllegalStateException();
  351. L2ObjectHashMap.this.remove(_lastRet);
  352. }
  353. }
  354. }