basisu_containers_impl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. // basisu_containers_impl.h
  2. // Do not include directly
  3. #ifdef _MSC_VER
  4. #pragma warning (disable:4127) // warning C4127: conditional expression is constant
  5. #endif
  6. namespace basisu
  7. {
  8. // A container operation has internally panicked in an unrecoverable way.
  9. // Either an allocation has failed, or a range or consistency check has failed.
  10. #ifdef _MSC_VER
  11. __declspec(noreturn)
  12. #else
  13. [[noreturn]]
  14. #endif
  15. void container_abort(const char* pMsg, ...)
  16. {
  17. assert(0);
  18. va_list args;
  19. va_start(args, pMsg);
  20. char buf[1024] = {};
  21. #ifdef _MSC_VER
  22. vsprintf_s(buf, sizeof(buf), pMsg, args);
  23. #else
  24. vsnprintf(buf, sizeof(buf), pMsg, args);
  25. #endif
  26. va_end(args);
  27. fputs(buf, stderr);
  28. std::terminate();
  29. }
  30. bool elemental_vector::increase_capacity(size_t min_new_capacity, bool grow_hint, size_t element_size, object_mover pMover, bool nofail_flag)
  31. {
  32. assert(m_size <= m_capacity);
  33. assert(min_new_capacity >= m_size);
  34. assert(element_size);
  35. // Basic sanity check min_new_capacity
  36. if (!can_fit_into_size_t((uint64_t)min_new_capacity * element_size))
  37. {
  38. assert(0);
  39. if (nofail_flag)
  40. return false;
  41. container_abort("elemental_vector::increase_capacity: requesting too many elements\n");
  42. }
  43. // Check for sane library limits
  44. if (sizeof(void*) == sizeof(uint64_t))
  45. {
  46. // 16 GB
  47. assert(min_new_capacity < (0x400000000ULL / element_size));
  48. }
  49. else
  50. {
  51. // ~1.99 GB
  52. assert(min_new_capacity < (0x7FFF0000U / element_size));
  53. }
  54. // If vector is already large enough just return.
  55. if (m_capacity >= min_new_capacity)
  56. return true;
  57. uint64_t new_capacity_u64 = min_new_capacity;
  58. if ((grow_hint) && (!helpers::is_power_of_2(new_capacity_u64)))
  59. {
  60. new_capacity_u64 = helpers::next_pow2(new_capacity_u64);
  61. if (!can_fit_into_size_t(new_capacity_u64))
  62. {
  63. assert(0);
  64. if (nofail_flag)
  65. return false;
  66. container_abort("elemental_vector::increase_capacity: vector too large\n");
  67. }
  68. }
  69. const uint64_t desired_size_u64 = element_size * new_capacity_u64;
  70. if (!can_fit_into_size_t(desired_size_u64))
  71. {
  72. assert(0);
  73. if (nofail_flag)
  74. return false;
  75. container_abort("elemental_vector::increase_capacity: vector too large\n");
  76. }
  77. const size_t desired_size = static_cast<size_t>(desired_size_u64);
  78. size_t actual_size = 0;
  79. BASISU_NOTE_UNUSED(actual_size);
  80. if (!pMover)
  81. {
  82. void* new_p = realloc(m_p, desired_size);
  83. if (!new_p)
  84. {
  85. assert(0);
  86. if (nofail_flag)
  87. return false;
  88. container_abort("elemental_vector::increase_capacity: realloc() failed allocating %zu bytes", desired_size);
  89. }
  90. #if BASISU_VECTOR_DETERMINISTIC
  91. actual_size = desired_size;
  92. #elif defined(_MSC_VER)
  93. actual_size = _msize(new_p);
  94. #elif HAS_MALLOC_USABLE_SIZE
  95. actual_size = malloc_usable_size(new_p);
  96. #else
  97. actual_size = desired_size;
  98. #endif
  99. m_p = new_p;
  100. }
  101. else
  102. {
  103. void* new_p = malloc(desired_size);
  104. if (!new_p)
  105. {
  106. assert(0);
  107. if (nofail_flag)
  108. return false;
  109. container_abort("elemental_vector::increase_capacity: malloc() failed allocating %zu bytes", desired_size);
  110. }
  111. #if BASISU_VECTOR_DETERMINISTIC
  112. actual_size = desired_size;
  113. #elif defined(_MSC_VER)
  114. actual_size = _msize(new_p);
  115. #elif HAS_MALLOC_USABLE_SIZE
  116. actual_size = malloc_usable_size(new_p);
  117. #else
  118. actual_size = desired_size;
  119. #endif
  120. (*pMover)(new_p, m_p, m_size);
  121. if (m_p)
  122. free(m_p);
  123. m_p = new_p;
  124. }
  125. #if BASISU_VECTOR_DETERMINISTIC
  126. m_capacity = static_cast<size_t>(new_capacity_u64);
  127. #else
  128. if (actual_size > desired_size)
  129. m_capacity = static_cast<size_t>(actual_size / element_size);
  130. else
  131. m_capacity = static_cast<size_t>(new_capacity_u64);
  132. #endif
  133. return true;
  134. }
  135. #if BASISU_HASHMAP_TEST
  136. #define HASHMAP_TEST_VERIFY(c) do { if (!(c)) handle_hashmap_test_verify_failure(__LINE__); } while(0)
  137. static void handle_hashmap_test_verify_failure(int line)
  138. {
  139. container_abort("HASHMAP_TEST_VERIFY() faild on line %i\n", line);
  140. }
  141. class counted_obj
  142. {
  143. public:
  144. counted_obj(uint32_t v = 0) :
  145. m_val(v)
  146. {
  147. m_count++;
  148. }
  149. counted_obj(const counted_obj& obj) :
  150. m_val(obj.m_val)
  151. {
  152. if (m_val != UINT64_MAX)
  153. m_count++;
  154. }
  155. counted_obj(counted_obj&& obj) :
  156. m_val(obj.m_val)
  157. {
  158. obj.m_val = UINT64_MAX;
  159. }
  160. counted_obj& operator= (counted_obj&& rhs)
  161. {
  162. if (this != &rhs)
  163. {
  164. m_val = rhs.m_val;
  165. rhs.m_val = UINT64_MAX;
  166. }
  167. return *this;
  168. }
  169. ~counted_obj()
  170. {
  171. if (m_val != UINT64_MAX)
  172. {
  173. assert(m_count > 0);
  174. m_count--;
  175. }
  176. }
  177. static uint32_t m_count;
  178. uint64_t m_val;
  179. operator size_t() const { return (size_t)m_val; }
  180. bool operator== (const counted_obj& rhs) const { return m_val == rhs.m_val; }
  181. bool operator== (const uint32_t rhs) const { return m_val == rhs; }
  182. };
  183. uint32_t counted_obj::m_count;
  184. static uint32_t urand32()
  185. {
  186. uint32_t a = rand();
  187. uint32_t b = rand() << 15;
  188. uint32_t c = rand() << (32 - 15);
  189. return a ^ b ^ c;
  190. }
  191. static int irand32(int l, int h)
  192. {
  193. assert(l < h);
  194. if (l >= h)
  195. return l;
  196. uint32_t range = static_cast<uint32_t>(h - l);
  197. uint32_t rnd = urand32();
  198. uint32_t rnd_range = static_cast<uint32_t>((((uint64_t)range) * ((uint64_t)rnd)) >> 32U);
  199. int result = l + rnd_range;
  200. assert((result >= l) && (result < h));
  201. return result;
  202. }
  203. void hash_map_test()
  204. {
  205. {
  206. basisu::hash_map<uint32_t> s;
  207. uint_vec k;
  208. for (uint32_t i = 0; i < 1000000; i++)
  209. {
  210. s.insert(i);
  211. k.push_back(i);
  212. }
  213. for (uint32_t i = 0; i < k.size(); i++)
  214. {
  215. uint32_t r = rand() ^ (rand() << 15);
  216. uint32_t j = i + (r % (k.size() - i));
  217. std::swap(k[i], k[j]);
  218. }
  219. basisu::hash_map<uint32_t> s1(s);
  220. for (uint32_t i = 0; i < 1000000; i++)
  221. {
  222. auto res = s.find(i);
  223. HASHMAP_TEST_VERIFY(res != s.end());
  224. HASHMAP_TEST_VERIFY(res->first == i);
  225. s.erase(i);
  226. }
  227. for (uint32_t it = 0; it < 1000000; it++)
  228. {
  229. uint32_t i = k[it];
  230. auto res = s1.find(i);
  231. HASHMAP_TEST_VERIFY(res != s.end());
  232. HASHMAP_TEST_VERIFY(res->first == i);
  233. s1.erase(i);
  234. }
  235. for (uint32_t i = 0; i < 1000000; i++)
  236. {
  237. auto res = s.find(i);
  238. HASHMAP_TEST_VERIFY(res == s.end());
  239. auto res1 = s1.find(i);
  240. HASHMAP_TEST_VERIFY(res1 == s1.end());
  241. }
  242. HASHMAP_TEST_VERIFY(s.empty());
  243. HASHMAP_TEST_VERIFY(s1.empty());
  244. }
  245. {
  246. typedef basisu::hash_map< uint32_t, basisu::vector<uint32_t> > hm;
  247. hm q;
  248. basisu::vector<uint32_t> a, b;
  249. a.push_back(1);
  250. b.push_back(2);
  251. b.push_back(3);
  252. basisu::vector<uint32_t> c(b);
  253. hm::insert_result ir;
  254. q.try_insert(ir, 1, std::move(a));
  255. q.try_insert(ir, 2, std::move(b));
  256. q.try_insert(ir, std::make_pair(3, c));
  257. }
  258. {
  259. typedef basisu::hash_map<counted_obj, counted_obj> my_hash_map;
  260. my_hash_map m;
  261. counted_obj a, b;
  262. m.insert(std::move(a), std::move(b));
  263. }
  264. {
  265. basisu::hash_map<uint64_t, uint64_t> k;
  266. basisu::hash_map<uint64_t, uint64_t> l;
  267. std::swap(k, l);
  268. k.begin();
  269. k.end();
  270. k.clear();
  271. k.empty();
  272. k.erase(0);
  273. k.insert(0, 1);
  274. k.find(0);
  275. k.get_equals();
  276. k.get_hasher();
  277. k.get_table_size();
  278. k.reset();
  279. k.reserve(1);
  280. k = l;
  281. k.set_equals(l.get_equals());
  282. k.set_hasher(l.get_hasher());
  283. k.get_table_size();
  284. }
  285. uint32_t seed = 0;
  286. for (; ; )
  287. {
  288. seed++;
  289. typedef basisu::hash_map<counted_obj, counted_obj> my_hash_map;
  290. my_hash_map m;
  291. const uint32_t n = irand32(1, 100000);
  292. printf("%u\n", n);
  293. srand(seed); // r1.seed(seed);
  294. basisu::vector<int> q;
  295. uint32_t count = 0;
  296. for (uint32_t i = 0; i < n; i++)
  297. {
  298. uint32_t v = urand32() & 0x7FFFFFFF;
  299. my_hash_map::insert_result res = m.insert(counted_obj(v), counted_obj(v ^ 0xdeadbeef));
  300. if (res.second)
  301. {
  302. count++;
  303. q.push_back(v);
  304. }
  305. }
  306. HASHMAP_TEST_VERIFY(m.size() == count);
  307. srand(seed);
  308. my_hash_map cm(m);
  309. m.clear();
  310. m = cm;
  311. cm.reset();
  312. for (uint32_t i = 0; i < n; i++)
  313. {
  314. uint32_t v = urand32() & 0x7FFFFFFF;
  315. my_hash_map::const_iterator it = m.find(counted_obj(v));
  316. HASHMAP_TEST_VERIFY(it != m.end());
  317. HASHMAP_TEST_VERIFY(it->first == v);
  318. HASHMAP_TEST_VERIFY(it->second == (v ^ 0xdeadbeef));
  319. }
  320. for (uint32_t t = 0; t < 2; t++)
  321. {
  322. const uint32_t nd = irand32(1, q.size_u32() + 1);
  323. for (uint32_t i = 0; i < nd; i++)
  324. {
  325. uint32_t p = irand32(0, q.size_u32());
  326. int k = q[p];
  327. if (k >= 0)
  328. {
  329. q[p] = -k - 1;
  330. bool s = m.erase(counted_obj(k));
  331. HASHMAP_TEST_VERIFY(s);
  332. }
  333. }
  334. typedef basisu::hash_map<uint32_t, empty_type> uint_hash_set;
  335. uint_hash_set s;
  336. for (uint32_t i = 0; i < q.size(); i++)
  337. {
  338. int v = q[i];
  339. if (v >= 0)
  340. {
  341. my_hash_map::const_iterator it = m.find(counted_obj(v));
  342. HASHMAP_TEST_VERIFY(it != m.end());
  343. HASHMAP_TEST_VERIFY(it->first == (uint32_t)v);
  344. HASHMAP_TEST_VERIFY(it->second == ((uint32_t)v ^ 0xdeadbeef));
  345. s.insert(v);
  346. }
  347. else
  348. {
  349. my_hash_map::const_iterator it = m.find(counted_obj(-v - 1));
  350. HASHMAP_TEST_VERIFY(it == m.end());
  351. }
  352. }
  353. uint32_t found_count = 0;
  354. for (my_hash_map::const_iterator it = m.begin(); it != m.end(); ++it)
  355. {
  356. HASHMAP_TEST_VERIFY(it->second == ((uint32_t)it->first ^ 0xdeadbeef));
  357. uint_hash_set::const_iterator fit(s.find((uint32_t)it->first));
  358. HASHMAP_TEST_VERIFY(fit != s.end());
  359. HASHMAP_TEST_VERIFY(fit->first == it->first);
  360. found_count++;
  361. }
  362. HASHMAP_TEST_VERIFY(found_count == s.size());
  363. }
  364. HASHMAP_TEST_VERIFY(counted_obj::m_count == m.size() * 2);
  365. }
  366. }
  367. #endif // BASISU_HASHMAP_TEST
  368. // String formatting
  369. bool fmt_variant::to_string(std::string& res, std::string& fmt) const
  370. {
  371. res.resize(0);
  372. // Scan for allowed formatting characters.
  373. for (size_t i = 0; i < fmt.size(); i++)
  374. {
  375. const char c = fmt[i];
  376. if (isdigit(c) || (c == '.') || (c == ' ') || (c == '#') || (c == '+') || (c == '-'))
  377. continue;
  378. if (isalpha(c))
  379. {
  380. if ((i + 1) == fmt.size())
  381. continue;
  382. }
  383. return false;
  384. }
  385. if (fmt.size() && (fmt.back() == 'c'))
  386. {
  387. if ((m_type == variant_type::cI32) || (m_type == variant_type::cU32))
  388. {
  389. if (m_u32 > 255)
  390. return false;
  391. // Explictly allowing caller to pass in a char of 0, which is ignored.
  392. if (m_u32)
  393. res.push_back((uint8_t)m_u32);
  394. return true;
  395. }
  396. else
  397. return false;
  398. }
  399. switch (m_type)
  400. {
  401. case variant_type::cInvalid:
  402. {
  403. return false;
  404. }
  405. case variant_type::cI32:
  406. {
  407. if (fmt.size())
  408. {
  409. int e = fmt.back();
  410. if (isalpha(e))
  411. {
  412. if ((e != 'x') && (e != 'X') && (e != 'i') && (e != 'd') && (e != 'u'))
  413. return false;
  414. }
  415. else
  416. {
  417. fmt += "i";
  418. }
  419. res = string_format((std::string("%") + fmt).c_str(), m_i32);
  420. }
  421. else
  422. {
  423. res = string_format("%i", m_i32);
  424. }
  425. break;
  426. }
  427. case variant_type::cU32:
  428. {
  429. if (fmt.size())
  430. {
  431. int e = fmt.back();
  432. if (isalpha(e))
  433. {
  434. if ((e != 'x') && (e != 'X') && (e != 'i') && (e != 'd') && (e != 'u'))
  435. return false;
  436. }
  437. else
  438. {
  439. fmt += "u";
  440. }
  441. res = string_format((std::string("%") + fmt).c_str(), m_u32);
  442. }
  443. else
  444. {
  445. res = string_format("%u", m_u32);
  446. }
  447. break;
  448. }
  449. case variant_type::cI64:
  450. {
  451. if (fmt.size())
  452. {
  453. int e = fmt.back();
  454. if (isalpha(e))
  455. {
  456. if (e == 'x')
  457. {
  458. fmt.pop_back();
  459. fmt += PRIx64;
  460. }
  461. else if (e == 'X')
  462. {
  463. fmt.pop_back();
  464. fmt += PRIX64;
  465. }
  466. else
  467. return false;
  468. }
  469. else
  470. {
  471. fmt += PRId64;
  472. }
  473. res = string_format((std::string("%") + fmt).c_str(), m_i64);
  474. }
  475. else
  476. {
  477. res = string_format("%" PRId64, m_i64);
  478. }
  479. break;
  480. }
  481. case variant_type::cU64:
  482. {
  483. if (fmt.size())
  484. {
  485. int e = fmt.back();
  486. if (isalpha(e))
  487. {
  488. if (e == 'x')
  489. {
  490. fmt.pop_back();
  491. fmt += PRIx64;
  492. }
  493. else if (e == 'X')
  494. {
  495. fmt.pop_back();
  496. fmt += PRIX64;
  497. }
  498. else
  499. return false;
  500. }
  501. else
  502. {
  503. fmt += PRIu64;
  504. }
  505. res = string_format((std::string("%") + fmt).c_str(), m_u64);
  506. }
  507. else
  508. {
  509. res = string_format("%" PRIu64, m_u64);
  510. }
  511. break;
  512. }
  513. case variant_type::cFlt:
  514. {
  515. if (fmt.size())
  516. {
  517. int e = fmt.back();
  518. if (isalpha(e))
  519. {
  520. if ((e != 'f') && (e != 'g') && (e != 'e') && (e != 'E'))
  521. return false;
  522. }
  523. else
  524. {
  525. fmt += "f";
  526. }
  527. res = string_format((std::string("%") + fmt).c_str(), m_flt);
  528. }
  529. else
  530. {
  531. res = string_format("%f", m_flt);
  532. }
  533. break;
  534. }
  535. case variant_type::cDbl:
  536. {
  537. if (fmt.size())
  538. {
  539. int e = fmt.back();
  540. if (isalpha(e))
  541. {
  542. if ((e != 'f') && (e != 'g') && (e != 'e') && (e != 'E'))
  543. return false;
  544. }
  545. else
  546. {
  547. fmt += "f";
  548. }
  549. res = string_format((std::string("%") + fmt).c_str(), m_dbl);
  550. }
  551. else
  552. {
  553. res = string_format("%f", m_dbl);
  554. }
  555. break;
  556. }
  557. case variant_type::cStrPtr:
  558. {
  559. if (fmt.size())
  560. return false;
  561. if (!m_pStr)
  562. return false;
  563. res = m_pStr;
  564. break;
  565. }
  566. case variant_type::cBool:
  567. {
  568. if (fmt.size())
  569. return false;
  570. res = m_bool ? "true" : "false";
  571. break;
  572. }
  573. case variant_type::cStdStr:
  574. {
  575. if (fmt.size())
  576. return false;
  577. res = m_str;
  578. break;
  579. }
  580. default:
  581. {
  582. return false;
  583. }
  584. }
  585. return true;
  586. }
  587. bool fmt_variants(std::string& res, const char* pFmt, const fmt_variant_vec& variants)
  588. {
  589. res.resize(0);
  590. // Must specify a format string
  591. if (!pFmt)
  592. {
  593. assert(0);
  594. return false;
  595. }
  596. // Check format string's length
  597. const size_t fmt_len = strlen(pFmt);
  598. if (!fmt_len)
  599. {
  600. if (variants.size())
  601. {
  602. assert(0);
  603. return false;
  604. }
  605. return true;
  606. }
  607. // Wildly estimate output length
  608. res.reserve(fmt_len + 32);
  609. std::string var_fmt;
  610. var_fmt.reserve(16);
  611. std::string tmp;
  612. tmp.reserve(16);
  613. size_t variant_index = 0;
  614. bool inside_brackets = false;
  615. const char* p = pFmt;
  616. while (*p)
  617. {
  618. const uint8_t c = *p++;
  619. if (inside_brackets)
  620. {
  621. if (c == '}')
  622. {
  623. inside_brackets = false;
  624. if (variant_index >= variants.size())
  625. {
  626. assert(0);
  627. return false;
  628. }
  629. if (!variants[variant_index].to_string(tmp, var_fmt))
  630. {
  631. assert(0);
  632. return false;
  633. }
  634. res += tmp;
  635. variant_index++;
  636. }
  637. else
  638. {
  639. // Check for forbidden formatting characters.
  640. if ((c == '*') || (c == 'n') || (c == '%'))
  641. {
  642. assert(0);
  643. return false;
  644. }
  645. var_fmt.push_back(c);
  646. }
  647. }
  648. else if (c == '{')
  649. {
  650. // Check for escaped '{'
  651. if (*p == '{')
  652. {
  653. res.push_back((char)c);
  654. p++;
  655. }
  656. else
  657. {
  658. inside_brackets = true;
  659. var_fmt.resize(0);
  660. }
  661. }
  662. else
  663. {
  664. res.push_back((char)c);
  665. }
  666. }
  667. if (inside_brackets)
  668. {
  669. assert(0);
  670. return false;
  671. }
  672. if (variant_index != variants.size())
  673. {
  674. assert(0);
  675. return false;
  676. }
  677. return true;
  678. }
  679. } // namespace basisu