2
0

basisu_containers_impl.h 15 KB

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