basisu_containers_impl.h 15 KB

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