L2ObjectHashMap.java 9.3 KB

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