PrimeFinder.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 1999 CERN - European Organization for Nuclear Research.
  3. *
  4. * Permission to use, copy, modify, distribute and sell this software
  5. * and its documentation for any purpose is hereby granted without fee,
  6. * provided that the above copyright notice appear in all copies and
  7. * that both that copyright notice and this permission notice appear in
  8. * supporting documentation. CERN makes no representations about the
  9. * suitability of this software for any purpose. It is provided "as is"
  10. * without expressed or implied warranty.
  11. */
  12. package com.l2jserver.util;
  13. import java.util.Arrays;
  14. /**
  15. * <b>Modified for Trove to use the java.util.Arrays sort/search<br>
  16. * algorithms instead of those provided with colt.</b><br>
  17. * Used to keep hash table capacities prime numbers.<br>
  18. * Not of interest for users; only for implementors of hashtables.<br>
  19. * <p>
  20. * Choosing prime numbers as hash table capacities is a good idea<br>
  21. * to keep them working fast, particularly under hash table expansions.<br>
  22. * <p>
  23. * However, JDK 1.2, JGL 3.1 and many other toolkits do nothing to keep capacities prime.<br>
  24. * This class provides efficient means to choose prime capacities.
  25. * <p>
  26. * Choosing a prime is <tt>O(log 300)</tt> (binary search in a list of 300 ints).<br>
  27. * Memory requirements: 1 KB static memory.<br>
  28. * @author wolfgang.hoschek@cern.ch
  29. * @version 1.0, 09/24/99
  30. */
  31. public final class PrimeFinder
  32. {
  33. /**
  34. * The largest prime this class can generate; currently equal to <tt>Integer.MAX_VALUE</tt>.
  35. */
  36. public static final int LARGEST_PRIME = Integer.MAX_VALUE; // yes, it is prime.
  37. /**
  38. * The prime number list consists of 11 chunks.<br>
  39. * Each chunk contains prime numbers.<br>
  40. * A chunk starts with a prime P1.<br>
  41. * The next element is a prime P2.<br>
  42. * P2 is the smallest prime for which holds: P2 >= 2*P1.<br>
  43. * The next element is P3, for which the same holds with respect to P2, and so on. Chunks are chosen such that for any desired capacity >= 1000<br>
  44. * the list includes a prime number <= desired capacity * 1.11.<br>
  45. * Therefore, primes can be retrieved which are quite close to any<br>
  46. * desired capacity, which in turn avoids wasting memory.<br>
  47. * For example, the list includes<br>
  48. * 1039,1117,1201,1277,1361,1439,1523,1597,1759,1907,2081.<br>
  49. * So if you need a prime >= 1040, you will find a prime <= 1040*1.11=1154.<br>
  50. * Chunks are chosen such that they are optimized for a hashtable growthfactor of 2.0;<br>
  51. * If your hashtable has such a growthfactor then, after initially<br>
  52. * "rounding to a prime" upon hashtable construction, it will<br>
  53. * later expand to prime capacities such that there exist no better primes.<br>
  54. * In total these are about 32*10=320 numbers -> 1 KB of static memory needed.<br>
  55. * If you are stingy, then delete every second or fourth chunk.
  56. */
  57. private static final int[] PRIME_CAPACITIES =
  58. {
  59. // chunk #0
  60. LARGEST_PRIME,
  61. // chunk #1
  62. 5,
  63. 11,
  64. 23,
  65. 47,
  66. 97,
  67. 197,
  68. 397,
  69. 797,
  70. 1597,
  71. 3203,
  72. 6421,
  73. 12853,
  74. 25717,
  75. 51437,
  76. 102877,
  77. 205759,
  78. 411527,
  79. 823117,
  80. 1646237,
  81. 3292489,
  82. 6584983,
  83. 13169977,
  84. 26339969,
  85. 52679969,
  86. 105359939,
  87. 210719881,
  88. 421439783,
  89. 842879579,
  90. 1685759167,
  91. // chunk #2
  92. 433,
  93. 877,
  94. 1759,
  95. 3527,
  96. 7057,
  97. 14143,
  98. 28289,
  99. 56591,
  100. 113189,
  101. 226379,
  102. 452759,
  103. 905551,
  104. 1811107,
  105. 3622219,
  106. 7244441,
  107. 14488931,
  108. 28977863,
  109. 57955739,
  110. 115911563,
  111. 231823147,
  112. 463646329,
  113. 927292699,
  114. 1854585413,
  115. // chunk #3
  116. 953,
  117. 1907,
  118. 3821,
  119. 7643,
  120. 15287,
  121. 30577,
  122. 61169,
  123. 122347,
  124. 244703,
  125. 489407,
  126. 978821,
  127. 1957651,
  128. 3915341,
  129. 7830701,
  130. 15661423,
  131. 31322867,
  132. 62645741,
  133. 125291483,
  134. 250582987,
  135. 501165979,
  136. 1002331963,
  137. 2004663929,
  138. // chunk #4
  139. 1039,
  140. 2081,
  141. 4177,
  142. 8363,
  143. 16729,
  144. 33461,
  145. 66923,
  146. 133853,
  147. 267713,
  148. 535481,
  149. 1070981,
  150. 2141977,
  151. 4283963,
  152. 8567929,
  153. 17135863,
  154. 34271747,
  155. 68543509,
  156. 137087021,
  157. 274174111,
  158. 548348231,
  159. 1096696463,
  160. // chunk #5
  161. 31,
  162. 67,
  163. 137,
  164. 277,
  165. 557,
  166. 1117,
  167. 2237,
  168. 4481,
  169. 8963,
  170. 17929,
  171. 35863,
  172. 71741,
  173. 143483,
  174. 286973,
  175. 573953,
  176. 1147921,
  177. 2295859,
  178. 4591721,
  179. 9183457,
  180. 18366923,
  181. 36733847,
  182. 73467739,
  183. 146935499,
  184. 293871013,
  185. 587742049,
  186. 1175484103,
  187. // chunk #6
  188. 599,
  189. 1201,
  190. 2411,
  191. 4831,
  192. 9677,
  193. 19373,
  194. 38747,
  195. 77509,
  196. 155027,
  197. 310081,
  198. 620171,
  199. 1240361,
  200. 2480729,
  201. 4961459,
  202. 9922933,
  203. 19845871,
  204. 39691759,
  205. 79383533,
  206. 158767069,
  207. 317534141,
  208. 635068283,
  209. 1270136683,
  210. // chunk #7
  211. 311,
  212. 631,
  213. 1277,
  214. 2557,
  215. 5119,
  216. 10243,
  217. 20507,
  218. 41017,
  219. 82037,
  220. 164089,
  221. 328213,
  222. 656429,
  223. 1312867,
  224. 2625761,
  225. 5251529,
  226. 10503061,
  227. 21006137,
  228. 42012281,
  229. 84024581,
  230. 168049163,
  231. 336098327,
  232. 672196673,
  233. 1344393353,
  234. // chunk #8
  235. 3,
  236. 7,
  237. 17,
  238. 37,
  239. 79,
  240. 163,
  241. 331,
  242. 673,
  243. 1361,
  244. 2729,
  245. 5471,
  246. 10949,
  247. 21911,
  248. 43853,
  249. 87719,
  250. 175447,
  251. 350899,
  252. 701819,
  253. 1403641,
  254. 2807303,
  255. 5614657,
  256. 11229331,
  257. 22458671,
  258. 44917381,
  259. 89834777,
  260. 179669557,
  261. 359339171,
  262. 718678369,
  263. 1437356741,
  264. // chunk #9
  265. 43,
  266. 89,
  267. 179,
  268. 359,
  269. 719,
  270. 1439,
  271. 2879,
  272. 5779,
  273. 11579,
  274. 23159,
  275. 46327,
  276. 92657,
  277. 185323,
  278. 370661,
  279. 741337,
  280. 1482707,
  281. 2965421,
  282. 5930887,
  283. 11861791,
  284. 23723597,
  285. 47447201,
  286. 94894427,
  287. 189788857,
  288. 379577741,
  289. 759155483,
  290. 1518310967,
  291. // chunk #10
  292. 379,
  293. 761,
  294. 1523,
  295. 3049,
  296. 6101,
  297. 12203,
  298. 24407,
  299. 48817,
  300. 97649,
  301. 195311,
  302. 390647,
  303. 781301,
  304. 1562611,
  305. 3125257,
  306. 6250537,
  307. 12501169,
  308. 25002389,
  309. 50004791,
  310. 100009607,
  311. 200019221,
  312. 400038451,
  313. 800076929,
  314. 1600153859
  315. };
  316. static
  317. { // initializer
  318. // The above prime numbers are formatted for human readability.
  319. // To find numbers fast, we sort them once and for all.
  320. Arrays.sort(PRIME_CAPACITIES);
  321. }
  322. /**
  323. * Returns a prime number which is <code>&gt;= desiredCapacity</code> and very close to <code>desiredCapacity</code> (within 11% if <code>desiredCapacity &gt;= 1000</code>).
  324. * @param desiredCapacity the capacity desired by the user.
  325. * @return the capacity which should be used for a hashtable.
  326. */
  327. public static final int nextPrime(int desiredCapacity)
  328. {
  329. int i = Arrays.binarySearch(PRIME_CAPACITIES, desiredCapacity);
  330. if (i < 0)
  331. {
  332. // desired capacity not found, choose next prime greater
  333. // than desired capacity
  334. i = -i - 1; // remember the semantics of binarySearch...
  335. }
  336. return PRIME_CAPACITIES[i];
  337. }
  338. }