L2ObjectHashSet.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 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 L2ObjectHashSet<T extends L2Object> extends L2ObjectSet<T>
  43. {
  44. private static final boolean TRACE = false;
  45. private static final boolean DEBUG = false;
  46. private final static int[] PRIMES = {
  47. 5, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293,
  48. 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333, 2801,
  49. 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591, 17519,
  50. 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523,
  51. 108631, 130363, 156437, 187751, 225307, 270371, 324449, 389357,
  52. 467237, 560689, 672827, 807403, 968897, 1162687, 1395263, 1674319,
  53. 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369
  54. };
  55. private T[] _table;
  56. private int[] _collisions;
  57. private int _count;
  58. private static int getPrime(int min)
  59. {
  60. for (int i=0; i < PRIMES.length; i++)
  61. {
  62. if (PRIMES[i] >= min)
  63. return PRIMES[i];
  64. }
  65. throw new OutOfMemoryError();
  66. }
  67. @SuppressWarnings("unchecked")
  68. public L2ObjectHashSet()
  69. {
  70. int size = PRIMES[0];
  71. _table = (T[])new L2Object[size];
  72. _collisions = new int[(size+31)>>5];
  73. if (DEBUG) check();
  74. }
  75. /* (non-Javadoc)
  76. * @see com.l2jserver.util.L2ObjectSet#size()
  77. */
  78. @Override
  79. public int size()
  80. {
  81. return _count;
  82. }
  83. /* (non-Javadoc)
  84. * @see com.l2jserver.util.L2ObjectSet#isEmpty()
  85. */
  86. @Override
  87. public boolean isEmpty()
  88. {
  89. return _count == 0;
  90. }
  91. /* (non-Javadoc)
  92. * @see com.l2jserver.util.L2ObjectSet#clear()
  93. */
  94. @Override
  95. @SuppressWarnings("unchecked")
  96. public synchronized void clear()
  97. {
  98. int size = PRIMES[0];
  99. _table = (T[])new L2Object[size];
  100. _collisions = new int[(size+31)>>5];
  101. _count = 0;
  102. if (DEBUG) check();
  103. }
  104. private void check()
  105. {
  106. if (DEBUG)
  107. {
  108. int cnt = 0;
  109. assert _collisions.length == ((_table.length+31)>>5);
  110. for (int i=0; i < _table.length; i++)
  111. {
  112. L2Object obj = _table[i];
  113. if (obj != null)
  114. cnt++;
  115. }
  116. assert cnt == _count;
  117. }
  118. }
  119. /* (non-Javadoc)
  120. * @see com.l2jserver.util.L2ObjectSet#put(T)
  121. */
  122. @Override
  123. public synchronized void put(T obj)
  124. {
  125. if (obj == null)
  126. return;
  127. if (contains(obj))
  128. return;
  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 ((_collisions[pos>>5] & (1<<(pos&31))) == 0)
  145. {
  146. // found an empty slot without previous collisions,
  147. // but use previously found slot
  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 && (_collisions[pos>>5] & (1<<(pos&31))) == 0)
  165. {
  166. _table[slot] = obj;
  167. _count++;
  168. if (TRACE) System.err.println("ht: put obj id="+hashcode+" at slot="+slot);
  169. if (DEBUG) check();
  170. return;
  171. }
  172. }
  173. // set collision bit
  174. _collisions[pos>>5] |= 1<<(pos&31);
  175. // calculate next slot
  176. seed += incr;
  177. } while (++ntry < _table.length);
  178. if (DEBUG) check();
  179. throw new IllegalStateException();
  180. }
  181. /* (non-Javadoc)
  182. * @see com.l2jserver.util.L2ObjectSet#remove(T)
  183. */
  184. @Override
  185. public synchronized void remove(T obj)
  186. {
  187. if (obj == null)
  188. return;
  189. if (!contains(obj))
  190. return;
  191. int hashcode = obj.getObjectId();
  192. assert hashcode > 0;
  193. int seed = hashcode;
  194. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  195. int ntry = 0;
  196. do
  197. {
  198. int pos = (seed % _table.length) & 0x7FFFFFFF;
  199. if (_table[pos] == obj)
  200. {
  201. // found the object
  202. _table[pos] = null;
  203. _count--;
  204. if (TRACE) System.err.println("ht: remove obj id="+hashcode+" from slot="+pos);
  205. if (DEBUG) check();
  206. return;
  207. }
  208. // check for collision (if we previously deleted element)
  209. if (_table[pos] == null && (_collisions[pos>>5] & (1<<(pos&31))) == 0) {
  210. if (DEBUG) check();
  211. return; //throw new IllegalArgumentException();
  212. }
  213. // calculate next slot
  214. seed += incr;
  215. } while (++ntry < _table.length);
  216. if (DEBUG) check();
  217. throw new IllegalStateException();
  218. }
  219. /* (non-Javadoc)
  220. * @see com.l2jserver.util.L2ObjectSet#contains(T)
  221. */
  222. @Override
  223. public boolean contains(T obj)
  224. {
  225. final int size = _table.length;
  226. if (size <= 11)
  227. {
  228. // for small tables linear check is fast
  229. for (int i=0; i < size; i++)
  230. {
  231. if (_table[i] == obj)
  232. return true;
  233. }
  234. return false;
  235. }
  236. int hashcode = obj.getObjectId();
  237. assert hashcode > 0;
  238. int seed = hashcode;
  239. int incr = 1 + (((seed >> 5) + 1) % (size - 1));
  240. int ntry = 0;
  241. do
  242. {
  243. int pos = (seed % size) & 0x7FFFFFFF;
  244. if (_table[pos] == obj)
  245. return true;
  246. // check for collision (if we previously deleted element)
  247. if (_table[pos] == null && (_collisions[pos>>5] & (1<<(pos&31))) == 0) {
  248. return false;
  249. }
  250. // calculate next slot
  251. seed += incr;
  252. } while (++ntry < size);
  253. return false;
  254. }
  255. @SuppressWarnings("unchecked")
  256. private /*already synchronized in put()*/ void expand()
  257. {
  258. int newSize = getPrime(_table.length+1);
  259. L2Object[] newTable = new L2Object[newSize];
  260. int[] newCollisions = new int[(newSize+31)>>5];
  261. // over all old entries
  262. next_entry:
  263. for (int i=0; i < _table.length; i++)
  264. {
  265. L2Object obj = _table[i];
  266. if (obj == null)
  267. continue;
  268. final int hashcode = obj.getObjectId();
  269. int seed = hashcode;
  270. int incr = 1 + (((seed >> 5) + 1) % (newSize - 1));
  271. int ntry = 0;
  272. do
  273. {
  274. int pos = (seed % newSize) & 0x7FFFFFFF;
  275. if (newTable[pos] == null)
  276. {
  277. // found an empty slot without previous collisions,
  278. // but use previously found slot
  279. newTable[pos] = obj;
  280. if (TRACE) System.err.println("ht: move obj id="+hashcode+" from slot="+i+" to slot="+pos);
  281. continue next_entry;
  282. }
  283. // set collision bit
  284. newCollisions[pos>>5] |= 1<<(pos&31);
  285. // calculate next slot
  286. seed += incr;
  287. } while (++ntry < newSize);
  288. throw new IllegalStateException();
  289. }
  290. _table = (T[])newTable;
  291. _collisions = newCollisions;
  292. if (DEBUG) check();
  293. }
  294. /* (non-Javadoc)
  295. * @see com.l2jserver.util.L2ObjectSet#iterator()
  296. */
  297. @Override
  298. public Iterator<T> iterator()
  299. {
  300. return new Itr(_table);
  301. }
  302. class Itr implements Iterator<T>
  303. {
  304. private final T[] _array;
  305. private int _nextIdx;
  306. private T _nextObj;
  307. private T _lastRet;
  308. Itr(T[] pArray)
  309. {
  310. this._array = pArray;
  311. for (; _nextIdx < _array.length; _nextIdx++)
  312. {
  313. _nextObj = _array[_nextIdx];
  314. if (_nextObj != null)
  315. return;
  316. }
  317. }
  318. public boolean hasNext()
  319. {
  320. return _nextObj != null;
  321. }
  322. public T next()
  323. {
  324. if (_nextObj == null)
  325. throw new NoSuchElementException();
  326. _lastRet = _nextObj;
  327. for (_nextIdx++; _nextIdx < _array.length; _nextIdx++)
  328. {
  329. _nextObj = _array[_nextIdx];
  330. if (_nextObj != null)
  331. break;
  332. }
  333. if (_nextIdx >= _array.length)
  334. _nextObj = null;
  335. return _lastRet;
  336. }
  337. public void remove()
  338. {
  339. if (_lastRet == null)
  340. throw new IllegalStateException();
  341. L2ObjectHashSet.this.remove(_lastRet);
  342. }
  343. }
  344. }