2
0

L2ObjectHashSet.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. * @author mkizub
  27. * @param <T> type of values stored in this hash table.
  28. */
  29. public final class L2ObjectHashSet<T extends L2Object> extends L2ObjectSet<T>
  30. {
  31. private static final boolean TRACE = false;
  32. private static final boolean DEBUG = false;
  33. private static final int[] PRIMES =
  34. {
  35. 5,
  36. 7,
  37. 11,
  38. 17,
  39. 23,
  40. 29,
  41. 37,
  42. 47,
  43. 59,
  44. 71,
  45. 89,
  46. 107,
  47. 131,
  48. 163,
  49. 197,
  50. 239,
  51. 293,
  52. 353,
  53. 431,
  54. 521,
  55. 631,
  56. 761,
  57. 919,
  58. 1103,
  59. 1327,
  60. 1597,
  61. 1931,
  62. 2333,
  63. 2801,
  64. 3371,
  65. 4049,
  66. 4861,
  67. 5839,
  68. 7013,
  69. 8419,
  70. 10103,
  71. 12143,
  72. 14591,
  73. 17519,
  74. 21023,
  75. 25229,
  76. 30293,
  77. 36353,
  78. 43627,
  79. 52361,
  80. 62851,
  81. 75431,
  82. 90523,
  83. 108631,
  84. 130363,
  85. 156437,
  86. 187751,
  87. 225307,
  88. 270371,
  89. 324449,
  90. 389357,
  91. 467237,
  92. 560689,
  93. 672827,
  94. 807403,
  95. 968897,
  96. 1162687,
  97. 1395263,
  98. 1674319,
  99. 2009191,
  100. 2411033,
  101. 2893249,
  102. 3471899,
  103. 4166287,
  104. 4999559,
  105. 5999471,
  106. 7199369
  107. };
  108. private T[] _table;
  109. private int[] _collisions;
  110. private int _count;
  111. private static int getPrime(int min)
  112. {
  113. for (int element : PRIMES)
  114. {
  115. if (element >= min)
  116. {
  117. return element;
  118. }
  119. }
  120. throw new OutOfMemoryError();
  121. }
  122. @SuppressWarnings("unchecked")
  123. public L2ObjectHashSet()
  124. {
  125. int size = PRIMES[0];
  126. _table = (T[]) new L2Object[size];
  127. _collisions = new int[(size + 31) >> 5];
  128. if (DEBUG)
  129. {
  130. check();
  131. }
  132. }
  133. @Override
  134. public int size()
  135. {
  136. return _count;
  137. }
  138. @Override
  139. public boolean isEmpty()
  140. {
  141. return _count == 0;
  142. }
  143. @Override
  144. @SuppressWarnings("unchecked")
  145. public synchronized void clear()
  146. {
  147. int size = PRIMES[0];
  148. _table = (T[]) new L2Object[size];
  149. _collisions = new int[(size + 31) >> 5];
  150. _count = 0;
  151. if (DEBUG)
  152. {
  153. check();
  154. }
  155. }
  156. private void check()
  157. {
  158. if (DEBUG)
  159. {
  160. int cnt = 0;
  161. assert _collisions.length == ((_table.length + 31) >> 5);
  162. for (T obj : _table)
  163. {
  164. if (obj != null)
  165. {
  166. cnt++;
  167. }
  168. }
  169. assert cnt == _count;
  170. }
  171. }
  172. @Override
  173. public synchronized void put(T obj)
  174. {
  175. if (obj == null)
  176. {
  177. return;
  178. }
  179. if (contains(obj))
  180. {
  181. return;
  182. }
  183. if (_count >= (_table.length / 2))
  184. {
  185. expand();
  186. }
  187. final int hashcode = obj.getObjectId();
  188. assert hashcode > 0;
  189. int seed = hashcode;
  190. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  191. int ntry = 0;
  192. int slot = -1; // keep last found slot
  193. do
  194. {
  195. int pos = (seed % _table.length) & 0x7FFFFFFF;
  196. if (_table[pos] == null)
  197. {
  198. if (slot < 0)
  199. {
  200. slot = pos;
  201. }
  202. if ((_collisions[pos >> 5] & (1 << (pos & 31))) == 0)
  203. {
  204. // found an empty slot without previous collisions,
  205. // but use previously found slot
  206. _table[slot] = obj;
  207. _count++;
  208. if (TRACE)
  209. {
  210. System.err.println("ht: put obj id=" + hashcode + " at slot=" + slot);
  211. }
  212. if (DEBUG)
  213. {
  214. check();
  215. }
  216. return;
  217. }
  218. }
  219. else
  220. {
  221. // check if we are adding the same object
  222. if (_table[pos] == obj)
  223. {
  224. return;
  225. }
  226. // this should never happen
  227. assert obj.getObjectId() != _table[pos].getObjectId();
  228. // if there was no collisions at this slot, and we found a free
  229. // slot previously - use found slot
  230. if ((slot >= 0) && ((_collisions[pos >> 5] & (1 << (pos & 31))) == 0))
  231. {
  232. _table[slot] = obj;
  233. _count++;
  234. if (TRACE)
  235. {
  236. System.err.println("ht: put obj id=" + hashcode + " at slot=" + slot);
  237. }
  238. if (DEBUG)
  239. {
  240. check();
  241. }
  242. return;
  243. }
  244. }
  245. // set collision bit
  246. _collisions[pos >> 5] |= 1 << (pos & 31);
  247. // calculate next slot
  248. seed += incr;
  249. }
  250. while (++ntry < _table.length);
  251. if (DEBUG)
  252. {
  253. check();
  254. }
  255. throw new IllegalStateException();
  256. }
  257. @Override
  258. public synchronized void remove(T obj)
  259. {
  260. if (obj == null)
  261. {
  262. return;
  263. }
  264. if (!contains(obj))
  265. {
  266. return;
  267. }
  268. int hashcode = obj.getObjectId();
  269. assert hashcode > 0;
  270. int seed = hashcode;
  271. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  272. int ntry = 0;
  273. do
  274. {
  275. int pos = (seed % _table.length) & 0x7FFFFFFF;
  276. if (_table[pos] == obj)
  277. {
  278. // found the object
  279. _table[pos] = null;
  280. _count--;
  281. if (TRACE)
  282. {
  283. System.err.println("ht: remove obj id=" + hashcode + " from slot=" + pos);
  284. }
  285. if (DEBUG)
  286. {
  287. check();
  288. }
  289. return;
  290. }
  291. // check for collision (if we previously deleted element)
  292. if ((_table[pos] == null) && ((_collisions[pos >> 5] & (1 << (pos & 31))) == 0))
  293. {
  294. if (DEBUG)
  295. {
  296. check();
  297. }
  298. return; // throw new IllegalArgumentException();
  299. }
  300. // calculate next slot
  301. seed += incr;
  302. }
  303. while (++ntry < _table.length);
  304. if (DEBUG)
  305. {
  306. check();
  307. }
  308. throw new IllegalStateException();
  309. }
  310. @Override
  311. public boolean contains(T obj)
  312. {
  313. final int size = _table.length;
  314. if (size <= 11)
  315. {
  316. // for small tables linear check is fast
  317. for (int i = 0; i < size; i++)
  318. {
  319. if (_table[i] == obj)
  320. {
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. int hashcode = obj.getObjectId();
  327. assert hashcode > 0;
  328. int seed = hashcode;
  329. int incr = 1 + (((seed >> 5) + 1) % (size - 1));
  330. int ntry = 0;
  331. do
  332. {
  333. int pos = (seed % size) & 0x7FFFFFFF;
  334. if (_table[pos] == obj)
  335. {
  336. return true;
  337. }
  338. // check for collision (if we previously deleted element)
  339. if ((_table[pos] == null) && ((_collisions[pos >> 5] & (1 << (pos & 31))) == 0))
  340. {
  341. return false;
  342. }
  343. // calculate next slot
  344. seed += incr;
  345. }
  346. while (++ntry < size);
  347. return false;
  348. }
  349. @SuppressWarnings("unchecked")
  350. private/* already synchronized in put() */void expand()
  351. {
  352. int newSize = getPrime(_table.length + 1);
  353. L2Object[] newTable = new L2Object[newSize];
  354. int[] newCollisions = new int[(newSize + 31) >> 5];
  355. // over all old entries
  356. next_entry:
  357. for (int i = 0; i < _table.length; i++)
  358. {
  359. L2Object obj = _table[i];
  360. if (obj == null)
  361. {
  362. continue;
  363. }
  364. final int hashcode = obj.getObjectId();
  365. int seed = hashcode;
  366. int incr = 1 + (((seed >> 5) + 1) % (newSize - 1));
  367. int ntry = 0;
  368. do
  369. {
  370. int pos = (seed % newSize) & 0x7FFFFFFF;
  371. if (newTable[pos] == null)
  372. {
  373. // found an empty slot without previous collisions,
  374. // but use previously found slot
  375. newTable[pos] = obj;
  376. if (TRACE)
  377. {
  378. System.err.println("ht: move obj id=" + hashcode + " from slot=" + i + " to slot=" + pos);
  379. }
  380. continue next_entry;
  381. }
  382. // set collision bit
  383. newCollisions[pos >> 5] |= 1 << (pos & 31);
  384. // calculate next slot
  385. seed += incr;
  386. }
  387. while (++ntry < newSize);
  388. throw new IllegalStateException();
  389. }
  390. _table = (T[]) newTable;
  391. _collisions = newCollisions;
  392. if (DEBUG)
  393. {
  394. check();
  395. }
  396. }
  397. @Override
  398. public Iterator<T> iterator()
  399. {
  400. return new Itr(_table);
  401. }
  402. class Itr implements Iterator<T>
  403. {
  404. private final T[] _array;
  405. private int _nextIdx;
  406. private T _nextObj;
  407. private T _lastRet;
  408. Itr(T[] pArray)
  409. {
  410. this._array = pArray;
  411. for (; _nextIdx < _array.length; _nextIdx++)
  412. {
  413. _nextObj = _array[_nextIdx];
  414. if (_nextObj != null)
  415. {
  416. return;
  417. }
  418. }
  419. }
  420. @Override
  421. public boolean hasNext()
  422. {
  423. return _nextObj != null;
  424. }
  425. @Override
  426. public T next()
  427. {
  428. if (_nextObj == null)
  429. {
  430. throw new NoSuchElementException();
  431. }
  432. _lastRet = _nextObj;
  433. for (_nextIdx++; _nextIdx < _array.length; _nextIdx++)
  434. {
  435. _nextObj = _array[_nextIdx];
  436. if (_nextObj != null)
  437. {
  438. break;
  439. }
  440. }
  441. if (_nextIdx >= _array.length)
  442. {
  443. _nextObj = null;
  444. }
  445. return _lastRet;
  446. }
  447. @Override
  448. public void remove()
  449. {
  450. if (_lastRet == null)
  451. {
  452. throw new IllegalStateException();
  453. }
  454. L2ObjectHashSet.this.remove(_lastRet);
  455. }
  456. }
  457. }