CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/basis_universal/basisu_containers_impl.h
Views: 1401
// basisu_containers_impl.h1// Do not include directly23#ifdef _MSC_VER4#pragma warning (disable:4127) // warning C4127: conditional expression is constant5#endif67namespace basisu8{9bool elemental_vector::increase_capacity(uint32_t min_new_capacity, bool grow_hint, uint32_t element_size, object_mover pMover, bool nofail)10{11assert(m_size <= m_capacity);1213if (sizeof(void *) == sizeof(uint64_t))14assert(min_new_capacity < (0x400000000ULL / element_size));15else16assert(min_new_capacity < (0x7FFF0000U / element_size));1718if (m_capacity >= min_new_capacity)19return true;2021size_t new_capacity = min_new_capacity;22if ((grow_hint) && (!helpers::is_power_of_2((uint64_t)new_capacity)))23{24new_capacity = (size_t)helpers::next_pow2((uint64_t)new_capacity);2526assert(new_capacity && (new_capacity > m_capacity));2728if (new_capacity < min_new_capacity)29{30if (nofail)31return false;32fprintf(stderr, "vector too large\n");33abort();34}35}3637const size_t desired_size = element_size * new_capacity;38size_t actual_size = 0;39if (!pMover)40{41void* new_p = realloc(m_p, desired_size);42if (!new_p)43{44if (nofail)45return false;4647char buf[256];48#ifdef _MSC_VER49sprintf_s(buf, sizeof(buf), "vector: realloc() failed allocating %u bytes", (uint32_t)desired_size);50#else51sprintf(buf, "vector: realloc() failed allocating %u bytes", (uint32_t)desired_size);52#endif53fprintf(stderr, "%s", buf);54abort();55}5657#if BASISU_VECTOR_DETERMINISTIC58actual_size = desired_size;59#elif defined(_MSC_VER)60actual_size = _msize(new_p);61#elif HAS_MALLOC_USABLE_SIZE62actual_size = malloc_usable_size(new_p);63#else64actual_size = desired_size;65#endif66m_p = new_p;67}68else69{70void* new_p = malloc(desired_size);71if (!new_p)72{73if (nofail)74return false;7576char buf[256];77#ifdef _MSC_VER78sprintf_s(buf, sizeof(buf), "vector: malloc() failed allocating %u bytes", (uint32_t)desired_size);79#else80sprintf(buf, "vector: malloc() failed allocating %u bytes", (uint32_t)desired_size);81#endif82fprintf(stderr, "%s", buf);83abort();84}8586#if BASISU_VECTOR_DETERMINISTIC87actual_size = desired_size;88#elif defined(_MSC_VER)89actual_size = _msize(new_p);90#elif HAS_MALLOC_USABLE_SIZE91actual_size = malloc_usable_size(new_p);92#else93actual_size = desired_size;94#endif9596(*pMover)(new_p, m_p, m_size);9798if (m_p)99free(m_p);100101m_p = new_p;102}103104if (actual_size > desired_size)105m_capacity = static_cast<uint32_t>(actual_size / element_size);106else107m_capacity = static_cast<uint32_t>(new_capacity);108109return true;110}111112#if BASISU_HASHMAP_TEST113114#define HASHMAP_TEST_VERIFY(c) do { if (!(c)) handle_hashmap_test_verify_failure(__LINE__); } while(0)115116static void handle_hashmap_test_verify_failure(int line)117{118fprintf(stderr, "HASHMAP_TEST_VERIFY() faild on line %i\n", line);119abort();120}121122class counted_obj123{124public:125counted_obj(uint32_t v = 0) :126m_val(v)127{128m_count++;129}130131counted_obj(const counted_obj& obj) :132m_val(obj.m_val)133{134m_count++;135}136137~counted_obj()138{139assert(m_count > 0);140m_count--;141}142143static uint32_t m_count;144145uint32_t m_val;146147operator size_t() const { return m_val; }148149bool operator== (const counted_obj& rhs) const { return m_val == rhs.m_val; }150bool operator== (const uint32_t rhs) const { return m_val == rhs; }151152};153154uint32_t counted_obj::m_count;155156static uint32_t urand32()157{158uint32_t a = rand();159uint32_t b = rand() << 15;160uint32_t c = rand() << (32 - 15);161return a ^ b ^ c;162}163164static int irand32(int l, int h)165{166assert(l < h);167if (l >= h)168return l;169170uint32_t range = static_cast<uint32_t>(h - l);171172uint32_t rnd = urand32();173174uint32_t rnd_range = static_cast<uint32_t>((((uint64_t)range) * ((uint64_t)rnd)) >> 32U);175176int result = l + rnd_range;177assert((result >= l) && (result < h));178return result;179}180181void hash_map_test()182{183{184basisu::hash_map<uint64_t, uint64_t> k;185basisu::hash_map<uint64_t, uint64_t> l;186std::swap(k, l);187188k.begin();189k.end();190k.clear();191k.empty();192k.erase(0);193k.insert(0, 1);194k.find(0);195k.get_equals();196k.get_hasher();197k.get_table_size();198k.reset();199k.reserve(1);200k = l;201k.set_equals(l.get_equals());202k.set_hasher(l.get_hasher());203k.get_table_size();204}205206uint32_t seed = 0;207for (; ; )208{209seed++;210211typedef basisu::hash_map<counted_obj, counted_obj> my_hash_map;212my_hash_map m;213214const uint32_t n = irand32(0, 100000);215216printf("%u\n", n);217218srand(seed); // r1.seed(seed);219220basisu::vector<int> q;221222uint32_t count = 0;223for (uint32_t i = 0; i < n; i++)224{225uint32_t v = urand32() & 0x7FFFFFFF;226my_hash_map::insert_result res = m.insert(counted_obj(v), counted_obj(v ^ 0xdeadbeef));227if (res.second)228{229count++;230q.push_back(v);231}232}233234HASHMAP_TEST_VERIFY(m.size() == count);235236srand(seed);237238my_hash_map cm(m);239m.clear();240m = cm;241cm.reset();242243for (uint32_t i = 0; i < n; i++)244{245uint32_t v = urand32() & 0x7FFFFFFF;246my_hash_map::const_iterator it = m.find(counted_obj(v));247HASHMAP_TEST_VERIFY(it != m.end());248HASHMAP_TEST_VERIFY(it->first == v);249HASHMAP_TEST_VERIFY(it->second == (v ^ 0xdeadbeef));250}251252for (uint32_t t = 0; t < 2; t++)253{254const uint32_t nd = irand32(1, q.size() + 1);255for (uint32_t i = 0; i < nd; i++)256{257uint32_t p = irand32(0, q.size());258259int k = q[p];260if (k >= 0)261{262q[p] = -k - 1;263264bool s = m.erase(counted_obj(k));265HASHMAP_TEST_VERIFY(s);266}267}268269typedef basisu::hash_map<uint32_t, empty_type> uint_hash_set;270uint_hash_set s;271272for (uint32_t i = 0; i < q.size(); i++)273{274int v = q[i];275276if (v >= 0)277{278my_hash_map::const_iterator it = m.find(counted_obj(v));279HASHMAP_TEST_VERIFY(it != m.end());280HASHMAP_TEST_VERIFY(it->first == (uint32_t)v);281HASHMAP_TEST_VERIFY(it->second == ((uint32_t)v ^ 0xdeadbeef));282283s.insert(v);284}285else286{287my_hash_map::const_iterator it = m.find(counted_obj(-v - 1));288HASHMAP_TEST_VERIFY(it == m.end());289}290}291292uint32_t found_count = 0;293for (my_hash_map::const_iterator it = m.begin(); it != m.end(); ++it)294{295HASHMAP_TEST_VERIFY(it->second == ((uint32_t)it->first ^ 0xdeadbeef));296297uint_hash_set::const_iterator fit(s.find((uint32_t)it->first));298HASHMAP_TEST_VERIFY(fit != s.end());299300HASHMAP_TEST_VERIFY(fit->first == it->first);301302found_count++;303}304305HASHMAP_TEST_VERIFY(found_count == s.size());306}307308HASHMAP_TEST_VERIFY(counted_obj::m_count == m.size() * 2);309}310}311312#endif // BASISU_HASHMAP_TEST313314} // namespace basisu315316317