basisu_containers.h 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. // basisu_containers.h
  2. #pragma once
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <assert.h>
  7. #include <algorithm>
  8. #if defined(__linux__) && !defined(ANDROID)
  9. // Only for malloc_usable_size() in basisu_containers_impl.h
  10. #include <malloc.h>
  11. #define HAS_MALLOC_USABLE_SIZE 1
  12. #endif
  13. // Set to 1 to always check vector operator[], front(), and back() even in release.
  14. #define BASISU_VECTOR_FORCE_CHECKING 0
  15. // If 1, the vector container will not query the CRT to get the size of resized memory blocks.
  16. #define BASISU_VECTOR_DETERMINISTIC 1
  17. #ifdef _MSC_VER
  18. #define BASISU_FORCE_INLINE __forceinline
  19. #else
  20. #define BASISU_FORCE_INLINE inline
  21. #endif
  22. namespace basisu
  23. {
  24. enum { cInvalidIndex = -1 };
  25. namespace helpers
  26. {
  27. inline bool is_power_of_2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
  28. inline bool is_power_of_2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
  29. template<class T> const T& minimum(const T& a, const T& b) { return (b < a) ? b : a; }
  30. template<class T> const T& maximum(const T& a, const T& b) { return (a < b) ? b : a; }
  31. inline uint32_t floor_log2i(uint32_t v)
  32. {
  33. uint32_t l = 0;
  34. while (v > 1U)
  35. {
  36. v >>= 1;
  37. l++;
  38. }
  39. return l;
  40. }
  41. inline uint32_t next_pow2(uint32_t val)
  42. {
  43. val--;
  44. val |= val >> 16;
  45. val |= val >> 8;
  46. val |= val >> 4;
  47. val |= val >> 2;
  48. val |= val >> 1;
  49. return val + 1;
  50. }
  51. inline uint64_t next_pow2(uint64_t val)
  52. {
  53. val--;
  54. val |= val >> 32;
  55. val |= val >> 16;
  56. val |= val >> 8;
  57. val |= val >> 4;
  58. val |= val >> 2;
  59. val |= val >> 1;
  60. return val + 1;
  61. }
  62. } // namespace helpers
  63. template <typename T>
  64. inline T* construct(T* p)
  65. {
  66. return new (static_cast<void*>(p)) T;
  67. }
  68. template <typename T, typename U>
  69. inline T* construct(T* p, const U& init)
  70. {
  71. return new (static_cast<void*>(p)) T(init);
  72. }
  73. template <typename T>
  74. inline void construct_array(T* p, size_t n)
  75. {
  76. T* q = p + n;
  77. for (; p != q; ++p)
  78. new (static_cast<void*>(p)) T;
  79. }
  80. template <typename T, typename U>
  81. inline void construct_array(T* p, size_t n, const U& init)
  82. {
  83. T* q = p + n;
  84. for (; p != q; ++p)
  85. new (static_cast<void*>(p)) T(init);
  86. }
  87. template <typename T>
  88. inline void destruct(T* p)
  89. {
  90. (void)p;
  91. p->~T();
  92. }
  93. template <typename T> inline void destruct_array(T* p, size_t n)
  94. {
  95. T* q = p + n;
  96. for (; p != q; ++p)
  97. p->~T();
  98. }
  99. template<typename T> struct int_traits { enum { cMin = INT32_MIN, cMax = INT32_MAX, cSigned = true }; };
  100. template<> struct int_traits<int8_t> { enum { cMin = INT8_MIN, cMax = INT8_MAX, cSigned = true }; };
  101. template<> struct int_traits<int16_t> { enum { cMin = INT16_MIN, cMax = INT16_MAX, cSigned = true }; };
  102. template<> struct int_traits<int32_t> { enum { cMin = INT32_MIN, cMax = INT32_MAX, cSigned = true }; };
  103. template<> struct int_traits<uint8_t> { enum { cMin = 0, cMax = UINT8_MAX, cSigned = false }; };
  104. template<> struct int_traits<uint16_t> { enum { cMin = 0, cMax = UINT16_MAX, cSigned = false }; };
  105. template<> struct int_traits<uint32_t> { enum { cMin = 0, cMax = UINT32_MAX, cSigned = false }; };
  106. template<typename T>
  107. struct scalar_type
  108. {
  109. enum { cFlag = false };
  110. static inline void construct(T* p) { basisu::construct(p); }
  111. static inline void construct(T* p, const T& init) { basisu::construct(p, init); }
  112. static inline void construct_array(T* p, size_t n) { basisu::construct_array(p, n); }
  113. static inline void destruct(T* p) { basisu::destruct(p); }
  114. static inline void destruct_array(T* p, size_t n) { basisu::destruct_array(p, n); }
  115. };
  116. template<typename T> struct scalar_type<T*>
  117. {
  118. enum { cFlag = true };
  119. static inline void construct(T** p) { memset(p, 0, sizeof(T*)); }
  120. static inline void construct(T** p, T* init) { *p = init; }
  121. static inline void construct_array(T** p, size_t n) { memset(p, 0, sizeof(T*) * n); }
  122. static inline void destruct(T** p) { p; }
  123. static inline void destruct_array(T** p, size_t n) { p, n; }
  124. };
  125. #define BASISU_DEFINE_BUILT_IN_TYPE(X) \
  126. template<> struct scalar_type<X> { \
  127. enum { cFlag = true }; \
  128. static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \
  129. static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \
  130. static inline void construct_array(X* p, size_t n) { memset(p, 0, sizeof(X) * n); } \
  131. static inline void destruct(X* p) { p; } \
  132. static inline void destruct_array(X* p, size_t n) { p, n; } };
  133. BASISU_DEFINE_BUILT_IN_TYPE(bool)
  134. BASISU_DEFINE_BUILT_IN_TYPE(char)
  135. BASISU_DEFINE_BUILT_IN_TYPE(unsigned char)
  136. BASISU_DEFINE_BUILT_IN_TYPE(short)
  137. BASISU_DEFINE_BUILT_IN_TYPE(unsigned short)
  138. BASISU_DEFINE_BUILT_IN_TYPE(int)
  139. BASISU_DEFINE_BUILT_IN_TYPE(unsigned int)
  140. BASISU_DEFINE_BUILT_IN_TYPE(long)
  141. BASISU_DEFINE_BUILT_IN_TYPE(unsigned long)
  142. #ifdef __GNUC__
  143. BASISU_DEFINE_BUILT_IN_TYPE(long long)
  144. BASISU_DEFINE_BUILT_IN_TYPE(unsigned long long)
  145. #else
  146. BASISU_DEFINE_BUILT_IN_TYPE(__int64)
  147. BASISU_DEFINE_BUILT_IN_TYPE(unsigned __int64)
  148. #endif
  149. BASISU_DEFINE_BUILT_IN_TYPE(float)
  150. BASISU_DEFINE_BUILT_IN_TYPE(double)
  151. BASISU_DEFINE_BUILT_IN_TYPE(long double)
  152. #undef BASISU_DEFINE_BUILT_IN_TYPE
  153. template<typename T>
  154. struct bitwise_movable { enum { cFlag = false }; };
  155. #define BASISU_DEFINE_BITWISE_MOVABLE(Q) template<> struct bitwise_movable<Q> { enum { cFlag = true }; };
  156. template<typename T>
  157. struct bitwise_copyable { enum { cFlag = false }; };
  158. #define BASISU_DEFINE_BITWISE_COPYABLE(Q) template<> struct bitwise_copyable<Q> { enum { cFlag = true }; };
  159. #define BASISU_IS_POD(T) __is_pod(T)
  160. #define BASISU_IS_SCALAR_TYPE(T) (scalar_type<T>::cFlag)
  161. #if defined(__GNUC__) && __GNUC__<5
  162. #define BASISU_IS_TRIVIALLY_COPYABLE(...) __has_trivial_copy(__VA_ARGS__)
  163. #else
  164. #define BASISU_IS_TRIVIALLY_COPYABLE(...) std::is_trivially_copyable<__VA_ARGS__>::value
  165. #endif
  166. // TODO: clean this up
  167. #define BASISU_IS_BITWISE_COPYABLE(T) (BASISU_IS_SCALAR_TYPE(T) || BASISU_IS_POD(T) || BASISU_IS_TRIVIALLY_COPYABLE(T) || (bitwise_copyable<T>::cFlag))
  168. #define BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) (BASISU_IS_BITWISE_COPYABLE(T) || (bitwise_movable<T>::cFlag))
  169. #define BASISU_HAS_DESTRUCTOR(T) ((!scalar_type<T>::cFlag) && (!__is_pod(T)))
  170. typedef char(&yes_t)[1];
  171. typedef char(&no_t)[2];
  172. template <class U> yes_t class_test(int U::*);
  173. template <class U> no_t class_test(...);
  174. template <class T> struct is_class
  175. {
  176. enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) };
  177. };
  178. template <typename T> struct is_pointer
  179. {
  180. enum { value = false };
  181. };
  182. template <typename T> struct is_pointer<T*>
  183. {
  184. enum { value = true };
  185. };
  186. struct empty_type { };
  187. BASISU_DEFINE_BITWISE_COPYABLE(empty_type);
  188. BASISU_DEFINE_BITWISE_MOVABLE(empty_type);
  189. template<typename T> struct rel_ops
  190. {
  191. friend bool operator!=(const T& x, const T& y) { return (!(x == y)); }
  192. friend bool operator> (const T& x, const T& y) { return (y < x); }
  193. friend bool operator<=(const T& x, const T& y) { return (!(y < x)); }
  194. friend bool operator>=(const T& x, const T& y) { return (!(x < y)); }
  195. };
  196. struct elemental_vector
  197. {
  198. void* m_p;
  199. uint32_t m_size;
  200. uint32_t m_capacity;
  201. typedef void (*object_mover)(void* pDst, void* pSrc, uint32_t num);
  202. bool increase_capacity(uint32_t min_new_capacity, bool grow_hint, uint32_t element_size, object_mover pRelocate, bool nofail);
  203. };
  204. template<typename T>
  205. class vector : public rel_ops< vector<T> >
  206. {
  207. public:
  208. typedef T* iterator;
  209. typedef const T* const_iterator;
  210. typedef T value_type;
  211. typedef T& reference;
  212. typedef const T& const_reference;
  213. typedef T* pointer;
  214. typedef const T* const_pointer;
  215. inline vector() :
  216. m_p(NULL),
  217. m_size(0),
  218. m_capacity(0)
  219. {
  220. }
  221. inline vector(uint32_t n, const T& init) :
  222. m_p(NULL),
  223. m_size(0),
  224. m_capacity(0)
  225. {
  226. increase_capacity(n, false);
  227. construct_array(m_p, n, init);
  228. m_size = n;
  229. }
  230. inline vector(const vector& other) :
  231. m_p(NULL),
  232. m_size(0),
  233. m_capacity(0)
  234. {
  235. increase_capacity(other.m_size, false);
  236. m_size = other.m_size;
  237. if (BASISU_IS_BITWISE_COPYABLE(T))
  238. {
  239. if ((m_p) && (other.m_p))
  240. memcpy(m_p, other.m_p, m_size * sizeof(T));
  241. }
  242. else
  243. {
  244. T* pDst = m_p;
  245. const T* pSrc = other.m_p;
  246. for (uint32_t i = m_size; i > 0; i--)
  247. construct(pDst++, *pSrc++);
  248. }
  249. }
  250. inline explicit vector(size_t size) :
  251. m_p(NULL),
  252. m_size(0),
  253. m_capacity(0)
  254. {
  255. resize(size);
  256. }
  257. inline ~vector()
  258. {
  259. if (m_p)
  260. {
  261. scalar_type<T>::destruct_array(m_p, m_size);
  262. free(m_p);
  263. }
  264. }
  265. inline vector& operator= (const vector& other)
  266. {
  267. if (this == &other)
  268. return *this;
  269. if (m_capacity >= other.m_size)
  270. resize(0);
  271. else
  272. {
  273. clear();
  274. increase_capacity(other.m_size, false);
  275. }
  276. if (BASISU_IS_BITWISE_COPYABLE(T))
  277. {
  278. if ((m_p) && (other.m_p))
  279. memcpy(m_p, other.m_p, other.m_size * sizeof(T));
  280. }
  281. else
  282. {
  283. T* pDst = m_p;
  284. const T* pSrc = other.m_p;
  285. for (uint32_t i = other.m_size; i > 0; i--)
  286. construct(pDst++, *pSrc++);
  287. }
  288. m_size = other.m_size;
  289. return *this;
  290. }
  291. BASISU_FORCE_INLINE const T* begin() const { return m_p; }
  292. BASISU_FORCE_INLINE T* begin() { return m_p; }
  293. BASISU_FORCE_INLINE const T* end() const { return m_p + m_size; }
  294. BASISU_FORCE_INLINE T* end() { return m_p + m_size; }
  295. BASISU_FORCE_INLINE bool empty() const { return !m_size; }
  296. BASISU_FORCE_INLINE uint32_t size() const { return m_size; }
  297. BASISU_FORCE_INLINE uint32_t size_in_bytes() const { return m_size * sizeof(T); }
  298. BASISU_FORCE_INLINE uint32_t capacity() const { return m_capacity; }
  299. // operator[] will assert on out of range indices, but in final builds there is (and will never be) any range checking on this method.
  300. //BASISU_FORCE_INLINE const T& operator[] (uint32_t i) const { assert(i < m_size); return m_p[i]; }
  301. //BASISU_FORCE_INLINE T& operator[] (uint32_t i) { assert(i < m_size); return m_p[i]; }
  302. #if !BASISU_VECTOR_FORCE_CHECKING
  303. BASISU_FORCE_INLINE const T& operator[] (size_t i) const { assert(i < m_size); return m_p[i]; }
  304. BASISU_FORCE_INLINE T& operator[] (size_t i) { assert(i < m_size); return m_p[i]; }
  305. #else
  306. BASISU_FORCE_INLINE const T& operator[] (size_t i) const
  307. {
  308. if (i >= m_size)
  309. {
  310. fprintf(stderr, "operator[] invalid index: %u, max entries %u, type size %u\n", (uint32_t)i, m_size, (uint32_t)sizeof(T));
  311. abort();
  312. }
  313. return m_p[i];
  314. }
  315. BASISU_FORCE_INLINE T& operator[] (size_t i)
  316. {
  317. if (i >= m_size)
  318. {
  319. fprintf(stderr, "operator[] invalid index: %u, max entries %u, type size %u\n", (uint32_t)i, m_size, (uint32_t)sizeof(T));
  320. abort();
  321. }
  322. return m_p[i];
  323. }
  324. #endif
  325. // at() always includes range checking, even in final builds, unlike operator [].
  326. // The first element is returned if the index is out of range.
  327. BASISU_FORCE_INLINE const T& at(size_t i) const { assert(i < m_size); return (i >= m_size) ? m_p[0] : m_p[i]; }
  328. BASISU_FORCE_INLINE T& at(size_t i) { assert(i < m_size); return (i >= m_size) ? m_p[0] : m_p[i]; }
  329. #if !BASISU_VECTOR_FORCE_CHECKING
  330. BASISU_FORCE_INLINE const T& front() const { assert(m_size); return m_p[0]; }
  331. BASISU_FORCE_INLINE T& front() { assert(m_size); return m_p[0]; }
  332. BASISU_FORCE_INLINE const T& back() const { assert(m_size); return m_p[m_size - 1]; }
  333. BASISU_FORCE_INLINE T& back() { assert(m_size); return m_p[m_size - 1]; }
  334. #else
  335. BASISU_FORCE_INLINE const T& front() const
  336. {
  337. if (!m_size)
  338. {
  339. fprintf(stderr, "front: vector is empty, type size %u\n", (uint32_t)sizeof(T));
  340. abort();
  341. }
  342. return m_p[0];
  343. }
  344. BASISU_FORCE_INLINE T& front()
  345. {
  346. if (!m_size)
  347. {
  348. fprintf(stderr, "front: vector is empty, type size %u\n", (uint32_t)sizeof(T));
  349. abort();
  350. }
  351. return m_p[0];
  352. }
  353. BASISU_FORCE_INLINE const T& back() const
  354. {
  355. if(!m_size)
  356. {
  357. fprintf(stderr, "back: vector is empty, type size %u\n", (uint32_t)sizeof(T));
  358. abort();
  359. }
  360. return m_p[m_size - 1];
  361. }
  362. BASISU_FORCE_INLINE T& back()
  363. {
  364. if (!m_size)
  365. {
  366. fprintf(stderr, "back: vector is empty, type size %u\n", (uint32_t)sizeof(T));
  367. abort();
  368. }
  369. return m_p[m_size - 1];
  370. }
  371. #endif
  372. BASISU_FORCE_INLINE const T* get_ptr() const { return m_p; }
  373. BASISU_FORCE_INLINE T* get_ptr() { return m_p; }
  374. BASISU_FORCE_INLINE const T* data() const { return m_p; }
  375. BASISU_FORCE_INLINE T* data() { return m_p; }
  376. // clear() sets the container to empty, then frees the allocated block.
  377. inline void clear()
  378. {
  379. if (m_p)
  380. {
  381. scalar_type<T>::destruct_array(m_p, m_size);
  382. free(m_p);
  383. m_p = NULL;
  384. m_size = 0;
  385. m_capacity = 0;
  386. }
  387. }
  388. inline void clear_no_destruction()
  389. {
  390. if (m_p)
  391. {
  392. free(m_p);
  393. m_p = NULL;
  394. m_size = 0;
  395. m_capacity = 0;
  396. }
  397. }
  398. inline void reserve(size_t new_capacity_size_t)
  399. {
  400. if (new_capacity_size_t > UINT32_MAX)
  401. {
  402. assert(0);
  403. return;
  404. }
  405. uint32_t new_capacity = (uint32_t)new_capacity_size_t;
  406. if (new_capacity > m_capacity)
  407. increase_capacity(new_capacity, false);
  408. else if (new_capacity < m_capacity)
  409. {
  410. // Must work around the lack of a "decrease_capacity()" method.
  411. // This case is rare enough in practice that it's probably not worth implementing an optimized in-place resize.
  412. vector tmp;
  413. tmp.increase_capacity(helpers::maximum(m_size, new_capacity), false);
  414. tmp = *this;
  415. swap(tmp);
  416. }
  417. }
  418. inline bool try_reserve(size_t new_capacity_size_t)
  419. {
  420. if (new_capacity_size_t > UINT32_MAX)
  421. {
  422. assert(0);
  423. return false;
  424. }
  425. uint32_t new_capacity = (uint32_t)new_capacity_size_t;
  426. if (new_capacity > m_capacity)
  427. {
  428. if (!increase_capacity(new_capacity, false))
  429. return false;
  430. }
  431. else if (new_capacity < m_capacity)
  432. {
  433. // Must work around the lack of a "decrease_capacity()" method.
  434. // This case is rare enough in practice that it's probably not worth implementing an optimized in-place resize.
  435. vector tmp;
  436. tmp.increase_capacity(helpers::maximum(m_size, new_capacity), false);
  437. tmp = *this;
  438. swap(tmp);
  439. }
  440. return true;
  441. }
  442. // resize(0) sets the container to empty, but does not free the allocated block.
  443. inline void resize(size_t new_size_size_t, bool grow_hint = false)
  444. {
  445. if (new_size_size_t > UINT32_MAX)
  446. {
  447. assert(0);
  448. return;
  449. }
  450. uint32_t new_size = (uint32_t)new_size_size_t;
  451. if (m_size != new_size)
  452. {
  453. if (new_size < m_size)
  454. scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
  455. else
  456. {
  457. if (new_size > m_capacity)
  458. increase_capacity(new_size, (new_size == (m_size + 1)) || grow_hint);
  459. scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
  460. }
  461. m_size = new_size;
  462. }
  463. }
  464. inline bool try_resize(size_t new_size_size_t, bool grow_hint = false)
  465. {
  466. if (new_size_size_t > UINT32_MAX)
  467. {
  468. assert(0);
  469. return false;
  470. }
  471. uint32_t new_size = (uint32_t)new_size_size_t;
  472. if (m_size != new_size)
  473. {
  474. if (new_size < m_size)
  475. scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
  476. else
  477. {
  478. if (new_size > m_capacity)
  479. {
  480. if (!increase_capacity(new_size, (new_size == (m_size + 1)) || grow_hint, true))
  481. return false;
  482. }
  483. scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
  484. }
  485. m_size = new_size;
  486. }
  487. return true;
  488. }
  489. // 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).
  490. // Otherwise it blows away the allocated block. See http://www.codercorner.com/blog/?p=494
  491. inline void reset()
  492. {
  493. if (m_size >= (m_capacity >> 1))
  494. resize(0);
  495. else
  496. clear();
  497. }
  498. inline T* enlarge(uint32_t i)
  499. {
  500. uint32_t cur_size = m_size;
  501. resize(cur_size + i, true);
  502. return get_ptr() + cur_size;
  503. }
  504. inline T* try_enlarge(uint32_t i)
  505. {
  506. uint32_t cur_size = m_size;
  507. if (!try_resize(cur_size + i, true))
  508. return NULL;
  509. return get_ptr() + cur_size;
  510. }
  511. BASISU_FORCE_INLINE void push_back(const T& obj)
  512. {
  513. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  514. if (m_size >= m_capacity)
  515. increase_capacity(m_size + 1, true);
  516. scalar_type<T>::construct(m_p + m_size, obj);
  517. m_size++;
  518. }
  519. inline bool try_push_back(const T& obj)
  520. {
  521. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  522. if (m_size >= m_capacity)
  523. {
  524. if (!increase_capacity(m_size + 1, true, true))
  525. return false;
  526. }
  527. scalar_type<T>::construct(m_p + m_size, obj);
  528. m_size++;
  529. return true;
  530. }
  531. inline void push_back_value(T obj)
  532. {
  533. if (m_size >= m_capacity)
  534. increase_capacity(m_size + 1, true);
  535. scalar_type<T>::construct(m_p + m_size, obj);
  536. m_size++;
  537. }
  538. inline void pop_back()
  539. {
  540. assert(m_size);
  541. if (m_size)
  542. {
  543. m_size--;
  544. scalar_type<T>::destruct(&m_p[m_size]);
  545. }
  546. }
  547. inline void insert(uint32_t index, const T* p, uint32_t n)
  548. {
  549. assert(index <= m_size);
  550. if (!n)
  551. return;
  552. const uint32_t orig_size = m_size;
  553. resize(m_size + n, true);
  554. const uint32_t num_to_move = orig_size - index;
  555. if (BASISU_IS_BITWISE_COPYABLE(T))
  556. {
  557. // This overwrites the destination object bits, but bitwise copyable means we don't need to worry about destruction.
  558. memmove(m_p + index + n, m_p + index, sizeof(T) * num_to_move);
  559. }
  560. else
  561. {
  562. const T* pSrc = m_p + orig_size - 1;
  563. T* pDst = const_cast<T*>(pSrc) + n;
  564. for (uint32_t i = 0; i < num_to_move; i++)
  565. {
  566. assert((pDst - m_p) < (int)m_size);
  567. *pDst-- = *pSrc--;
  568. }
  569. }
  570. T* pDst = m_p + index;
  571. if (BASISU_IS_BITWISE_COPYABLE(T))
  572. {
  573. // This copies in the new bits, overwriting the existing objects, which is OK for copyable types that don't need destruction.
  574. memcpy(pDst, p, sizeof(T) * n);
  575. }
  576. else
  577. {
  578. for (uint32_t i = 0; i < n; i++)
  579. {
  580. assert((pDst - m_p) < (int)m_size);
  581. *pDst++ = *p++;
  582. }
  583. }
  584. }
  585. inline void insert(T* p, const T& obj)
  586. {
  587. int64_t ofs = p - begin();
  588. if ((ofs < 0) || (ofs > UINT32_MAX))
  589. {
  590. assert(0);
  591. return;
  592. }
  593. insert((uint32_t)ofs, &obj, 1);
  594. }
  595. // push_front() isn't going to be very fast - it's only here for usability.
  596. inline void push_front(const T& obj)
  597. {
  598. insert(0, &obj, 1);
  599. }
  600. vector& append(const vector& other)
  601. {
  602. if (other.m_size)
  603. insert(m_size, &other[0], other.m_size);
  604. return *this;
  605. }
  606. vector& append(const T* p, uint32_t n)
  607. {
  608. if (n)
  609. insert(m_size, p, n);
  610. return *this;
  611. }
  612. inline void erase(uint32_t start, uint32_t n)
  613. {
  614. assert((start + n) <= m_size);
  615. if ((start + n) > m_size)
  616. return;
  617. if (!n)
  618. return;
  619. const uint32_t num_to_move = m_size - (start + n);
  620. T* pDst = m_p + start;
  621. const T* pSrc = m_p + start + n;
  622. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T))
  623. {
  624. // This test is overly cautious.
  625. if ((!BASISU_IS_BITWISE_COPYABLE(T)) || (BASISU_HAS_DESTRUCTOR(T)))
  626. {
  627. // Type has been marked explictly as bitwise movable, which means we can move them around but they may need to be destructed.
  628. // First destroy the erased objects.
  629. scalar_type<T>::destruct_array(pDst, n);
  630. }
  631. // Copy "down" the objects to preserve, filling in the empty slots.
  632. memmove(pDst, pSrc, num_to_move * sizeof(T));
  633. }
  634. else
  635. {
  636. // Type is not bitwise copyable or movable.
  637. // Move them down one at a time by using the equals operator, and destroying anything that's left over at the end.
  638. T* pDst_end = pDst + num_to_move;
  639. while (pDst != pDst_end)
  640. *pDst++ = *pSrc++;
  641. scalar_type<T>::destruct_array(pDst_end, n);
  642. }
  643. m_size -= n;
  644. }
  645. inline void erase(uint32_t index)
  646. {
  647. erase(index, 1);
  648. }
  649. inline void erase(T* p)
  650. {
  651. assert((p >= m_p) && (p < (m_p + m_size)));
  652. erase(static_cast<uint32_t>(p - m_p));
  653. }
  654. inline void erase(T *pFirst, T *pEnd)
  655. {
  656. assert(pFirst <= pEnd);
  657. assert(pFirst >= begin() && pFirst <= end());
  658. assert(pEnd >= begin() && pEnd <= end());
  659. int64_t ofs = pFirst - begin();
  660. if ((ofs < 0) || (ofs > UINT32_MAX))
  661. {
  662. assert(0);
  663. return;
  664. }
  665. int64_t n = pEnd - pFirst;
  666. if ((n < 0) || (n > UINT32_MAX))
  667. {
  668. assert(0);
  669. return;
  670. }
  671. erase((uint32_t)ofs, (uint32_t)n);
  672. }
  673. void erase_unordered(uint32_t index)
  674. {
  675. assert(index < m_size);
  676. if ((index + 1) < m_size)
  677. (*this)[index] = back();
  678. pop_back();
  679. }
  680. inline bool operator== (const vector& rhs) const
  681. {
  682. if (m_size != rhs.m_size)
  683. return false;
  684. else if (m_size)
  685. {
  686. if (scalar_type<T>::cFlag)
  687. return memcmp(m_p, rhs.m_p, sizeof(T) * m_size) == 0;
  688. else
  689. {
  690. const T* pSrc = m_p;
  691. const T* pDst = rhs.m_p;
  692. for (uint32_t i = m_size; i; i--)
  693. if (!(*pSrc++ == *pDst++))
  694. return false;
  695. }
  696. }
  697. return true;
  698. }
  699. inline bool operator< (const vector& rhs) const
  700. {
  701. const uint32_t min_size = helpers::minimum(m_size, rhs.m_size);
  702. const T* pSrc = m_p;
  703. const T* pSrc_end = m_p + min_size;
  704. const T* pDst = rhs.m_p;
  705. while ((pSrc < pSrc_end) && (*pSrc == *pDst))
  706. {
  707. pSrc++;
  708. pDst++;
  709. }
  710. if (pSrc < pSrc_end)
  711. return *pSrc < *pDst;
  712. return m_size < rhs.m_size;
  713. }
  714. inline void swap(vector& other)
  715. {
  716. std::swap(m_p, other.m_p);
  717. std::swap(m_size, other.m_size);
  718. std::swap(m_capacity, other.m_capacity);
  719. }
  720. inline void sort()
  721. {
  722. std::sort(begin(), end());
  723. }
  724. inline void unique()
  725. {
  726. if (!empty())
  727. {
  728. sort();
  729. resize(std::unique(begin(), end()) - begin());
  730. }
  731. }
  732. inline void reverse()
  733. {
  734. uint32_t j = m_size >> 1;
  735. for (uint32_t i = 0; i < j; i++)
  736. std::swap(m_p[i], m_p[m_size - 1 - i]);
  737. }
  738. inline int find(const T& key) const
  739. {
  740. const T* p = m_p;
  741. const T* p_end = m_p + m_size;
  742. uint32_t index = 0;
  743. while (p != p_end)
  744. {
  745. if (key == *p)
  746. return index;
  747. p++;
  748. index++;
  749. }
  750. return cInvalidIndex;
  751. }
  752. inline int find_sorted(const T& key) const
  753. {
  754. if (m_size)
  755. {
  756. // Uniform binary search - Knuth Algorithm 6.2.1 U, unrolled twice.
  757. int i = ((m_size + 1) >> 1) - 1;
  758. int m = m_size;
  759. for (; ; )
  760. {
  761. assert(i >= 0 && i < (int)m_size);
  762. const T* pKey_i = m_p + i;
  763. int cmp = key < *pKey_i;
  764. #if defined(_DEBUG) || defined(DEBUG)
  765. int cmp2 = *pKey_i < key;
  766. assert((cmp != cmp2) || (key == *pKey_i));
  767. #endif
  768. if ((!cmp) && (key == *pKey_i)) return i;
  769. m >>= 1;
  770. if (!m) break;
  771. cmp = -cmp;
  772. i += (((m + 1) >> 1) ^ cmp) - cmp;
  773. if (i < 0)
  774. break;
  775. assert(i >= 0 && i < (int)m_size);
  776. pKey_i = m_p + i;
  777. cmp = key < *pKey_i;
  778. #if defined(_DEBUG) || defined(DEBUG)
  779. cmp2 = *pKey_i < key;
  780. assert((cmp != cmp2) || (key == *pKey_i));
  781. #endif
  782. if ((!cmp) && (key == *pKey_i)) return i;
  783. m >>= 1;
  784. if (!m) break;
  785. cmp = -cmp;
  786. i += (((m + 1) >> 1) ^ cmp) - cmp;
  787. if (i < 0)
  788. break;
  789. }
  790. }
  791. return cInvalidIndex;
  792. }
  793. template<typename Q>
  794. inline int find_sorted(const T& key, Q less_than) const
  795. {
  796. if (m_size)
  797. {
  798. // Uniform binary search - Knuth Algorithm 6.2.1 U, unrolled twice.
  799. int i = ((m_size + 1) >> 1) - 1;
  800. int m = m_size;
  801. for (; ; )
  802. {
  803. assert(i >= 0 && i < (int)m_size);
  804. const T* pKey_i = m_p + i;
  805. int cmp = less_than(key, *pKey_i);
  806. if ((!cmp) && (!less_than(*pKey_i, key))) return i;
  807. m >>= 1;
  808. if (!m) break;
  809. cmp = -cmp;
  810. i += (((m + 1) >> 1) ^ cmp) - cmp;
  811. if (i < 0)
  812. break;
  813. assert(i >= 0 && i < (int)m_size);
  814. pKey_i = m_p + i;
  815. cmp = less_than(key, *pKey_i);
  816. if ((!cmp) && (!less_than(*pKey_i, key))) return i;
  817. m >>= 1;
  818. if (!m) break;
  819. cmp = -cmp;
  820. i += (((m + 1) >> 1) ^ cmp) - cmp;
  821. if (i < 0)
  822. break;
  823. }
  824. }
  825. return cInvalidIndex;
  826. }
  827. inline uint32_t count_occurences(const T& key) const
  828. {
  829. uint32_t c = 0;
  830. const T* p = m_p;
  831. const T* p_end = m_p + m_size;
  832. while (p != p_end)
  833. {
  834. if (key == *p)
  835. c++;
  836. p++;
  837. }
  838. return c;
  839. }
  840. inline void set_all(const T& o)
  841. {
  842. if ((sizeof(T) == 1) && (scalar_type<T>::cFlag))
  843. memset(m_p, *reinterpret_cast<const uint8_t*>(&o), m_size);
  844. else
  845. {
  846. T* pDst = m_p;
  847. T* pDst_end = pDst + m_size;
  848. while (pDst != pDst_end)
  849. *pDst++ = o;
  850. }
  851. }
  852. // Caller assumes ownership of the heap block associated with the container. Container is cleared.
  853. inline void* assume_ownership()
  854. {
  855. T* p = m_p;
  856. m_p = NULL;
  857. m_size = 0;
  858. m_capacity = 0;
  859. return p;
  860. }
  861. // Caller is granting ownership of the indicated heap block.
  862. // Block must have size constructed elements, and have enough room for capacity elements.
  863. // The block must have been allocated using malloc().
  864. // 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.
  865. inline bool grant_ownership(T* p, uint32_t size, uint32_t capacity)
  866. {
  867. // To to prevent the caller from obviously shooting themselves in the foot.
  868. if (((p + capacity) > m_p) && (p < (m_p + m_capacity)))
  869. {
  870. // Can grant ownership of a block inside the container itself!
  871. assert(0);
  872. return false;
  873. }
  874. if (size > capacity)
  875. {
  876. assert(0);
  877. return false;
  878. }
  879. if (!p)
  880. {
  881. if (capacity)
  882. {
  883. assert(0);
  884. return false;
  885. }
  886. }
  887. else if (!capacity)
  888. {
  889. assert(0);
  890. return false;
  891. }
  892. clear();
  893. m_p = p;
  894. m_size = size;
  895. m_capacity = capacity;
  896. return true;
  897. }
  898. private:
  899. T* m_p;
  900. uint32_t m_size;
  901. uint32_t m_capacity;
  902. template<typename Q> struct is_vector { enum { cFlag = false }; };
  903. template<typename Q> struct is_vector< vector<Q> > { enum { cFlag = true }; };
  904. static void object_mover(void* pDst_void, void* pSrc_void, uint32_t num)
  905. {
  906. T* pSrc = static_cast<T*>(pSrc_void);
  907. T* const pSrc_end = pSrc + num;
  908. T* pDst = static_cast<T*>(pDst_void);
  909. while (pSrc != pSrc_end)
  910. {
  911. // placement new
  912. new (static_cast<void*>(pDst)) T(*pSrc);
  913. pSrc->~T();
  914. ++pSrc;
  915. ++pDst;
  916. }
  917. }
  918. inline bool increase_capacity(uint32_t min_new_capacity, bool grow_hint, bool nofail = false)
  919. {
  920. return reinterpret_cast<elemental_vector*>(this)->increase_capacity(
  921. min_new_capacity, grow_hint, sizeof(T),
  922. (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) || (is_vector<T>::cFlag)) ? NULL : object_mover, nofail);
  923. }
  924. };
  925. template<typename T> struct bitwise_movable< vector<T> > { enum { cFlag = true }; };
  926. // Hash map
  927. template <typename T>
  928. struct hasher
  929. {
  930. inline size_t operator() (const T& key) const { return static_cast<size_t>(key); }
  931. };
  932. template <typename T>
  933. struct equal_to
  934. {
  935. inline bool operator()(const T& a, const T& b) const { return a == b; }
  936. };
  937. // Important: The Hasher and Equals objects must be bitwise movable!
  938. template<typename Key, typename Value = empty_type, typename Hasher = hasher<Key>, typename Equals = equal_to<Key> >
  939. class hash_map
  940. {
  941. public:
  942. class iterator;
  943. class const_iterator;
  944. private:
  945. friend class iterator;
  946. friend class const_iterator;
  947. enum state
  948. {
  949. cStateInvalid = 0,
  950. cStateValid = 1
  951. };
  952. enum
  953. {
  954. cMinHashSize = 4U
  955. };
  956. public:
  957. typedef hash_map<Key, Value, Hasher, Equals> hash_map_type;
  958. typedef std::pair<Key, Value> value_type;
  959. typedef Key key_type;
  960. typedef Value referent_type;
  961. typedef Hasher hasher_type;
  962. typedef Equals equals_type;
  963. hash_map() :
  964. m_hash_shift(32), m_num_valid(0), m_grow_threshold(0)
  965. {
  966. }
  967. hash_map(const hash_map& other) :
  968. m_values(other.m_values),
  969. m_hash_shift(other.m_hash_shift),
  970. m_hasher(other.m_hasher),
  971. m_equals(other.m_equals),
  972. m_num_valid(other.m_num_valid),
  973. m_grow_threshold(other.m_grow_threshold)
  974. {
  975. }
  976. hash_map& operator= (const hash_map& other)
  977. {
  978. if (this == &other)
  979. return *this;
  980. clear();
  981. m_values = other.m_values;
  982. m_hash_shift = other.m_hash_shift;
  983. m_num_valid = other.m_num_valid;
  984. m_grow_threshold = other.m_grow_threshold;
  985. m_hasher = other.m_hasher;
  986. m_equals = other.m_equals;
  987. return *this;
  988. }
  989. inline ~hash_map()
  990. {
  991. clear();
  992. }
  993. const Equals& get_equals() const { return m_equals; }
  994. Equals& get_equals() { return m_equals; }
  995. void set_equals(const Equals& equals) { m_equals = equals; }
  996. const Hasher& get_hasher() const { return m_hasher; }
  997. Hasher& get_hasher() { return m_hasher; }
  998. void set_hasher(const Hasher& hasher) { m_hasher = hasher; }
  999. inline void clear()
  1000. {
  1001. if (!m_values.empty())
  1002. {
  1003. if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
  1004. {
  1005. node* p = &get_node(0);
  1006. node* p_end = p + m_values.size();
  1007. uint32_t num_remaining = m_num_valid;
  1008. while (p != p_end)
  1009. {
  1010. if (p->state)
  1011. {
  1012. destruct_value_type(p);
  1013. num_remaining--;
  1014. if (!num_remaining)
  1015. break;
  1016. }
  1017. p++;
  1018. }
  1019. }
  1020. m_values.clear_no_destruction();
  1021. m_hash_shift = 32;
  1022. m_num_valid = 0;
  1023. m_grow_threshold = 0;
  1024. }
  1025. }
  1026. inline void reset()
  1027. {
  1028. if (!m_num_valid)
  1029. return;
  1030. if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
  1031. {
  1032. node* p = &get_node(0);
  1033. node* p_end = p + m_values.size();
  1034. uint32_t num_remaining = m_num_valid;
  1035. while (p != p_end)
  1036. {
  1037. if (p->state)
  1038. {
  1039. destruct_value_type(p);
  1040. p->state = cStateInvalid;
  1041. num_remaining--;
  1042. if (!num_remaining)
  1043. break;
  1044. }
  1045. p++;
  1046. }
  1047. }
  1048. else if (sizeof(node) <= 32)
  1049. {
  1050. memset(&m_values[0], 0, m_values.size_in_bytes());
  1051. }
  1052. else
  1053. {
  1054. node* p = &get_node(0);
  1055. node* p_end = p + m_values.size();
  1056. uint32_t num_remaining = m_num_valid;
  1057. while (p != p_end)
  1058. {
  1059. if (p->state)
  1060. {
  1061. p->state = cStateInvalid;
  1062. num_remaining--;
  1063. if (!num_remaining)
  1064. break;
  1065. }
  1066. p++;
  1067. }
  1068. }
  1069. m_num_valid = 0;
  1070. }
  1071. inline uint32_t size()
  1072. {
  1073. return m_num_valid;
  1074. }
  1075. inline uint32_t get_table_size()
  1076. {
  1077. return m_values.size();
  1078. }
  1079. inline bool empty()
  1080. {
  1081. return !m_num_valid;
  1082. }
  1083. inline void reserve(uint32_t new_capacity)
  1084. {
  1085. uint64_t new_hash_size = helpers::maximum(1U, new_capacity);
  1086. new_hash_size = new_hash_size * 2ULL;
  1087. if (!helpers::is_power_of_2(new_hash_size))
  1088. new_hash_size = helpers::next_pow2(new_hash_size);
  1089. new_hash_size = helpers::maximum<uint64_t>(cMinHashSize, new_hash_size);
  1090. new_hash_size = helpers::minimum<uint64_t>(0x80000000UL, new_hash_size);
  1091. if (new_hash_size > m_values.size())
  1092. rehash((uint32_t)new_hash_size);
  1093. }
  1094. class iterator
  1095. {
  1096. friend class hash_map<Key, Value, Hasher, Equals>;
  1097. friend class hash_map<Key, Value, Hasher, Equals>::const_iterator;
  1098. public:
  1099. inline iterator() : m_pTable(NULL), m_index(0) { }
  1100. inline iterator(hash_map_type& table, uint32_t index) : m_pTable(&table), m_index(index) { }
  1101. inline iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  1102. inline iterator& operator= (const iterator& other)
  1103. {
  1104. m_pTable = other.m_pTable;
  1105. m_index = other.m_index;
  1106. return *this;
  1107. }
  1108. // post-increment
  1109. inline iterator operator++(int)
  1110. {
  1111. iterator result(*this);
  1112. ++*this;
  1113. return result;
  1114. }
  1115. // pre-increment
  1116. inline iterator& operator++()
  1117. {
  1118. probe();
  1119. return *this;
  1120. }
  1121. inline value_type& operator*() const { return *get_cur(); }
  1122. inline value_type* operator->() const { return get_cur(); }
  1123. inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  1124. inline bool operator != (const iterator& b) const { return !(*this == b); }
  1125. inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  1126. inline bool operator != (const const_iterator& b) const { return !(*this == b); }
  1127. private:
  1128. hash_map_type* m_pTable;
  1129. uint32_t m_index;
  1130. inline value_type* get_cur() const
  1131. {
  1132. assert(m_pTable && (m_index < m_pTable->m_values.size()));
  1133. assert(m_pTable->get_node_state(m_index) == cStateValid);
  1134. return &m_pTable->get_node(m_index);
  1135. }
  1136. inline void probe()
  1137. {
  1138. assert(m_pTable);
  1139. m_index = m_pTable->find_next(m_index);
  1140. }
  1141. };
  1142. class const_iterator
  1143. {
  1144. friend class hash_map<Key, Value, Hasher, Equals>;
  1145. friend class hash_map<Key, Value, Hasher, Equals>::iterator;
  1146. public:
  1147. inline const_iterator() : m_pTable(NULL), m_index(0) { }
  1148. inline const_iterator(const hash_map_type& table, uint32_t index) : m_pTable(&table), m_index(index) { }
  1149. inline const_iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  1150. inline const_iterator(const const_iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  1151. inline const_iterator& operator= (const const_iterator& other)
  1152. {
  1153. m_pTable = other.m_pTable;
  1154. m_index = other.m_index;
  1155. return *this;
  1156. }
  1157. inline const_iterator& operator= (const iterator& other)
  1158. {
  1159. m_pTable = other.m_pTable;
  1160. m_index = other.m_index;
  1161. return *this;
  1162. }
  1163. // post-increment
  1164. inline const_iterator operator++(int)
  1165. {
  1166. const_iterator result(*this);
  1167. ++*this;
  1168. return result;
  1169. }
  1170. // pre-increment
  1171. inline const_iterator& operator++()
  1172. {
  1173. probe();
  1174. return *this;
  1175. }
  1176. inline const value_type& operator*() const { return *get_cur(); }
  1177. inline const value_type* operator->() const { return get_cur(); }
  1178. inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  1179. inline bool operator != (const const_iterator& b) const { return !(*this == b); }
  1180. inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  1181. inline bool operator != (const iterator& b) const { return !(*this == b); }
  1182. private:
  1183. const hash_map_type* m_pTable;
  1184. uint32_t m_index;
  1185. inline const value_type* get_cur() const
  1186. {
  1187. assert(m_pTable && (m_index < m_pTable->m_values.size()));
  1188. assert(m_pTable->get_node_state(m_index) == cStateValid);
  1189. return &m_pTable->get_node(m_index);
  1190. }
  1191. inline void probe()
  1192. {
  1193. assert(m_pTable);
  1194. m_index = m_pTable->find_next(m_index);
  1195. }
  1196. };
  1197. inline const_iterator begin() const
  1198. {
  1199. if (!m_num_valid)
  1200. return end();
  1201. return const_iterator(*this, find_next(UINT32_MAX));
  1202. }
  1203. inline const_iterator end() const
  1204. {
  1205. return const_iterator(*this, m_values.size());
  1206. }
  1207. inline iterator begin()
  1208. {
  1209. if (!m_num_valid)
  1210. return end();
  1211. return iterator(*this, find_next(UINT32_MAX));
  1212. }
  1213. inline iterator end()
  1214. {
  1215. return iterator(*this, m_values.size());
  1216. }
  1217. // insert_result.first will always point to inserted key/value (or the already existing key/value).
  1218. // 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).
  1219. typedef std::pair<iterator, bool> insert_result;
  1220. inline insert_result insert(const Key& k, const Value& v = Value())
  1221. {
  1222. insert_result result;
  1223. if (!insert_no_grow(result, k, v))
  1224. {
  1225. grow();
  1226. // This must succeed.
  1227. if (!insert_no_grow(result, k, v))
  1228. {
  1229. fprintf(stderr, "insert() failed");
  1230. abort();
  1231. }
  1232. }
  1233. return result;
  1234. }
  1235. inline insert_result insert(const value_type& v)
  1236. {
  1237. return insert(v.first, v.second);
  1238. }
  1239. inline const_iterator find(const Key& k) const
  1240. {
  1241. return const_iterator(*this, find_index(k));
  1242. }
  1243. inline iterator find(const Key& k)
  1244. {
  1245. return iterator(*this, find_index(k));
  1246. }
  1247. inline bool erase(const Key& k)
  1248. {
  1249. uint32_t i = find_index(k);
  1250. if (i >= m_values.size())
  1251. return false;
  1252. node* pDst = &get_node(i);
  1253. destruct_value_type(pDst);
  1254. pDst->state = cStateInvalid;
  1255. m_num_valid--;
  1256. for (; ; )
  1257. {
  1258. uint32_t r, j = i;
  1259. node* pSrc = pDst;
  1260. do
  1261. {
  1262. if (!i)
  1263. {
  1264. i = m_values.size() - 1;
  1265. pSrc = &get_node(i);
  1266. }
  1267. else
  1268. {
  1269. i--;
  1270. pSrc--;
  1271. }
  1272. if (!pSrc->state)
  1273. return true;
  1274. r = hash_key(pSrc->first);
  1275. } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
  1276. move_node(pDst, pSrc);
  1277. pDst = pSrc;
  1278. }
  1279. }
  1280. inline void swap(hash_map_type& other)
  1281. {
  1282. m_values.swap(other.m_values);
  1283. std::swap(m_hash_shift, other.m_hash_shift);
  1284. std::swap(m_num_valid, other.m_num_valid);
  1285. std::swap(m_grow_threshold, other.m_grow_threshold);
  1286. std::swap(m_hasher, other.m_hasher);
  1287. std::swap(m_equals, other.m_equals);
  1288. }
  1289. private:
  1290. struct node : public value_type
  1291. {
  1292. uint8_t state;
  1293. };
  1294. static inline void construct_value_type(value_type* pDst, const Key& k, const Value& v)
  1295. {
  1296. if (BASISU_IS_BITWISE_COPYABLE(Key))
  1297. memcpy(&pDst->first, &k, sizeof(Key));
  1298. else
  1299. scalar_type<Key>::construct(&pDst->first, k);
  1300. if (BASISU_IS_BITWISE_COPYABLE(Value))
  1301. memcpy(&pDst->second, &v, sizeof(Value));
  1302. else
  1303. scalar_type<Value>::construct(&pDst->second, v);
  1304. }
  1305. static inline void construct_value_type(value_type* pDst, const value_type* pSrc)
  1306. {
  1307. if ((BASISU_IS_BITWISE_COPYABLE(Key)) && (BASISU_IS_BITWISE_COPYABLE(Value)))
  1308. {
  1309. memcpy(pDst, pSrc, sizeof(value_type));
  1310. }
  1311. else
  1312. {
  1313. if (BASISU_IS_BITWISE_COPYABLE(Key))
  1314. memcpy(&pDst->first, &pSrc->first, sizeof(Key));
  1315. else
  1316. scalar_type<Key>::construct(&pDst->first, pSrc->first);
  1317. if (BASISU_IS_BITWISE_COPYABLE(Value))
  1318. memcpy(&pDst->second, &pSrc->second, sizeof(Value));
  1319. else
  1320. scalar_type<Value>::construct(&pDst->second, pSrc->second);
  1321. }
  1322. }
  1323. static inline void destruct_value_type(value_type* p)
  1324. {
  1325. scalar_type<Key>::destruct(&p->first);
  1326. scalar_type<Value>::destruct(&p->second);
  1327. }
  1328. // Moves *pSrc to *pDst efficiently.
  1329. // pDst should NOT be constructed on entry.
  1330. static inline void move_node(node* pDst, node* pSrc, bool update_src_state = true)
  1331. {
  1332. assert(!pDst->state);
  1333. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key) && BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
  1334. {
  1335. memcpy(pDst, pSrc, sizeof(node));
  1336. }
  1337. else
  1338. {
  1339. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key))
  1340. memcpy(&pDst->first, &pSrc->first, sizeof(Key));
  1341. else
  1342. {
  1343. scalar_type<Key>::construct(&pDst->first, pSrc->first);
  1344. scalar_type<Key>::destruct(&pSrc->first);
  1345. }
  1346. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
  1347. memcpy(&pDst->second, &pSrc->second, sizeof(Value));
  1348. else
  1349. {
  1350. scalar_type<Value>::construct(&pDst->second, pSrc->second);
  1351. scalar_type<Value>::destruct(&pSrc->second);
  1352. }
  1353. pDst->state = cStateValid;
  1354. }
  1355. if (update_src_state)
  1356. pSrc->state = cStateInvalid;
  1357. }
  1358. struct raw_node
  1359. {
  1360. inline raw_node()
  1361. {
  1362. node* p = reinterpret_cast<node*>(this);
  1363. p->state = cStateInvalid;
  1364. }
  1365. inline ~raw_node()
  1366. {
  1367. node* p = reinterpret_cast<node*>(this);
  1368. if (p->state)
  1369. hash_map_type::destruct_value_type(p);
  1370. }
  1371. inline raw_node(const raw_node& other)
  1372. {
  1373. node* pDst = reinterpret_cast<node*>(this);
  1374. const node* pSrc = reinterpret_cast<const node*>(&other);
  1375. if (pSrc->state)
  1376. {
  1377. hash_map_type::construct_value_type(pDst, pSrc);
  1378. pDst->state = cStateValid;
  1379. }
  1380. else
  1381. pDst->state = cStateInvalid;
  1382. }
  1383. inline raw_node& operator= (const raw_node& rhs)
  1384. {
  1385. if (this == &rhs)
  1386. return *this;
  1387. node* pDst = reinterpret_cast<node*>(this);
  1388. const node* pSrc = reinterpret_cast<const node*>(&rhs);
  1389. if (pSrc->state)
  1390. {
  1391. if (pDst->state)
  1392. {
  1393. pDst->first = pSrc->first;
  1394. pDst->second = pSrc->second;
  1395. }
  1396. else
  1397. {
  1398. hash_map_type::construct_value_type(pDst, pSrc);
  1399. pDst->state = cStateValid;
  1400. }
  1401. }
  1402. else if (pDst->state)
  1403. {
  1404. hash_map_type::destruct_value_type(pDst);
  1405. pDst->state = cStateInvalid;
  1406. }
  1407. return *this;
  1408. }
  1409. uint8_t m_bits[sizeof(node)];
  1410. };
  1411. typedef basisu::vector<raw_node> node_vector;
  1412. node_vector m_values;
  1413. uint32_t m_hash_shift;
  1414. Hasher m_hasher;
  1415. Equals m_equals;
  1416. uint32_t m_num_valid;
  1417. uint32_t m_grow_threshold;
  1418. inline uint32_t hash_key(const Key& k) const
  1419. {
  1420. assert((1U << (32U - m_hash_shift)) == m_values.size());
  1421. uint32_t hash = static_cast<uint32_t>(m_hasher(k));
  1422. // Fibonacci hashing
  1423. hash = (2654435769U * hash) >> m_hash_shift;
  1424. assert(hash < m_values.size());
  1425. return hash;
  1426. }
  1427. inline const node& get_node(uint32_t index) const
  1428. {
  1429. return *reinterpret_cast<const node*>(&m_values[index]);
  1430. }
  1431. inline node& get_node(uint32_t index)
  1432. {
  1433. return *reinterpret_cast<node*>(&m_values[index]);
  1434. }
  1435. inline state get_node_state(uint32_t index) const
  1436. {
  1437. return static_cast<state>(get_node(index).state);
  1438. }
  1439. inline void set_node_state(uint32_t index, bool valid)
  1440. {
  1441. get_node(index).state = valid;
  1442. }
  1443. inline void grow()
  1444. {
  1445. uint64_t n = m_values.size() * 3ULL; // was * 2
  1446. if (!helpers::is_power_of_2(n))
  1447. n = helpers::next_pow2(n);
  1448. if (n > 0x80000000UL)
  1449. n = 0x80000000UL;
  1450. rehash(helpers::maximum<uint32_t>(cMinHashSize, (uint32_t)n));
  1451. }
  1452. inline void rehash(uint32_t new_hash_size)
  1453. {
  1454. assert(new_hash_size >= m_num_valid);
  1455. assert(helpers::is_power_of_2(new_hash_size));
  1456. if ((new_hash_size < m_num_valid) || (new_hash_size == m_values.size()))
  1457. return;
  1458. hash_map new_map;
  1459. new_map.m_values.resize(new_hash_size);
  1460. new_map.m_hash_shift = 32U - helpers::floor_log2i(new_hash_size);
  1461. assert(new_hash_size == (1U << (32U - new_map.m_hash_shift)));
  1462. new_map.m_grow_threshold = UINT_MAX;
  1463. node* pNode = reinterpret_cast<node*>(m_values.begin());
  1464. node* pNode_end = pNode + m_values.size();
  1465. while (pNode != pNode_end)
  1466. {
  1467. if (pNode->state)
  1468. {
  1469. new_map.move_into(pNode);
  1470. if (new_map.m_num_valid == m_num_valid)
  1471. break;
  1472. }
  1473. pNode++;
  1474. }
  1475. new_map.m_grow_threshold = (new_hash_size + 1U) >> 1U;
  1476. m_values.clear_no_destruction();
  1477. m_hash_shift = 32;
  1478. swap(new_map);
  1479. }
  1480. inline uint32_t find_next(uint32_t index) const
  1481. {
  1482. index++;
  1483. if (index >= m_values.size())
  1484. return index;
  1485. const node* pNode = &get_node(index);
  1486. for (; ; )
  1487. {
  1488. if (pNode->state)
  1489. break;
  1490. if (++index >= m_values.size())
  1491. break;
  1492. pNode++;
  1493. }
  1494. return index;
  1495. }
  1496. inline uint32_t find_index(const Key& k) const
  1497. {
  1498. if (m_num_valid)
  1499. {
  1500. uint32_t index = hash_key(k);
  1501. const node* pNode = &get_node(index);
  1502. if (pNode->state)
  1503. {
  1504. if (m_equals(pNode->first, k))
  1505. return index;
  1506. const uint32_t orig_index = index;
  1507. for (; ; )
  1508. {
  1509. if (!index)
  1510. {
  1511. index = m_values.size() - 1;
  1512. pNode = &get_node(index);
  1513. }
  1514. else
  1515. {
  1516. index--;
  1517. pNode--;
  1518. }
  1519. if (index == orig_index)
  1520. break;
  1521. if (!pNode->state)
  1522. break;
  1523. if (m_equals(pNode->first, k))
  1524. return index;
  1525. }
  1526. }
  1527. }
  1528. return m_values.size();
  1529. }
  1530. inline bool insert_no_grow(insert_result& result, const Key& k, const Value& v = Value())
  1531. {
  1532. if (!m_values.size())
  1533. return false;
  1534. uint32_t index = hash_key(k);
  1535. node* pNode = &get_node(index);
  1536. if (pNode->state)
  1537. {
  1538. if (m_equals(pNode->first, k))
  1539. {
  1540. result.first = iterator(*this, index);
  1541. result.second = false;
  1542. return true;
  1543. }
  1544. const uint32_t orig_index = index;
  1545. for (; ; )
  1546. {
  1547. if (!index)
  1548. {
  1549. index = m_values.size() - 1;
  1550. pNode = &get_node(index);
  1551. }
  1552. else
  1553. {
  1554. index--;
  1555. pNode--;
  1556. }
  1557. if (orig_index == index)
  1558. return false;
  1559. if (!pNode->state)
  1560. break;
  1561. if (m_equals(pNode->first, k))
  1562. {
  1563. result.first = iterator(*this, index);
  1564. result.second = false;
  1565. return true;
  1566. }
  1567. }
  1568. }
  1569. if (m_num_valid >= m_grow_threshold)
  1570. return false;
  1571. construct_value_type(pNode, k, v);
  1572. pNode->state = cStateValid;
  1573. m_num_valid++;
  1574. assert(m_num_valid <= m_values.size());
  1575. result.first = iterator(*this, index);
  1576. result.second = true;
  1577. return true;
  1578. }
  1579. inline void move_into(node* pNode)
  1580. {
  1581. uint32_t index = hash_key(pNode->first);
  1582. node* pDst_node = &get_node(index);
  1583. if (pDst_node->state)
  1584. {
  1585. const uint32_t orig_index = index;
  1586. for (; ; )
  1587. {
  1588. if (!index)
  1589. {
  1590. index = m_values.size() - 1;
  1591. pDst_node = &get_node(index);
  1592. }
  1593. else
  1594. {
  1595. index--;
  1596. pDst_node--;
  1597. }
  1598. if (index == orig_index)
  1599. {
  1600. assert(false);
  1601. return;
  1602. }
  1603. if (!pDst_node->state)
  1604. break;
  1605. }
  1606. }
  1607. move_node(pDst_node, pNode, false);
  1608. m_num_valid++;
  1609. }
  1610. };
  1611. template<typename Key, typename Value, typename Hasher, typename Equals>
  1612. struct bitwise_movable< hash_map<Key, Value, Hasher, Equals> > { enum { cFlag = true }; };
  1613. #if BASISU_HASHMAP_TEST
  1614. extern void hash_map_test();
  1615. #endif
  1616. } // namespace basisu
  1617. namespace std
  1618. {
  1619. template<typename T>
  1620. inline void swap(basisu::vector<T>& a, basisu::vector<T>& b)
  1621. {
  1622. a.swap(b);
  1623. }
  1624. template<typename Key, typename Value, typename Hasher, typename Equals>
  1625. inline void swap(basisu::hash_map<Key, Value, Hasher, Equals>& a, basisu::hash_map<Key, Value, Hasher, Equals>& b)
  1626. {
  1627. a.swap(b);
  1628. }
  1629. } // namespace std