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