L2ObjectHashSet.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 L2ObjectHashSet<T extends L2Object> extends L2ObjectSet<T>
  43. implements Iterable<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[] _collisions;
  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 L2ObjectHashSet()
  70. {
  71. int size = PRIMES[0];
  72. _table = (T[])new L2Object[size];
  73. _collisions = new int[(size+31)>>5];
  74. if (DEBUG) check();
  75. }
  76. /* (non-Javadoc)
  77. * @see com.l2jserver.util.L2ObjectSet#size()
  78. */
  79. @Override
  80. public int size()
  81. {
  82. return _count;
  83. }
  84. /* (non-Javadoc)
  85. * @see com.l2jserver.util.L2ObjectSet#isEmpty()
  86. */
  87. @Override
  88. public boolean isEmpty()
  89. {
  90. return _count == 0;
  91. }
  92. /* (non-Javadoc)
  93. * @see com.l2jserver.util.L2ObjectSet#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. _collisions = new int[(size+31)>>5];
  102. _count = 0;
  103. if (DEBUG) check();
  104. }
  105. private void check()
  106. {
  107. if (DEBUG)
  108. {
  109. int cnt = 0;
  110. assert _collisions.length == ((_table.length+31)>>5);
  111. for (int i=0; i < _table.length; i++)
  112. {
  113. L2Object obj = _table[i];
  114. if (obj != null)
  115. cnt++;
  116. }
  117. assert cnt == _count;
  118. }
  119. }
  120. /* (non-Javadoc)
  121. * @see com.l2jserver.util.L2ObjectSet#put(T)
  122. */
  123. @Override
  124. public synchronized void put(T obj)
  125. {
  126. if (obj == null)
  127. return;
  128. if (contains(obj))
  129. return;
  130. if (_count >= _table.length/2)
  131. expand();
  132. final int hashcode = obj.getObjectId();
  133. 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 ((_collisions[pos>>5] & (1<<(pos&31))) == 0)
  146. {
  147. // found an empty slot without previous collisions,
  148. // but use previously found slot
  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. 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 && (_collisions[pos>>5] & (1<<(pos&31))) == 0)
  166. {
  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. _collisions[pos>>5] |= 1<<(pos&31);
  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.L2ObjectSet#remove(T)
  184. */
  185. @Override
  186. public synchronized void remove(T obj)
  187. {
  188. if (obj == null)
  189. return;
  190. if (!contains(obj))
  191. return;
  192. int hashcode = obj.getObjectId();
  193. assert hashcode > 0;
  194. int seed = hashcode;
  195. int incr = 1 + (((seed >> 5) + 1) % (_table.length - 1));
  196. int ntry = 0;
  197. do
  198. {
  199. int pos = (seed % _table.length) & 0x7FFFFFFF;
  200. if (_table[pos] == obj)
  201. {
  202. // found the object
  203. _table[pos] = null;
  204. _count--;
  205. if (TRACE) System.err.println("ht: remove obj id="+hashcode+" from slot="+pos);
  206. if (DEBUG) check();
  207. return;
  208. }
  209. // check for collision (if we previously deleted element)
  210. if (_table[pos] == null && (_collisions[pos>>5] & (1<<(pos&31))) == 0) {
  211. if (DEBUG) check();
  212. return; //throw new IllegalArgumentException();
  213. }
  214. // calculate next slot
  215. seed += incr;
  216. } while (++ntry < _table.length);
  217. if (DEBUG) check();
  218. throw new IllegalStateException();
  219. }
  220. /* (non-Javadoc)
  221. * @see com.l2jserver.util.L2ObjectSet#contains(T)
  222. */
  223. @Override
  224. public boolean contains(T obj)
  225. {
  226. final int size = _table.length;
  227. if (size <= 11)
  228. {
  229. // for small tables linear check is fast
  230. for (int i=0; i < size; i++)
  231. {
  232. if (_table[i] == obj)
  233. return true;
  234. }
  235. return false;
  236. }
  237. int hashcode = obj.getObjectId();
  238. assert hashcode > 0;
  239. int seed = hashcode;
  240. int incr = 1 + (((seed >> 5) + 1) % (size - 1));
  241. int ntry = 0;
  242. do
  243. {
  244. int pos = (seed % size) & 0x7FFFFFFF;
  245. if (_table[pos] == obj)
  246. return true;
  247. // check for collision (if we previously deleted element)
  248. if (_table[pos] == null && (_collisions[pos>>5] & (1<<(pos&31))) == 0) {
  249. return false;
  250. }
  251. // calculate next slot
  252. seed += incr;
  253. } while (++ntry < size);
  254. return false;
  255. }
  256. @SuppressWarnings("unchecked")
  257. private /*already synchronized in put()*/ void expand()
  258. {
  259. int newSize = getPrime(_table.length+1);
  260. L2Object[] newTable = new L2Object[newSize];
  261. int[] newCollisions = new int[(newSize+31)>>5];
  262. // over all old entries
  263. next_entry:
  264. for (int i=0; i < _table.length; i++)
  265. {
  266. L2Object obj = _table[i];
  267. if (obj == null)
  268. continue;
  269. final int hashcode = obj.getObjectId();
  270. int seed = hashcode;
  271. int incr = 1 + (((seed >> 5) + 1) % (newSize - 1));
  272. int ntry = 0;
  273. do
  274. {
  275. int pos = (seed % newSize) & 0x7FFFFFFF;
  276. if (newTable[pos] == null)
  277. {
  278. // found an empty slot without previous collisions,
  279. // but use previously found slot
  280. newTable[pos] = obj;
  281. if (TRACE) System.err.println("ht: move obj id="+hashcode+" from slot="+i+" to slot="+pos);
  282. continue next_entry;
  283. }
  284. // set collision bit
  285. newCollisions[pos>>5] |= 1<<(pos&31);
  286. // calculate next slot
  287. seed += incr;
  288. } while (++ntry < newSize);
  289. throw new IllegalStateException();
  290. }
  291. _table = (T[])newTable;
  292. _collisions = newCollisions;
  293. if (DEBUG) check();
  294. }
  295. /* (non-Javadoc)
  296. * @see com.l2jserver.util.L2ObjectSet#iterator()
  297. */
  298. @Override
  299. public Iterator<T> iterator()
  300. {
  301. return new Itr(_table);
  302. }
  303. class Itr implements Iterator<T>
  304. {
  305. private final T[] _array;
  306. private int _nextIdx;
  307. private T _nextObj;
  308. private T _lastRet;
  309. Itr(T[] pArray)
  310. {
  311. this._array = pArray;
  312. for (; _nextIdx < _array.length; _nextIdx++)
  313. {
  314. _nextObj = _array[_nextIdx];
  315. if (_nextObj != null)
  316. return;
  317. }
  318. }
  319. public boolean hasNext()
  320. {
  321. return _nextObj != null;
  322. }
  323. public T next()
  324. {
  325. if (_nextObj == null)
  326. throw new NoSuchElementException();
  327. _lastRet = _nextObj;
  328. for (_nextIdx++; _nextIdx < _array.length; _nextIdx++)
  329. {
  330. _nextObj = _array[_nextIdx];
  331. if (_nextObj != null)
  332. break;
  333. }
  334. if (_nextIdx >= _array.length)
  335. _nextObj = null;
  336. return _lastRet;
  337. }
  338. public void remove()
  339. {
  340. if (_lastRet == null)
  341. throw new IllegalStateException();
  342. L2ObjectHashSet.this.remove(_lastRet);
  343. }
  344. }
  345. }