1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035 |
- // basisu_containers.h
- #pragma once
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdint.h>
- #include <assert.h>
- #include <algorithm>
- #if defined(__linux__) && !defined(ANDROID)
- // Only for malloc_usable_size() in basisu_containers_impl.h
- #include <malloc.h>
- #define HAS_MALLOC_USABLE_SIZE 1
- #endif
- // Set to 1 to always check vector operator[], front(), and back() even in release.
- #define BASISU_VECTOR_FORCE_CHECKING 0
- // If 1, the vector container will not query the CRT to get the size of resized memory blocks.
- #define BASISU_VECTOR_DETERMINISTIC 1
- #ifdef _MSC_VER
- #define BASISU_FORCE_INLINE __forceinline
- #else
- #define BASISU_FORCE_INLINE inline
- #endif
- namespace basisu
- {
- enum { cInvalidIndex = -1 };
- namespace helpers
- {
- inline bool is_power_of_2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
- inline bool is_power_of_2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
- template<class T> const T& minimum(const T& a, const T& b) { return (b < a) ? b : a; }
- template<class T> const T& maximum(const T& a, const T& b) { return (a < b) ? b : a; }
- inline uint32_t floor_log2i(uint32_t v)
- {
- uint32_t l = 0;
- while (v > 1U)
- {
- v >>= 1;
- l++;
- }
- return l;
- }
- inline uint32_t next_pow2(uint32_t val)
- {
- val--;
- val |= val >> 16;
- val |= val >> 8;
- val |= val >> 4;
- val |= val >> 2;
- val |= val >> 1;
- return val + 1;
- }
- inline uint64_t next_pow2(uint64_t val)
- {
- val--;
- val |= val >> 32;
- val |= val >> 16;
- val |= val >> 8;
- val |= val >> 4;
- val |= val >> 2;
- val |= val >> 1;
- return val + 1;
- }
- } // namespace helpers
- template <typename T>
- inline T* construct(T* p)
- {
- return new (static_cast<void*>(p)) T;
- }
- template <typename T, typename U>
- inline T* construct(T* p, const U& init)
- {
- return new (static_cast<void*>(p)) T(init);
- }
- template <typename T>
- inline void construct_array(T* p, size_t n)
- {
- T* q = p + n;
- for (; p != q; ++p)
- new (static_cast<void*>(p)) T;
- }
- template <typename T, typename U>
- inline void construct_array(T* p, size_t n, const U& init)
- {
- T* q = p + n;
- for (; p != q; ++p)
- new (static_cast<void*>(p)) T(init);
- }
- template <typename T>
- inline void destruct(T* p)
- {
- (void)p;
- p->~T();
- }
- template <typename T> inline void destruct_array(T* p, size_t n)
- {
- T* q = p + n;
- for (; p != q; ++p)
- p->~T();
- }
- template<typename T> struct int_traits { enum { cMin = INT32_MIN, cMax = INT32_MAX, cSigned = true }; };
- template<> struct int_traits<int8_t> { enum { cMin = INT8_MIN, cMax = INT8_MAX, cSigned = true }; };
- template<> struct int_traits<int16_t> { enum { cMin = INT16_MIN, cMax = INT16_MAX, cSigned = true }; };
- template<> struct int_traits<int32_t> { enum { cMin = INT32_MIN, cMax = INT32_MAX, cSigned = true }; };
- template<> struct int_traits<uint8_t> { enum { cMin = 0, cMax = UINT8_MAX, cSigned = false }; };
- template<> struct int_traits<uint16_t> { enum { cMin = 0, cMax = UINT16_MAX, cSigned = false }; };
- template<> struct int_traits<uint32_t> { enum { cMin = 0, cMax = UINT32_MAX, cSigned = false }; };
- template<typename T>
- struct scalar_type
- {
- enum { cFlag = false };
- static inline void construct(T* p) { basisu::construct(p); }
- static inline void construct(T* p, const T& init) { basisu::construct(p, init); }
- static inline void construct_array(T* p, size_t n) { basisu::construct_array(p, n); }
- static inline void destruct(T* p) { basisu::destruct(p); }
- static inline void destruct_array(T* p, size_t n) { basisu::destruct_array(p, n); }
- };
- template<typename T> struct scalar_type<T*>
- {
- enum { cFlag = true };
- static inline void construct(T** p) { memset(p, 0, sizeof(T*)); }
- static inline void construct(T** p, T* init) { *p = init; }
- static inline void construct_array(T** p, size_t n) { memset(p, 0, sizeof(T*) * n); }
- static inline void destruct(T** p) { p; }
- static inline void destruct_array(T** p, size_t n) { p, n; }
- };
- #define BASISU_DEFINE_BUILT_IN_TYPE(X) \
- template<> struct scalar_type<X> { \
- enum { cFlag = true }; \
- static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \
- static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \
- static inline void construct_array(X* p, size_t n) { memset(p, 0, sizeof(X) * n); } \
- static inline void destruct(X* p) { p; } \
- static inline void destruct_array(X* p, size_t n) { p, n; } };
- BASISU_DEFINE_BUILT_IN_TYPE(bool)
- BASISU_DEFINE_BUILT_IN_TYPE(char)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned char)
- BASISU_DEFINE_BUILT_IN_TYPE(short)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned short)
- BASISU_DEFINE_BUILT_IN_TYPE(int)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned int)
- BASISU_DEFINE_BUILT_IN_TYPE(long)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned long)
- #ifdef __GNUC__
- BASISU_DEFINE_BUILT_IN_TYPE(long long)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned long long)
- #else
- BASISU_DEFINE_BUILT_IN_TYPE(__int64)
- BASISU_DEFINE_BUILT_IN_TYPE(unsigned __int64)
- #endif
- BASISU_DEFINE_BUILT_IN_TYPE(float)
- BASISU_DEFINE_BUILT_IN_TYPE(double)
- BASISU_DEFINE_BUILT_IN_TYPE(long double)
- #undef BASISU_DEFINE_BUILT_IN_TYPE
- template<typename T>
- struct bitwise_movable { enum { cFlag = false }; };
- #define BASISU_DEFINE_BITWISE_MOVABLE(Q) template<> struct bitwise_movable<Q> { enum { cFlag = true }; };
- template<typename T>
- struct bitwise_copyable { enum { cFlag = false }; };
- #define BASISU_DEFINE_BITWISE_COPYABLE(Q) template<> struct bitwise_copyable<Q> { enum { cFlag = true }; };
- #define BASISU_IS_POD(T) __is_pod(T)
- #define BASISU_IS_SCALAR_TYPE(T) (scalar_type<T>::cFlag)
- #if !defined(BASISU_HAVE_STD_TRIVIALLY_COPYABLE) && defined(__GNUC__) && __GNUC__<5
- //#define BASISU_IS_TRIVIALLY_COPYABLE(...) __has_trivial_copy(__VA_ARGS__)
- #define BASISU_IS_TRIVIALLY_COPYABLE(...) __is_trivially_copyable(__VA_ARGS__)
- #else
- #define BASISU_IS_TRIVIALLY_COPYABLE(...) std::is_trivially_copyable<__VA_ARGS__>::value
- #endif
- // TODO: clean this up
- #define BASISU_IS_BITWISE_COPYABLE(T) (BASISU_IS_SCALAR_TYPE(T) || BASISU_IS_POD(T) || BASISU_IS_TRIVIALLY_COPYABLE(T) || (bitwise_copyable<T>::cFlag))
- #define BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) (BASISU_IS_BITWISE_COPYABLE(T) || (bitwise_movable<T>::cFlag))
- #define BASISU_HAS_DESTRUCTOR(T) ((!scalar_type<T>::cFlag) && (!__is_pod(T)))
- typedef char(&yes_t)[1];
- typedef char(&no_t)[2];
- template <class U> yes_t class_test(int U::*);
- template <class U> no_t class_test(...);
- template <class T> struct is_class
- {
- enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) };
- };
- template <typename T> struct is_pointer
- {
- enum { value = false };
- };
- template <typename T> struct is_pointer<T*>
- {
- enum { value = true };
- };
- struct empty_type { };
- BASISU_DEFINE_BITWISE_COPYABLE(empty_type);
- BASISU_DEFINE_BITWISE_MOVABLE(empty_type);
- template<typename T> struct rel_ops
- {
- friend bool operator!=(const T& x, const T& y) { return (!(x == y)); }
- friend bool operator> (const T& x, const T& y) { return (y < x); }
- friend bool operator<=(const T& x, const T& y) { return (!(y < x)); }
- friend bool operator>=(const T& x, const T& y) { return (!(x < y)); }
- };
- struct elemental_vector
- {
- void* m_p;
- uint32_t m_size;
- uint32_t m_capacity;
- typedef void (*object_mover)(void* pDst, void* pSrc, uint32_t num);
- bool increase_capacity(uint32_t min_new_capacity, bool grow_hint, uint32_t element_size, object_mover pRelocate, bool nofail);
- };
- template<typename T>
- class vector : public rel_ops< vector<T> >
- {
- public:
- typedef T* iterator;
- typedef const T* const_iterator;
- typedef T value_type;
- typedef T& reference;
- typedef const T& const_reference;
- typedef T* pointer;
- typedef const T* const_pointer;
- inline vector() :
- m_p(NULL),
- m_size(0),
- m_capacity(0)
- {
- }
- inline vector(uint32_t n, const T& init) :
- m_p(NULL),
- m_size(0),
- m_capacity(0)
- {
- increase_capacity(n, false);
- construct_array(m_p, n, init);
- m_size = n;
- }
- inline vector(const vector& other) :
- m_p(NULL),
- m_size(0),
- m_capacity(0)
- {
- increase_capacity(other.m_size, false);
- m_size = other.m_size;
- if (BASISU_IS_BITWISE_COPYABLE(T))
- {
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wclass-memaccess"
- #endif
- #endif
- if ((m_p) && (other.m_p))
- memcpy(m_p, other.m_p, m_size * sizeof(T));
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
- #endif
- }
- else
- {
- T* pDst = m_p;
- const T* pSrc = other.m_p;
- for (uint32_t i = m_size; i > 0; i--)
- construct(pDst++, *pSrc++);
- }
- }
- inline explicit vector(size_t size) :
- m_p(NULL),
- m_size(0),
- m_capacity(0)
- {
- resize(size);
- }
- inline ~vector()
- {
- if (m_p)
- {
- scalar_type<T>::destruct_array(m_p, m_size);
- free(m_p);
- }
- }
- inline vector& operator= (const vector& other)
- {
- if (this == &other)
- return *this;
- if (m_capacity >= other.m_size)
- resize(0);
- else
- {
- clear();
- increase_capacity(other.m_size, false);
- }
- if (BASISU_IS_BITWISE_COPYABLE(T))
- {
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wclass-memaccess"
- #endif
- #endif
- if ((m_p) && (other.m_p))
- memcpy(m_p, other.m_p, other.m_size * sizeof(T));
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
- #endif
- }
- else
- {
- T* pDst = m_p;
- const T* pSrc = other.m_p;
- for (uint32_t i = other.m_size; i > 0; i--)
- construct(pDst++, *pSrc++);
- }
- m_size = other.m_size;
- return *this;
- }
- BASISU_FORCE_INLINE const T* begin() const { return m_p; }
- BASISU_FORCE_INLINE T* begin() { return m_p; }
- BASISU_FORCE_INLINE const T* end() const { return m_p + m_size; }
- BASISU_FORCE_INLINE T* end() { return m_p + m_size; }
- BASISU_FORCE_INLINE bool empty() const { return !m_size; }
- BASISU_FORCE_INLINE uint32_t size() const { return m_size; }
- BASISU_FORCE_INLINE uint32_t size_in_bytes() const { return m_size * sizeof(T); }
- BASISU_FORCE_INLINE uint32_t capacity() const { return m_capacity; }
- // operator[] will assert on out of range indices, but in final builds there is (and will never be) any range checking on this method.
- //BASISU_FORCE_INLINE const T& operator[] (uint32_t i) const { assert(i < m_size); return m_p[i]; }
- //BASISU_FORCE_INLINE T& operator[] (uint32_t i) { assert(i < m_size); return m_p[i]; }
-
- #if !BASISU_VECTOR_FORCE_CHECKING
- BASISU_FORCE_INLINE const T& operator[] (size_t i) const { assert(i < m_size); return m_p[i]; }
- BASISU_FORCE_INLINE T& operator[] (size_t i) { assert(i < m_size); return m_p[i]; }
- #else
- BASISU_FORCE_INLINE const T& operator[] (size_t i) const
- {
- if (i >= m_size)
- {
- fprintf(stderr, "operator[] invalid index: %u, max entries %u, type size %u\n", (uint32_t)i, m_size, (uint32_t)sizeof(T));
- abort();
- }
- return m_p[i];
- }
- BASISU_FORCE_INLINE T& operator[] (size_t i)
- {
- if (i >= m_size)
- {
- fprintf(stderr, "operator[] invalid index: %u, max entries %u, type size %u\n", (uint32_t)i, m_size, (uint32_t)sizeof(T));
- abort();
- }
- return m_p[i];
- }
- #endif
- // at() always includes range checking, even in final builds, unlike operator [].
- // The first element is returned if the index is out of range.
- BASISU_FORCE_INLINE const T& at(size_t i) const { assert(i < m_size); return (i >= m_size) ? m_p[0] : m_p[i]; }
- BASISU_FORCE_INLINE T& at(size_t i) { assert(i < m_size); return (i >= m_size) ? m_p[0] : m_p[i]; }
-
- #if !BASISU_VECTOR_FORCE_CHECKING
- BASISU_FORCE_INLINE const T& front() const { assert(m_size); return m_p[0]; }
- BASISU_FORCE_INLINE T& front() { assert(m_size); return m_p[0]; }
- BASISU_FORCE_INLINE const T& back() const { assert(m_size); return m_p[m_size - 1]; }
- BASISU_FORCE_INLINE T& back() { assert(m_size); return m_p[m_size - 1]; }
- #else
- BASISU_FORCE_INLINE const T& front() const
- {
- if (!m_size)
- {
- fprintf(stderr, "front: vector is empty, type size %u\n", (uint32_t)sizeof(T));
- abort();
- }
- return m_p[0];
- }
- BASISU_FORCE_INLINE T& front()
- {
- if (!m_size)
- {
- fprintf(stderr, "front: vector is empty, type size %u\n", (uint32_t)sizeof(T));
- abort();
- }
- return m_p[0];
- }
- BASISU_FORCE_INLINE const T& back() const
- {
- if(!m_size)
- {
- fprintf(stderr, "back: vector is empty, type size %u\n", (uint32_t)sizeof(T));
- abort();
- }
- return m_p[m_size - 1];
- }
- BASISU_FORCE_INLINE T& back()
- {
- if (!m_size)
- {
- fprintf(stderr, "back: vector is empty, type size %u\n", (uint32_t)sizeof(T));
- abort();
- }
- return m_p[m_size - 1];
- }
- #endif
- BASISU_FORCE_INLINE const T* get_ptr() const { return m_p; }
- BASISU_FORCE_INLINE T* get_ptr() { return m_p; }
- BASISU_FORCE_INLINE const T* data() const { return m_p; }
- BASISU_FORCE_INLINE T* data() { return m_p; }
- // clear() sets the container to empty, then frees the allocated block.
- inline void clear()
- {
- if (m_p)
- {
- scalar_type<T>::destruct_array(m_p, m_size);
- free(m_p);
- m_p = NULL;
- m_size = 0;
- m_capacity = 0;
- }
- }
- inline void clear_no_destruction()
- {
- if (m_p)
- {
- free(m_p);
- m_p = NULL;
- m_size = 0;
- m_capacity = 0;
- }
- }
- inline void reserve(size_t new_capacity_size_t)
- {
- if (new_capacity_size_t > UINT32_MAX)
- {
- assert(0);
- return;
- }
- uint32_t new_capacity = (uint32_t)new_capacity_size_t;
- if (new_capacity > m_capacity)
- increase_capacity(new_capacity, false);
- else if (new_capacity < m_capacity)
- {
- // Must work around the lack of a "decrease_capacity()" method.
- // This case is rare enough in practice that it's probably not worth implementing an optimized in-place resize.
- vector tmp;
- tmp.increase_capacity(helpers::maximum(m_size, new_capacity), false);
- tmp = *this;
- swap(tmp);
- }
- }
- inline bool try_reserve(size_t new_capacity_size_t)
- {
- if (new_capacity_size_t > UINT32_MAX)
- {
- assert(0);
- return false;
- }
- uint32_t new_capacity = (uint32_t)new_capacity_size_t;
- if (new_capacity > m_capacity)
- {
- if (!increase_capacity(new_capacity, false, true))
- return false;
- }
- else if (new_capacity < m_capacity)
- {
- // Must work around the lack of a "decrease_capacity()" method.
- // This case is rare enough in practice that it's probably not worth implementing an optimized in-place resize.
- vector tmp;
- if (!tmp.increase_capacity(helpers::maximum(m_size, new_capacity), false, true))
- return false;
- tmp = *this;
- swap(tmp);
- }
- return true;
- }
- // resize(0) sets the container to empty, but does not free the allocated block.
- inline void resize(size_t new_size_size_t, bool grow_hint = false)
- {
- if (new_size_size_t > UINT32_MAX)
- {
- assert(0);
- return;
- }
- uint32_t new_size = (uint32_t)new_size_size_t;
- if (m_size != new_size)
- {
- if (new_size < m_size)
- scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
- else
- {
- if (new_size > m_capacity)
- increase_capacity(new_size, (new_size == (m_size + 1)) || grow_hint);
- scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
- }
- m_size = new_size;
- }
- }
- inline bool try_resize(size_t new_size_size_t, bool grow_hint = false)
- {
- if (new_size_size_t > UINT32_MAX)
- {
- assert(0);
- return false;
- }
- uint32_t new_size = (uint32_t)new_size_size_t;
- if (m_size != new_size)
- {
- if (new_size < m_size)
- scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
- else
- {
- if (new_size > m_capacity)
- {
- if (!increase_capacity(new_size, (new_size == (m_size + 1)) || grow_hint, true))
- return false;
- }
- scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
- }
- m_size = new_size;
- }
- return true;
- }
- // If size >= capacity/2, reset() sets the container's size to 0 but doesn't free the allocated block (because the container may be similarly loaded in the future).
- // Otherwise it blows away the allocated block. See http://www.codercorner.com/blog/?p=494
- inline void reset()
- {
- if (m_size >= (m_capacity >> 1))
- resize(0);
- else
- clear();
- }
- inline T* enlarge(uint32_t i)
- {
- uint32_t cur_size = m_size;
- resize(cur_size + i, true);
- return get_ptr() + cur_size;
- }
- inline T* try_enlarge(uint32_t i)
- {
- uint32_t cur_size = m_size;
- if (!try_resize(cur_size + i, true))
- return NULL;
- return get_ptr() + cur_size;
- }
- BASISU_FORCE_INLINE void push_back(const T& obj)
- {
- assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
- if (m_size >= m_capacity)
- increase_capacity(m_size + 1, true);
- scalar_type<T>::construct(m_p + m_size, obj);
- m_size++;
- }
- inline bool try_push_back(const T& obj)
- {
- assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
- if (m_size >= m_capacity)
- {
- if (!increase_capacity(m_size + 1, true, true))
- return false;
- }
- scalar_type<T>::construct(m_p + m_size, obj);
- m_size++;
- return true;
- }
- inline void push_back_value(T obj)
- {
- if (m_size >= m_capacity)
- increase_capacity(m_size + 1, true);
- scalar_type<T>::construct(m_p + m_size, obj);
- m_size++;
- }
- inline void pop_back()
- {
- assert(m_size);
- if (m_size)
- {
- m_size--;
- scalar_type<T>::destruct(&m_p[m_size]);
- }
- }
- inline void insert(uint32_t index, const T* p, uint32_t n)
- {
- assert(index <= m_size);
- if (!n)
- return;
- const uint32_t orig_size = m_size;
- resize(m_size + n, true);
- const uint32_t num_to_move = orig_size - index;
- if (BASISU_IS_BITWISE_COPYABLE(T))
- {
- // This overwrites the destination object bits, but bitwise copyable means we don't need to worry about destruction.
- memmove(m_p + index + n, m_p + index, sizeof(T) * num_to_move);
- }
- else
- {
- const T* pSrc = m_p + orig_size - 1;
- T* pDst = const_cast<T*>(pSrc) + n;
- for (uint32_t i = 0; i < num_to_move; i++)
- {
- assert((pDst - m_p) < (int)m_size);
- *pDst-- = *pSrc--;
- }
- }
- T* pDst = m_p + index;
- if (BASISU_IS_BITWISE_COPYABLE(T))
- {
- // This copies in the new bits, overwriting the existing objects, which is OK for copyable types that don't need destruction.
- memcpy(pDst, p, sizeof(T) * n);
- }
- else
- {
- for (uint32_t i = 0; i < n; i++)
- {
- assert((pDst - m_p) < (int)m_size);
- *pDst++ = *p++;
- }
- }
- }
- inline void insert(T* p, const T& obj)
- {
- int64_t ofs = p - begin();
- if ((ofs < 0) || (ofs > UINT32_MAX))
- {
- assert(0);
- return;
- }
- insert((uint32_t)ofs, &obj, 1);
- }
- // push_front() isn't going to be very fast - it's only here for usability.
- inline void push_front(const T& obj)
- {
- insert(0, &obj, 1);
- }
- vector& append(const vector& other)
- {
- if (other.m_size)
- insert(m_size, &other[0], other.m_size);
- return *this;
- }
- vector& append(const T* p, uint32_t n)
- {
- if (n)
- insert(m_size, p, n);
- return *this;
- }
-
- inline void erase(uint32_t start, uint32_t n)
- {
- assert((start + n) <= m_size);
- if ((start + n) > m_size)
- return;
- if (!n)
- return;
- const uint32_t num_to_move = m_size - (start + n);
- T* pDst = m_p + start;
- const T* pSrc = m_p + start + n;
- if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T))
- {
- // This test is overly cautious.
- if ((!BASISU_IS_BITWISE_COPYABLE(T)) || (BASISU_HAS_DESTRUCTOR(T)))
- {
- // Type has been marked explictly as bitwise movable, which means we can move them around but they may need to be destructed.
- // First destroy the erased objects.
- scalar_type<T>::destruct_array(pDst, n);
- }
- // Copy "down" the objects to preserve, filling in the empty slots.
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wclass-memaccess"
- #endif
- #endif
- memmove(pDst, pSrc, num_to_move * sizeof(T));
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
- #endif
- }
- else
- {
- // Type is not bitwise copyable or movable.
- // Move them down one at a time by using the equals operator, and destroying anything that's left over at the end.
- T* pDst_end = pDst + num_to_move;
- while (pDst != pDst_end)
- *pDst++ = *pSrc++;
- scalar_type<T>::destruct_array(pDst_end, n);
- }
- m_size -= n;
- }
- inline void erase(uint32_t index)
- {
- erase(index, 1);
- }
- inline void erase(T* p)
- {
- assert((p >= m_p) && (p < (m_p + m_size)));
- erase(static_cast<uint32_t>(p - m_p));
- }
- inline void erase(T *pFirst, T *pEnd)
- {
- assert(pFirst <= pEnd);
- assert(pFirst >= begin() && pFirst <= end());
- assert(pEnd >= begin() && pEnd <= end());
- int64_t ofs = pFirst - begin();
- if ((ofs < 0) || (ofs > UINT32_MAX))
- {
- assert(0);
- return;
- }
- int64_t n = pEnd - pFirst;
- if ((n < 0) || (n > UINT32_MAX))
- {
- assert(0);
- return;
- }
- erase((uint32_t)ofs, (uint32_t)n);
- }
- void erase_unordered(uint32_t index)
- {
- assert(index < m_size);
- if ((index + 1) < m_size)
- (*this)[index] = back();
- pop_back();
- }
- inline bool operator== (const vector& rhs) const
- {
- if (m_size != rhs.m_size)
- return false;
- else if (m_size)
- {
- if (scalar_type<T>::cFlag)
- return memcmp(m_p, rhs.m_p, sizeof(T) * m_size) == 0;
- else
- {
- const T* pSrc = m_p;
- const T* pDst = rhs.m_p;
- for (uint32_t i = m_size; i; i--)
- if (!(*pSrc++ == *pDst++))
- return false;
- }
- }
- return true;
- }
- inline bool operator< (const vector& rhs) const
- {
- const uint32_t min_size = helpers::minimum(m_size, rhs.m_size);
- const T* pSrc = m_p;
- const T* pSrc_end = m_p + min_size;
- const T* pDst = rhs.m_p;
- while ((pSrc < pSrc_end) && (*pSrc == *pDst))
- {
- pSrc++;
- pDst++;
- }
- if (pSrc < pSrc_end)
- return *pSrc < *pDst;
- return m_size < rhs.m_size;
- }
- inline void swap(vector& other)
- {
- std::swap(m_p, other.m_p);
- std::swap(m_size, other.m_size);
- std::swap(m_capacity, other.m_capacity);
- }
- inline void sort()
- {
- std::sort(begin(), end());
- }
- inline void unique()
- {
- if (!empty())
- {
- sort();
- resize(std::unique(begin(), end()) - begin());
- }
- }
- inline void reverse()
- {
- uint32_t j = m_size >> 1;
- for (uint32_t i = 0; i < j; i++)
- std::swap(m_p[i], m_p[m_size - 1 - i]);
- }
- inline int find(const T& key) const
- {
- const T* p = m_p;
- const T* p_end = m_p + m_size;
- uint32_t index = 0;
- while (p != p_end)
- {
- if (key == *p)
- return index;
- p++;
- index++;
- }
- return cInvalidIndex;
- }
- inline int find_sorted(const T& key) const
- {
- if (m_size)
- {
- // Uniform binary search - Knuth Algorithm 6.2.1 U, unrolled twice.
- int i = ((m_size + 1) >> 1) - 1;
- int m = m_size;
- for (; ; )
- {
- assert(i >= 0 && i < (int)m_size);
- const T* pKey_i = m_p + i;
- int cmp = key < *pKey_i;
- #if defined(_DEBUG) || defined(DEBUG)
- int cmp2 = *pKey_i < key;
- assert((cmp != cmp2) || (key == *pKey_i));
- #endif
- if ((!cmp) && (key == *pKey_i)) return i;
- m >>= 1;
- if (!m) break;
- cmp = -cmp;
- i += (((m + 1) >> 1) ^ cmp) - cmp;
- if (i < 0)
- break;
- assert(i >= 0 && i < (int)m_size);
- pKey_i = m_p + i;
- cmp = key < *pKey_i;
- #if defined(_DEBUG) || defined(DEBUG)
- cmp2 = *pKey_i < key;
- assert((cmp != cmp2) || (key == *pKey_i));
- #endif
- if ((!cmp) && (key == *pKey_i)) return i;
- m >>= 1;
- if (!m) break;
- cmp = -cmp;
- i += (((m + 1) >> 1) ^ cmp) - cmp;
- if (i < 0)
- break;
- }
- }
- return cInvalidIndex;
- }
- template<typename Q>
- inline int find_sorted(const T& key, Q less_than) const
- {
- if (m_size)
- {
- // Uniform binary search - Knuth Algorithm 6.2.1 U, unrolled twice.
- int i = ((m_size + 1) >> 1) - 1;
- int m = m_size;
- for (; ; )
- {
- assert(i >= 0 && i < (int)m_size);
- const T* pKey_i = m_p + i;
- int cmp = less_than(key, *pKey_i);
- if ((!cmp) && (!less_than(*pKey_i, key))) return i;
- m >>= 1;
- if (!m) break;
- cmp = -cmp;
- i += (((m + 1) >> 1) ^ cmp) - cmp;
- if (i < 0)
- break;
- assert(i >= 0 && i < (int)m_size);
- pKey_i = m_p + i;
- cmp = less_than(key, *pKey_i);
- if ((!cmp) && (!less_than(*pKey_i, key))) return i;
- m >>= 1;
- if (!m) break;
- cmp = -cmp;
- i += (((m + 1) >> 1) ^ cmp) - cmp;
- if (i < 0)
- break;
- }
- }
- return cInvalidIndex;
- }
- inline uint32_t count_occurences(const T& key) const
- {
- uint32_t c = 0;
- const T* p = m_p;
- const T* p_end = m_p + m_size;
- while (p != p_end)
- {
- if (key == *p)
- c++;
- p++;
- }
- return c;
- }
- inline void set_all(const T& o)
- {
- if ((sizeof(T) == 1) && (scalar_type<T>::cFlag))
- {
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wclass-memaccess"
- #endif
- #endif
- memset(m_p, *reinterpret_cast<const uint8_t*>(&o), m_size);
- #ifndef __EMSCRIPTEN__
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
- #endif
- }
- else
- {
- T* pDst = m_p;
- T* pDst_end = pDst + m_size;
- while (pDst != pDst_end)
- *pDst++ = o;
- }
- }
- // Caller assumes ownership of the heap block associated with the container. Container is cleared.
- inline void* assume_ownership()
- {
- T* p = m_p;
- m_p = NULL;
- m_size = 0;
- m_capacity = 0;
- return p;
- }
- // Caller is granting ownership of the indicated heap block.
- // Block must have size constructed elements, and have enough room for capacity elements.
- // The block must have been allocated using malloc().
- // Important: This method is used in Basis Universal. If you change how this container allocates memory, you'll need to change any users of this method.
- inline bool grant_ownership(T* p, uint32_t size, uint32_t capacity)
- {
- // To prevent the caller from obviously shooting themselves in the foot.
- if (((p + capacity) > m_p) && (p < (m_p + m_capacity)))
- {
- // Can grant ownership of a block inside the container itself!
- assert(0);
- return false;
- }
- if (size > capacity)
- {
- assert(0);
- return false;
- }
- if (!p)
- {
- if (capacity)
- {
- assert(0);
- return false;
- }
- }
- else if (!capacity)
- {
- assert(0);
- return false;
- }
- clear();
- m_p = p;
- m_size = size;
- m_capacity = capacity;
- return true;
- }
- private:
- T* m_p;
- uint32_t m_size;
- uint32_t m_capacity;
- template<typename Q> struct is_vector { enum { cFlag = false }; };
- template<typename Q> struct is_vector< vector<Q> > { enum { cFlag = true }; };
- static void object_mover(void* pDst_void, void* pSrc_void, uint32_t num)
- {
- T* pSrc = static_cast<T*>(pSrc_void);
- T* const pSrc_end = pSrc + num;
- T* pDst = static_cast<T*>(pDst_void);
- while (pSrc != pSrc_end)
- {
- // placement new
- new (static_cast<void*>(pDst)) T(*pSrc);
- pSrc->~T();
- ++pSrc;
- ++pDst;
- }
- }
- inline bool increase_capacity(uint32_t min_new_capacity, bool grow_hint, bool nofail = false)
- {
- return reinterpret_cast<elemental_vector*>(this)->increase_capacity(
- min_new_capacity, grow_hint, sizeof(T),
- (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) || (is_vector<T>::cFlag)) ? NULL : object_mover, nofail);
- }
- };
- template<typename T> struct bitwise_movable< vector<T> > { enum { cFlag = true }; };
- // Hash map
- template <typename T>
- struct hasher
- {
- inline size_t operator() (const T& key) const { return static_cast<size_t>(key); }
- };
- template <typename T>
- struct equal_to
- {
- inline bool operator()(const T& a, const T& b) const { return a == b; }
- };
- // Important: The Hasher and Equals objects must be bitwise movable!
- template<typename Key, typename Value = empty_type, typename Hasher = hasher<Key>, typename Equals = equal_to<Key> >
- class hash_map
- {
- public:
- class iterator;
- class const_iterator;
-
- private:
- friend class iterator;
- friend class const_iterator;
- enum state
- {
- cStateInvalid = 0,
- cStateValid = 1
- };
- enum
- {
- cMinHashSize = 4U
- };
- public:
- typedef hash_map<Key, Value, Hasher, Equals> hash_map_type;
- typedef std::pair<Key, Value> value_type;
- typedef Key key_type;
- typedef Value referent_type;
- typedef Hasher hasher_type;
- typedef Equals equals_type;
- hash_map() :
- m_hash_shift(32), m_num_valid(0), m_grow_threshold(0)
- {
- }
- hash_map(const hash_map& other) :
- m_values(other.m_values),
- m_hash_shift(other.m_hash_shift),
- m_hasher(other.m_hasher),
- m_equals(other.m_equals),
- m_num_valid(other.m_num_valid),
- m_grow_threshold(other.m_grow_threshold)
- {
- }
- hash_map& operator= (const hash_map& other)
- {
- if (this == &other)
- return *this;
- clear();
- m_values = other.m_values;
- m_hash_shift = other.m_hash_shift;
- m_num_valid = other.m_num_valid;
- m_grow_threshold = other.m_grow_threshold;
- m_hasher = other.m_hasher;
- m_equals = other.m_equals;
- return *this;
- }
- inline ~hash_map()
- {
- clear();
- }
- const Equals& get_equals() const { return m_equals; }
- Equals& get_equals() { return m_equals; }
- void set_equals(const Equals& equals) { m_equals = equals; }
- const Hasher& get_hasher() const { return m_hasher; }
- Hasher& get_hasher() { return m_hasher; }
- void set_hasher(const Hasher& hasher) { m_hasher = hasher; }
- inline void clear()
- {
- if (!m_values.empty())
- {
- if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
- {
- node* p = &get_node(0);
- node* p_end = p + m_values.size();
- uint32_t num_remaining = m_num_valid;
- while (p != p_end)
- {
- if (p->state)
- {
- destruct_value_type(p);
- num_remaining--;
- if (!num_remaining)
- break;
- }
- p++;
- }
- }
- m_values.clear_no_destruction();
- m_hash_shift = 32;
- m_num_valid = 0;
- m_grow_threshold = 0;
- }
- }
- inline void reset()
- {
- if (!m_num_valid)
- return;
- if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
- {
- node* p = &get_node(0);
- node* p_end = p + m_values.size();
- uint32_t num_remaining = m_num_valid;
- while (p != p_end)
- {
- if (p->state)
- {
- destruct_value_type(p);
- p->state = cStateInvalid;
- num_remaining--;
- if (!num_remaining)
- break;
- }
- p++;
- }
- }
- else if (sizeof(node) <= 32)
- {
- memset(&m_values[0], 0, m_values.size_in_bytes());
- }
- else
- {
- node* p = &get_node(0);
- node* p_end = p + m_values.size();
- uint32_t num_remaining = m_num_valid;
- while (p != p_end)
- {
- if (p->state)
- {
- p->state = cStateInvalid;
- num_remaining--;
- if (!num_remaining)
- break;
- }
- p++;
- }
- }
- m_num_valid = 0;
- }
- inline uint32_t size()
- {
- return m_num_valid;
- }
- inline uint32_t get_table_size()
- {
- return m_values.size();
- }
- inline bool empty()
- {
- return !m_num_valid;
- }
- inline void reserve(uint32_t new_capacity)
- {
- uint64_t new_hash_size = helpers::maximum(1U, new_capacity);
- new_hash_size = new_hash_size * 2ULL;
- if (!helpers::is_power_of_2(new_hash_size))
- new_hash_size = helpers::next_pow2(new_hash_size);
- new_hash_size = helpers::maximum<uint64_t>(cMinHashSize, new_hash_size);
- new_hash_size = helpers::minimum<uint64_t>(0x80000000UL, new_hash_size);
- if (new_hash_size > m_values.size())
- rehash((uint32_t)new_hash_size);
- }
-
- class iterator
- {
- friend class hash_map<Key, Value, Hasher, Equals>;
- friend class hash_map<Key, Value, Hasher, Equals>::const_iterator;
- public:
- inline iterator() : m_pTable(NULL), m_index(0) { }
- inline iterator(hash_map_type& table, uint32_t index) : m_pTable(&table), m_index(index) { }
- inline iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
- inline iterator& operator= (const iterator& other)
- {
- m_pTable = other.m_pTable;
- m_index = other.m_index;
- return *this;
- }
- // post-increment
- inline iterator operator++(int)
- {
- iterator result(*this);
- ++*this;
- return result;
- }
- // pre-increment
- inline iterator& operator++()
- {
- probe();
- return *this;
- }
- inline value_type& operator*() const { return *get_cur(); }
- inline value_type* operator->() const { return get_cur(); }
- inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
- inline bool operator != (const iterator& b) const { return !(*this == b); }
- inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
- inline bool operator != (const const_iterator& b) const { return !(*this == b); }
- private:
- hash_map_type* m_pTable;
- uint32_t m_index;
- inline value_type* get_cur() const
- {
- assert(m_pTable && (m_index < m_pTable->m_values.size()));
- assert(m_pTable->get_node_state(m_index) == cStateValid);
- return &m_pTable->get_node(m_index);
- }
- inline void probe()
- {
- assert(m_pTable);
- m_index = m_pTable->find_next(m_index);
- }
- };
- class const_iterator
- {
- friend class hash_map<Key, Value, Hasher, Equals>;
- friend class hash_map<Key, Value, Hasher, Equals>::iterator;
- public:
- inline const_iterator() : m_pTable(NULL), m_index(0) { }
- inline const_iterator(const hash_map_type& table, uint32_t index) : m_pTable(&table), m_index(index) { }
- inline const_iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
- inline const_iterator(const const_iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
- inline const_iterator& operator= (const const_iterator& other)
- {
- m_pTable = other.m_pTable;
- m_index = other.m_index;
- return *this;
- }
- inline const_iterator& operator= (const iterator& other)
- {
- m_pTable = other.m_pTable;
- m_index = other.m_index;
- return *this;
- }
- // post-increment
- inline const_iterator operator++(int)
- {
- const_iterator result(*this);
- ++*this;
- return result;
- }
- // pre-increment
- inline const_iterator& operator++()
- {
- probe();
- return *this;
- }
- inline const value_type& operator*() const { return *get_cur(); }
- inline const value_type* operator->() const { return get_cur(); }
- inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
- inline bool operator != (const const_iterator& b) const { return !(*this == b); }
- inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
- inline bool operator != (const iterator& b) const { return !(*this == b); }
- private:
- const hash_map_type* m_pTable;
- uint32_t m_index;
- inline const value_type* get_cur() const
- {
- assert(m_pTable && (m_index < m_pTable->m_values.size()));
- assert(m_pTable->get_node_state(m_index) == cStateValid);
- return &m_pTable->get_node(m_index);
- }
- inline void probe()
- {
- assert(m_pTable);
- m_index = m_pTable->find_next(m_index);
- }
- };
- inline const_iterator begin() const
- {
- if (!m_num_valid)
- return end();
- return const_iterator(*this, find_next(UINT32_MAX));
- }
- inline const_iterator end() const
- {
- return const_iterator(*this, m_values.size());
- }
- inline iterator begin()
- {
- if (!m_num_valid)
- return end();
- return iterator(*this, find_next(UINT32_MAX));
- }
- inline iterator end()
- {
- return iterator(*this, m_values.size());
- }
- // insert_result.first will always point to inserted key/value (or the already existing key/value).
- // insert_resutt.second will be true if a new key/value was inserted, or false if the key already existed (in which case first will point to the already existing value).
- typedef std::pair<iterator, bool> insert_result;
- inline insert_result insert(const Key& k, const Value& v = Value())
- {
- insert_result result;
- if (!insert_no_grow(result, k, v))
- {
- grow();
- // This must succeed.
- if (!insert_no_grow(result, k, v))
- {
- fprintf(stderr, "insert() failed");
- abort();
- }
- }
- return result;
- }
- inline insert_result insert(const value_type& v)
- {
- return insert(v.first, v.second);
- }
- inline const_iterator find(const Key& k) const
- {
- return const_iterator(*this, find_index(k));
- }
- inline iterator find(const Key& k)
- {
- return iterator(*this, find_index(k));
- }
- inline bool erase(const Key& k)
- {
- uint32_t i = find_index(k);
- if (i >= m_values.size())
- return false;
- node* pDst = &get_node(i);
- destruct_value_type(pDst);
- pDst->state = cStateInvalid;
- m_num_valid--;
- for (; ; )
- {
- uint32_t r, j = i;
- node* pSrc = pDst;
- do
- {
- if (!i)
- {
- i = m_values.size() - 1;
- pSrc = &get_node(i);
- }
- else
- {
- i--;
- pSrc--;
- }
- if (!pSrc->state)
- return true;
- r = hash_key(pSrc->first);
- } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
- move_node(pDst, pSrc);
- pDst = pSrc;
- }
- }
- inline void swap(hash_map_type& other)
- {
- m_values.swap(other.m_values);
- std::swap(m_hash_shift, other.m_hash_shift);
- std::swap(m_num_valid, other.m_num_valid);
- std::swap(m_grow_threshold, other.m_grow_threshold);
- std::swap(m_hasher, other.m_hasher);
- std::swap(m_equals, other.m_equals);
- }
- private:
- struct node : public value_type
- {
- uint8_t state;
- };
- static inline void construct_value_type(value_type* pDst, const Key& k, const Value& v)
- {
- if (BASISU_IS_BITWISE_COPYABLE(Key))
- memcpy(&pDst->first, &k, sizeof(Key));
- else
- scalar_type<Key>::construct(&pDst->first, k);
- if (BASISU_IS_BITWISE_COPYABLE(Value))
- memcpy(&pDst->second, &v, sizeof(Value));
- else
- scalar_type<Value>::construct(&pDst->second, v);
- }
- static inline void construct_value_type(value_type* pDst, const value_type* pSrc)
- {
- if ((BASISU_IS_BITWISE_COPYABLE(Key)) && (BASISU_IS_BITWISE_COPYABLE(Value)))
- {
- memcpy(pDst, pSrc, sizeof(value_type));
- }
- else
- {
- if (BASISU_IS_BITWISE_COPYABLE(Key))
- memcpy(&pDst->first, &pSrc->first, sizeof(Key));
- else
- scalar_type<Key>::construct(&pDst->first, pSrc->first);
- if (BASISU_IS_BITWISE_COPYABLE(Value))
- memcpy(&pDst->second, &pSrc->second, sizeof(Value));
- else
- scalar_type<Value>::construct(&pDst->second, pSrc->second);
- }
- }
- static inline void destruct_value_type(value_type* p)
- {
- scalar_type<Key>::destruct(&p->first);
- scalar_type<Value>::destruct(&p->second);
- }
- // Moves *pSrc to *pDst efficiently.
- // pDst should NOT be constructed on entry.
- static inline void move_node(node* pDst, node* pSrc, bool update_src_state = true)
- {
- assert(!pDst->state);
- if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key) && BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
- {
- memcpy(pDst, pSrc, sizeof(node));
- }
- else
- {
- if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key))
- memcpy(&pDst->first, &pSrc->first, sizeof(Key));
- else
- {
- scalar_type<Key>::construct(&pDst->first, pSrc->first);
- scalar_type<Key>::destruct(&pSrc->first);
- }
- if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
- memcpy(&pDst->second, &pSrc->second, sizeof(Value));
- else
- {
- scalar_type<Value>::construct(&pDst->second, pSrc->second);
- scalar_type<Value>::destruct(&pSrc->second);
- }
- pDst->state = cStateValid;
- }
- if (update_src_state)
- pSrc->state = cStateInvalid;
- }
- struct raw_node
- {
- inline raw_node()
- {
- node* p = reinterpret_cast<node*>(this);
- p->state = cStateInvalid;
- }
- inline ~raw_node()
- {
- node* p = reinterpret_cast<node*>(this);
- if (p->state)
- hash_map_type::destruct_value_type(p);
- }
- inline raw_node(const raw_node& other)
- {
- node* pDst = reinterpret_cast<node*>(this);
- const node* pSrc = reinterpret_cast<const node*>(&other);
- if (pSrc->state)
- {
- hash_map_type::construct_value_type(pDst, pSrc);
- pDst->state = cStateValid;
- }
- else
- pDst->state = cStateInvalid;
- }
- inline raw_node& operator= (const raw_node& rhs)
- {
- if (this == &rhs)
- return *this;
- node* pDst = reinterpret_cast<node*>(this);
- const node* pSrc = reinterpret_cast<const node*>(&rhs);
- if (pSrc->state)
- {
- if (pDst->state)
- {
- pDst->first = pSrc->first;
- pDst->second = pSrc->second;
- }
- else
- {
- hash_map_type::construct_value_type(pDst, pSrc);
- pDst->state = cStateValid;
- }
- }
- else if (pDst->state)
- {
- hash_map_type::destruct_value_type(pDst);
- pDst->state = cStateInvalid;
- }
- return *this;
- }
- uint8_t m_bits[sizeof(node)];
- };
- typedef basisu::vector<raw_node> node_vector;
- node_vector m_values;
- uint32_t m_hash_shift;
- Hasher m_hasher;
- Equals m_equals;
- uint32_t m_num_valid;
- uint32_t m_grow_threshold;
- inline uint32_t hash_key(const Key& k) const
- {
- assert((1U << (32U - m_hash_shift)) == m_values.size());
- uint32_t hash = static_cast<uint32_t>(m_hasher(k));
- // Fibonacci hashing
- hash = (2654435769U * hash) >> m_hash_shift;
- assert(hash < m_values.size());
- return hash;
- }
- inline const node& get_node(uint32_t index) const
- {
- return *reinterpret_cast<const node*>(&m_values[index]);
- }
- inline node& get_node(uint32_t index)
- {
- return *reinterpret_cast<node*>(&m_values[index]);
- }
- inline state get_node_state(uint32_t index) const
- {
- return static_cast<state>(get_node(index).state);
- }
- inline void set_node_state(uint32_t index, bool valid)
- {
- get_node(index).state = valid;
- }
- inline void grow()
- {
- uint64_t n = m_values.size() * 3ULL; // was * 2
-
- if (!helpers::is_power_of_2(n))
- n = helpers::next_pow2(n);
- if (n > 0x80000000UL)
- n = 0x80000000UL;
- rehash(helpers::maximum<uint32_t>(cMinHashSize, (uint32_t)n));
- }
- inline void rehash(uint32_t new_hash_size)
- {
- assert(new_hash_size >= m_num_valid);
- assert(helpers::is_power_of_2(new_hash_size));
- if ((new_hash_size < m_num_valid) || (new_hash_size == m_values.size()))
- return;
- hash_map new_map;
- new_map.m_values.resize(new_hash_size);
- new_map.m_hash_shift = 32U - helpers::floor_log2i(new_hash_size);
- assert(new_hash_size == (1U << (32U - new_map.m_hash_shift)));
- new_map.m_grow_threshold = UINT_MAX;
- node* pNode = reinterpret_cast<node*>(m_values.begin());
- node* pNode_end = pNode + m_values.size();
- while (pNode != pNode_end)
- {
- if (pNode->state)
- {
- new_map.move_into(pNode);
- if (new_map.m_num_valid == m_num_valid)
- break;
- }
- pNode++;
- }
- new_map.m_grow_threshold = (new_hash_size + 1U) >> 1U;
- m_values.clear_no_destruction();
- m_hash_shift = 32;
- swap(new_map);
- }
- inline uint32_t find_next(uint32_t index) const
- {
- index++;
- if (index >= m_values.size())
- return index;
- const node* pNode = &get_node(index);
- for (; ; )
- {
- if (pNode->state)
- break;
- if (++index >= m_values.size())
- break;
- pNode++;
- }
- return index;
- }
- inline uint32_t find_index(const Key& k) const
- {
- if (m_num_valid)
- {
- uint32_t index = hash_key(k);
- const node* pNode = &get_node(index);
- if (pNode->state)
- {
- if (m_equals(pNode->first, k))
- return index;
- const uint32_t orig_index = index;
- for (; ; )
- {
- if (!index)
- {
- index = m_values.size() - 1;
- pNode = &get_node(index);
- }
- else
- {
- index--;
- pNode--;
- }
- if (index == orig_index)
- break;
- if (!pNode->state)
- break;
- if (m_equals(pNode->first, k))
- return index;
- }
- }
- }
- return m_values.size();
- }
- inline bool insert_no_grow(insert_result& result, const Key& k, const Value& v = Value())
- {
- if (!m_values.size())
- return false;
- uint32_t index = hash_key(k);
- node* pNode = &get_node(index);
- if (pNode->state)
- {
- if (m_equals(pNode->first, k))
- {
- result.first = iterator(*this, index);
- result.second = false;
- return true;
- }
- const uint32_t orig_index = index;
- for (; ; )
- {
- if (!index)
- {
- index = m_values.size() - 1;
- pNode = &get_node(index);
- }
- else
- {
- index--;
- pNode--;
- }
- if (orig_index == index)
- return false;
- if (!pNode->state)
- break;
- if (m_equals(pNode->first, k))
- {
- result.first = iterator(*this, index);
- result.second = false;
- return true;
- }
- }
- }
- if (m_num_valid >= m_grow_threshold)
- return false;
- construct_value_type(pNode, k, v);
- pNode->state = cStateValid;
- m_num_valid++;
- assert(m_num_valid <= m_values.size());
- result.first = iterator(*this, index);
- result.second = true;
- return true;
- }
- inline void move_into(node* pNode)
- {
- uint32_t index = hash_key(pNode->first);
- node* pDst_node = &get_node(index);
- if (pDst_node->state)
- {
- const uint32_t orig_index = index;
- for (; ; )
- {
- if (!index)
- {
- index = m_values.size() - 1;
- pDst_node = &get_node(index);
- }
- else
- {
- index--;
- pDst_node--;
- }
- if (index == orig_index)
- {
- assert(false);
- return;
- }
- if (!pDst_node->state)
- break;
- }
- }
- move_node(pDst_node, pNode, false);
- m_num_valid++;
- }
- };
- template<typename Key, typename Value, typename Hasher, typename Equals>
- struct bitwise_movable< hash_map<Key, Value, Hasher, Equals> > { enum { cFlag = true }; };
-
- #if BASISU_HASHMAP_TEST
- extern void hash_map_test();
- #endif
-
- } // namespace basisu
- namespace std
- {
- template<typename T>
- inline void swap(basisu::vector<T>& a, basisu::vector<T>& b)
- {
- a.swap(b);
- }
- template<typename Key, typename Value, typename Hasher, typename Equals>
- inline void swap(basisu::hash_map<Key, Value, Hasher, Equals>& a, basisu::hash_map<Key, Value, Hasher, Equals>& b)
- {
- a.swap(b);
- }
- } // namespace std
|