hash.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //===-------------------------- hash.cpp ----------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "__hash_table"
  10. #include "algorithm"
  11. #include "stdexcept"
  12. #include "type_traits"
  13. #ifdef __clang__
  14. #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. namespace {
  18. // handle all next_prime(i) for i in [1, 210), special case 0
  19. const unsigned small_primes[] =
  20. {
  21. 0,
  22. 2,
  23. 3,
  24. 5,
  25. 7,
  26. 11,
  27. 13,
  28. 17,
  29. 19,
  30. 23,
  31. 29,
  32. 31,
  33. 37,
  34. 41,
  35. 43,
  36. 47,
  37. 53,
  38. 59,
  39. 61,
  40. 67,
  41. 71,
  42. 73,
  43. 79,
  44. 83,
  45. 89,
  46. 97,
  47. 101,
  48. 103,
  49. 107,
  50. 109,
  51. 113,
  52. 127,
  53. 131,
  54. 137,
  55. 139,
  56. 149,
  57. 151,
  58. 157,
  59. 163,
  60. 167,
  61. 173,
  62. 179,
  63. 181,
  64. 191,
  65. 193,
  66. 197,
  67. 199,
  68. 211
  69. };
  70. // potential primes = 210*k + indices[i], k >= 1
  71. // these numbers are not divisible by 2, 3, 5 or 7
  72. // (or any integer 2 <= j <= 10 for that matter).
  73. const unsigned indices[] =
  74. {
  75. 1,
  76. 11,
  77. 13,
  78. 17,
  79. 19,
  80. 23,
  81. 29,
  82. 31,
  83. 37,
  84. 41,
  85. 43,
  86. 47,
  87. 53,
  88. 59,
  89. 61,
  90. 67,
  91. 71,
  92. 73,
  93. 79,
  94. 83,
  95. 89,
  96. 97,
  97. 101,
  98. 103,
  99. 107,
  100. 109,
  101. 113,
  102. 121,
  103. 127,
  104. 131,
  105. 137,
  106. 139,
  107. 143,
  108. 149,
  109. 151,
  110. 157,
  111. 163,
  112. 167,
  113. 169,
  114. 173,
  115. 179,
  116. 181,
  117. 187,
  118. 191,
  119. 193,
  120. 197,
  121. 199,
  122. 209
  123. };
  124. }
  125. // Returns: If n == 0, returns 0. Else returns the lowest prime number that
  126. // is greater than or equal to n.
  127. //
  128. // The algorithm creates a list of small primes, plus an open-ended list of
  129. // potential primes. All prime numbers are potential prime numbers. However
  130. // some potential prime numbers are not prime. In an ideal world, all potential
  131. // prime numbers would be prime. Candidate prime numbers are chosen as the next
  132. // highest potential prime. Then this number is tested for prime by dividing it
  133. // by all potential prime numbers less than the sqrt of the candidate.
  134. //
  135. // This implementation defines potential primes as those numbers not divisible
  136. // by 2, 3, 5, and 7. Other (common) implementations define potential primes
  137. // as those not divisible by 2. A few other implementations define potential
  138. // primes as those not divisible by 2 or 3. By raising the number of small
  139. // primes which the potential prime is not divisible by, the set of potential
  140. // primes more closely approximates the set of prime numbers. And thus there
  141. // are fewer potential primes to search, and fewer potential primes to divide
  142. // against.
  143. template <size_t _Sz = sizeof(size_t)>
  144. inline _LIBCPP_INLINE_VISIBILITY
  145. typename enable_if<_Sz == 4, void>::type
  146. __check_for_overflow(size_t N)
  147. {
  148. #ifndef _LIBCPP_NO_EXCEPTIONS
  149. if (N > 0xFFFFFFFB)
  150. throw overflow_error("__next_prime overflow");
  151. #else
  152. (void)N;
  153. #endif
  154. }
  155. template <size_t _Sz = sizeof(size_t)>
  156. inline _LIBCPP_INLINE_VISIBILITY
  157. typename enable_if<_Sz == 8, void>::type
  158. __check_for_overflow(size_t N)
  159. {
  160. #ifndef _LIBCPP_NO_EXCEPTIONS
  161. if (N > 0xFFFFFFFFFFFFFFC5ull)
  162. throw overflow_error("__next_prime overflow");
  163. #else
  164. (void)N;
  165. #endif
  166. }
  167. size_t
  168. __next_prime(size_t n)
  169. {
  170. const size_t L = 210;
  171. const size_t N = sizeof(small_primes) / sizeof(small_primes[0]);
  172. // If n is small enough, search in small_primes
  173. if (n <= small_primes[N-1])
  174. return *std::lower_bound(small_primes, small_primes + N, n);
  175. // Else n > largest small_primes
  176. // Check for overflow
  177. __check_for_overflow(n);
  178. // Start searching list of potential primes: L * k0 + indices[in]
  179. const size_t M = sizeof(indices) / sizeof(indices[0]);
  180. // Select first potential prime >= n
  181. // Known a-priori n >= L
  182. size_t k0 = n / L;
  183. size_t in = static_cast<size_t>(std::lower_bound(indices, indices + M, n - k0 * L)
  184. - indices);
  185. n = L * k0 + indices[in];
  186. while (true)
  187. {
  188. // Divide n by all primes or potential primes (i) until:
  189. // 1. The division is even, so try next potential prime.
  190. // 2. The i > sqrt(n), in which case n is prime.
  191. // It is known a-priori that n is not divisible by 2, 3, 5 or 7,
  192. // so don't test those (j == 5 -> divide by 11 first). And the
  193. // potential primes start with 211, so don't test against the last
  194. // small prime.
  195. for (size_t j = 5; j < N - 1; ++j)
  196. {
  197. const std::size_t p = small_primes[j];
  198. const std::size_t q = n / p;
  199. if (q < p)
  200. return n;
  201. if (n == q * p)
  202. goto next;
  203. }
  204. // n wasn't divisible by small primes, try potential primes
  205. {
  206. size_t i = 211;
  207. while (true)
  208. {
  209. std::size_t q = n / i;
  210. if (q < i)
  211. return n;
  212. if (n == q * i)
  213. break;
  214. i += 10;
  215. q = n / i;
  216. if (q < i)
  217. return n;
  218. if (n == q * i)
  219. break;
  220. i += 2;
  221. q = n / i;
  222. if (q < i)
  223. return n;
  224. if (n == q * i)
  225. break;
  226. i += 4;
  227. q = n / i;
  228. if (q < i)
  229. return n;
  230. if (n == q * i)
  231. break;
  232. i += 2;
  233. q = n / i;
  234. if (q < i)
  235. return n;
  236. if (n == q * i)
  237. break;
  238. i += 4;
  239. q = n / i;
  240. if (q < i)
  241. return n;
  242. if (n == q * i)
  243. break;
  244. i += 6;
  245. q = n / i;
  246. if (q < i)
  247. return n;
  248. if (n == q * i)
  249. break;
  250. i += 2;
  251. q = n / i;
  252. if (q < i)
  253. return n;
  254. if (n == q * i)
  255. break;
  256. i += 6;
  257. q = n / i;
  258. if (q < i)
  259. return n;
  260. if (n == q * i)
  261. break;
  262. i += 4;
  263. q = n / i;
  264. if (q < i)
  265. return n;
  266. if (n == q * i)
  267. break;
  268. i += 2;
  269. q = n / i;
  270. if (q < i)
  271. return n;
  272. if (n == q * i)
  273. break;
  274. i += 4;
  275. q = n / i;
  276. if (q < i)
  277. return n;
  278. if (n == q * i)
  279. break;
  280. i += 6;
  281. q = n / i;
  282. if (q < i)
  283. return n;
  284. if (n == q * i)
  285. break;
  286. i += 6;
  287. q = n / i;
  288. if (q < i)
  289. return n;
  290. if (n == q * i)
  291. break;
  292. i += 2;
  293. q = n / i;
  294. if (q < i)
  295. return n;
  296. if (n == q * i)
  297. break;
  298. i += 6;
  299. q = n / i;
  300. if (q < i)
  301. return n;
  302. if (n == q * i)
  303. break;
  304. i += 4;
  305. q = n / i;
  306. if (q < i)
  307. return n;
  308. if (n == q * i)
  309. break;
  310. i += 2;
  311. q = n / i;
  312. if (q < i)
  313. return n;
  314. if (n == q * i)
  315. break;
  316. i += 6;
  317. q = n / i;
  318. if (q < i)
  319. return n;
  320. if (n == q * i)
  321. break;
  322. i += 4;
  323. q = n / i;
  324. if (q < i)
  325. return n;
  326. if (n == q * i)
  327. break;
  328. i += 6;
  329. q = n / i;
  330. if (q < i)
  331. return n;
  332. if (n == q * i)
  333. break;
  334. i += 8;
  335. q = n / i;
  336. if (q < i)
  337. return n;
  338. if (n == q * i)
  339. break;
  340. i += 4;
  341. q = n / i;
  342. if (q < i)
  343. return n;
  344. if (n == q * i)
  345. break;
  346. i += 2;
  347. q = n / i;
  348. if (q < i)
  349. return n;
  350. if (n == q * i)
  351. break;
  352. i += 4;
  353. q = n / i;
  354. if (q < i)
  355. return n;
  356. if (n == q * i)
  357. break;
  358. i += 2;
  359. q = n / i;
  360. if (q < i)
  361. return n;
  362. if (n == q * i)
  363. break;
  364. i += 4;
  365. q = n / i;
  366. if (q < i)
  367. return n;
  368. if (n == q * i)
  369. break;
  370. i += 8;
  371. q = n / i;
  372. if (q < i)
  373. return n;
  374. if (n == q * i)
  375. break;
  376. i += 6;
  377. q = n / i;
  378. if (q < i)
  379. return n;
  380. if (n == q * i)
  381. break;
  382. i += 4;
  383. q = n / i;
  384. if (q < i)
  385. return n;
  386. if (n == q * i)
  387. break;
  388. i += 6;
  389. q = n / i;
  390. if (q < i)
  391. return n;
  392. if (n == q * i)
  393. break;
  394. i += 2;
  395. q = n / i;
  396. if (q < i)
  397. return n;
  398. if (n == q * i)
  399. break;
  400. i += 4;
  401. q = n / i;
  402. if (q < i)
  403. return n;
  404. if (n == q * i)
  405. break;
  406. i += 6;
  407. q = n / i;
  408. if (q < i)
  409. return n;
  410. if (n == q * i)
  411. break;
  412. i += 2;
  413. q = n / i;
  414. if (q < i)
  415. return n;
  416. if (n == q * i)
  417. break;
  418. i += 6;
  419. q = n / i;
  420. if (q < i)
  421. return n;
  422. if (n == q * i)
  423. break;
  424. i += 6;
  425. q = n / i;
  426. if (q < i)
  427. return n;
  428. if (n == q * i)
  429. break;
  430. i += 4;
  431. q = n / i;
  432. if (q < i)
  433. return n;
  434. if (n == q * i)
  435. break;
  436. i += 2;
  437. q = n / i;
  438. if (q < i)
  439. return n;
  440. if (n == q * i)
  441. break;
  442. i += 4;
  443. q = n / i;
  444. if (q < i)
  445. return n;
  446. if (n == q * i)
  447. break;
  448. i += 6;
  449. q = n / i;
  450. if (q < i)
  451. return n;
  452. if (n == q * i)
  453. break;
  454. i += 2;
  455. q = n / i;
  456. if (q < i)
  457. return n;
  458. if (n == q * i)
  459. break;
  460. i += 6;
  461. q = n / i;
  462. if (q < i)
  463. return n;
  464. if (n == q * i)
  465. break;
  466. i += 4;
  467. q = n / i;
  468. if (q < i)
  469. return n;
  470. if (n == q * i)
  471. break;
  472. i += 2;
  473. q = n / i;
  474. if (q < i)
  475. return n;
  476. if (n == q * i)
  477. break;
  478. i += 4;
  479. q = n / i;
  480. if (q < i)
  481. return n;
  482. if (n == q * i)
  483. break;
  484. i += 2;
  485. q = n / i;
  486. if (q < i)
  487. return n;
  488. if (n == q * i)
  489. break;
  490. i += 10;
  491. q = n / i;
  492. if (q < i)
  493. return n;
  494. if (n == q * i)
  495. break;
  496. // This will loop i to the next "plane" of potential primes
  497. i += 2;
  498. }
  499. }
  500. next:
  501. // n is not prime. Increment n to next potential prime.
  502. if (++in == M)
  503. {
  504. ++k0;
  505. in = 0;
  506. }
  507. n = L * k0 + indices[in];
  508. }
  509. }
  510. _LIBCPP_END_NAMESPACE_STD