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