2
0

basisu_containers.h 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202
  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. #define BASISU_HASHMAP_TEST 0
  23. namespace basisu
  24. {
  25. enum { cInvalidIndex = -1 };
  26. template <typename S> inline S clamp(S value, S low, S high) { return (value < low) ? low : ((value > high) ? high : value); }
  27. template <typename S> inline S maximum(S a, S b) { return (a > b) ? a : b; }
  28. template <typename S> inline S maximum(S a, S b, S c) { return maximum(maximum(a, b), c); }
  29. template <typename S> inline S maximum(S a, S b, S c, S d) { return maximum(maximum(maximum(a, b), c), d); }
  30. template <typename S> inline S minimum(S a, S b) { return (a < b) ? a : b; }
  31. template <typename S> inline S minimum(S a, S b, S c) { return minimum(minimum(a, b), c); }
  32. template <typename S> inline S minimum(S a, S b, S c, S d) { return minimum(minimum(minimum(a, b), c), d); }
  33. #ifdef _MSC_VER
  34. __declspec(noreturn)
  35. #else
  36. [[noreturn]]
  37. #endif
  38. void container_abort(const char* pMsg, ...);
  39. namespace helpers
  40. {
  41. inline bool is_power_of_2(uint32_t x) { return x && ((x & (x - 1U)) == 0U); }
  42. inline bool is_power_of_2(uint64_t x) { return x && ((x & (x - 1U)) == 0U); }
  43. template<class T> const T& minimum(const T& a, const T& b) { return (b < a) ? b : a; }
  44. template<class T> const T& maximum(const T& a, const T& b) { return (a < b) ? b : a; }
  45. inline uint32_t floor_log2i(uint32_t v)
  46. {
  47. uint32_t l = 0;
  48. while (v > 1U)
  49. {
  50. v >>= 1;
  51. l++;
  52. }
  53. return l;
  54. }
  55. inline uint32_t floor_log2i(uint64_t v)
  56. {
  57. uint32_t l = 0;
  58. while (v > 1U)
  59. {
  60. v >>= 1;
  61. l++;
  62. }
  63. return l;
  64. }
  65. inline uint32_t next_pow2(uint32_t val)
  66. {
  67. val--;
  68. val |= val >> 16;
  69. val |= val >> 8;
  70. val |= val >> 4;
  71. val |= val >> 2;
  72. val |= val >> 1;
  73. return val + 1;
  74. }
  75. inline uint64_t next_pow2(uint64_t val)
  76. {
  77. val--;
  78. val |= val >> 32;
  79. val |= val >> 16;
  80. val |= val >> 8;
  81. val |= val >> 4;
  82. val |= val >> 2;
  83. val |= val >> 1;
  84. return val + 1;
  85. }
  86. } // namespace helpers
  87. template <typename T>
  88. inline T* construct(T* p)
  89. {
  90. return new (static_cast<void*>(p)) T;
  91. }
  92. template <typename T, typename U>
  93. inline T* construct(T* p, const U& init)
  94. {
  95. return new (static_cast<void*>(p)) T(init);
  96. }
  97. template <typename T>
  98. inline void construct_array(T* p, size_t n)
  99. {
  100. T* q = p + n;
  101. for (; p != q; ++p)
  102. new (static_cast<void*>(p)) T;
  103. }
  104. template <typename T, typename U>
  105. inline void construct_array(T* p, size_t n, const U& init)
  106. {
  107. T* q = p + n;
  108. for (; p != q; ++p)
  109. new (static_cast<void*>(p)) T(init);
  110. }
  111. template <typename T>
  112. inline void destruct(T* p)
  113. {
  114. p->~T();
  115. }
  116. template <typename T> inline void destruct_array(T* p, size_t n)
  117. {
  118. T* q = p + n;
  119. for (; p != q; ++p)
  120. p->~T();
  121. }
  122. template<typename T>
  123. struct scalar_type
  124. {
  125. enum { cFlag = false };
  126. static inline void construct(T* p) { basisu::construct(p); }
  127. static inline void construct(T* p, const T& init) { basisu::construct(p, init); }
  128. static inline void construct_array(T* p, size_t n) { basisu::construct_array(p, n); }
  129. static inline void destruct(T* p) { basisu::destruct(p); }
  130. static inline void destruct_array(T* p, size_t n) { basisu::destruct_array(p, n); }
  131. };
  132. template<typename T> struct scalar_type<T*>
  133. {
  134. enum { cFlag = true };
  135. static inline void construct(T** p) { memset(p, 0, sizeof(T*)); }
  136. static inline void construct(T** p, T* init) { *p = init; }
  137. static inline void construct_array(T** p, size_t n) { memset(p, 0, sizeof(T*) * n); }
  138. static inline void destruct(T** p) { p; }
  139. static inline void destruct_array(T** p, size_t n) { p, n; }
  140. };
  141. #define BASISU_DEFINE_BUILT_IN_TYPE(X) \
  142. template<> struct scalar_type<X> { \
  143. enum { cFlag = true }; \
  144. static inline void construct(X* p) { memset(p, 0, sizeof(X)); } \
  145. static inline void construct(X* p, const X& init) { memcpy(p, &init, sizeof(X)); } \
  146. static inline void construct_array(X* p, size_t n) { memset(p, 0, sizeof(X) * n); } \
  147. static inline void destruct(X* p) { p; } \
  148. static inline void destruct_array(X* p, size_t n) { p, n; } };
  149. BASISU_DEFINE_BUILT_IN_TYPE(bool)
  150. BASISU_DEFINE_BUILT_IN_TYPE(char)
  151. BASISU_DEFINE_BUILT_IN_TYPE(unsigned char)
  152. BASISU_DEFINE_BUILT_IN_TYPE(short)
  153. BASISU_DEFINE_BUILT_IN_TYPE(unsigned short)
  154. BASISU_DEFINE_BUILT_IN_TYPE(int)
  155. BASISU_DEFINE_BUILT_IN_TYPE(unsigned int)
  156. BASISU_DEFINE_BUILT_IN_TYPE(long)
  157. BASISU_DEFINE_BUILT_IN_TYPE(unsigned long)
  158. #ifdef __GNUC__
  159. BASISU_DEFINE_BUILT_IN_TYPE(long long)
  160. BASISU_DEFINE_BUILT_IN_TYPE(unsigned long long)
  161. #else
  162. BASISU_DEFINE_BUILT_IN_TYPE(__int64)
  163. BASISU_DEFINE_BUILT_IN_TYPE(unsigned __int64)
  164. #endif
  165. BASISU_DEFINE_BUILT_IN_TYPE(float)
  166. BASISU_DEFINE_BUILT_IN_TYPE(double)
  167. BASISU_DEFINE_BUILT_IN_TYPE(long double)
  168. #undef BASISU_DEFINE_BUILT_IN_TYPE
  169. template<typename T>
  170. struct bitwise_movable { enum { cFlag = false }; };
  171. #define BASISU_DEFINE_BITWISE_MOVABLE(Q) template<> struct bitwise_movable<Q> { enum { cFlag = true }; };
  172. template<typename T>
  173. struct bitwise_copyable { enum { cFlag = false }; };
  174. #define BASISU_DEFINE_BITWISE_COPYABLE(Q) template<> struct bitwise_copyable<Q> { enum { cFlag = true }; };
  175. #define BASISU_IS_POD(T) __is_pod(T)
  176. #define BASISU_IS_SCALAR_TYPE(T) (scalar_type<T>::cFlag)
  177. #if !defined(BASISU_HAVE_STD_TRIVIALLY_COPYABLE) && defined(__GNUC__) && (__GNUC__ < 5)
  178. #define BASISU_IS_TRIVIALLY_COPYABLE(...) __is_trivially_copyable(__VA_ARGS__)
  179. #else
  180. #define BASISU_IS_TRIVIALLY_COPYABLE(...) std::is_trivially_copyable<__VA_ARGS__>::value
  181. #endif
  182. // TODO: clean this up, it's still confusing (copying vs. movable).
  183. #define BASISU_IS_BITWISE_COPYABLE(T) (BASISU_IS_SCALAR_TYPE(T) || BASISU_IS_POD(T) || BASISU_IS_TRIVIALLY_COPYABLE(T) || std::is_trivial<T>::value || (bitwise_copyable<T>::cFlag))
  184. #define BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) (BASISU_IS_BITWISE_COPYABLE(T) || (bitwise_movable<T>::cFlag))
  185. #define BASISU_HAS_DESTRUCTOR(T) ((!scalar_type<T>::cFlag) && (!__is_pod(T)) && (!std::is_trivially_destructible<T>::value))
  186. typedef char(&yes_t)[1];
  187. typedef char(&no_t)[2];
  188. template <class U> yes_t class_test(int U::*);
  189. template <class U> no_t class_test(...);
  190. template <class T> struct is_class
  191. {
  192. enum { value = (sizeof(class_test<T>(0)) == sizeof(yes_t)) };
  193. };
  194. template <typename T> struct is_pointer
  195. {
  196. enum { value = false };
  197. };
  198. template <typename T> struct is_pointer<T*>
  199. {
  200. enum { value = true };
  201. };
  202. struct empty_type { };
  203. BASISU_DEFINE_BITWISE_COPYABLE(empty_type);
  204. BASISU_DEFINE_BITWISE_MOVABLE(empty_type);
  205. template<typename T> struct rel_ops
  206. {
  207. friend bool operator!=(const T& x, const T& y) { return (!(x == y)); }
  208. friend bool operator> (const T& x, const T& y) { return (y < x); }
  209. friend bool operator<=(const T& x, const T& y) { return (!(y < x)); }
  210. friend bool operator>=(const T& x, const T& y) { return (!(x < y)); }
  211. };
  212. struct elemental_vector
  213. {
  214. void* m_p;
  215. size_t m_size;
  216. size_t m_capacity;
  217. typedef void (*object_mover)(void* pDst, void* pSrc, size_t num);
  218. bool increase_capacity(size_t min_new_capacity, bool grow_hint, size_t element_size, object_mover pRelocate, bool nofail);
  219. };
  220. // Returns true if a+b would overflow a size_t.
  221. inline bool add_overflow_check(size_t a, size_t b)
  222. {
  223. size_t c = a + b;
  224. return c < a;
  225. }
  226. // Returns false on overflow, true if OK.
  227. template<typename T>
  228. inline bool can_fit_into_size_t(T val)
  229. {
  230. static_assert(std::is_integral<T>::value, "T must be an integral type");
  231. return (val >= 0) && (static_cast<size_t>(val) == val);
  232. }
  233. // Returns true if a*b would overflow a size_t.
  234. inline bool mul_overflow_check(size_t a, size_t b)
  235. {
  236. // Avoid the division on 32-bit platforms
  237. if (sizeof(size_t) == sizeof(uint32_t))
  238. return !can_fit_into_size_t(static_cast<uint64_t>(a) * b);
  239. else
  240. return b && (a > (SIZE_MAX / b));
  241. }
  242. template<typename T>
  243. class writable_span;
  244. template<typename T>
  245. class readable_span
  246. {
  247. public:
  248. using value_type = T;
  249. using size_type = size_t;
  250. using const_pointer = const T*;
  251. using const_reference = const T&;
  252. using const_iterator = const T*;
  253. inline readable_span() :
  254. m_p(nullptr),
  255. m_size(0)
  256. {
  257. }
  258. inline readable_span(const writable_span<T>& other);
  259. inline readable_span& operator= (const writable_span<T>& rhs);
  260. inline readable_span(const_pointer p, size_t n)
  261. {
  262. set(p, n);
  263. }
  264. inline readable_span(const_pointer s, const_pointer e)
  265. {
  266. set(s, e);
  267. }
  268. inline readable_span(const readable_span& other) :
  269. m_p(other.m_p),
  270. m_size(other.m_size)
  271. {
  272. assert(!m_size || m_p);
  273. }
  274. inline readable_span(readable_span&& other) :
  275. m_p(other.m_p),
  276. m_size(other.m_size)
  277. {
  278. assert(!m_size || m_p);
  279. other.m_p = nullptr;
  280. other.m_size = 0;
  281. }
  282. template <size_t N>
  283. inline readable_span(const T(&arr)[N]) :
  284. m_p(arr),
  285. m_size(N)
  286. {
  287. }
  288. template <size_t N>
  289. inline readable_span& set(const T(&arr)[N])
  290. {
  291. m_p = arr;
  292. m_size = N;
  293. return *this;
  294. }
  295. inline readable_span& set(const_pointer p, size_t n)
  296. {
  297. if (!p && n)
  298. {
  299. assert(0);
  300. m_p = nullptr;
  301. m_size = 0;
  302. }
  303. else
  304. {
  305. m_p = p;
  306. m_size = n;
  307. }
  308. return *this;
  309. }
  310. inline readable_span& set(const_pointer s, const_pointer e)
  311. {
  312. if ((e < s) || (!s && e))
  313. {
  314. assert(0);
  315. m_p = nullptr;
  316. m_size = 0;
  317. }
  318. else
  319. {
  320. m_p = s;
  321. m_size = e - s;
  322. }
  323. return *this;
  324. }
  325. inline bool operator== (const readable_span& rhs) const
  326. {
  327. return (m_p == rhs.m_p) && (m_size == rhs.m_size);
  328. }
  329. inline bool operator!= (const readable_span& rhs) const
  330. {
  331. return (m_p != rhs.m_p) || (m_size != rhs.m_size);
  332. }
  333. // only true if the region is totally inside the span
  334. inline bool is_inside_ptr(const_pointer p, size_t n) const
  335. {
  336. if (!is_valid())
  337. {
  338. assert(0);
  339. return false;
  340. }
  341. if (!p)
  342. {
  343. assert(!n);
  344. return false;
  345. }
  346. return (p >= m_p) && ((p + n) <= end());
  347. }
  348. inline bool is_inside(size_t ofs, size_t size) const
  349. {
  350. if (add_overflow_check(ofs, size))
  351. {
  352. assert(0);
  353. return false;
  354. }
  355. if (!is_valid())
  356. {
  357. assert(0);
  358. return false;
  359. }
  360. if ((ofs + size) > m_size)
  361. return false;
  362. return true;
  363. }
  364. inline readable_span subspan(size_t ofs, size_t n) const
  365. {
  366. if (!is_valid())
  367. {
  368. assert(0);
  369. return readable_span((const_pointer)nullptr, (size_t)0);
  370. }
  371. if (add_overflow_check(ofs, n))
  372. {
  373. assert(0);
  374. return readable_span((const_pointer)nullptr, (size_t)0);
  375. }
  376. if ((ofs + n) > m_size)
  377. {
  378. assert(0);
  379. return readable_span((const_pointer)nullptr, (size_t)0);
  380. }
  381. return readable_span(m_p + ofs, n);
  382. }
  383. void clear()
  384. {
  385. m_p = nullptr;
  386. m_size = 0;
  387. }
  388. inline bool empty() const { return !m_size; }
  389. // true if the span is non-nullptr and is not empty
  390. inline bool is_valid() const { return m_p && m_size; }
  391. inline bool is_nullptr() const { return m_p == nullptr; }
  392. inline size_t size() const { return m_size; }
  393. inline size_t size_in_bytes() const { assert(can_fit_into_size_t((uint64_t)m_size * sizeof(T))); return m_size * sizeof(T); }
  394. inline const_pointer get_ptr() const { return m_p; }
  395. inline const_iterator begin() const { return m_p; }
  396. inline const_iterator end() const { assert(m_p || !m_size); return m_p + m_size; }
  397. inline const_iterator cbegin() const { return m_p; }
  398. inline const_iterator cend() const { assert(m_p || !m_size); return m_p + m_size; }
  399. inline const_reference front() const
  400. {
  401. if (!(m_p && m_size))
  402. container_abort("readable_span invalid\n");
  403. return m_p[0];
  404. }
  405. inline const_reference back() const
  406. {
  407. if (!(m_p && m_size))
  408. container_abort("readable_span invalid\n");
  409. return m_p[m_size - 1];
  410. }
  411. inline readable_span& operator= (const readable_span& rhs)
  412. {
  413. m_p = rhs.m_p;
  414. m_size = rhs.m_size;
  415. return *this;
  416. }
  417. inline readable_span& operator= (readable_span&& rhs)
  418. {
  419. if (this != &rhs)
  420. {
  421. m_p = rhs.m_p;
  422. m_size = rhs.m_size;
  423. rhs.m_p = nullptr;
  424. rhs.m_size = 0;
  425. }
  426. return *this;
  427. }
  428. inline const_reference operator* () const
  429. {
  430. if (!(m_p && m_size))
  431. container_abort("readable_span invalid\n");
  432. return *m_p;
  433. }
  434. inline const_pointer operator-> () const
  435. {
  436. if (!(m_p && m_size))
  437. container_abort("readable_span invalid\n");
  438. return m_p;
  439. }
  440. inline readable_span& remove_prefix(size_t n)
  441. {
  442. if ((!m_p) || (n > m_size))
  443. {
  444. assert(0);
  445. return *this;
  446. }
  447. m_p += n;
  448. m_size -= n;
  449. return *this;
  450. }
  451. inline readable_span& remove_suffix(size_t n)
  452. {
  453. if ((!m_p) || (n > m_size))
  454. {
  455. assert(0);
  456. return *this;
  457. }
  458. m_size -= n;
  459. return *this;
  460. }
  461. inline readable_span& enlarge(size_t n)
  462. {
  463. if (!m_p)
  464. {
  465. assert(0);
  466. return *this;
  467. }
  468. if (add_overflow_check(m_size, n))
  469. {
  470. assert(0);
  471. return *this;
  472. }
  473. m_size += n;
  474. return *this;
  475. }
  476. bool copy_from(size_t src_ofs, size_t src_size, T* pDst, size_t dst_ofs) const
  477. {
  478. if (!src_size)
  479. return true;
  480. if (!pDst)
  481. {
  482. assert(0);
  483. return false;
  484. }
  485. if (!is_inside(src_ofs, src_size))
  486. {
  487. assert(0);
  488. return false;
  489. }
  490. const_pointer pS = m_p + src_ofs;
  491. if (BASISU_IS_BITWISE_COPYABLE(T))
  492. {
  493. const uint64_t num_bytes = (uint64_t)src_size * sizeof(T);
  494. if (!can_fit_into_size_t(num_bytes))
  495. {
  496. assert(0);
  497. return false;
  498. }
  499. memcpy(pDst, pS, (size_t)num_bytes);
  500. }
  501. else
  502. {
  503. T* pD = pDst + dst_ofs;
  504. T* pDst_end = pD + src_size;
  505. while (pD != pDst_end)
  506. *pD++ = *pS++;
  507. }
  508. return true;
  509. }
  510. inline const_reference operator[] (size_t idx) const
  511. {
  512. if ((!is_valid()) || (idx >= m_size))
  513. container_abort("readable_span: invalid span or index\n");
  514. return m_p[idx];
  515. }
  516. inline uint16_t read_le16(size_t ofs) const
  517. {
  518. static_assert(sizeof(T) == 1, "T must be byte size");
  519. if (!is_inside(ofs, sizeof(uint16_t)))
  520. {
  521. assert(0);
  522. return false;
  523. }
  524. const uint8_t a = (uint8_t)m_p[ofs];
  525. const uint8_t b = (uint8_t)m_p[ofs + 1];
  526. return a | (b << 8u);
  527. }
  528. template<typename R>
  529. inline R read_val(size_t ofs) const
  530. {
  531. static_assert(sizeof(T) == 1, "T must be byte size");
  532. if (!is_inside(ofs, sizeof(R)))
  533. {
  534. assert(0);
  535. return (R)0;
  536. }
  537. return *reinterpret_cast<const R*>(&m_p[ofs]);
  538. }
  539. inline uint16_t read_be16(size_t ofs) const
  540. {
  541. static_assert(sizeof(T) == 1, "T must be byte size");
  542. if (!is_inside(ofs, sizeof(uint16_t)))
  543. {
  544. assert(0);
  545. return 0;
  546. }
  547. const uint8_t b = (uint8_t)m_p[ofs];
  548. const uint8_t a = (uint8_t)m_p[ofs + 1];
  549. return a | (b << 8u);
  550. }
  551. inline uint32_t read_le32(size_t ofs) const
  552. {
  553. static_assert(sizeof(T) == 1, "T must be byte size");
  554. if (!is_inside(ofs, sizeof(uint32_t)))
  555. {
  556. assert(0);
  557. return 0;
  558. }
  559. const uint8_t a = (uint8_t)m_p[ofs];
  560. const uint8_t b = (uint8_t)m_p[ofs + 1];
  561. const uint8_t c = (uint8_t)m_p[ofs + 2];
  562. const uint8_t d = (uint8_t)m_p[ofs + 3];
  563. return a | (b << 8u) | (c << 16u) | (d << 24u);
  564. }
  565. inline uint32_t read_be32(size_t ofs) const
  566. {
  567. static_assert(sizeof(T) == 1, "T must be byte size");
  568. if (!is_inside(ofs, sizeof(uint32_t)))
  569. {
  570. assert(0);
  571. return 0;
  572. }
  573. const uint8_t d = (uint8_t)m_p[ofs];
  574. const uint8_t c = (uint8_t)m_p[ofs + 1];
  575. const uint8_t b = (uint8_t)m_p[ofs + 2];
  576. const uint8_t a = (uint8_t)m_p[ofs + 3];
  577. return a | (b << 8u) | (c << 16u) | (d << 24u);
  578. }
  579. inline uint64_t read_le64(size_t ofs) const
  580. {
  581. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  582. {
  583. assert(0);
  584. return 0;
  585. }
  586. const uint64_t l = read_le32(ofs);
  587. const uint64_t h = read_le32(ofs + sizeof(uint32_t));
  588. return l | (h << 32u);
  589. }
  590. inline uint64_t read_be64(size_t ofs) const
  591. {
  592. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  593. {
  594. assert(0);
  595. return 0;
  596. }
  597. const uint64_t h = read_be32(ofs);
  598. const uint64_t l = read_be32(ofs + sizeof(uint32_t));
  599. return l | (h << 32u);
  600. }
  601. private:
  602. const_pointer m_p;
  603. size_t m_size;
  604. };
  605. template<typename T>
  606. class writable_span
  607. {
  608. friend readable_span<T>;
  609. public:
  610. using value_type = T;
  611. using size_type = size_t;
  612. using const_pointer = const T*;
  613. using const_reference = const T&;
  614. using const_iterator = const T*;
  615. using pointer = T*;
  616. using reference = T&;
  617. using iterator = T*;
  618. inline writable_span() :
  619. m_p(nullptr),
  620. m_size(0)
  621. {
  622. }
  623. inline writable_span(T* p, size_t n)
  624. {
  625. set(p, n);
  626. }
  627. inline writable_span(T* s, T* e)
  628. {
  629. set(s, e);
  630. }
  631. inline writable_span(const writable_span& other) :
  632. m_p(other.m_p),
  633. m_size(other.m_size)
  634. {
  635. assert(!m_size || m_p);
  636. }
  637. inline writable_span(writable_span&& other) :
  638. m_p(other.m_p),
  639. m_size(other.m_size)
  640. {
  641. assert(!m_size || m_p);
  642. other.m_p = nullptr;
  643. other.m_size = 0;
  644. }
  645. template <size_t N>
  646. inline writable_span(T(&arr)[N]) :
  647. m_p(arr),
  648. m_size(N)
  649. {
  650. }
  651. readable_span<T> get_readable_span() const
  652. {
  653. return readable_span<T>(m_p, m_size);
  654. }
  655. template <size_t N>
  656. inline writable_span& set(T(&arr)[N])
  657. {
  658. m_p = arr;
  659. m_size = N;
  660. return *this;
  661. }
  662. inline writable_span& set(T* p, size_t n)
  663. {
  664. if (!p && n)
  665. {
  666. assert(0);
  667. m_p = nullptr;
  668. m_size = 0;
  669. }
  670. else
  671. {
  672. m_p = p;
  673. m_size = n;
  674. }
  675. return *this;
  676. }
  677. inline writable_span& set(T* s, T* e)
  678. {
  679. if ((e < s) || (!s && e))
  680. {
  681. assert(0);
  682. m_p = nullptr;
  683. m_size = 0;
  684. }
  685. else
  686. {
  687. m_p = s;
  688. m_size = e - s;
  689. }
  690. return *this;
  691. }
  692. inline bool operator== (const writable_span& rhs) const
  693. {
  694. return (m_p == rhs.m_p) && (m_size == rhs.m_size);
  695. }
  696. inline bool operator== (const readable_span<T>& rhs) const
  697. {
  698. return (m_p == rhs.m_p) && (m_size == rhs.m_size);
  699. }
  700. inline bool operator!= (const writable_span& rhs) const
  701. {
  702. return (m_p != rhs.m_p) || (m_size != rhs.m_size);
  703. }
  704. inline bool operator!= (const readable_span<T>& rhs) const
  705. {
  706. return (m_p != rhs.m_p) || (m_size != rhs.m_size);
  707. }
  708. // only true if the region is totally inside the span
  709. inline bool is_inside_ptr(const_pointer p, size_t n) const
  710. {
  711. if (!is_valid())
  712. {
  713. assert(0);
  714. return false;
  715. }
  716. if (!p)
  717. {
  718. assert(!n);
  719. return false;
  720. }
  721. return (p >= m_p) && ((p + n) <= end());
  722. }
  723. inline bool is_inside(size_t ofs, size_t size) const
  724. {
  725. if (add_overflow_check(ofs, size))
  726. {
  727. assert(0);
  728. return false;
  729. }
  730. if (!is_valid())
  731. {
  732. assert(0);
  733. return false;
  734. }
  735. if ((ofs + size) > m_size)
  736. return false;
  737. return true;
  738. }
  739. inline writable_span subspan(size_t ofs, size_t n) const
  740. {
  741. if (!is_valid())
  742. {
  743. assert(0);
  744. return writable_span((T*)nullptr, (size_t)0);
  745. }
  746. if (add_overflow_check(ofs, n))
  747. {
  748. assert(0);
  749. return writable_span((T*)nullptr, (size_t)0);
  750. }
  751. if ((ofs + n) > m_size)
  752. {
  753. assert(0);
  754. return writable_span((T*)nullptr, (size_t)0);
  755. }
  756. return writable_span(m_p + ofs, n);
  757. }
  758. void clear()
  759. {
  760. m_p = nullptr;
  761. m_size = 0;
  762. }
  763. inline bool empty() const { return !m_size; }
  764. // true if the span is non-nullptr and is not empty
  765. inline bool is_valid() const { return m_p && m_size; }
  766. inline bool is_nullptr() const { return m_p == nullptr; }
  767. inline size_t size() const { return m_size; }
  768. inline size_t size_in_bytes() const { assert(can_fit_into_size_t((uint64_t)m_size * sizeof(T))); return m_size * sizeof(T); }
  769. inline T* get_ptr() const { return m_p; }
  770. inline iterator begin() const { return m_p; }
  771. inline iterator end() const { assert(m_p || !m_size); return m_p + m_size; }
  772. inline const_iterator cbegin() const { return m_p; }
  773. inline const_iterator cend() const { assert(m_p || !m_size); return m_p + m_size; }
  774. inline T& front() const
  775. {
  776. if (!(m_p && m_size))
  777. container_abort("writable_span invalid\n");
  778. return m_p[0];
  779. }
  780. inline T& back() const
  781. {
  782. if (!(m_p && m_size))
  783. container_abort("writable_span invalid\n");
  784. return m_p[m_size - 1];
  785. }
  786. inline writable_span& operator= (const writable_span& rhs)
  787. {
  788. m_p = rhs.m_p;
  789. m_size = rhs.m_size;
  790. return *this;
  791. }
  792. inline writable_span& operator= (writable_span&& rhs)
  793. {
  794. if (this != &rhs)
  795. {
  796. m_p = rhs.m_p;
  797. m_size = rhs.m_size;
  798. rhs.m_p = nullptr;
  799. rhs.m_size = 0;
  800. }
  801. return *this;
  802. }
  803. inline T& operator* () const
  804. {
  805. if (!(m_p && m_size))
  806. container_abort("writable_span invalid\n");
  807. return *m_p;
  808. }
  809. inline T* operator-> () const
  810. {
  811. if (!(m_p && m_size))
  812. container_abort("writable_span invalid\n");
  813. return m_p;
  814. }
  815. inline bool set_all(size_t ofs, size_t size, const_reference val)
  816. {
  817. if (!size)
  818. return true;
  819. if (!is_inside(ofs, size))
  820. {
  821. assert(0);
  822. return false;
  823. }
  824. T* pDst = m_p + ofs;
  825. if ((sizeof(T) == sizeof(uint8_t)) && (BASISU_IS_BITWISE_COPYABLE(T)))
  826. {
  827. memset(pDst, (int)((uint8_t)val), size);
  828. }
  829. else
  830. {
  831. T* pDst_end = pDst + size;
  832. while (pDst != pDst_end)
  833. *pDst++ = val;
  834. }
  835. return true;
  836. }
  837. inline bool set_all(const_reference val)
  838. {
  839. return set_all(0, m_size, val);
  840. }
  841. inline writable_span& remove_prefix(size_t n)
  842. {
  843. if ((!m_p) || (n > m_size))
  844. {
  845. assert(0);
  846. return *this;
  847. }
  848. m_p += n;
  849. m_size -= n;
  850. return *this;
  851. }
  852. inline writable_span& remove_suffix(size_t n)
  853. {
  854. if ((!m_p) || (n > m_size))
  855. {
  856. assert(0);
  857. return *this;
  858. }
  859. m_size -= n;
  860. return *this;
  861. }
  862. inline writable_span& enlarge(size_t n)
  863. {
  864. if (!m_p)
  865. {
  866. assert(0);
  867. return *this;
  868. }
  869. if (add_overflow_check(m_size, n))
  870. {
  871. assert(0);
  872. return *this;
  873. }
  874. m_size += n;
  875. return *this;
  876. }
  877. // copy from this span to the destination ptr
  878. bool copy_from(size_t src_ofs, size_t src_size, T* pDst, size_t dst_ofs) const
  879. {
  880. if (!src_size)
  881. return true;
  882. if (!pDst)
  883. {
  884. assert(0);
  885. return false;
  886. }
  887. if (!is_inside(src_ofs, src_size))
  888. {
  889. assert(0);
  890. return false;
  891. }
  892. const_pointer pS = m_p + src_ofs;
  893. if (BASISU_IS_BITWISE_COPYABLE(T))
  894. {
  895. const uint64_t num_bytes = (uint64_t)src_size * sizeof(T);
  896. if (!can_fit_into_size_t(num_bytes))
  897. {
  898. assert(0);
  899. return false;
  900. }
  901. memcpy(pDst, pS, (size_t)num_bytes);
  902. }
  903. else
  904. {
  905. T* pD = pDst + dst_ofs;
  906. T* pDst_end = pD + src_size;
  907. while (pD != pDst_end)
  908. *pD++ = *pS++;
  909. }
  910. return true;
  911. }
  912. // copy from the source ptr into this span
  913. bool copy_into(const_pointer pSrc, size_t src_ofs, size_t src_size, size_t dst_ofs) const
  914. {
  915. if (!src_size)
  916. return true;
  917. if (!pSrc)
  918. {
  919. assert(0);
  920. return false;
  921. }
  922. if (add_overflow_check(src_ofs, src_size) || add_overflow_check(dst_ofs, src_size))
  923. {
  924. assert(0);
  925. return false;
  926. }
  927. if (!is_valid())
  928. {
  929. assert(0);
  930. return false;
  931. }
  932. if (!is_inside(dst_ofs, src_size))
  933. {
  934. assert(0);
  935. return false;
  936. }
  937. const_pointer pS = pSrc + src_ofs;
  938. T* pD = m_p + dst_ofs;
  939. if (BASISU_IS_BITWISE_COPYABLE(T))
  940. {
  941. const uint64_t num_bytes = (uint64_t)src_size * sizeof(T);
  942. if (!can_fit_into_size_t(num_bytes))
  943. {
  944. assert(0);
  945. return false;
  946. }
  947. memcpy(pD, pS, (size_t)num_bytes);
  948. }
  949. else
  950. {
  951. T* pDst_end = pD + src_size;
  952. while (pD != pDst_end)
  953. *pD++ = *pS++;
  954. }
  955. return true;
  956. }
  957. // copy from a source span into this span
  958. bool copy_into(const readable_span<T>& src, size_t src_ofs, size_t src_size, size_t dst_ofs) const
  959. {
  960. if (!src.is_inside(src_ofs, src_size))
  961. {
  962. assert(0);
  963. return false;
  964. }
  965. return copy_into(src.get_ptr(), src_ofs, src_size, dst_ofs);
  966. }
  967. // copy from a source span into this span
  968. bool copy_into(const writable_span& src, size_t src_ofs, size_t src_size, size_t dst_ofs) const
  969. {
  970. if (!src.is_inside(src_ofs, src_size))
  971. {
  972. assert(0);
  973. return false;
  974. }
  975. return copy_into(src.get_ptr(), src_ofs, src_size, dst_ofs);
  976. }
  977. inline T& operator[] (size_t idx) const
  978. {
  979. if ((!is_valid()) || (idx >= m_size))
  980. container_abort("writable_span: invalid span or index\n");
  981. return m_p[idx];
  982. }
  983. template<typename R>
  984. inline R read_val(size_t ofs) const
  985. {
  986. static_assert(sizeof(T) == 1, "T must be byte size");
  987. if (!is_inside(ofs, sizeof(R)))
  988. {
  989. assert(0);
  990. return (R)0;
  991. }
  992. return *reinterpret_cast<const R*>(&m_p[ofs]);
  993. }
  994. template<typename R>
  995. inline bool write_val(size_t ofs, R val) const
  996. {
  997. static_assert(sizeof(T) == 1, "T must be byte size");
  998. if (!is_inside(ofs, sizeof(R)))
  999. {
  1000. assert(0);
  1001. return false;
  1002. }
  1003. *reinterpret_cast<R*>(&m_p[ofs]) = val;
  1004. return true;
  1005. }
  1006. inline bool write_le16(size_t ofs, uint16_t val) const
  1007. {
  1008. static_assert(sizeof(T) == 1, "T must be byte size");
  1009. if (!is_inside(ofs, sizeof(uint16_t)))
  1010. {
  1011. assert(0);
  1012. return false;
  1013. }
  1014. m_p[ofs] = (uint8_t)val;
  1015. m_p[ofs + 1] = (uint8_t)(val >> 8u);
  1016. return true;
  1017. }
  1018. inline bool write_be16(size_t ofs, uint16_t val) const
  1019. {
  1020. static_assert(sizeof(T) == 1, "T must be byte size");
  1021. if (!is_inside(ofs, sizeof(uint16_t)))
  1022. {
  1023. assert(0);
  1024. return false;
  1025. }
  1026. m_p[ofs + 1] = (uint8_t)val;
  1027. m_p[ofs] = (uint8_t)(val >> 8u);
  1028. return true;
  1029. }
  1030. inline bool write_le32(size_t ofs, uint32_t val) const
  1031. {
  1032. static_assert(sizeof(T) == 1, "T must be byte size");
  1033. if (!is_inside(ofs, sizeof(uint32_t)))
  1034. {
  1035. assert(0);
  1036. return false;
  1037. }
  1038. m_p[ofs] = (uint8_t)val;
  1039. m_p[ofs + 1] = (uint8_t)(val >> 8u);
  1040. m_p[ofs + 2] = (uint8_t)(val >> 16u);
  1041. m_p[ofs + 3] = (uint8_t)(val >> 24u);
  1042. return true;
  1043. }
  1044. inline bool write_be32(size_t ofs, uint32_t val) const
  1045. {
  1046. static_assert(sizeof(T) == 1, "T must be byte size");
  1047. if (!is_inside(ofs, sizeof(uint32_t)))
  1048. {
  1049. assert(0);
  1050. return false;
  1051. }
  1052. m_p[ofs + 3] = (uint8_t)val;
  1053. m_p[ofs + 2] = (uint8_t)(val >> 8u);
  1054. m_p[ofs + 1] = (uint8_t)(val >> 16u);
  1055. m_p[ofs] = (uint8_t)(val >> 24u);
  1056. return true;
  1057. }
  1058. inline bool write_le64(size_t ofs, uint64_t val) const
  1059. {
  1060. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  1061. {
  1062. assert(0);
  1063. return false;
  1064. }
  1065. return write_le32(ofs, (uint32_t)val) && write_le32(ofs + sizeof(uint32_t), (uint32_t)(val >> 32u));
  1066. }
  1067. inline bool write_be64(size_t ofs, uint64_t val) const
  1068. {
  1069. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  1070. {
  1071. assert(0);
  1072. return false;
  1073. }
  1074. return write_be32(ofs + sizeof(uint32_t), (uint32_t)val) && write_be32(ofs, (uint32_t)(val >> 32u));
  1075. }
  1076. inline uint16_t read_le16(size_t ofs) const
  1077. {
  1078. static_assert(sizeof(T) == 1, "T must be byte size");
  1079. if (!is_inside(ofs, sizeof(uint16_t)))
  1080. {
  1081. assert(0);
  1082. return 0;
  1083. }
  1084. const uint8_t a = (uint8_t)m_p[ofs];
  1085. const uint8_t b = (uint8_t)m_p[ofs + 1];
  1086. return a | (b << 8u);
  1087. }
  1088. inline uint16_t read_be16(size_t ofs) const
  1089. {
  1090. static_assert(sizeof(T) == 1, "T must be byte size");
  1091. if (!is_inside(ofs, sizeof(uint16_t)))
  1092. {
  1093. assert(0);
  1094. return 0;
  1095. }
  1096. const uint8_t b = (uint8_t)m_p[ofs];
  1097. const uint8_t a = (uint8_t)m_p[ofs + 1];
  1098. return a | (b << 8u);
  1099. }
  1100. inline uint32_t read_le32(size_t ofs) const
  1101. {
  1102. static_assert(sizeof(T) == 1, "T must be byte size");
  1103. if (!is_inside(ofs, sizeof(uint32_t)))
  1104. {
  1105. assert(0);
  1106. return 0;
  1107. }
  1108. const uint8_t a = (uint8_t)m_p[ofs];
  1109. const uint8_t b = (uint8_t)m_p[ofs + 1];
  1110. const uint8_t c = (uint8_t)m_p[ofs + 2];
  1111. const uint8_t d = (uint8_t)m_p[ofs + 3];
  1112. return a | (b << 8u) | (c << 16u) | (d << 24u);
  1113. }
  1114. inline uint32_t read_be32(size_t ofs) const
  1115. {
  1116. static_assert(sizeof(T) == 1, "T must be byte size");
  1117. if (!is_inside(ofs, sizeof(uint32_t)))
  1118. {
  1119. assert(0);
  1120. return 0;
  1121. }
  1122. const uint8_t d = (uint8_t)m_p[ofs];
  1123. const uint8_t c = (uint8_t)m_p[ofs + 1];
  1124. const uint8_t b = (uint8_t)m_p[ofs + 2];
  1125. const uint8_t a = (uint8_t)m_p[ofs + 3];
  1126. return a | (b << 8u) | (c << 16u) | (d << 24u);
  1127. }
  1128. inline uint64_t read_le64(size_t ofs) const
  1129. {
  1130. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  1131. {
  1132. assert(0);
  1133. return 0;
  1134. }
  1135. const uint64_t l = read_le32(ofs);
  1136. const uint64_t h = read_le32(ofs + sizeof(uint32_t));
  1137. return l | (h << 32u);
  1138. }
  1139. inline uint64_t read_be64(size_t ofs) const
  1140. {
  1141. if (!add_overflow_check(ofs, sizeof(uint64_t)))
  1142. {
  1143. assert(0);
  1144. return 0;
  1145. }
  1146. const uint64_t h = read_be32(ofs);
  1147. const uint64_t l = read_be32(ofs + sizeof(uint32_t));
  1148. return l | (h << 32u);
  1149. }
  1150. private:
  1151. T* m_p;
  1152. size_t m_size;
  1153. };
  1154. template<typename T>
  1155. inline readable_span<T>::readable_span(const writable_span<T>& other) :
  1156. m_p(other.m_p),
  1157. m_size(other.m_size)
  1158. {
  1159. }
  1160. template<typename T>
  1161. inline readable_span<T>& readable_span<T>::operator= (const writable_span<T>& rhs)
  1162. {
  1163. m_p = rhs.m_p;
  1164. m_size = rhs.m_size;
  1165. return *this;
  1166. }
  1167. template<typename T>
  1168. inline bool span_copy(const writable_span<T>& dst, const readable_span<T>& src)
  1169. {
  1170. return dst.copy_into(src, 0, src.size(), 0);
  1171. }
  1172. template<typename T>
  1173. inline bool span_copy(const writable_span<T>& dst, const writable_span<T>& src)
  1174. {
  1175. return dst.copy_into(src, 0, src.size(), 0);
  1176. }
  1177. template<typename T>
  1178. inline bool span_copy(const writable_span<T>& dst, size_t dst_ofs, const writable_span<T>& src, size_t src_ofs, size_t len)
  1179. {
  1180. return dst.copy_into(src, src_ofs, len, dst_ofs);
  1181. }
  1182. template<typename T>
  1183. inline bool span_copy(const writable_span<T>& dst, size_t dst_ofs, const readable_span<T>& src, size_t src_ofs, size_t len)
  1184. {
  1185. return dst.copy_into(src, src_ofs, len, dst_ofs);
  1186. }
  1187. template<typename T>
  1188. class vector : public rel_ops< vector<T> >
  1189. {
  1190. public:
  1191. typedef T* iterator;
  1192. typedef const T* const_iterator;
  1193. typedef T value_type;
  1194. typedef T& reference;
  1195. typedef const T& const_reference;
  1196. typedef T* pointer;
  1197. typedef const T* const_pointer;
  1198. inline vector() :
  1199. m_p(nullptr),
  1200. m_size(0),
  1201. m_capacity(0)
  1202. {
  1203. }
  1204. inline vector(size_t n, const T& init) :
  1205. m_p(nullptr),
  1206. m_size(0),
  1207. m_capacity(0)
  1208. {
  1209. increase_capacity(n, false);
  1210. construct_array(m_p, n, init);
  1211. m_size = n;
  1212. }
  1213. inline vector(vector&& other) :
  1214. m_p(other.m_p),
  1215. m_size(other.m_size),
  1216. m_capacity(other.m_capacity)
  1217. {
  1218. other.m_p = nullptr;
  1219. other.m_size = 0;
  1220. other.m_capacity = 0;
  1221. }
  1222. inline vector(const vector& other) :
  1223. m_p(nullptr),
  1224. m_size(0),
  1225. m_capacity(0)
  1226. {
  1227. increase_capacity(other.m_size, false);
  1228. m_size = other.m_size;
  1229. if (BASISU_IS_BITWISE_COPYABLE(T))
  1230. {
  1231. #ifndef __EMSCRIPTEN__
  1232. #ifdef __GNUC__
  1233. #pragma GCC diagnostic push
  1234. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  1235. #endif
  1236. #endif
  1237. if ((m_p) && (other.m_p))
  1238. {
  1239. memcpy(m_p, other.m_p, m_size * sizeof(T));
  1240. }
  1241. #ifndef __EMSCRIPTEN__
  1242. #ifdef __GNUC__
  1243. #pragma GCC diagnostic pop
  1244. #endif
  1245. #endif
  1246. }
  1247. else
  1248. {
  1249. T* pDst = m_p;
  1250. const T* pSrc = other.m_p;
  1251. for (size_t i = m_size; i > 0; i--)
  1252. construct(pDst++, *pSrc++);
  1253. }
  1254. }
  1255. inline explicit vector(size_t size) :
  1256. m_p(nullptr),
  1257. m_size(0),
  1258. m_capacity(0)
  1259. {
  1260. resize(size);
  1261. }
  1262. inline explicit vector(std::initializer_list<T> init_list) :
  1263. m_p(nullptr),
  1264. m_size(0),
  1265. m_capacity(0)
  1266. {
  1267. resize(init_list.size());
  1268. size_t idx = 0;
  1269. for (const T& elem : init_list)
  1270. m_p[idx++] = elem;
  1271. assert(idx == m_size);
  1272. }
  1273. inline vector(const readable_span<T>& rs) :
  1274. m_p(nullptr),
  1275. m_size(0),
  1276. m_capacity(0)
  1277. {
  1278. set(rs);
  1279. }
  1280. inline vector(const writable_span<T>& ws) :
  1281. m_p(nullptr),
  1282. m_size(0),
  1283. m_capacity(0)
  1284. {
  1285. set(ws);
  1286. }
  1287. // Set contents of vector to contents of the readable span
  1288. bool set(const readable_span<T>& rs)
  1289. {
  1290. if (!rs.is_valid())
  1291. {
  1292. assert(0);
  1293. return false;
  1294. }
  1295. const size_t new_size = rs.size();
  1296. // Could call resize(), but it'll redundantly construct trivial types.
  1297. if (m_size != new_size)
  1298. {
  1299. if (new_size < m_size)
  1300. {
  1301. if (BASISU_HAS_DESTRUCTOR(T))
  1302. {
  1303. scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
  1304. }
  1305. }
  1306. else
  1307. {
  1308. if (new_size > m_capacity)
  1309. {
  1310. if (!increase_capacity(new_size, false, true))
  1311. return false;
  1312. }
  1313. }
  1314. // Don't bother constructing trivial types, because we're going to memcpy() over them anyway.
  1315. if (!BASISU_IS_BITWISE_COPYABLE(T))
  1316. {
  1317. scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
  1318. }
  1319. m_size = new_size;
  1320. }
  1321. if (!rs.copy_from(0, rs.size(), m_p, 0))
  1322. {
  1323. assert(0);
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. // Set contents of vector to contents of the writable span
  1329. inline bool set(const writable_span<T>& ws)
  1330. {
  1331. return set(ws.get_readable_span());
  1332. }
  1333. inline ~vector()
  1334. {
  1335. if (m_p)
  1336. {
  1337. if (BASISU_HAS_DESTRUCTOR(T))
  1338. {
  1339. scalar_type<T>::destruct_array(m_p, m_size);
  1340. }
  1341. free(m_p);
  1342. }
  1343. }
  1344. inline vector& operator= (const vector& other)
  1345. {
  1346. if (this == &other)
  1347. return *this;
  1348. if (m_capacity >= other.m_size)
  1349. resize(0);
  1350. else
  1351. {
  1352. clear();
  1353. increase_capacity(other.m_size, false);
  1354. }
  1355. if (BASISU_IS_BITWISE_COPYABLE(T))
  1356. {
  1357. #ifndef __EMSCRIPTEN__
  1358. #ifdef __GNUC__
  1359. #pragma GCC diagnostic push
  1360. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  1361. #endif
  1362. #endif
  1363. if ((m_p) && (other.m_p))
  1364. memcpy(m_p, other.m_p, other.m_size * sizeof(T));
  1365. #ifndef __EMSCRIPTEN__
  1366. #ifdef __GNUC__
  1367. #pragma GCC diagnostic pop
  1368. #endif
  1369. #endif
  1370. }
  1371. else
  1372. {
  1373. T* pDst = m_p;
  1374. const T* pSrc = other.m_p;
  1375. for (size_t i = other.m_size; i > 0; i--)
  1376. construct(pDst++, *pSrc++);
  1377. }
  1378. m_size = other.m_size;
  1379. return *this;
  1380. }
  1381. inline vector& operator= (vector&& rhs)
  1382. {
  1383. if (this != &rhs)
  1384. {
  1385. clear();
  1386. m_p = rhs.m_p;
  1387. m_size = rhs.m_size;
  1388. m_capacity = rhs.m_capacity;
  1389. rhs.m_p = nullptr;
  1390. rhs.m_size = 0;
  1391. rhs.m_capacity = 0;
  1392. }
  1393. return *this;
  1394. }
  1395. BASISU_FORCE_INLINE const T* begin() const { return m_p; }
  1396. BASISU_FORCE_INLINE T* begin() { return m_p; }
  1397. BASISU_FORCE_INLINE const T* end() const { return m_p + m_size; }
  1398. BASISU_FORCE_INLINE T* end() { return m_p + m_size; }
  1399. BASISU_FORCE_INLINE bool empty() const { return !m_size; }
  1400. BASISU_FORCE_INLINE size_t size() const { return m_size; }
  1401. BASISU_FORCE_INLINE uint32_t size_u32() const { assert(m_size <= UINT32_MAX); return static_cast<uint32_t>(m_size); }
  1402. BASISU_FORCE_INLINE size_t size_in_bytes() const { return m_size * sizeof(T); }
  1403. BASISU_FORCE_INLINE uint32_t size_in_bytes_u32() const { assert((m_size * sizeof(T)) <= UINT32_MAX); return static_cast<uint32_t>(m_size * sizeof(T)); }
  1404. BASISU_FORCE_INLINE size_t capacity() const { return m_capacity; }
  1405. #if !BASISU_VECTOR_FORCE_CHECKING
  1406. BASISU_FORCE_INLINE const T& operator[] (size_t i) const { assert(i < m_size); return m_p[i]; }
  1407. BASISU_FORCE_INLINE T& operator[] (size_t i) { assert(i < m_size); return m_p[i]; }
  1408. #else
  1409. BASISU_FORCE_INLINE const T& operator[] (size_t i) const
  1410. {
  1411. if (i >= m_size)
  1412. container_abort("vector::operator[] invalid index: %zu, max entries %u, type size %zu\n", i, m_size, sizeof(T));
  1413. return m_p[i];
  1414. }
  1415. BASISU_FORCE_INLINE T& operator[] (size_t i)
  1416. {
  1417. if (i >= m_size)
  1418. container_abort("vector::operator[] invalid index: %zu, max entries %u, type size %zu\n", i, m_size, sizeof(T));
  1419. return m_p[i];
  1420. }
  1421. #endif
  1422. // at() always includes range checking, even in final builds, unlike operator [].
  1423. BASISU_FORCE_INLINE const T& at(size_t i) const
  1424. {
  1425. if (i >= m_size)
  1426. container_abort("vector::at() invalid index: %zu, max entries %u, type size %zu\n", i, m_size, sizeof(T));
  1427. return m_p[i];
  1428. }
  1429. BASISU_FORCE_INLINE T& at(size_t i)
  1430. {
  1431. if (i >= m_size)
  1432. container_abort("vector::at() invalid index: %zu, max entries %u, type size %zu\n", i, m_size, sizeof(T));
  1433. return m_p[i];
  1434. }
  1435. #if !BASISU_VECTOR_FORCE_CHECKING
  1436. BASISU_FORCE_INLINE const T& front() const { assert(m_size); return m_p[0]; }
  1437. BASISU_FORCE_INLINE T& front() { assert(m_size); return m_p[0]; }
  1438. BASISU_FORCE_INLINE const T& back() const { assert(m_size); return m_p[m_size - 1]; }
  1439. BASISU_FORCE_INLINE T& back() { assert(m_size); return m_p[m_size - 1]; }
  1440. #else
  1441. BASISU_FORCE_INLINE const T& front() const
  1442. {
  1443. if (!m_size)
  1444. container_abort("front: vector is empty, type size %zu\n", sizeof(T));
  1445. return m_p[0];
  1446. }
  1447. BASISU_FORCE_INLINE T& front()
  1448. {
  1449. if (!m_size)
  1450. container_abort("front: vector is empty, type size %zu\n", sizeof(T));
  1451. return m_p[0];
  1452. }
  1453. BASISU_FORCE_INLINE const T& back() const
  1454. {
  1455. if (!m_size)
  1456. container_abort("back: vector is empty, type size %zu\n", sizeof(T));
  1457. return m_p[m_size - 1];
  1458. }
  1459. BASISU_FORCE_INLINE T& back()
  1460. {
  1461. if (!m_size)
  1462. container_abort("back: vector is empty, type size %zu\n", sizeof(T));
  1463. return m_p[m_size - 1];
  1464. }
  1465. #endif
  1466. BASISU_FORCE_INLINE const T* get_ptr() const { return m_p; }
  1467. BASISU_FORCE_INLINE T* get_ptr() { return m_p; }
  1468. BASISU_FORCE_INLINE const T* data() const { return m_p; }
  1469. BASISU_FORCE_INLINE T* data() { return m_p; }
  1470. // clear() sets the container to empty, then frees the allocated block.
  1471. inline void clear()
  1472. {
  1473. if (m_p)
  1474. {
  1475. if (BASISU_HAS_DESTRUCTOR(T))
  1476. {
  1477. scalar_type<T>::destruct_array(m_p, m_size);
  1478. }
  1479. free(m_p);
  1480. m_p = nullptr;
  1481. m_size = 0;
  1482. m_capacity = 0;
  1483. }
  1484. }
  1485. inline void clear_no_destruction()
  1486. {
  1487. if (m_p)
  1488. {
  1489. free(m_p);
  1490. m_p = nullptr;
  1491. m_size = 0;
  1492. m_capacity = 0;
  1493. }
  1494. }
  1495. inline void reserve(size_t new_capacity)
  1496. {
  1497. if (!try_reserve(new_capacity))
  1498. container_abort("vector:reserve: try_reserve failed!\n");
  1499. }
  1500. inline bool try_reserve(size_t new_capacity)
  1501. {
  1502. if (new_capacity > m_capacity)
  1503. {
  1504. if (!increase_capacity(new_capacity, false, true))
  1505. return false;
  1506. }
  1507. else if (new_capacity < m_capacity)
  1508. {
  1509. // Must work around the lack of a "decrease_capacity()" method.
  1510. // This case is rare enough in practice that it's probably not worth implementing an optimized in-place resize.
  1511. vector tmp;
  1512. if (!tmp.increase_capacity(helpers::maximum(m_size, new_capacity), false, true))
  1513. return false;
  1514. tmp = *this;
  1515. swap(tmp);
  1516. }
  1517. return true;
  1518. }
  1519. // try_resize(0) sets the container to empty, but does not free the allocated block.
  1520. inline bool try_resize(size_t new_size, bool grow_hint = false)
  1521. {
  1522. if (m_size != new_size)
  1523. {
  1524. if (new_size < m_size)
  1525. {
  1526. if (BASISU_HAS_DESTRUCTOR(T))
  1527. {
  1528. scalar_type<T>::destruct_array(m_p + new_size, m_size - new_size);
  1529. }
  1530. }
  1531. else
  1532. {
  1533. if (new_size > m_capacity)
  1534. {
  1535. if (!increase_capacity(new_size, (new_size == (m_size + 1)) || grow_hint, true))
  1536. return false;
  1537. }
  1538. scalar_type<T>::construct_array(m_p + m_size, new_size - m_size);
  1539. }
  1540. m_size = new_size;
  1541. }
  1542. return true;
  1543. }
  1544. // resize(0) sets the container to empty, but does not free the allocated block.
  1545. inline void resize(size_t new_size, bool grow_hint = false)
  1546. {
  1547. if (!try_resize(new_size, grow_hint))
  1548. container_abort("vector::resize failed, new size %zu\n", new_size);
  1549. }
  1550. // 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).
  1551. // Otherwise it blows away the allocated block. See http://www.codercorner.com/blog/?p=494
  1552. inline void reset()
  1553. {
  1554. if (m_size >= (m_capacity >> 1))
  1555. resize(0);
  1556. else
  1557. clear();
  1558. }
  1559. inline T* try_enlarge(size_t i)
  1560. {
  1561. size_t cur_size = m_size;
  1562. if (add_overflow_check(cur_size, i))
  1563. return nullptr;
  1564. if (!try_resize(cur_size + i, true))
  1565. return nullptr;
  1566. return get_ptr() + cur_size;
  1567. }
  1568. inline T* enlarge(size_t i)
  1569. {
  1570. T* p = try_enlarge(i);
  1571. if (!p)
  1572. container_abort("vector::enlarge failed, amount %zu!\n", i);
  1573. return p;
  1574. }
  1575. BASISU_FORCE_INLINE void push_back(const T& obj)
  1576. {
  1577. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  1578. if (m_size >= m_capacity)
  1579. {
  1580. if (add_overflow_check(m_size, 1))
  1581. container_abort("vector::push_back: vector too large\n");
  1582. increase_capacity(m_size + 1, true);
  1583. }
  1584. scalar_type<T>::construct(m_p + m_size, obj);
  1585. m_size++;
  1586. }
  1587. BASISU_FORCE_INLINE void push_back_value(T&& obj)
  1588. {
  1589. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  1590. if (m_size >= m_capacity)
  1591. {
  1592. if (add_overflow_check(m_size, 1))
  1593. container_abort("vector::push_back_value: vector too large\n");
  1594. increase_capacity(m_size + 1, true);
  1595. }
  1596. new ((void*)(m_p + m_size)) T(std::move(obj));
  1597. m_size++;
  1598. }
  1599. inline bool try_push_back(const T& obj)
  1600. {
  1601. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  1602. if (m_size >= m_capacity)
  1603. {
  1604. if (add_overflow_check(m_size, 1))
  1605. return false;
  1606. if (!increase_capacity(m_size + 1, true, true))
  1607. return false;
  1608. }
  1609. scalar_type<T>::construct(m_p + m_size, obj);
  1610. m_size++;
  1611. return true;
  1612. }
  1613. inline bool try_push_back(T&& obj)
  1614. {
  1615. assert(!m_p || (&obj < m_p) || (&obj >= (m_p + m_size)));
  1616. if (m_size >= m_capacity)
  1617. {
  1618. if (add_overflow_check(m_size, 1))
  1619. return false;
  1620. if (!increase_capacity(m_size + 1, true, true))
  1621. return false;
  1622. }
  1623. new ((void*)(m_p + m_size)) T(std::move(obj));
  1624. m_size++;
  1625. return true;
  1626. }
  1627. // obj is explictly passed in by value, not ref
  1628. inline void push_back_value(T obj)
  1629. {
  1630. if (m_size >= m_capacity)
  1631. {
  1632. if (add_overflow_check(m_size, 1))
  1633. container_abort("vector::push_back_value: vector too large\n");
  1634. increase_capacity(m_size + 1, true);
  1635. }
  1636. scalar_type<T>::construct(m_p + m_size, obj);
  1637. m_size++;
  1638. }
  1639. // obj is explictly passed in by value, not ref
  1640. inline bool try_push_back_value(T obj)
  1641. {
  1642. if (m_size >= m_capacity)
  1643. {
  1644. if (add_overflow_check(m_size, 1))
  1645. return false;
  1646. if (!increase_capacity(m_size + 1, true, true))
  1647. return false;
  1648. }
  1649. scalar_type<T>::construct(m_p + m_size, obj);
  1650. m_size++;
  1651. return true;
  1652. }
  1653. template<typename... Args>
  1654. BASISU_FORCE_INLINE void emplace_back(Args&&... args)
  1655. {
  1656. if (m_size >= m_capacity)
  1657. {
  1658. if (add_overflow_check(m_size, 1))
  1659. container_abort("vector::enlarge: vector too large\n");
  1660. increase_capacity(m_size + 1, true);
  1661. }
  1662. new ((void*)(m_p + m_size)) T(std::forward<Args>(args)...); // perfect forwarding
  1663. m_size++;
  1664. }
  1665. template<typename... Args>
  1666. BASISU_FORCE_INLINE bool try_emplace_back(Args&&... args)
  1667. {
  1668. if (m_size >= m_capacity)
  1669. {
  1670. if (add_overflow_check(m_size, 1))
  1671. return false;
  1672. if (!increase_capacity(m_size + 1, true, true))
  1673. return false;
  1674. }
  1675. new ((void*)(m_p + m_size)) T(std::forward<Args>(args)...); // perfect forwarding
  1676. m_size++;
  1677. return true;
  1678. }
  1679. inline void pop_back()
  1680. {
  1681. assert(m_size);
  1682. if (m_size)
  1683. {
  1684. m_size--;
  1685. scalar_type<T>::destruct(&m_p[m_size]);
  1686. }
  1687. }
  1688. inline bool try_insert(size_t index, const T* p, size_t n)
  1689. {
  1690. assert(index <= m_size);
  1691. if (index > m_size)
  1692. return false;
  1693. if (!n)
  1694. return true;
  1695. const size_t orig_size = m_size;
  1696. if (add_overflow_check(m_size, n))
  1697. return false;
  1698. if (!try_resize(m_size + n, true))
  1699. return false;
  1700. const size_t num_to_move = orig_size - index;
  1701. if (BASISU_IS_BITWISE_COPYABLE(T))
  1702. {
  1703. // This overwrites the destination object bits, but bitwise copyable means we don't need to worry about destruction.
  1704. memmove(m_p + index + n, m_p + index, sizeof(T) * num_to_move);
  1705. }
  1706. else
  1707. {
  1708. const T* pSrc = m_p + orig_size - 1;
  1709. T* pDst = const_cast<T*>(pSrc) + n;
  1710. for (size_t i = 0; i < num_to_move; i++)
  1711. {
  1712. assert((uint64_t)(pDst - m_p) < (uint64_t)m_size);
  1713. *pDst = std::move(*pSrc);
  1714. pDst--;
  1715. pSrc--;
  1716. }
  1717. }
  1718. T* pDst = m_p + index;
  1719. if (BASISU_IS_BITWISE_COPYABLE(T))
  1720. {
  1721. // This copies in the new bits, overwriting the existing objects, which is OK for copyable types that don't need destruction.
  1722. memcpy(pDst, p, sizeof(T) * n);
  1723. }
  1724. else
  1725. {
  1726. for (size_t i = 0; i < n; i++)
  1727. {
  1728. assert((uint64_t)(pDst - m_p) < (uint64_t)m_size);
  1729. *pDst++ = *p++;
  1730. }
  1731. }
  1732. return true;
  1733. }
  1734. inline void insert(size_t index, const T* p, size_t n)
  1735. {
  1736. if (!try_insert(index, p, n))
  1737. container_abort("vector::insert() failed!\n");
  1738. }
  1739. inline bool try_insert(T* p, const T& obj)
  1740. {
  1741. if (p < begin())
  1742. {
  1743. assert(0);
  1744. return false;
  1745. }
  1746. uint64_t ofs = p - begin();
  1747. if (ofs > m_size)
  1748. {
  1749. assert(0);
  1750. return false;
  1751. }
  1752. if ((size_t)ofs != ofs)
  1753. {
  1754. assert(0);
  1755. return false;
  1756. }
  1757. return try_insert((size_t)ofs, &obj, 1);
  1758. }
  1759. inline void insert(T* p, const T& obj)
  1760. {
  1761. if (!try_insert(p, obj))
  1762. container_abort("vector::insert() failed!\n");
  1763. }
  1764. // push_front() isn't going to be very fast - it's only here for usability.
  1765. inline void push_front(const T& obj)
  1766. {
  1767. insert(0, &obj, 1);
  1768. }
  1769. inline bool try_push_front(const T& obj)
  1770. {
  1771. return try_insert(0, &obj, 1);
  1772. }
  1773. vector& append(const vector& other)
  1774. {
  1775. if (other.m_size)
  1776. insert(m_size, &other[0], other.m_size);
  1777. return *this;
  1778. }
  1779. bool try_append(const vector& other)
  1780. {
  1781. if (other.m_size)
  1782. return try_insert(m_size, &other[0], other.m_size);
  1783. return true;
  1784. }
  1785. vector& append(const T* p, size_t n)
  1786. {
  1787. if (n)
  1788. insert(m_size, p, n);
  1789. return *this;
  1790. }
  1791. bool try_append(const T* p, size_t n)
  1792. {
  1793. if (n)
  1794. return try_insert(m_size, p, n);
  1795. return true;
  1796. }
  1797. inline bool erase(size_t start, size_t n)
  1798. {
  1799. if (add_overflow_check(start, n))
  1800. {
  1801. assert(0);
  1802. return false;
  1803. }
  1804. assert((start + n) <= m_size);
  1805. if ((start + n) > m_size)
  1806. {
  1807. assert(0);
  1808. return false;
  1809. }
  1810. if (!n)
  1811. return true;
  1812. const size_t num_to_move = m_size - (start + n);
  1813. T* pDst = m_p + start;
  1814. const T* pSrc = m_p + start + n;
  1815. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T))
  1816. {
  1817. // This test is overly cautious.
  1818. if ((!BASISU_IS_BITWISE_COPYABLE(T)) || (BASISU_HAS_DESTRUCTOR(T)))
  1819. {
  1820. // Type has been marked explictly as bitwise movable, which means we can move them around but they may need to be destructed.
  1821. // First destroy the erased objects.
  1822. scalar_type<T>::destruct_array(pDst, n);
  1823. }
  1824. // Copy "down" the objects to preserve, filling in the empty slots.
  1825. #ifndef __EMSCRIPTEN__
  1826. #ifdef __GNUC__
  1827. #pragma GCC diagnostic push
  1828. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  1829. #endif
  1830. #endif
  1831. memmove(pDst, pSrc, num_to_move * sizeof(T));
  1832. #ifndef __EMSCRIPTEN__
  1833. #ifdef __GNUC__
  1834. #pragma GCC diagnostic pop
  1835. #endif
  1836. #endif
  1837. }
  1838. else
  1839. {
  1840. // Type is not bitwise copyable or movable.
  1841. // Move them down one at a time by using the equals operator, and destroying anything that's left over at the end.
  1842. T* pDst_end = pDst + num_to_move;
  1843. while (pDst != pDst_end)
  1844. {
  1845. *pDst = std::move(*pSrc);
  1846. ++pDst;
  1847. ++pSrc;
  1848. }
  1849. scalar_type<T>::destruct_array(pDst_end, n);
  1850. }
  1851. m_size -= n;
  1852. return true;
  1853. }
  1854. inline bool erase_index(size_t index)
  1855. {
  1856. return erase(index, 1);
  1857. }
  1858. inline bool erase(T* p)
  1859. {
  1860. assert((p >= m_p) && (p < (m_p + m_size)));
  1861. if (p < m_p)
  1862. return false;
  1863. return erase_index(static_cast<size_t>(p - m_p));
  1864. }
  1865. inline bool erase(T* pFirst, T* pEnd)
  1866. {
  1867. assert(pFirst <= pEnd);
  1868. assert(pFirst >= begin() && pFirst <= end());
  1869. assert(pEnd >= begin() && pEnd <= end());
  1870. if ((pFirst < begin()) || (pEnd < pFirst))
  1871. {
  1872. assert(0);
  1873. return false;
  1874. }
  1875. uint64_t ofs = pFirst - begin();
  1876. if ((size_t)ofs != ofs)
  1877. {
  1878. assert(0);
  1879. return false;
  1880. }
  1881. uint64_t n = pEnd - pFirst;
  1882. if ((size_t)n != n)
  1883. {
  1884. assert(0);
  1885. return false;
  1886. }
  1887. return erase((size_t)ofs, (size_t)n);
  1888. }
  1889. bool erase_unordered(size_t index)
  1890. {
  1891. if (index >= m_size)
  1892. {
  1893. assert(0);
  1894. return false;
  1895. }
  1896. if ((index + 1) < m_size)
  1897. {
  1898. (*this)[index] = std::move(back());
  1899. }
  1900. pop_back();
  1901. return true;
  1902. }
  1903. inline bool operator== (const vector& rhs) const
  1904. {
  1905. if (m_size != rhs.m_size)
  1906. return false;
  1907. else if (m_size)
  1908. {
  1909. if (scalar_type<T>::cFlag)
  1910. return memcmp(m_p, rhs.m_p, sizeof(T) * m_size) == 0;
  1911. else
  1912. {
  1913. const T* pSrc = m_p;
  1914. const T* pDst = rhs.m_p;
  1915. for (size_t i = m_size; i; i--)
  1916. if (!(*pSrc++ == *pDst++))
  1917. return false;
  1918. }
  1919. }
  1920. return true;
  1921. }
  1922. inline bool operator< (const vector& rhs) const
  1923. {
  1924. const size_t min_size = helpers::minimum(m_size, rhs.m_size);
  1925. const T* pSrc = m_p;
  1926. const T* pSrc_end = m_p + min_size;
  1927. const T* pDst = rhs.m_p;
  1928. while ((pSrc < pSrc_end) && (*pSrc == *pDst))
  1929. {
  1930. pSrc++;
  1931. pDst++;
  1932. }
  1933. if (pSrc < pSrc_end)
  1934. return *pSrc < *pDst;
  1935. return m_size < rhs.m_size;
  1936. }
  1937. inline void swap(vector& other)
  1938. {
  1939. std::swap(m_p, other.m_p);
  1940. std::swap(m_size, other.m_size);
  1941. std::swap(m_capacity, other.m_capacity);
  1942. }
  1943. inline void sort()
  1944. {
  1945. std::sort(begin(), end());
  1946. }
  1947. inline void unique()
  1948. {
  1949. if (!empty())
  1950. {
  1951. sort();
  1952. resize(std::unique(begin(), end()) - begin());
  1953. }
  1954. }
  1955. inline void reverse()
  1956. {
  1957. const size_t j = m_size >> 1;
  1958. for (size_t i = 0; i < j; i++)
  1959. std::swap(m_p[i], m_p[m_size - 1 - i]);
  1960. }
  1961. inline bool find(const T& key, size_t &idx) const
  1962. {
  1963. idx = 0;
  1964. const T* p = m_p;
  1965. const T* p_end = m_p + m_size;
  1966. size_t index = 0;
  1967. while (p != p_end)
  1968. {
  1969. if (key == *p)
  1970. {
  1971. idx = index;
  1972. return true;
  1973. }
  1974. p++;
  1975. index++;
  1976. }
  1977. return false;
  1978. }
  1979. inline bool find_sorted(const T& key, size_t& idx) const
  1980. {
  1981. idx = 0;
  1982. if (!m_size)
  1983. return false;
  1984. // Inclusive range
  1985. size_t low = 0, high = m_size - 1;
  1986. while (low <= high)
  1987. {
  1988. size_t mid = (size_t)(((uint64_t)low + (uint64_t)high) >> 1);
  1989. const T* pTrial_key = m_p + mid;
  1990. // Sanity check comparison operator
  1991. assert(!((*pTrial_key < key) && (key < *pTrial_key)));
  1992. if (*pTrial_key < key)
  1993. {
  1994. if (add_overflow_check(mid, 1))
  1995. break;
  1996. low = mid + 1;
  1997. }
  1998. else if (key < *pTrial_key)
  1999. {
  2000. if (!mid)
  2001. break;
  2002. high = mid - 1;
  2003. }
  2004. else
  2005. {
  2006. idx = mid;
  2007. return true;
  2008. }
  2009. }
  2010. return false;
  2011. }
  2012. inline size_t count_occurences(const T& key) const
  2013. {
  2014. size_t c = 0;
  2015. const T* p = m_p;
  2016. const T* p_end = m_p + m_size;
  2017. while (p != p_end)
  2018. {
  2019. if (key == *p)
  2020. c++;
  2021. p++;
  2022. }
  2023. return c;
  2024. }
  2025. inline void set_all(const T& o)
  2026. {
  2027. if ((sizeof(T) == 1) && (scalar_type<T>::cFlag))
  2028. {
  2029. #ifndef __EMSCRIPTEN__
  2030. #ifdef __GNUC__
  2031. #pragma GCC diagnostic push
  2032. #pragma GCC diagnostic ignored "-Wclass-memaccess"
  2033. #endif
  2034. #endif
  2035. memset(m_p, *reinterpret_cast<const uint8_t*>(&o), m_size);
  2036. #ifndef __EMSCRIPTEN__
  2037. #ifdef __GNUC__
  2038. #pragma GCC diagnostic pop
  2039. #endif
  2040. #endif
  2041. }
  2042. else
  2043. {
  2044. T* pDst = m_p;
  2045. T* pDst_end = pDst + m_size;
  2046. while (pDst != pDst_end)
  2047. *pDst++ = o;
  2048. }
  2049. }
  2050. // Caller assumes ownership of the heap block associated with the container. Container is cleared.
  2051. // Caller must use free() on the returned pointer.
  2052. inline void* assume_ownership()
  2053. {
  2054. T* p = m_p;
  2055. m_p = nullptr;
  2056. m_size = 0;
  2057. m_capacity = 0;
  2058. return p;
  2059. }
  2060. // Caller is granting ownership of the indicated heap block.
  2061. // Block must have size constructed elements, and have enough room for capacity elements.
  2062. // The block must have been allocated using malloc().
  2063. // 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.
  2064. inline bool grant_ownership(T* p, size_t size, size_t capacity)
  2065. {
  2066. // To prevent the caller from obviously shooting themselves in the foot.
  2067. if (((p + capacity) > m_p) && (p < (m_p + m_capacity)))
  2068. {
  2069. // Can grant ownership of a block inside the container itself!
  2070. assert(0);
  2071. return false;
  2072. }
  2073. if (size > capacity)
  2074. {
  2075. assert(0);
  2076. return false;
  2077. }
  2078. if (!p)
  2079. {
  2080. if (capacity)
  2081. {
  2082. assert(0);
  2083. return false;
  2084. }
  2085. }
  2086. else if (!capacity)
  2087. {
  2088. assert(0);
  2089. return false;
  2090. }
  2091. clear();
  2092. m_p = p;
  2093. m_size = size;
  2094. m_capacity = capacity;
  2095. return true;
  2096. }
  2097. readable_span<T> get_readable_span() const
  2098. {
  2099. return readable_span<T>(m_p, m_size);
  2100. }
  2101. writable_span<T> get_writable_span()
  2102. {
  2103. return writable_span<T>(m_p, m_size);
  2104. }
  2105. private:
  2106. T* m_p;
  2107. size_t m_size; // the number of constructed objects
  2108. size_t m_capacity; // the size of the allocation
  2109. template<typename Q> struct is_vector { enum { cFlag = false }; };
  2110. template<typename Q> struct is_vector< vector<Q> > { enum { cFlag = true }; };
  2111. static void object_mover(void* pDst_void, void* pSrc_void, size_t num)
  2112. {
  2113. T* pSrc = static_cast<T*>(pSrc_void);
  2114. T* const pSrc_end = pSrc + num;
  2115. T* pDst = static_cast<T*>(pDst_void);
  2116. while (pSrc != pSrc_end)
  2117. {
  2118. new ((void*)(pDst)) T(std::move(*pSrc));
  2119. scalar_type<T>::destruct(pSrc);
  2120. ++pSrc;
  2121. ++pDst;
  2122. }
  2123. }
  2124. inline bool increase_capacity(size_t min_new_capacity, bool grow_hint, bool nofail = false)
  2125. {
  2126. return reinterpret_cast<elemental_vector*>(this)->increase_capacity(
  2127. min_new_capacity, grow_hint, sizeof(T),
  2128. (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(T) || (is_vector<T>::cFlag)) ? nullptr : object_mover, nofail);
  2129. }
  2130. };
  2131. template<typename T> struct bitwise_movable< vector<T> > { enum { cFlag = true }; };
  2132. // Hash map
  2133. // rg TODO 9/8/2024: I've upgraded this class to support 64-bit size_t, and it needs a lot more testing.
  2134. const uint32_t SIZE_T_BITS = sizeof(size_t) * 8U;
  2135. inline uint32_t safe_shift_left(uint32_t v, uint32_t l)
  2136. {
  2137. return (l < 32U) ? (v << l) : 0;
  2138. }
  2139. inline uint64_t safe_shift_left(uint64_t v, uint32_t l)
  2140. {
  2141. return (l < 64U) ? (v << l) : 0;
  2142. }
  2143. template <typename T>
  2144. struct hasher
  2145. {
  2146. inline size_t operator() (const T& key) const { return static_cast<size_t>(key); }
  2147. };
  2148. template <typename T>
  2149. struct equal_to
  2150. {
  2151. inline bool operator()(const T& a, const T& b) const { return a == b; }
  2152. };
  2153. // Important: The Hasher and Equals objects must be bitwise movable!
  2154. template<typename Key, typename Value = empty_type, typename Hasher = hasher<Key>, typename Equals = equal_to<Key> >
  2155. class hash_map
  2156. {
  2157. public:
  2158. class iterator;
  2159. class const_iterator;
  2160. private:
  2161. friend class iterator;
  2162. friend class const_iterator;
  2163. enum state
  2164. {
  2165. cStateInvalid = 0,
  2166. cStateValid = 1
  2167. };
  2168. enum
  2169. {
  2170. cMinHashSize = 4U
  2171. };
  2172. public:
  2173. typedef hash_map<Key, Value, Hasher, Equals> hash_map_type;
  2174. typedef std::pair<Key, Value> value_type;
  2175. typedef Key key_type;
  2176. typedef Value referent_type;
  2177. typedef Hasher hasher_type;
  2178. typedef Equals equals_type;
  2179. hash_map() :
  2180. m_num_valid(0),
  2181. m_grow_threshold(0),
  2182. m_hash_shift(SIZE_T_BITS)
  2183. {
  2184. static_assert((SIZE_T_BITS == 32) || (SIZE_T_BITS == 64), "SIZE_T_BITS must be 32 or 64");
  2185. }
  2186. hash_map(const hash_map& other) :
  2187. m_values(other.m_values),
  2188. m_num_valid(other.m_num_valid),
  2189. m_grow_threshold(other.m_grow_threshold),
  2190. m_hash_shift(other.m_hash_shift),
  2191. m_hasher(other.m_hasher),
  2192. m_equals(other.m_equals)
  2193. {
  2194. static_assert((SIZE_T_BITS == 32) || (SIZE_T_BITS == 64), "SIZE_T_BITS must be 32 or 64");
  2195. }
  2196. hash_map(hash_map&& other) :
  2197. m_values(std::move(other.m_values)),
  2198. m_num_valid(other.m_num_valid),
  2199. m_grow_threshold(other.m_grow_threshold),
  2200. m_hash_shift(other.m_hash_shift),
  2201. m_hasher(std::move(other.m_hasher)),
  2202. m_equals(std::move(other.m_equals))
  2203. {
  2204. static_assert((SIZE_T_BITS == 32) || (SIZE_T_BITS == 64), "SIZE_T_BITS must be 32 or 64");
  2205. other.m_hash_shift = SIZE_T_BITS;
  2206. other.m_num_valid = 0;
  2207. other.m_grow_threshold = 0;
  2208. }
  2209. hash_map& operator= (const hash_map& other)
  2210. {
  2211. if (this == &other)
  2212. return *this;
  2213. clear();
  2214. m_values = other.m_values;
  2215. m_hash_shift = other.m_hash_shift;
  2216. m_num_valid = other.m_num_valid;
  2217. m_grow_threshold = other.m_grow_threshold;
  2218. m_hasher = other.m_hasher;
  2219. m_equals = other.m_equals;
  2220. return *this;
  2221. }
  2222. hash_map& operator= (hash_map&& other)
  2223. {
  2224. if (this == &other)
  2225. return *this;
  2226. clear();
  2227. m_values = std::move(other.m_values);
  2228. m_hash_shift = other.m_hash_shift;
  2229. m_num_valid = other.m_num_valid;
  2230. m_grow_threshold = other.m_grow_threshold;
  2231. m_hasher = std::move(other.m_hasher);
  2232. m_equals = std::move(other.m_equals);
  2233. other.m_hash_shift = SIZE_T_BITS;
  2234. other.m_num_valid = 0;
  2235. other.m_grow_threshold = 0;
  2236. return *this;
  2237. }
  2238. inline ~hash_map()
  2239. {
  2240. clear();
  2241. }
  2242. inline const Equals& get_equals() const { return m_equals; }
  2243. inline Equals& get_equals() { return m_equals; }
  2244. inline void set_equals(const Equals& equals) { m_equals = equals; }
  2245. inline const Hasher& get_hasher() const { return m_hasher; }
  2246. inline Hasher& get_hasher() { return m_hasher; }
  2247. inline void set_hasher(const Hasher& hasher) { m_hasher = hasher; }
  2248. inline void clear()
  2249. {
  2250. if (m_values.empty())
  2251. return;
  2252. if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
  2253. {
  2254. node* p = &get_node(0);
  2255. node* p_end = p + m_values.size();
  2256. size_t num_remaining = m_num_valid;
  2257. while (p != p_end)
  2258. {
  2259. if (p->state)
  2260. {
  2261. destruct_value_type(p);
  2262. num_remaining--;
  2263. if (!num_remaining)
  2264. break;
  2265. }
  2266. p++;
  2267. }
  2268. }
  2269. m_values.clear_no_destruction();
  2270. m_hash_shift = SIZE_T_BITS;
  2271. m_num_valid = 0;
  2272. m_grow_threshold = 0;
  2273. }
  2274. inline void reset()
  2275. {
  2276. if (!m_num_valid)
  2277. return;
  2278. if (BASISU_HAS_DESTRUCTOR(Key) || BASISU_HAS_DESTRUCTOR(Value))
  2279. {
  2280. node* p = &get_node(0);
  2281. node* p_end = p + m_values.size();
  2282. size_t num_remaining = m_num_valid;
  2283. while (p != p_end)
  2284. {
  2285. if (p->state)
  2286. {
  2287. destruct_value_type(p);
  2288. p->state = cStateInvalid;
  2289. num_remaining--;
  2290. if (!num_remaining)
  2291. break;
  2292. }
  2293. p++;
  2294. }
  2295. }
  2296. else if (sizeof(node) <= 16)
  2297. {
  2298. memset(&m_values[0], 0, m_values.size_in_bytes());
  2299. }
  2300. else
  2301. {
  2302. node* p = &get_node(0);
  2303. node* p_end = p + m_values.size();
  2304. size_t num_remaining = m_num_valid;
  2305. while (p != p_end)
  2306. {
  2307. if (p->state)
  2308. {
  2309. p->state = cStateInvalid;
  2310. num_remaining--;
  2311. if (!num_remaining)
  2312. break;
  2313. }
  2314. p++;
  2315. }
  2316. }
  2317. m_num_valid = 0;
  2318. }
  2319. inline size_t size()
  2320. {
  2321. return m_num_valid;
  2322. }
  2323. inline size_t get_table_size()
  2324. {
  2325. return m_values.size();
  2326. }
  2327. inline bool empty()
  2328. {
  2329. return !m_num_valid;
  2330. }
  2331. inline bool reserve(size_t new_capacity)
  2332. {
  2333. if (!new_capacity)
  2334. return true;
  2335. uint64_t new_hash_size = new_capacity;
  2336. new_hash_size = new_hash_size * 2ULL;
  2337. if (!helpers::is_power_of_2(new_hash_size))
  2338. new_hash_size = helpers::next_pow2(new_hash_size);
  2339. new_hash_size = helpers::maximum<uint64_t>(cMinHashSize, new_hash_size);
  2340. if (!can_fit_into_size_t(new_hash_size))
  2341. {
  2342. assert(0);
  2343. return false;
  2344. }
  2345. assert(new_hash_size >= new_capacity);
  2346. if (new_hash_size <= m_values.size())
  2347. return true;
  2348. return rehash((size_t)new_hash_size);
  2349. }
  2350. class iterator
  2351. {
  2352. friend class hash_map<Key, Value, Hasher, Equals>;
  2353. friend class hash_map<Key, Value, Hasher, Equals>::const_iterator;
  2354. public:
  2355. inline iterator() : m_pTable(nullptr), m_index(0) { }
  2356. inline iterator(hash_map_type& table, size_t index) : m_pTable(&table), m_index(index) { }
  2357. inline iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  2358. inline iterator& operator= (const iterator& other)
  2359. {
  2360. m_pTable = other.m_pTable;
  2361. m_index = other.m_index;
  2362. return *this;
  2363. }
  2364. // post-increment
  2365. inline iterator operator++(int)
  2366. {
  2367. iterator result(*this);
  2368. ++*this;
  2369. return result;
  2370. }
  2371. // pre-increment
  2372. inline iterator& operator++()
  2373. {
  2374. probe();
  2375. return *this;
  2376. }
  2377. inline value_type& operator*() const { return *get_cur(); }
  2378. inline value_type* operator->() const { return get_cur(); }
  2379. inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  2380. inline bool operator != (const iterator& b) const { return !(*this == b); }
  2381. inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  2382. inline bool operator != (const const_iterator& b) const { return !(*this == b); }
  2383. private:
  2384. hash_map_type* m_pTable;
  2385. size_t m_index;
  2386. inline value_type* get_cur() const
  2387. {
  2388. assert(m_pTable && (m_index < m_pTable->m_values.size()));
  2389. assert(m_pTable->get_node_state(m_index) == cStateValid);
  2390. return &m_pTable->get_node(m_index);
  2391. }
  2392. inline void probe()
  2393. {
  2394. assert(m_pTable);
  2395. m_index = m_pTable->find_next(m_index);
  2396. }
  2397. };
  2398. class const_iterator
  2399. {
  2400. friend class hash_map<Key, Value, Hasher, Equals>;
  2401. friend class hash_map<Key, Value, Hasher, Equals>::iterator;
  2402. public:
  2403. inline const_iterator() : m_pTable(nullptr), m_index(0) { }
  2404. inline const_iterator(const hash_map_type& table, size_t index) : m_pTable(&table), m_index(index) { }
  2405. inline const_iterator(const iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  2406. inline const_iterator(const const_iterator& other) : m_pTable(other.m_pTable), m_index(other.m_index) { }
  2407. inline const_iterator& operator= (const const_iterator& other)
  2408. {
  2409. m_pTable = other.m_pTable;
  2410. m_index = other.m_index;
  2411. return *this;
  2412. }
  2413. inline const_iterator& operator= (const iterator& other)
  2414. {
  2415. m_pTable = other.m_pTable;
  2416. m_index = other.m_index;
  2417. return *this;
  2418. }
  2419. // post-increment
  2420. inline const_iterator operator++(int)
  2421. {
  2422. const_iterator result(*this);
  2423. ++*this;
  2424. return result;
  2425. }
  2426. // pre-increment
  2427. inline const_iterator& operator++()
  2428. {
  2429. probe();
  2430. return *this;
  2431. }
  2432. inline const value_type& operator*() const { return *get_cur(); }
  2433. inline const value_type* operator->() const { return get_cur(); }
  2434. inline bool operator == (const const_iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  2435. inline bool operator != (const const_iterator& b) const { return !(*this == b); }
  2436. inline bool operator == (const iterator& b) const { return (m_pTable == b.m_pTable) && (m_index == b.m_index); }
  2437. inline bool operator != (const iterator& b) const { return !(*this == b); }
  2438. private:
  2439. const hash_map_type* m_pTable;
  2440. size_t m_index;
  2441. inline const value_type* get_cur() const
  2442. {
  2443. assert(m_pTable && (m_index < m_pTable->m_values.size()));
  2444. assert(m_pTable->get_node_state(m_index) == cStateValid);
  2445. return &m_pTable->get_node(m_index);
  2446. }
  2447. inline void probe()
  2448. {
  2449. assert(m_pTable);
  2450. m_index = m_pTable->find_next(m_index);
  2451. }
  2452. };
  2453. inline const_iterator begin() const
  2454. {
  2455. if (!m_num_valid)
  2456. return end();
  2457. return const_iterator(*this, find_next(std::numeric_limits<size_t>::max()));
  2458. }
  2459. inline const_iterator end() const
  2460. {
  2461. return const_iterator(*this, m_values.size());
  2462. }
  2463. inline iterator begin()
  2464. {
  2465. if (!m_num_valid)
  2466. return end();
  2467. return iterator(*this, find_next(std::numeric_limits<size_t>::max()));
  2468. }
  2469. inline iterator end()
  2470. {
  2471. return iterator(*this, m_values.size());
  2472. }
  2473. // insert_result.first will always point to inserted key/value (or the already existing key/value).
  2474. // insert_result.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).
  2475. typedef std::pair<iterator, bool> insert_result;
  2476. inline insert_result insert(const Key& k, const Value& v = Value())
  2477. {
  2478. insert_result result;
  2479. if (!insert_no_grow(result, k, v))
  2480. {
  2481. if (!try_grow())
  2482. container_abort("hash_map::try_grow() failed");
  2483. // This must succeed.
  2484. if (!insert_no_grow(result, k, v))
  2485. container_abort("hash_map::insert() failed");
  2486. }
  2487. return result;
  2488. }
  2489. inline bool try_insert(insert_result& result, const Key& k, const Value& v = Value())
  2490. {
  2491. if (!insert_no_grow(result, k, v))
  2492. {
  2493. if (!try_grow())
  2494. return false;
  2495. if (!insert_no_grow(result, k, v))
  2496. return false;
  2497. }
  2498. return true;
  2499. }
  2500. inline insert_result insert(Key&& k, Value&& v = Value())
  2501. {
  2502. insert_result result;
  2503. if (!insert_no_grow_move(result, std::move(k), std::move(v)))
  2504. {
  2505. if (!try_grow())
  2506. container_abort("hash_map::try_grow() failed");
  2507. // This must succeed.
  2508. if (!insert_no_grow_move(result, std::move(k), std::move(v)))
  2509. container_abort("hash_map::insert() failed");
  2510. }
  2511. return result;
  2512. }
  2513. inline bool try_insert(insert_result& result, Key&& k, Value&& v = Value())
  2514. {
  2515. if (!insert_no_grow_move(result, std::move(k), std::move(v)))
  2516. {
  2517. if (!try_grow())
  2518. return false;
  2519. if (!insert_no_grow_move(result, std::move(k), std::move(v)))
  2520. return false;
  2521. }
  2522. return true;
  2523. }
  2524. inline insert_result insert(const value_type& v)
  2525. {
  2526. return insert(v.first, v.second);
  2527. }
  2528. inline bool try_insert(insert_result& result, const value_type& v)
  2529. {
  2530. return try_insert(result, v.first, v.second);
  2531. }
  2532. inline insert_result insert(value_type&& v)
  2533. {
  2534. return insert(std::move(v.first), std::move(v.second));
  2535. }
  2536. inline bool try_insert(insert_result& result, value_type&& v)
  2537. {
  2538. return try_insert(result, std::move(v.first), std::move(v.second));
  2539. }
  2540. inline const_iterator find(const Key& k) const
  2541. {
  2542. return const_iterator(*this, find_index(k));
  2543. }
  2544. inline iterator find(const Key& k)
  2545. {
  2546. return iterator(*this, find_index(k));
  2547. }
  2548. inline bool contains(const Key& k) const
  2549. {
  2550. const size_t idx = find_index(k);
  2551. return idx != m_values.size();
  2552. }
  2553. inline bool erase(const Key& k)
  2554. {
  2555. size_t i = find_index(k);
  2556. if (i >= m_values.size())
  2557. return false;
  2558. node* pDst = &get_node(i);
  2559. destruct_value_type(pDst);
  2560. pDst->state = cStateInvalid;
  2561. m_num_valid--;
  2562. for (; ; )
  2563. {
  2564. size_t r, j = i;
  2565. node* pSrc = pDst;
  2566. do
  2567. {
  2568. if (!i)
  2569. {
  2570. i = m_values.size() - 1;
  2571. pSrc = &get_node(i);
  2572. }
  2573. else
  2574. {
  2575. i--;
  2576. pSrc--;
  2577. }
  2578. if (!pSrc->state)
  2579. return true;
  2580. r = hash_key(pSrc->first);
  2581. } while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
  2582. move_node(pDst, pSrc);
  2583. pDst = pSrc;
  2584. }
  2585. }
  2586. inline void swap(hash_map_type& other)
  2587. {
  2588. m_values.swap(other.m_values);
  2589. std::swap(m_hash_shift, other.m_hash_shift);
  2590. std::swap(m_num_valid, other.m_num_valid);
  2591. std::swap(m_grow_threshold, other.m_grow_threshold);
  2592. std::swap(m_hasher, other.m_hasher);
  2593. std::swap(m_equals, other.m_equals);
  2594. }
  2595. private:
  2596. struct node : public value_type
  2597. {
  2598. uint8_t state;
  2599. };
  2600. static inline void construct_value_type(value_type* pDst, const Key& k, const Value& v)
  2601. {
  2602. if (BASISU_IS_BITWISE_COPYABLE(Key))
  2603. memcpy(&pDst->first, &k, sizeof(Key));
  2604. else
  2605. scalar_type<Key>::construct(&pDst->first, k);
  2606. if (BASISU_IS_BITWISE_COPYABLE(Value))
  2607. memcpy(&pDst->second, &v, sizeof(Value));
  2608. else
  2609. scalar_type<Value>::construct(&pDst->second, v);
  2610. }
  2611. static inline void construct_value_type(value_type* pDst, const value_type* pSrc)
  2612. {
  2613. if ((BASISU_IS_BITWISE_COPYABLE(Key)) && (BASISU_IS_BITWISE_COPYABLE(Value)))
  2614. {
  2615. memcpy(pDst, pSrc, sizeof(value_type));
  2616. }
  2617. else
  2618. {
  2619. if (BASISU_IS_BITWISE_COPYABLE(Key))
  2620. memcpy(&pDst->first, &pSrc->first, sizeof(Key));
  2621. else
  2622. scalar_type<Key>::construct(&pDst->first, pSrc->first);
  2623. if (BASISU_IS_BITWISE_COPYABLE(Value))
  2624. memcpy(&pDst->second, &pSrc->second, sizeof(Value));
  2625. else
  2626. scalar_type<Value>::construct(&pDst->second, pSrc->second);
  2627. }
  2628. }
  2629. static inline void destruct_value_type(value_type* p)
  2630. {
  2631. scalar_type<Key>::destruct(&p->first);
  2632. scalar_type<Value>::destruct(&p->second);
  2633. }
  2634. // Moves nodes *pSrc to *pDst efficiently from one hashmap to another.
  2635. // pDst should NOT be constructed on entry.
  2636. static inline void move_node(node* pDst, node* pSrc, bool update_src_state = true)
  2637. {
  2638. assert(!pDst->state);
  2639. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key) && BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
  2640. {
  2641. memcpy(pDst, pSrc, sizeof(node));
  2642. assert(pDst->state == cStateValid);
  2643. }
  2644. else
  2645. {
  2646. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Key))
  2647. memcpy(&pDst->first, &pSrc->first, sizeof(Key));
  2648. else
  2649. {
  2650. new ((void*)&pDst->first) Key(std::move(pSrc->first));
  2651. scalar_type<Key>::destruct(&pSrc->first);
  2652. }
  2653. if (BASISU_IS_BITWISE_COPYABLE_OR_MOVABLE(Value))
  2654. memcpy(&pDst->second, &pSrc->second, sizeof(Value));
  2655. else
  2656. {
  2657. new ((void*)&pDst->second) Value(std::move(pSrc->second));
  2658. scalar_type<Value>::destruct(&pSrc->second);
  2659. }
  2660. pDst->state = cStateValid;
  2661. }
  2662. if (update_src_state)
  2663. pSrc->state = cStateInvalid;
  2664. }
  2665. struct raw_node
  2666. {
  2667. inline raw_node()
  2668. {
  2669. node* p = reinterpret_cast<node*>(this);
  2670. p->state = cStateInvalid;
  2671. }
  2672. // In practice, this should never be called (right?). We manage destruction ourselves.
  2673. inline ~raw_node()
  2674. {
  2675. node* p = reinterpret_cast<node*>(this);
  2676. if (p->state)
  2677. hash_map_type::destruct_value_type(p);
  2678. }
  2679. inline raw_node(const raw_node& other)
  2680. {
  2681. node* pDst = reinterpret_cast<node*>(this);
  2682. const node* pSrc = reinterpret_cast<const node*>(&other);
  2683. if (pSrc->state)
  2684. {
  2685. hash_map_type::construct_value_type(pDst, pSrc);
  2686. pDst->state = cStateValid;
  2687. }
  2688. else
  2689. pDst->state = cStateInvalid;
  2690. }
  2691. inline raw_node& operator= (const raw_node& rhs)
  2692. {
  2693. if (this == &rhs)
  2694. return *this;
  2695. node* pDst = reinterpret_cast<node*>(this);
  2696. const node* pSrc = reinterpret_cast<const node*>(&rhs);
  2697. if (pSrc->state)
  2698. {
  2699. if (pDst->state)
  2700. {
  2701. pDst->first = pSrc->first;
  2702. pDst->second = pSrc->second;
  2703. }
  2704. else
  2705. {
  2706. hash_map_type::construct_value_type(pDst, pSrc);
  2707. pDst->state = cStateValid;
  2708. }
  2709. }
  2710. else if (pDst->state)
  2711. {
  2712. hash_map_type::destruct_value_type(pDst);
  2713. pDst->state = cStateInvalid;
  2714. }
  2715. return *this;
  2716. }
  2717. uint8_t m_bits[sizeof(node)];
  2718. };
  2719. typedef basisu::vector<raw_node> node_vector;
  2720. node_vector m_values;
  2721. size_t m_num_valid;
  2722. size_t m_grow_threshold;
  2723. uint32_t m_hash_shift;
  2724. Hasher m_hasher;
  2725. Equals m_equals;
  2726. inline size_t hash_key(const Key& k) const
  2727. {
  2728. assert((safe_shift_left(static_cast<uint64_t>(1), (SIZE_T_BITS - m_hash_shift))) == m_values.size());
  2729. // Fibonacci hashing
  2730. if (SIZE_T_BITS == 32)
  2731. {
  2732. assert(m_hash_shift != 32);
  2733. uint32_t hash = static_cast<uint32_t>(m_hasher(k));
  2734. hash = (2654435769U * hash) >> m_hash_shift;
  2735. assert(hash < m_values.size());
  2736. return (size_t)hash;
  2737. }
  2738. else
  2739. {
  2740. assert(m_hash_shift != 64);
  2741. uint64_t hash = static_cast<uint64_t>(m_hasher(k));
  2742. hash = (0x9E3779B97F4A7C15ULL * hash) >> m_hash_shift;
  2743. assert(hash < m_values.size());
  2744. return (size_t)hash;
  2745. }
  2746. }
  2747. inline const node& get_node(size_t index) const
  2748. {
  2749. return *reinterpret_cast<const node*>(&m_values[index]);
  2750. }
  2751. inline node& get_node(size_t index)
  2752. {
  2753. return *reinterpret_cast<node*>(&m_values[index]);
  2754. }
  2755. inline state get_node_state(size_t index) const
  2756. {
  2757. return static_cast<state>(get_node(index).state);
  2758. }
  2759. inline void set_node_state(size_t index, bool valid)
  2760. {
  2761. get_node(index).state = valid;
  2762. }
  2763. inline bool try_grow()
  2764. {
  2765. uint64_t n = m_values.size() * 2ULL;
  2766. if (!helpers::is_power_of_2(n))
  2767. n = helpers::next_pow2(n);
  2768. if (!can_fit_into_size_t(n))
  2769. {
  2770. assert(0);
  2771. return false;
  2772. }
  2773. return rehash(helpers::maximum<size_t>(cMinHashSize, (size_t)n));
  2774. }
  2775. // new_hash_size must be a power of 2.
  2776. inline bool rehash(size_t new_hash_size)
  2777. {
  2778. if (!helpers::is_power_of_2((uint64_t)new_hash_size))
  2779. {
  2780. assert(0);
  2781. return false;
  2782. }
  2783. if (new_hash_size < m_num_valid)
  2784. {
  2785. assert(0);
  2786. return false;
  2787. }
  2788. if (new_hash_size == m_values.size())
  2789. return true;
  2790. hash_map new_map;
  2791. if (!new_map.m_values.try_resize(new_hash_size))
  2792. return false;
  2793. new_map.m_hash_shift = SIZE_T_BITS - helpers::floor_log2i((uint64_t)new_hash_size);
  2794. assert(new_hash_size == safe_shift_left(static_cast<uint64_t>(1), SIZE_T_BITS - new_map.m_hash_shift));
  2795. new_map.m_grow_threshold = std::numeric_limits<size_t>::max();
  2796. node* pNode = reinterpret_cast<node*>(m_values.begin());
  2797. node* pNode_end = pNode + m_values.size();
  2798. while (pNode != pNode_end)
  2799. {
  2800. if (pNode->state)
  2801. {
  2802. new_map.move_into(pNode);
  2803. if (new_map.m_num_valid == m_num_valid)
  2804. break;
  2805. }
  2806. pNode++;
  2807. }
  2808. new_map.m_grow_threshold = new_hash_size >> 1U;
  2809. if (new_hash_size & 1)
  2810. new_map.m_grow_threshold++;
  2811. m_values.clear_no_destruction();
  2812. m_hash_shift = SIZE_T_BITS;
  2813. swap(new_map);
  2814. return true;
  2815. }
  2816. inline size_t find_next(size_t index) const
  2817. {
  2818. index++;
  2819. if (index >= m_values.size())
  2820. return index;
  2821. const node* pNode = &get_node(index);
  2822. for (; ; )
  2823. {
  2824. if (pNode->state)
  2825. break;
  2826. if (++index >= m_values.size())
  2827. break;
  2828. pNode++;
  2829. }
  2830. return index;
  2831. }
  2832. inline size_t find_index(const Key& k) const
  2833. {
  2834. if (m_num_valid)
  2835. {
  2836. size_t index = hash_key(k);
  2837. const node* pNode = &get_node(index);
  2838. if (pNode->state)
  2839. {
  2840. if (m_equals(pNode->first, k))
  2841. return index;
  2842. const size_t orig_index = index;
  2843. for (; ; )
  2844. {
  2845. if (!index)
  2846. {
  2847. index = m_values.size() - 1;
  2848. pNode = &get_node(index);
  2849. }
  2850. else
  2851. {
  2852. index--;
  2853. pNode--;
  2854. }
  2855. if (index == orig_index)
  2856. break;
  2857. if (!pNode->state)
  2858. break;
  2859. if (m_equals(pNode->first, k))
  2860. return index;
  2861. }
  2862. }
  2863. }
  2864. return m_values.size();
  2865. }
  2866. inline bool insert_no_grow(insert_result& result, const Key& k, const Value& v)
  2867. {
  2868. if (!m_values.size())
  2869. return false;
  2870. size_t index = hash_key(k);
  2871. node* pNode = &get_node(index);
  2872. if (pNode->state)
  2873. {
  2874. if (m_equals(pNode->first, k))
  2875. {
  2876. result.first = iterator(*this, index);
  2877. result.second = false;
  2878. return true;
  2879. }
  2880. const size_t orig_index = index;
  2881. for (; ; )
  2882. {
  2883. if (!index)
  2884. {
  2885. index = m_values.size() - 1;
  2886. pNode = &get_node(index);
  2887. }
  2888. else
  2889. {
  2890. index--;
  2891. pNode--;
  2892. }
  2893. if (orig_index == index)
  2894. return false;
  2895. if (!pNode->state)
  2896. break;
  2897. if (m_equals(pNode->first, k))
  2898. {
  2899. result.first = iterator(*this, index);
  2900. result.second = false;
  2901. return true;
  2902. }
  2903. }
  2904. }
  2905. if (m_num_valid >= m_grow_threshold)
  2906. return false;
  2907. construct_value_type(pNode, k, v);
  2908. pNode->state = cStateValid;
  2909. m_num_valid++;
  2910. assert(m_num_valid <= m_values.size());
  2911. result.first = iterator(*this, index);
  2912. result.second = true;
  2913. return true;
  2914. }
  2915. // Move user supplied key/value into a node.
  2916. static inline void move_value_type(value_type* pDst, Key&& k, Value&& v)
  2917. {
  2918. // Not checking for is MOVABLE because the caller could later destruct k and/or v (what state do we set them to?)
  2919. if (BASISU_IS_BITWISE_COPYABLE(Key))
  2920. {
  2921. memcpy(&pDst->first, &k, sizeof(Key));
  2922. }
  2923. else
  2924. {
  2925. new ((void*)&pDst->first) Key(std::move(k));
  2926. // No destruction - user will do that (we don't own k).
  2927. }
  2928. if (BASISU_IS_BITWISE_COPYABLE(Value))
  2929. {
  2930. memcpy(&pDst->second, &v, sizeof(Value));
  2931. }
  2932. else
  2933. {
  2934. new ((void*)&pDst->second) Value(std::move(v));
  2935. // No destruction - user will do that (we don't own v).
  2936. }
  2937. }
  2938. // Insert user provided k/v, by moving, into the current hash table
  2939. inline bool insert_no_grow_move(insert_result& result, Key&& k, Value&& v)
  2940. {
  2941. if (!m_values.size())
  2942. return false;
  2943. size_t index = hash_key(k);
  2944. node* pNode = &get_node(index);
  2945. if (pNode->state)
  2946. {
  2947. if (m_equals(pNode->first, k))
  2948. {
  2949. result.first = iterator(*this, index);
  2950. result.second = false;
  2951. return true;
  2952. }
  2953. const size_t orig_index = index;
  2954. for (; ; )
  2955. {
  2956. if (!index)
  2957. {
  2958. index = m_values.size() - 1;
  2959. pNode = &get_node(index);
  2960. }
  2961. else
  2962. {
  2963. index--;
  2964. pNode--;
  2965. }
  2966. if (orig_index == index)
  2967. return false;
  2968. if (!pNode->state)
  2969. break;
  2970. if (m_equals(pNode->first, k))
  2971. {
  2972. result.first = iterator(*this, index);
  2973. result.second = false;
  2974. return true;
  2975. }
  2976. }
  2977. }
  2978. if (m_num_valid >= m_grow_threshold)
  2979. return false;
  2980. move_value_type(pNode, std::move(k), std::move(v));
  2981. pNode->state = cStateValid;
  2982. m_num_valid++;
  2983. assert(m_num_valid <= m_values.size());
  2984. result.first = iterator(*this, index);
  2985. result.second = true;
  2986. return true;
  2987. }
  2988. // Insert pNode by moving into the current hash table
  2989. inline void move_into(node* pNode)
  2990. {
  2991. size_t index = hash_key(pNode->first);
  2992. node* pDst_node = &get_node(index);
  2993. if (pDst_node->state)
  2994. {
  2995. const size_t orig_index = index;
  2996. for (; ; )
  2997. {
  2998. if (!index)
  2999. {
  3000. index = m_values.size() - 1;
  3001. pDst_node = &get_node(index);
  3002. }
  3003. else
  3004. {
  3005. index--;
  3006. pDst_node--;
  3007. }
  3008. if (index == orig_index)
  3009. {
  3010. assert(false);
  3011. return;
  3012. }
  3013. if (!pDst_node->state)
  3014. break;
  3015. }
  3016. }
  3017. // No need to update the source node's state (it's going away)
  3018. move_node(pDst_node, pNode, false);
  3019. m_num_valid++;
  3020. }
  3021. };
  3022. template<typename Key, typename Value, typename Hasher, typename Equals>
  3023. struct bitwise_movable< hash_map<Key, Value, Hasher, Equals> > { enum { cFlag = true }; };
  3024. #if BASISU_HASHMAP_TEST
  3025. extern void hash_map_test();
  3026. #endif
  3027. // String formatting
  3028. inline std::string string_format(const char* pFmt, ...)
  3029. {
  3030. char buf[2048];
  3031. va_list args;
  3032. va_start(args, pFmt);
  3033. #ifdef _WIN32
  3034. vsprintf_s(buf, sizeof(buf), pFmt, args);
  3035. #else
  3036. vsnprintf(buf, sizeof(buf), pFmt, args);
  3037. #endif
  3038. va_end(args);
  3039. return std::string(buf);
  3040. }
  3041. enum class variant_type
  3042. {
  3043. cInvalid,
  3044. cI32, cU32,
  3045. cI64, cU64,
  3046. cFlt, cDbl, cBool,
  3047. cStrPtr, cStdStr
  3048. };
  3049. struct fmt_variant
  3050. {
  3051. union
  3052. {
  3053. int32_t m_i32;
  3054. uint32_t m_u32;
  3055. int64_t m_i64;
  3056. uint64_t m_u64;
  3057. float m_flt;
  3058. double m_dbl;
  3059. bool m_bool;
  3060. const char* m_pStr;
  3061. };
  3062. std::string m_str;
  3063. variant_type m_type;
  3064. inline fmt_variant() :
  3065. m_u64(0),
  3066. m_type(variant_type::cInvalid)
  3067. {
  3068. }
  3069. inline fmt_variant(const fmt_variant& other) :
  3070. m_u64(other.m_u64),
  3071. m_str(other.m_str),
  3072. m_type(other.m_type)
  3073. {
  3074. }
  3075. inline fmt_variant(fmt_variant&& other) :
  3076. m_u64(other.m_u64),
  3077. m_str(std::move(other.m_str)),
  3078. m_type(other.m_type)
  3079. {
  3080. other.m_type = variant_type::cInvalid;
  3081. other.m_u64 = 0;
  3082. }
  3083. inline fmt_variant& operator= (fmt_variant&& other)
  3084. {
  3085. if (this == &other)
  3086. return *this;
  3087. m_type = other.m_type;
  3088. m_u64 = other.m_u64;
  3089. m_str = std::move(other.m_str);
  3090. other.m_type = variant_type::cInvalid;
  3091. other.m_u64 = 0;
  3092. return *this;
  3093. }
  3094. inline fmt_variant& operator= (const fmt_variant& rhs)
  3095. {
  3096. if (this == &rhs)
  3097. return *this;
  3098. m_u64 = rhs.m_u64;
  3099. m_type = rhs.m_type;
  3100. m_str = rhs.m_str;
  3101. return *this;
  3102. }
  3103. inline fmt_variant(int32_t v) : m_i32(v), m_type(variant_type::cI32) { }
  3104. inline fmt_variant(uint32_t v) : m_u32(v), m_type(variant_type::cU32) { }
  3105. inline fmt_variant(int64_t v) : m_i64(v), m_type(variant_type::cI64) { }
  3106. inline fmt_variant(uint64_t v) : m_u64(v), m_type(variant_type::cU64) { }
  3107. #ifdef _MSC_VER
  3108. inline fmt_variant(unsigned long v) : m_u64(v), m_type(variant_type::cU64) {}
  3109. inline fmt_variant(long v) : m_i64(v), m_type(variant_type::cI64) {}
  3110. #endif
  3111. inline fmt_variant(float v) : m_flt(v), m_type(variant_type::cFlt) { }
  3112. inline fmt_variant(double v) : m_dbl(v), m_type(variant_type::cDbl) { }
  3113. inline fmt_variant(const char* pStr) : m_pStr(pStr), m_type(variant_type::cStrPtr) { }
  3114. inline fmt_variant(const std::string& str) : m_u64(0), m_str(str), m_type(variant_type::cStdStr) { }
  3115. inline fmt_variant(bool val) : m_bool(val), m_type(variant_type::cBool) { }
  3116. bool to_string(std::string& res, std::string& fmt) const;
  3117. };
  3118. typedef basisu::vector<fmt_variant> fmt_variant_vec;
  3119. bool fmt_variants(std::string& res, const char* pFmt, const fmt_variant_vec& variants);
  3120. template <typename... Args>
  3121. inline bool fmt_string(std::string& res, const char* pFmt, Args&&... args)
  3122. {
  3123. return fmt_variants(res, pFmt, fmt_variant_vec{ fmt_variant(std::forward<Args>(args))... });
  3124. }
  3125. template <typename... Args>
  3126. inline std::string fmt_string(const char* pFmt, Args&&... args)
  3127. {
  3128. std::string res;
  3129. fmt_variants(res, pFmt, fmt_variant_vec{ fmt_variant(std::forward<Args>(args))... });
  3130. return res;
  3131. }
  3132. template <typename... Args>
  3133. inline int fmt_printf(const char* pFmt, Args&&... args)
  3134. {
  3135. std::string res;
  3136. if (!fmt_variants(res, pFmt, fmt_variant_vec{ fmt_variant(std::forward<Args>(args))... }))
  3137. return EOF;
  3138. return fputs(res.c_str(), stdout);
  3139. }
  3140. template <typename... Args>
  3141. inline int fmt_fprintf(FILE* pFile, const char* pFmt, Args&&... args)
  3142. {
  3143. std::string res;
  3144. if (!fmt_variants(res, pFmt, fmt_variant_vec{ fmt_variant(std::forward<Args>(args))... }))
  3145. return EOF;
  3146. return fputs(res.c_str(), pFile);
  3147. }
  3148. // fixed_array - zero initialized by default, operator[] is always bounds checked.
  3149. template <std::size_t N, typename T>
  3150. class fixed_array
  3151. {
  3152. static_assert(N >= 1, "fixed_array size must be at least 1");
  3153. public:
  3154. using value_type = T;
  3155. using size_type = std::size_t;
  3156. using difference_type = std::ptrdiff_t;
  3157. using reference = T&;
  3158. using const_reference = const T&;
  3159. using pointer = T*;
  3160. using const_pointer = const T*;
  3161. using iterator = T*;
  3162. using const_iterator = const T*;
  3163. T m_data[N];
  3164. BASISU_FORCE_INLINE fixed_array()
  3165. {
  3166. initialize_array();
  3167. }
  3168. BASISU_FORCE_INLINE fixed_array(std::initializer_list<T> list)
  3169. {
  3170. assert(list.size() <= N);
  3171. std::size_t copy_size = std::min(list.size(), N);
  3172. std::copy_n(list.begin(), copy_size, m_data); // Copy up to min(list.size(), N)
  3173. if (list.size() < N)
  3174. {
  3175. // Initialize the rest of the array
  3176. std::fill(m_data + copy_size, m_data + N, T{});
  3177. }
  3178. }
  3179. BASISU_FORCE_INLINE T& operator[](std::size_t index)
  3180. {
  3181. if (index >= N)
  3182. container_abort("fixed_array: Index out of bounds.");
  3183. return m_data[index];
  3184. }
  3185. BASISU_FORCE_INLINE const T& operator[](std::size_t index) const
  3186. {
  3187. if (index >= N)
  3188. container_abort("fixed_array: Index out of bounds.");
  3189. return m_data[index];
  3190. }
  3191. BASISU_FORCE_INLINE T* begin() { return m_data; }
  3192. BASISU_FORCE_INLINE const T* begin() const { return m_data; }
  3193. BASISU_FORCE_INLINE T* end() { return m_data + N; }
  3194. BASISU_FORCE_INLINE const T* end() const { return m_data + N; }
  3195. BASISU_FORCE_INLINE const T* data() const { return m_data; }
  3196. BASISU_FORCE_INLINE T* data() { return m_data; }
  3197. BASISU_FORCE_INLINE const T& front() const { return m_data[0]; }
  3198. BASISU_FORCE_INLINE T& front() { return m_data[0]; }
  3199. BASISU_FORCE_INLINE const T& back() const { return m_data[N - 1]; }
  3200. BASISU_FORCE_INLINE T& back() { return m_data[N - 1]; }
  3201. BASISU_FORCE_INLINE constexpr std::size_t size() const { return N; }
  3202. BASISU_FORCE_INLINE void clear()
  3203. {
  3204. initialize_array(); // Reinitialize the array
  3205. }
  3206. BASISU_FORCE_INLINE void set_all(const T& value)
  3207. {
  3208. std::fill(m_data, m_data + N, value);
  3209. }
  3210. BASISU_FORCE_INLINE readable_span<T> get_readable_span() const
  3211. {
  3212. return readable_span<T>(m_data, N);
  3213. }
  3214. BASISU_FORCE_INLINE writable_span<T> get_writable_span()
  3215. {
  3216. return writable_span<T>(m_data, N);
  3217. }
  3218. private:
  3219. BASISU_FORCE_INLINE void initialize_array()
  3220. {
  3221. if constexpr (std::is_integral<T>::value || std::is_floating_point<T>::value)
  3222. memset(m_data, 0, sizeof(m_data));
  3223. else
  3224. std::fill(m_data, m_data + N, T{});
  3225. }
  3226. BASISU_FORCE_INLINE T& access_element(std::size_t index)
  3227. {
  3228. if (index >= N)
  3229. container_abort("fixed_array: Index out of bounds.");
  3230. return m_data[index];
  3231. }
  3232. BASISU_FORCE_INLINE const T& access_element(std::size_t index) const
  3233. {
  3234. if (index >= N)
  3235. container_abort("fixed_array: Index out of bounds.");
  3236. return m_data[index];
  3237. }
  3238. };
  3239. // 2D array
  3240. template<typename T>
  3241. class vector2D
  3242. {
  3243. typedef basisu::vector<T> vec_type;
  3244. uint32_t m_width, m_height;
  3245. vec_type m_values;
  3246. public:
  3247. vector2D() :
  3248. m_width(0),
  3249. m_height(0)
  3250. {
  3251. }
  3252. vector2D(uint32_t w, uint32_t h) :
  3253. m_width(0),
  3254. m_height(0)
  3255. {
  3256. resize(w, h);
  3257. }
  3258. vector2D(const vector2D& other)
  3259. {
  3260. *this = other;
  3261. }
  3262. vector2D(vector2D&& other) :
  3263. m_width(0),
  3264. m_height(0)
  3265. {
  3266. *this = std::move(other);
  3267. }
  3268. vector2D& operator= (const vector2D& other)
  3269. {
  3270. if (this != &other)
  3271. {
  3272. m_width = other.m_width;
  3273. m_height = other.m_height;
  3274. m_values = other.m_values;
  3275. }
  3276. return *this;
  3277. }
  3278. vector2D& operator= (vector2D&& other)
  3279. {
  3280. if (this != &other)
  3281. {
  3282. m_width = other.m_width;
  3283. m_height = other.m_height;
  3284. m_values = std::move(other.m_values);
  3285. other.m_width = 0;
  3286. other.m_height = 0;
  3287. }
  3288. return *this;
  3289. }
  3290. inline bool operator== (const vector2D& rhs) const
  3291. {
  3292. return (m_width == rhs.m_width) && (m_height == rhs.m_height) && (m_values == rhs.m_values);
  3293. }
  3294. inline size_t size_in_bytes() const { return m_values.size_in_bytes(); }
  3295. inline uint32_t get_width() const { return m_width; }
  3296. inline uint32_t get_height() const { return m_height; }
  3297. inline const T& operator() (uint32_t x, uint32_t y) const { assert(x < m_width && y < m_height); return m_values[x + y * m_width]; }
  3298. inline T& operator() (uint32_t x, uint32_t y) { assert(x < m_width && y < m_height); return m_values[x + y * m_width]; }
  3299. inline size_t size() const { return m_values.size(); }
  3300. inline const T& operator[] (uint32_t i) const { return m_values[i]; }
  3301. inline T& operator[] (uint32_t i) { return m_values[i]; }
  3302. inline const T& at_clamped(int x, int y) const { return (*this)(clamp<int>(x, 0, m_width - 1), clamp<int>(y, 0, m_height - 1)); }
  3303. inline T& at_clamped(int x, int y) { return (*this)(clamp<int>(x, 0, m_width - 1), clamp<int>(y, 0, m_height - 1)); }
  3304. void clear()
  3305. {
  3306. m_width = 0;
  3307. m_height = 0;
  3308. m_values.clear();
  3309. }
  3310. void set_all(const T& val)
  3311. {
  3312. vector_set_all(m_values, val);
  3313. }
  3314. inline const T* get_ptr() const { return m_values.data(); }
  3315. inline T* get_ptr() { return m_values.data(); }
  3316. vector2D& resize(uint32_t new_width, uint32_t new_height)
  3317. {
  3318. if ((m_width == new_width) && (m_height == new_height))
  3319. return *this;
  3320. const uint64_t total_vals = (uint64_t)new_width * new_height;
  3321. if (!can_fit_into_size_t(total_vals))
  3322. {
  3323. // What can we do?
  3324. assert(0);
  3325. return *this;
  3326. }
  3327. vec_type oldVals((size_t)total_vals);
  3328. oldVals.swap(m_values);
  3329. const uint32_t w = minimum(m_width, new_width);
  3330. const uint32_t h = minimum(m_height, new_height);
  3331. if ((w) && (h))
  3332. {
  3333. for (uint32_t y = 0; y < h; y++)
  3334. for (uint32_t x = 0; x < w; x++)
  3335. m_values[x + y * new_width] = oldVals[x + y * m_width];
  3336. }
  3337. m_width = new_width;
  3338. m_height = new_height;
  3339. return *this;
  3340. }
  3341. bool try_resize(uint32_t new_width, uint32_t new_height)
  3342. {
  3343. if ((m_width == new_width) && (m_height == new_height))
  3344. return true;
  3345. const uint64_t total_vals = (uint64_t)new_width * new_height;
  3346. if (!can_fit_into_size_t(total_vals))
  3347. {
  3348. // What can we do?
  3349. assert(0);
  3350. return false;
  3351. }
  3352. vec_type oldVals;
  3353. if (!oldVals.try_resize((size_t)total_vals))
  3354. return false;
  3355. oldVals.swap(m_values);
  3356. const uint32_t w = minimum(m_width, new_width);
  3357. const uint32_t h = minimum(m_height, new_height);
  3358. if ((w) && (h))
  3359. {
  3360. for (uint32_t y = 0; y < h; y++)
  3361. for (uint32_t x = 0; x < w; x++)
  3362. m_values[x + y * new_width] = oldVals[x + y * m_width];
  3363. }
  3364. m_width = new_width;
  3365. m_height = new_height;
  3366. return true;
  3367. }
  3368. const vector2D& extract_block_clamped(T* pDst, uint32_t src_x, uint32_t src_y, uint32_t w, uint32_t h) const
  3369. {
  3370. // HACK HACK
  3371. if (((src_x + w) > m_width) || ((src_y + h) > m_height))
  3372. {
  3373. // Slower clamping case
  3374. for (uint32_t y = 0; y < h; y++)
  3375. for (uint32_t x = 0; x < w; x++)
  3376. *pDst++ = at_clamped(src_x + x, src_y + y);
  3377. }
  3378. else
  3379. {
  3380. const T* pSrc = &m_values[src_x + src_y * m_width];
  3381. for (uint32_t y = 0; y < h; y++)
  3382. {
  3383. memcpy(pDst, pSrc, w * sizeof(T));
  3384. pSrc += m_width;
  3385. pDst += w;
  3386. }
  3387. }
  3388. return *this;
  3389. }
  3390. };
  3391. } // namespace basisu
  3392. namespace std
  3393. {
  3394. template<typename T>
  3395. inline void swap(basisu::vector<T>& a, basisu::vector<T>& b)
  3396. {
  3397. a.swap(b);
  3398. }
  3399. template<typename Key, typename Value, typename Hasher, typename Equals>
  3400. inline void swap(basisu::hash_map<Key, Value, Hasher, Equals>& a, basisu::hash_map<Key, Value, Hasher, Equals>& b)
  3401. {
  3402. a.swap(b);
  3403. }
  3404. } // namespace std