暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FixedList.gen.cs 274KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504
  1. 
  2. //------------------------------------------------------------------------------
  3. // <auto-generated>
  4. // This code was generated by a tool.
  5. //
  6. // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/FixedList.tt
  7. //
  8. // Changes to this file may cause incorrect behavior and will be lost if
  9. // the code is regenerated.
  10. // </auto-generated>
  11. //------------------------------------------------------------------------------
  12. using System.Collections.Generic;
  13. using System.Collections;
  14. using System.Diagnostics;
  15. using System.Runtime.CompilerServices;
  16. using System.Runtime.InteropServices;
  17. using System;
  18. using Unity.Collections.LowLevel.Unsafe;
  19. using Unity.Mathematics;
  20. using UnityEngine.Internal;
  21. using UnityEngine;
  22. #if UNITY_PROPERTIES_EXISTS
  23. using Unity.Properties;
  24. #endif
  25. namespace Unity.Collections
  26. {
  27. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(FixedBytes30) })]
  28. [Serializable]
  29. internal struct FixedList<T,U>
  30. : INativeList<T>
  31. where T : unmanaged
  32. where U : unmanaged
  33. {
  34. [SerializeField] internal ushort length;
  35. [SerializeField] internal U buffer;
  36. /// <summary>
  37. /// The current number of items in this list.
  38. /// </summary>
  39. /// <value>The current number of items in this list.</value>
  40. [CreateProperty]
  41. public int Length
  42. {
  43. get => length;
  44. set
  45. {
  46. FixedList.CheckResize<U,T>(value);
  47. length = (ushort)value;
  48. }
  49. }
  50. /// <summary>
  51. /// A property in order to display items in the Entity Inspector.
  52. /// </summary>
  53. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  54. /// <summary>
  55. /// Whether the list is empty.
  56. /// </summary>
  57. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  58. public bool IsEmpty => Length == 0;
  59. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  60. unsafe internal byte* Buffer
  61. {
  62. get
  63. {
  64. fixed(U* u = &buffer)
  65. return (byte*)u + FixedList.PaddingBytes<T>();
  66. }
  67. }
  68. /// <summary>
  69. /// The number of elements that can fit in this list.
  70. /// </summary>
  71. /// <value>The number of elements that can fit in this list.</value>
  72. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  73. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  74. public int Capacity
  75. {
  76. get
  77. {
  78. return FixedList.Capacity<U,T>();
  79. }
  80. set
  81. {
  82. CollectionHelper.CheckCapacityInRange(value, Length);
  83. }
  84. }
  85. /// <summary>
  86. /// The element at a given index.
  87. /// </summary>
  88. /// <param name="index">An index.</param>
  89. /// <value>The value to store at the index.</value>
  90. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  91. public T this[int index]
  92. {
  93. get
  94. {
  95. CollectionHelper.CheckIndexInRange(index, length);
  96. unsafe
  97. {
  98. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  99. }
  100. }
  101. set
  102. {
  103. CollectionHelper.CheckIndexInRange(index, length);
  104. unsafe
  105. {
  106. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Returns the element at a given index.
  112. /// </summary>
  113. /// <param name="index">An index.</param>
  114. /// <returns>A reference to the element at the index.</returns>
  115. public ref T ElementAt(int index)
  116. {
  117. CollectionHelper.CheckIndexInRange(index, length);
  118. unsafe
  119. {
  120. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  121. }
  122. }
  123. /// <summary>
  124. /// Returns the hash code of this list.
  125. /// </summary>
  126. /// <remarks>
  127. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  128. /// <returns>The hash code of this list.</returns>
  129. public override int GetHashCode()
  130. {
  131. unsafe
  132. {
  133. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  134. }
  135. }
  136. /// <summary>
  137. /// Appends an element to the end of this list. Increments the length by 1.
  138. /// </summary>
  139. /// <remarks>The same as <see cref="AddNoResize"/> (because a fixed list is never resized).</remarks>
  140. /// <param name="item">The element to append at the end of the list.</param>
  141. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  142. public void Add(in T item)
  143. {
  144. this[Length++] = item;
  145. }
  146. /// <summary>
  147. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  148. /// </summary>
  149. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  150. /// <param name="ptr">A buffer.</param>
  151. /// <param name="length">The number of elements from the buffer to append.</param>
  152. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  153. public unsafe void AddRange(void* ptr, int length)
  154. {
  155. T* data = (T*)ptr;
  156. for (var i = 0; i < length; ++i)
  157. {
  158. this[Length++] = data[i];
  159. }
  160. }
  161. /// <summary>
  162. /// Appends an element to the end of this list. Increments the length by 1.
  163. /// </summary>
  164. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  165. /// <param name="item">The element to append at the end of the list.</param>
  166. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  167. public void AddNoResize(in T item) => Add(item);
  168. /// <summary>
  169. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  170. /// </summary>
  171. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  172. /// <param name="ptr">A buffer.</param>
  173. /// <param name="length">The number of elements from the buffer to append.</param>
  174. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  175. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  176. /// <summary>
  177. /// Sets the length to 0.
  178. /// </summary>
  179. /// <remarks> Does *not* zero out the bytes.</remarks>
  180. public void Clear()
  181. {
  182. Length = 0;
  183. }
  184. /// <summary>
  185. /// Shifts elements toward the end of this list, increasing its length.
  186. /// </summary>
  187. /// <remarks>
  188. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  189. ///
  190. /// The length is increased by `end - begin`.
  191. ///
  192. /// If `end` equals `begin`, the method does nothing.
  193. ///
  194. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  195. ///
  196. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  197. /// </remarks>
  198. /// <param name="begin">The index of the first element that will be shifted up.</param>
  199. /// <param name="end">The index where the first shifted element will end up.</param>
  200. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  201. public void InsertRangeWithBeginEnd(int begin, int end)
  202. {
  203. int items = end - begin;
  204. if(items < 1)
  205. return;
  206. int itemsToCopy = length - begin;
  207. Length += items;
  208. if(itemsToCopy < 1)
  209. return;
  210. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  211. unsafe
  212. {
  213. byte *b = Buffer;
  214. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  215. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  216. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  217. }
  218. }
  219. /// <summary>
  220. /// Inserts a single element at an index. Increments the length by 1.
  221. /// </summary>
  222. /// <param name="index">The index at which to insert the element.</param>
  223. /// <param name="item">The element to insert.</param>
  224. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  225. public void Insert(int index, in T item)
  226. {
  227. InsertRangeWithBeginEnd(index, index+1);
  228. this[index] = item;
  229. }
  230. /// <summary>
  231. /// Copies the last element of this list to an index. Decrements the length by 1.
  232. /// </summary>
  233. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  234. /// <param name="index">The index to overwrite with the last element.</param>
  235. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  236. public void RemoveAtSwapBack(int index)
  237. {
  238. RemoveRangeSwapBack(index, 1);
  239. }
  240. /// <summary>
  241. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  242. /// </summary>
  243. /// <remarks>
  244. /// Copies the last `count`-numbered elements to the range starting at `index`.
  245. ///
  246. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  247. ///
  248. /// Does nothing if the count is less than 1.
  249. /// </remarks>
  250. /// <param name="index">The first index of the destination range.</param>
  251. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  252. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  253. public void RemoveRangeSwapBack(int index, int count)
  254. {
  255. if (count > 0)
  256. {
  257. int copyFrom = math.max(Length - count, index + count);
  258. unsafe
  259. {
  260. var sizeOf = UnsafeUtility.SizeOf<T>();
  261. void* dst = Buffer + index * sizeOf;
  262. void* src = Buffer + copyFrom * sizeOf;
  263. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  264. }
  265. Length -= count;
  266. }
  267. }
  268. /// <summary>
  269. /// Truncates the list by replacing the item at the specified index range with the items from the end the list. The list
  270. /// is shortened by number of elements in range.
  271. /// </summary>
  272. /// <param name="begin">The first index of the item to remove.</param>
  273. /// <param name="end">The index past-the-last item to remove.</param>
  274. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  275. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  276. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  277. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  278. /// <summary>
  279. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  280. /// </summary>
  281. /// <param name="index">The index of the element to remove.</param>
  282. /// <remarks>
  283. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  284. /// </remarks>
  285. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  286. public void RemoveAt(int index)
  287. {
  288. RemoveRange(index, 1);
  289. }
  290. /// <summary>
  291. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  292. /// </summary>
  293. /// <remarks>
  294. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  295. /// </remarks>
  296. /// <param name="index">The first index of the range to remove.</param>
  297. /// <param name="count">The number of elements to remove.</param>
  298. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  299. public void RemoveRange(int index, int count)
  300. {
  301. if (count > 0)
  302. {
  303. int copyFrom = math.min(index + count, Length);
  304. unsafe
  305. {
  306. var sizeOf = UnsafeUtility.SizeOf<T>();
  307. void* dst = Buffer + index * sizeOf;
  308. void* src = Buffer + copyFrom * sizeOf;
  309. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  310. }
  311. Length -= count;
  312. }
  313. }
  314. /// <summary>
  315. /// Truncates the list by removing the items at the specified index range, and shifting all remaining items to replace removed items. The list
  316. /// is shortened by number of elements in range.
  317. /// </summary>
  318. /// <param name="begin">The first index of the item to remove.</param>
  319. /// <param name="end">The index past-the-last item to remove.</param>
  320. /// <remarks>
  321. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  322. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  323. /// </remarks>
  324. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  325. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  326. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  327. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  328. /// <summary>
  329. /// Returns a managed array that is a copy of this list.
  330. /// </summary>
  331. /// <returns>A managed array that is a copy of this list.</returns>
  332. [NotBurstCompatible]
  333. public T[] ToArray()
  334. {
  335. var result = new T[Length];
  336. unsafe
  337. {
  338. byte* s = Buffer;
  339. fixed(T* d = result)
  340. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  341. }
  342. return result;
  343. }
  344. /// <summary>
  345. /// Returns an array that is a copy of this list.
  346. /// </summary>
  347. /// <param name="allocator">The allocator to use.</param>
  348. /// <returns>An array that is a copy of this list.</returns>
  349. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  350. {
  351. unsafe
  352. {
  353. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  354. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  355. return copy;
  356. }
  357. }
  358. }
  359. [BurstCompatible]
  360. struct FixedList
  361. {
  362. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  363. internal static int PaddingBytes<T>() where T : struct
  364. {
  365. return math.max(0, math.min(6, (1 << math.tzcnt(UnsafeUtility.SizeOf<T>())) - 2));
  366. }
  367. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  368. internal static int StorageBytes<BUFFER,T>() where BUFFER : struct where T : struct
  369. {
  370. return UnsafeUtility.SizeOf<BUFFER>() - PaddingBytes<T>();
  371. }
  372. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  373. internal static int Capacity<BUFFER,T>() where BUFFER : struct where T : struct
  374. {
  375. return StorageBytes<BUFFER,T>() / UnsafeUtility.SizeOf<T>();
  376. }
  377. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  378. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  379. internal static void CheckResize<BUFFER,T>(int newLength) where BUFFER : struct where T : struct
  380. {
  381. var Capacity = Capacity<BUFFER,T>();
  382. if (newLength < 0 || newLength > Capacity)
  383. throw new IndexOutOfRangeException($"NewLength {newLength} is out of range of '{Capacity}' Capacity.");
  384. }
  385. }
  386. [Obsolete("Renamed to FixedList32Bytes<T> (UnityUpgradable) -> FixedList32Bytes<T>", true)]
  387. public struct FixedList32<T> where T : unmanaged {}
  388. /// <summary>
  389. /// An unmanaged, resizable list whose content is all stored directly in the 32-byte struct. Useful for small lists.
  390. /// </summary>
  391. /// <typeparam name="T">The type of the elements.</typeparam>
  392. [Serializable]
  393. [DebuggerTypeProxy(typeof(FixedList32BytesDebugView<>))]
  394. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  395. public struct FixedList32Bytes<T>
  396. : INativeList<T>
  397. , IEnumerable<T> // Used by collection initializers.
  398. , IEquatable<FixedList32Bytes<T>>
  399. , IComparable<FixedList32Bytes<T>>
  400. , IEquatable<FixedList64Bytes<T>>
  401. , IComparable<FixedList64Bytes<T>>
  402. , IEquatable<FixedList128Bytes<T>>
  403. , IComparable<FixedList128Bytes<T>>
  404. , IEquatable<FixedList512Bytes<T>>
  405. , IComparable<FixedList512Bytes<T>>
  406. , IEquatable<FixedList4096Bytes<T>>
  407. , IComparable<FixedList4096Bytes<T>>
  408. where T : unmanaged
  409. {
  410. [SerializeField] internal ushort length;
  411. [SerializeField] internal FixedBytes30 buffer;
  412. /// <summary>
  413. /// The current number of items in this list.
  414. /// </summary>
  415. /// <value>The current number of items in this list.</value>
  416. [CreateProperty]
  417. public int Length
  418. {
  419. get => length;
  420. set
  421. {
  422. FixedList.CheckResize<FixedBytes30,T>(value);
  423. length = (ushort)value;
  424. }
  425. }
  426. /// <summary>
  427. /// A property in order to display items in the Entity Inspector.
  428. /// </summary>
  429. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  430. /// <summary>
  431. /// Whether this list is empty.
  432. /// </summary>
  433. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  434. public bool IsEmpty => Length == 0;
  435. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  436. unsafe internal byte* Buffer
  437. {
  438. get
  439. {
  440. fixed(byte* b = &buffer.offset0000.byte0000)
  441. return b + FixedList.PaddingBytes<T>();
  442. }
  443. }
  444. /// <summary>
  445. /// The number of elements that can fit in this list.
  446. /// </summary>
  447. /// <value>The number of elements that can fit in this list.</value>
  448. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  449. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  450. public int Capacity
  451. {
  452. get
  453. {
  454. return FixedList.Capacity<FixedBytes30,T>();
  455. }
  456. set
  457. {
  458. CollectionHelper.CheckCapacityInRange(value, Length);
  459. }
  460. }
  461. /// <summary>
  462. /// The element at a given index.
  463. /// </summary>
  464. /// <param name="index">An index.</param>
  465. /// <value>The value to store at the index.</value>
  466. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  467. public T this[int index]
  468. {
  469. get
  470. {
  471. CollectionHelper.CheckIndexInRange(index, length);
  472. unsafe
  473. {
  474. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  475. }
  476. }
  477. set
  478. {
  479. CollectionHelper.CheckIndexInRange(index, length);
  480. unsafe
  481. {
  482. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  483. }
  484. }
  485. }
  486. /// <summary>
  487. /// Returns the element at a given index.
  488. /// </summary>
  489. /// <param name="index">An index.</param>
  490. /// <returns>The list element at the index.</returns>
  491. public ref T ElementAt(int index)
  492. {
  493. CollectionHelper.CheckIndexInRange(index, length);
  494. unsafe
  495. {
  496. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  497. }
  498. }
  499. /// <summary>
  500. /// Returns the hash code of this list.
  501. /// </summary>
  502. /// <remarks>
  503. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  504. /// <returns>The hash code of this list.</returns>
  505. public override int GetHashCode()
  506. {
  507. unsafe
  508. {
  509. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  510. }
  511. }
  512. /// <summary>
  513. /// Appends an element to the end of this list. Increments the length by 1.
  514. /// </summary>
  515. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  516. /// <param name="item">The element to append at the end of the list.</param>
  517. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  518. public void Add(in T item)
  519. {
  520. this[Length++] = item;
  521. }
  522. /// <summary>
  523. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  524. /// </summary>
  525. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  526. /// <param name="ptr">A buffer.</param>
  527. /// <param name="length">The number of elements from the buffer to append.</param>
  528. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  529. public unsafe void AddRange(void* ptr, int length)
  530. {
  531. T* data = (T*)ptr;
  532. for (var i = 0; i < length; ++i)
  533. {
  534. this[Length++] = data[i];
  535. }
  536. }
  537. /// <summary>
  538. /// Appends an element to the end of this list. Increments the length by 1.
  539. /// </summary>
  540. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  541. /// <param name="item">The element to append at the end of the list.</param>
  542. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  543. public void AddNoResize(in T item) => Add(item);
  544. /// <summary>
  545. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  546. /// </summary>
  547. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  548. /// <param name="ptr">A buffer.</param>
  549. /// <param name="length">The number of elements from the buffer to append.</param>
  550. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  551. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  552. /// <summary>
  553. /// Sets the length to 0.
  554. /// </summary>
  555. /// <remarks> Does *not* zero out the bytes.</remarks>
  556. public void Clear()
  557. {
  558. Length = 0;
  559. }
  560. /// <summary>
  561. /// Shifts elements toward the end of this list, increasing its length.
  562. /// </summary>
  563. /// <remarks>
  564. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  565. ///
  566. /// The length is increased by `end - begin`.
  567. ///
  568. /// If `end` equals `begin`, the method does nothing.
  569. ///
  570. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  571. ///
  572. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  573. /// </remarks>
  574. /// <param name="begin">The index of the first element that will be shifted up.</param>
  575. /// <param name="end">The index where the first shifted element will end up.</param>
  576. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  577. public void InsertRangeWithBeginEnd(int begin, int end)
  578. {
  579. int items = end - begin;
  580. if(items < 1)
  581. return;
  582. int itemsToCopy = length - begin;
  583. Length += items;
  584. if(itemsToCopy < 1)
  585. return;
  586. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  587. unsafe
  588. {
  589. byte *b = Buffer;
  590. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  591. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  592. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  593. }
  594. }
  595. /// <summary>
  596. /// Inserts a single element at an index. Increments the length by 1.
  597. /// </summary>
  598. /// <param name="index">The index at which to insert the element.</param>
  599. /// <param name="item">The element to insert.</param>
  600. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  601. public void Insert(int index, in T item)
  602. {
  603. InsertRangeWithBeginEnd(index, index+1);
  604. this[index] = item;
  605. }
  606. /// <summary>
  607. /// Copies the last element of this list to an index. Decrements the length by 1.
  608. /// </summary>
  609. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  610. /// <param name="index">The index to overwrite with the last element.</param>
  611. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  612. public void RemoveAtSwapBack(int index)
  613. {
  614. RemoveRangeSwapBack(index, 1);
  615. }
  616. /// <summary>
  617. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  618. /// </summary>
  619. /// <remarks>
  620. /// Copies the last `count`-numbered elements to the range starting at `index`.
  621. ///
  622. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  623. ///
  624. /// Does nothing if the count is less than 1.
  625. /// </remarks>
  626. /// <param name="index">The first index of the destination range.</param>
  627. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  628. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  629. public void RemoveRangeSwapBack(int index, int count)
  630. {
  631. if (count > 0)
  632. {
  633. int copyFrom = math.max(Length - count, index + count);
  634. unsafe
  635. {
  636. var sizeOf = UnsafeUtility.SizeOf<T>();
  637. void* dst = Buffer + index * sizeOf;
  638. void* src = Buffer + copyFrom * sizeOf;
  639. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  640. }
  641. Length -= count;
  642. }
  643. }
  644. /// <summary>
  645. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  646. /// </summary>
  647. /// <param name="begin">The first index of the item to remove.</param>
  648. /// <param name="end">The index past-the-last item to remove.</param>
  649. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  650. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  651. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  652. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  653. /// <summary>
  654. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  655. /// </summary>
  656. /// <param name="index">The index of the element to remove.</param>
  657. /// <remarks>
  658. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  659. /// </remarks>
  660. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  661. public void RemoveAt(int index)
  662. {
  663. RemoveRange(index, 1);
  664. }
  665. /// <summary>
  666. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  667. /// </summary>
  668. /// <remarks>
  669. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  670. /// </remarks>
  671. /// <param name="index">The first index of the range to remove.</param>
  672. /// <param name="count">The number of elements to remove.</param>
  673. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  674. public void RemoveRange(int index, int count)
  675. {
  676. if (count > 0)
  677. {
  678. int copyFrom = math.min(index + count, Length);
  679. unsafe
  680. {
  681. var sizeOf = UnsafeUtility.SizeOf<T>();
  682. void* dst = Buffer + index * sizeOf;
  683. void* src = Buffer + copyFrom * sizeOf;
  684. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  685. }
  686. Length -= count;
  687. }
  688. }
  689. /// <summary>
  690. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  691. /// </summary>
  692. /// <param name="begin">The first index of the item to remove.</param>
  693. /// <param name="end">The index past-the-last item to remove.</param>
  694. /// <remarks>
  695. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  696. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  697. /// </remarks>
  698. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  699. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  700. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  701. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  702. /// <summary>
  703. /// Returns a managed array that is a copy of this list.
  704. /// </summary>
  705. /// <returns>A managed array that is a copy of this list.</returns>
  706. [NotBurstCompatible]
  707. public T[] ToArray()
  708. {
  709. var result = new T[Length];
  710. unsafe
  711. {
  712. byte* s = Buffer;
  713. fixed(T* d = result)
  714. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  715. }
  716. return result;
  717. }
  718. /// <summary>
  719. /// Returns an array that is a copy of this list.
  720. /// </summary>
  721. /// <param name="allocator">The allocator to use.</param>
  722. /// <returns>An array that is a copy of this list.</returns>
  723. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  724. {
  725. unsafe
  726. {
  727. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  728. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  729. return copy;
  730. }
  731. }
  732. /// <summary>
  733. /// Returns true if two lists are equal.
  734. /// </summary>
  735. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  736. /// <param name="a">The first list to compare for equality.</param>
  737. /// <param name="b">The second list to compare for equality.</param>
  738. /// <returns>True if the two lists are equal.</returns>
  739. public static bool operator ==(in FixedList32Bytes<T> a, in FixedList32Bytes<T> b)
  740. {
  741. unsafe
  742. {
  743. if(a.length != b.length)
  744. return false;
  745. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  746. }
  747. }
  748. /// <summary>
  749. /// Returns true if two lists are unequal.
  750. /// </summary>
  751. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  752. /// <param name="a">The first list to compare for inequality.</param>
  753. /// <param name="b">The second list to compare for inequality.</param>
  754. /// <returns>True if the two lists are unequal.</returns>
  755. public static bool operator !=(in FixedList32Bytes<T> a, in FixedList32Bytes<T> b)
  756. {
  757. return !(a == b);
  758. }
  759. /// <summary>
  760. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  761. /// </summary>
  762. /// <param name="other">A list to to compare with.</param>
  763. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  764. ///
  765. /// 0 denotes that both lists should have the same position in a sort.
  766. /// -1 denotes that this list should precede the other list in a sort.
  767. /// +1 denotes that this list should follow the other list in a sort.
  768. /// </returns>
  769. public int CompareTo(FixedList32Bytes<T> other)
  770. {
  771. unsafe
  772. {
  773. fixed(byte* a = &buffer.offset0000.byte0000)
  774. {
  775. byte* b = &other.buffer.offset0000.byte0000;
  776. var aa = a + FixedList.PaddingBytes<T>();
  777. var bb = b + FixedList.PaddingBytes<T>();
  778. var mini = math.min(Length, other.Length);
  779. for(var i = 0; i < mini; ++i)
  780. {
  781. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  782. if(j != 0)
  783. return j;
  784. }
  785. return Length.CompareTo(other.Length);
  786. }
  787. }
  788. }
  789. /// <summary>
  790. /// Returns true if this list and another list are equal.
  791. /// </summary>
  792. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  793. /// <param name="other">The list to compare for equality.</param>
  794. /// <returns>True if the two lists are equal.</returns>
  795. public bool Equals(FixedList32Bytes<T> other)
  796. {
  797. return CompareTo(other) == 0;
  798. }
  799. /// <summary>
  800. /// Returns true if two lists are equal.
  801. /// </summary>
  802. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  803. /// <param name="a">The first list to compare for equality.</param>
  804. /// <param name="b">The second list to compare for equality.</param>
  805. /// <returns>True if the two lists are equal.</returns>
  806. public static bool operator ==(in FixedList32Bytes<T> a, in FixedList64Bytes<T> b)
  807. {
  808. unsafe
  809. {
  810. if(a.length != b.length)
  811. return false;
  812. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  813. }
  814. }
  815. /// <summary>
  816. /// Returns true if two lists are unequal.
  817. /// </summary>
  818. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  819. /// <param name="a">The first list to compare for inequality.</param>
  820. /// <param name="b">The second list to compare for inequality.</param>
  821. /// <returns>True if the two lists are unequal.</returns>
  822. public static bool operator !=(in FixedList32Bytes<T> a, in FixedList64Bytes<T> b)
  823. {
  824. return !(a == b);
  825. }
  826. /// <summary>
  827. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  828. /// </summary>
  829. /// <param name="other">A list to to compare with.</param>
  830. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  831. ///
  832. /// 0 denotes that both lists should have the same position in a sort.
  833. /// -1 denotes that this list should precede the other list in a sort.
  834. /// +1 denotes that this list should follow the other list in a sort.
  835. /// </returns>
  836. public int CompareTo(FixedList64Bytes<T> other)
  837. {
  838. unsafe
  839. {
  840. fixed(byte* a = &buffer.offset0000.byte0000)
  841. {
  842. byte* b = &other.buffer.offset0000.byte0000;
  843. var aa = a + FixedList.PaddingBytes<T>();
  844. var bb = b + FixedList.PaddingBytes<T>();
  845. var mini = math.min(Length, other.Length);
  846. for(var i = 0; i < mini; ++i)
  847. {
  848. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  849. if(j != 0)
  850. return j;
  851. }
  852. return Length.CompareTo(other.Length);
  853. }
  854. }
  855. }
  856. /// <summary>
  857. /// Returns true if this list and another list are equal.
  858. /// </summary>
  859. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  860. /// <param name="other">The list to compare for equality.</param>
  861. /// <returns>True if the two lists are equal.</returns>
  862. public bool Equals(FixedList64Bytes<T> other)
  863. {
  864. return CompareTo(other) == 0;
  865. }
  866. /// <summary>
  867. /// Initializes and returns an instance of FixedList32Bytes with content copied from another list.
  868. /// </summary>
  869. /// <param name="other">The list to copy.</param>
  870. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  871. public FixedList32Bytes(in FixedList64Bytes<T> other)
  872. {
  873. this = default;
  874. var error = Initialize(other);
  875. if(error != 0)
  876. FixedList.CheckResize<FixedBytes30,T>(other.Length);
  877. }
  878. /// <summary>
  879. /// Initializes an instance of FixedList32Bytes with content copied from another list.
  880. /// </summary>
  881. /// <param name="other">The list to copy.</param>
  882. /// <returns>zero on success, or non-zero on error.</returns>
  883. internal int Initialize(in FixedList64Bytes<T> other)
  884. {
  885. if(other.Length > Capacity)
  886. return (int)CopyError.Truncation;
  887. length = other.length;
  888. buffer = new FixedBytes30();
  889. unsafe
  890. {
  891. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  892. }
  893. return 0;
  894. }
  895. /// <summary>
  896. /// Returns a new list that is a copy of another list.
  897. /// </summary>
  898. /// <param name="other">The list to copy.</param>
  899. /// <returns>A new list that is a copy of the other.</returns>
  900. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  901. public static implicit operator FixedList32Bytes<T>(in FixedList64Bytes<T> other)
  902. {
  903. return new FixedList32Bytes<T>(other);
  904. }
  905. /// <summary>
  906. /// Returns true if two lists are equal.
  907. /// </summary>
  908. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  909. /// <param name="a">The first list to compare for equality.</param>
  910. /// <param name="b">The second list to compare for equality.</param>
  911. /// <returns>True if the two lists are equal.</returns>
  912. public static bool operator ==(in FixedList32Bytes<T> a, in FixedList128Bytes<T> b)
  913. {
  914. unsafe
  915. {
  916. if(a.length != b.length)
  917. return false;
  918. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  919. }
  920. }
  921. /// <summary>
  922. /// Returns true if two lists are unequal.
  923. /// </summary>
  924. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  925. /// <param name="a">The first list to compare for inequality.</param>
  926. /// <param name="b">The second list to compare for inequality.</param>
  927. /// <returns>True if the two lists are unequal.</returns>
  928. public static bool operator !=(in FixedList32Bytes<T> a, in FixedList128Bytes<T> b)
  929. {
  930. return !(a == b);
  931. }
  932. /// <summary>
  933. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  934. /// </summary>
  935. /// <param name="other">A list to to compare with.</param>
  936. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  937. ///
  938. /// 0 denotes that both lists should have the same position in a sort.
  939. /// -1 denotes that this list should precede the other list in a sort.
  940. /// +1 denotes that this list should follow the other list in a sort.
  941. /// </returns>
  942. public int CompareTo(FixedList128Bytes<T> other)
  943. {
  944. unsafe
  945. {
  946. fixed(byte* a = &buffer.offset0000.byte0000)
  947. {
  948. byte* b = &other.buffer.offset0000.byte0000;
  949. var aa = a + FixedList.PaddingBytes<T>();
  950. var bb = b + FixedList.PaddingBytes<T>();
  951. var mini = math.min(Length, other.Length);
  952. for(var i = 0; i < mini; ++i)
  953. {
  954. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  955. if(j != 0)
  956. return j;
  957. }
  958. return Length.CompareTo(other.Length);
  959. }
  960. }
  961. }
  962. /// <summary>
  963. /// Returns true if this list and another list are equal.
  964. /// </summary>
  965. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  966. /// <param name="other">The list to compare for equality.</param>
  967. /// <returns>True if the two lists are equal.</returns>
  968. public bool Equals(FixedList128Bytes<T> other)
  969. {
  970. return CompareTo(other) == 0;
  971. }
  972. /// <summary>
  973. /// Initializes and returns an instance of FixedList32Bytes with content copied from another list.
  974. /// </summary>
  975. /// <param name="other">The list to copy.</param>
  976. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  977. public FixedList32Bytes(in FixedList128Bytes<T> other)
  978. {
  979. this = default;
  980. var error = Initialize(other);
  981. if(error != 0)
  982. FixedList.CheckResize<FixedBytes30,T>(other.Length);
  983. }
  984. /// <summary>
  985. /// Initializes an instance of FixedList32Bytes with content copied from another list.
  986. /// </summary>
  987. /// <param name="other">The list to copy.</param>
  988. /// <returns>zero on success, or non-zero on error.</returns>
  989. internal int Initialize(in FixedList128Bytes<T> other)
  990. {
  991. if(other.Length > Capacity)
  992. return (int)CopyError.Truncation;
  993. length = other.length;
  994. buffer = new FixedBytes30();
  995. unsafe
  996. {
  997. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  998. }
  999. return 0;
  1000. }
  1001. /// <summary>
  1002. /// Returns a new list that is a copy of another list.
  1003. /// </summary>
  1004. /// <param name="other">The list to copy.</param>
  1005. /// <returns>A new list that is a copy of the other.</returns>
  1006. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  1007. public static implicit operator FixedList32Bytes<T>(in FixedList128Bytes<T> other)
  1008. {
  1009. return new FixedList32Bytes<T>(other);
  1010. }
  1011. /// <summary>
  1012. /// Returns true if two lists are equal.
  1013. /// </summary>
  1014. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1015. /// <param name="a">The first list to compare for equality.</param>
  1016. /// <param name="b">The second list to compare for equality.</param>
  1017. /// <returns>True if the two lists are equal.</returns>
  1018. public static bool operator ==(in FixedList32Bytes<T> a, in FixedList512Bytes<T> b)
  1019. {
  1020. unsafe
  1021. {
  1022. if(a.length != b.length)
  1023. return false;
  1024. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  1025. }
  1026. }
  1027. /// <summary>
  1028. /// Returns true if two lists are unequal.
  1029. /// </summary>
  1030. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1031. /// <param name="a">The first list to compare for inequality.</param>
  1032. /// <param name="b">The second list to compare for inequality.</param>
  1033. /// <returns>True if the two lists are unequal.</returns>
  1034. public static bool operator !=(in FixedList32Bytes<T> a, in FixedList512Bytes<T> b)
  1035. {
  1036. return !(a == b);
  1037. }
  1038. /// <summary>
  1039. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  1040. /// </summary>
  1041. /// <param name="other">A list to to compare with.</param>
  1042. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  1043. ///
  1044. /// 0 denotes that both lists should have the same position in a sort.
  1045. /// -1 denotes that this list should precede the other list in a sort.
  1046. /// +1 denotes that this list should follow the other list in a sort.
  1047. /// </returns>
  1048. public int CompareTo(FixedList512Bytes<T> other)
  1049. {
  1050. unsafe
  1051. {
  1052. fixed(byte* a = &buffer.offset0000.byte0000)
  1053. {
  1054. byte* b = &other.buffer.offset0000.byte0000;
  1055. var aa = a + FixedList.PaddingBytes<T>();
  1056. var bb = b + FixedList.PaddingBytes<T>();
  1057. var mini = math.min(Length, other.Length);
  1058. for(var i = 0; i < mini; ++i)
  1059. {
  1060. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  1061. if(j != 0)
  1062. return j;
  1063. }
  1064. return Length.CompareTo(other.Length);
  1065. }
  1066. }
  1067. }
  1068. /// <summary>
  1069. /// Returns true if this list and another list are equal.
  1070. /// </summary>
  1071. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1072. /// <param name="other">The list to compare for equality.</param>
  1073. /// <returns>True if the two lists are equal.</returns>
  1074. public bool Equals(FixedList512Bytes<T> other)
  1075. {
  1076. return CompareTo(other) == 0;
  1077. }
  1078. /// <summary>
  1079. /// Initializes and returns an instance of FixedList32Bytes with content copied from another list.
  1080. /// </summary>
  1081. /// <param name="other">The list to copy.</param>
  1082. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  1083. public FixedList32Bytes(in FixedList512Bytes<T> other)
  1084. {
  1085. this = default;
  1086. var error = Initialize(other);
  1087. if(error != 0)
  1088. FixedList.CheckResize<FixedBytes30,T>(other.Length);
  1089. }
  1090. /// <summary>
  1091. /// Initializes an instance of FixedList32Bytes with content copied from another list.
  1092. /// </summary>
  1093. /// <param name="other">The list to copy.</param>
  1094. /// <returns>zero on success, or non-zero on error.</returns>
  1095. internal int Initialize(in FixedList512Bytes<T> other)
  1096. {
  1097. if(other.Length > Capacity)
  1098. return (int)CopyError.Truncation;
  1099. length = other.length;
  1100. buffer = new FixedBytes30();
  1101. unsafe
  1102. {
  1103. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  1104. }
  1105. return 0;
  1106. }
  1107. /// <summary>
  1108. /// Returns a new list that is a copy of another list.
  1109. /// </summary>
  1110. /// <param name="other">The list to copy.</param>
  1111. /// <returns>A new list that is a copy of the other.</returns>
  1112. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  1113. public static implicit operator FixedList32Bytes<T>(in FixedList512Bytes<T> other)
  1114. {
  1115. return new FixedList32Bytes<T>(other);
  1116. }
  1117. /// <summary>
  1118. /// Returns true if two lists are equal.
  1119. /// </summary>
  1120. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1121. /// <param name="a">The first list to compare for equality.</param>
  1122. /// <param name="b">The second list to compare for equality.</param>
  1123. /// <returns>True if the two lists are equal.</returns>
  1124. public static bool operator ==(in FixedList32Bytes<T> a, in FixedList4096Bytes<T> b)
  1125. {
  1126. unsafe
  1127. {
  1128. if(a.length != b.length)
  1129. return false;
  1130. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  1131. }
  1132. }
  1133. /// <summary>
  1134. /// Returns true if two lists are unequal.
  1135. /// </summary>
  1136. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1137. /// <param name="a">The first list to compare for inequality.</param>
  1138. /// <param name="b">The second list to compare for inequality.</param>
  1139. /// <returns>True if the two lists are unequal.</returns>
  1140. public static bool operator !=(in FixedList32Bytes<T> a, in FixedList4096Bytes<T> b)
  1141. {
  1142. return !(a == b);
  1143. }
  1144. /// <summary>
  1145. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  1146. /// </summary>
  1147. /// <param name="other">A list to to compare with.</param>
  1148. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  1149. ///
  1150. /// 0 denotes that both lists should have the same position in a sort.
  1151. /// -1 denotes that this list should precede the other list in a sort.
  1152. /// +1 denotes that this list should follow the other list in a sort.
  1153. /// </returns>
  1154. public int CompareTo(FixedList4096Bytes<T> other)
  1155. {
  1156. unsafe
  1157. {
  1158. fixed(byte* a = &buffer.offset0000.byte0000)
  1159. {
  1160. byte* b = &other.buffer.offset0000.byte0000;
  1161. var aa = a + FixedList.PaddingBytes<T>();
  1162. var bb = b + FixedList.PaddingBytes<T>();
  1163. var mini = math.min(Length, other.Length);
  1164. for(var i = 0; i < mini; ++i)
  1165. {
  1166. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  1167. if(j != 0)
  1168. return j;
  1169. }
  1170. return Length.CompareTo(other.Length);
  1171. }
  1172. }
  1173. }
  1174. /// <summary>
  1175. /// Returns true if this list and another list are equal.
  1176. /// </summary>
  1177. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1178. /// <param name="other">The list to compare for equality.</param>
  1179. /// <returns>True if the two lists are equal.</returns>
  1180. public bool Equals(FixedList4096Bytes<T> other)
  1181. {
  1182. return CompareTo(other) == 0;
  1183. }
  1184. /// <summary>
  1185. /// Initializes and returns an instance of FixedList32Bytes with content copied from another list.
  1186. /// </summary>
  1187. /// <param name="other">The list to copy.</param>
  1188. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  1189. public FixedList32Bytes(in FixedList4096Bytes<T> other)
  1190. {
  1191. this = default;
  1192. var error = Initialize(other);
  1193. if(error != 0)
  1194. FixedList.CheckResize<FixedBytes30,T>(other.Length);
  1195. }
  1196. /// <summary>
  1197. /// Initializes an instance of FixedList32Bytes with content copied from another list.
  1198. /// </summary>
  1199. /// <param name="other">The list to copy.</param>
  1200. /// <returns>zero on success, or non-zero on error.</returns>
  1201. internal int Initialize(in FixedList4096Bytes<T> other)
  1202. {
  1203. if(other.Length > Capacity)
  1204. return (int)CopyError.Truncation;
  1205. length = other.length;
  1206. buffer = new FixedBytes30();
  1207. unsafe
  1208. {
  1209. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  1210. }
  1211. return 0;
  1212. }
  1213. /// <summary>
  1214. /// Returns a new list that is a copy of another list.
  1215. /// </summary>
  1216. /// <param name="other">The list to copy.</param>
  1217. /// <returns>A new list that is a copy of the other.</returns>
  1218. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList32Bytes&lt;T&gt;.</exception>
  1219. public static implicit operator FixedList32Bytes<T>(in FixedList4096Bytes<T> other)
  1220. {
  1221. return new FixedList32Bytes<T>(other);
  1222. }
  1223. /// <summary>
  1224. /// Returns true if the list is equal to an object.
  1225. /// </summary>
  1226. /// <remarks>Two lists are equal if their length and bytes are equal.
  1227. ///
  1228. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  1229. /// </remarks>
  1230. /// <param name="obj">An object to compare for equality.</param>
  1231. /// <returns>True if the list is equal to the object.</returns>
  1232. [NotBurstCompatible]
  1233. public override bool Equals(object obj)
  1234. {
  1235. if(obj is FixedList32Bytes<T> aFixedList32Bytes) return Equals(aFixedList32Bytes);
  1236. if(obj is FixedList64Bytes<T> aFixedList64Bytes) return Equals(aFixedList64Bytes);
  1237. if(obj is FixedList128Bytes<T> aFixedList128Bytes) return Equals(aFixedList128Bytes);
  1238. if(obj is FixedList512Bytes<T> aFixedList512Bytes) return Equals(aFixedList512Bytes);
  1239. if(obj is FixedList4096Bytes<T> aFixedList4096Bytes) return Equals(aFixedList4096Bytes);
  1240. return false;
  1241. }
  1242. /// <summary>
  1243. /// An enumerator over the elements of a FixedList32Bytes&lt;T&gt;.
  1244. /// </summary>
  1245. /// <remarks>
  1246. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  1247. /// </remarks>
  1248. public struct Enumerator : IEnumerator<T>
  1249. {
  1250. FixedList32Bytes<T> m_List;
  1251. int m_Index;
  1252. /// <summary>
  1253. /// Initializes and returns an instance of FixedList32Bytes&lt;T&gt;.
  1254. /// </summary>
  1255. /// <param name="list">The list for which to create an enumerator.</param>
  1256. public Enumerator(ref FixedList32Bytes<T> list)
  1257. {
  1258. m_List = list;
  1259. m_Index = -1;
  1260. }
  1261. /// <summary>
  1262. /// Does nothing.
  1263. /// </summary>
  1264. public void Dispose()
  1265. {
  1266. }
  1267. /// <summary>
  1268. /// Advances the enumerator to the next element.
  1269. /// </summary>
  1270. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  1271. public bool MoveNext()
  1272. {
  1273. m_Index++;
  1274. return m_Index < m_List.Length;
  1275. }
  1276. /// <summary>
  1277. /// Resets the enumerator to its initial state.
  1278. /// </summary>
  1279. public void Reset()
  1280. {
  1281. m_Index = -1;
  1282. }
  1283. /// <summary>
  1284. /// The current element.
  1285. /// </summary>
  1286. /// <value>The current element.</value>
  1287. public T Current => m_List[m_Index]; // Let FixedList32Bytes<T> indexer check for out of range.
  1288. object IEnumerator.Current => Current;
  1289. }
  1290. /// <summary>
  1291. /// Returns an enumerator for iterating over the elements of this list.
  1292. /// </summary>
  1293. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  1294. public Enumerator GetEnumerator()
  1295. {
  1296. return new Enumerator(ref this);
  1297. }
  1298. /// <summary>
  1299. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  1300. /// </summary>
  1301. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  1302. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  1303. IEnumerator IEnumerable.GetEnumerator()
  1304. {
  1305. throw new NotImplementedException();
  1306. }
  1307. /// <summary>
  1308. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  1309. /// </summary>
  1310. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  1311. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  1312. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  1313. {
  1314. throw new NotImplementedException();
  1315. }
  1316. }
  1317. /// <summary>
  1318. /// Provides extension methods for FixedList32Bytes.
  1319. /// </summary>
  1320. [BurstCompatible]
  1321. public unsafe static class FixedList32BytesExtensions
  1322. {
  1323. /// <summary>
  1324. /// Finds the index of the first occurrence of a particular value in this list.
  1325. /// </summary>
  1326. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1327. /// <typeparam name="U">The value type.</typeparam>
  1328. /// <param name="list">The list to search.</param>
  1329. /// <param name="value">The value to locate.</param>
  1330. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  1331. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1332. public static int IndexOf<T, U>(this ref FixedList32Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  1333. {
  1334. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  1335. }
  1336. /// <summary>
  1337. /// Returns true if a particular value is present in this list.
  1338. /// </summary>
  1339. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1340. /// <typeparam name="U">The value type.</typeparam>
  1341. /// <param name="list">The list to search.</param>
  1342. /// <param name="value">The value to locate.</param>
  1343. /// <returns>True if the value is present in this list.</returns>
  1344. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1345. public static bool Contains<T, U>(this ref FixedList32Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  1346. {
  1347. return list.IndexOf(value) != -1;
  1348. }
  1349. /// <summary>
  1350. /// Removes the first occurrence of a particular value in this list.
  1351. /// </summary>
  1352. /// <remarks>
  1353. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  1354. ///
  1355. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.FixedList32BytesExtensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  1356. /// </remarks>
  1357. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1358. /// <typeparam name="U">The value type.</typeparam>
  1359. /// <param name="list">The list to search.</param>
  1360. /// <param name="value">The value to locate and remove.</param>
  1361. /// <returns>True if the value was found and removed.</returns>
  1362. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1363. public static bool Remove<T, U>(this ref FixedList32Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  1364. {
  1365. int index = list.IndexOf(value);
  1366. if (index < 0)
  1367. {
  1368. return false;
  1369. }
  1370. list.RemoveAt(index);
  1371. return true;
  1372. }
  1373. /// <summary>
  1374. /// Removes the first occurrence of a particular value in this list.
  1375. /// </summary>
  1376. /// <remarks>
  1377. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  1378. ///
  1379. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  1380. /// </remarks>
  1381. /// <typeparam name="T">The type of elements in this list.</typeparam>
  1382. /// <typeparam name="U">The value type.</typeparam>
  1383. /// <param name="list">The list to search.</param>
  1384. /// <param name="value">The value to locate and remove.</param>
  1385. /// <returns>Returns true if the item is removed.</returns>
  1386. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  1387. public static bool RemoveSwapBack<T, U>(this ref FixedList32Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  1388. {
  1389. var index = list.IndexOf(value);
  1390. if (index == -1)
  1391. {
  1392. return false;
  1393. }
  1394. list.RemoveAtSwapBack(index);
  1395. return true;
  1396. }
  1397. }
  1398. sealed class FixedList32BytesDebugView<T> where T : unmanaged
  1399. {
  1400. FixedList32Bytes<T> m_List;
  1401. public FixedList32BytesDebugView(FixedList32Bytes<T> list)
  1402. {
  1403. m_List = list;
  1404. }
  1405. public T[] Items => m_List.ToArray();
  1406. }
  1407. [Obsolete("Renamed to FixedList64Bytes<T> (UnityUpgradable) -> FixedList64Bytes<T>", true)]
  1408. public struct FixedList64<T> where T : unmanaged {}
  1409. /// <summary>
  1410. /// An unmanaged, resizable list whose content is all stored directly in the 64-byte struct. Useful for small lists.
  1411. /// </summary>
  1412. /// <typeparam name="T">The type of the elements.</typeparam>
  1413. [Serializable]
  1414. [DebuggerTypeProxy(typeof(FixedList64BytesDebugView<>))]
  1415. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  1416. public struct FixedList64Bytes<T>
  1417. : INativeList<T>
  1418. , IEnumerable<T> // Used by collection initializers.
  1419. , IEquatable<FixedList32Bytes<T>>
  1420. , IComparable<FixedList32Bytes<T>>
  1421. , IEquatable<FixedList64Bytes<T>>
  1422. , IComparable<FixedList64Bytes<T>>
  1423. , IEquatable<FixedList128Bytes<T>>
  1424. , IComparable<FixedList128Bytes<T>>
  1425. , IEquatable<FixedList512Bytes<T>>
  1426. , IComparable<FixedList512Bytes<T>>
  1427. , IEquatable<FixedList4096Bytes<T>>
  1428. , IComparable<FixedList4096Bytes<T>>
  1429. where T : unmanaged
  1430. {
  1431. [SerializeField] internal ushort length;
  1432. [SerializeField] internal FixedBytes62 buffer;
  1433. /// <summary>
  1434. /// The current number of items in this list.
  1435. /// </summary>
  1436. /// <value>The current number of items in this list.</value>
  1437. [CreateProperty]
  1438. public int Length
  1439. {
  1440. get => length;
  1441. set
  1442. {
  1443. FixedList.CheckResize<FixedBytes62,T>(value);
  1444. length = (ushort)value;
  1445. }
  1446. }
  1447. /// <summary>
  1448. /// A property in order to display items in the Entity Inspector.
  1449. /// </summary>
  1450. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  1451. /// <summary>
  1452. /// Whether this list is empty.
  1453. /// </summary>
  1454. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  1455. public bool IsEmpty => Length == 0;
  1456. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  1457. unsafe internal byte* Buffer
  1458. {
  1459. get
  1460. {
  1461. fixed(byte* b = &buffer.offset0000.byte0000)
  1462. return b + FixedList.PaddingBytes<T>();
  1463. }
  1464. }
  1465. /// <summary>
  1466. /// The number of elements that can fit in this list.
  1467. /// </summary>
  1468. /// <value>The number of elements that can fit in this list.</value>
  1469. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  1470. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  1471. public int Capacity
  1472. {
  1473. get
  1474. {
  1475. return FixedList.Capacity<FixedBytes62,T>();
  1476. }
  1477. set
  1478. {
  1479. CollectionHelper.CheckCapacityInRange(value, Length);
  1480. }
  1481. }
  1482. /// <summary>
  1483. /// The element at a given index.
  1484. /// </summary>
  1485. /// <param name="index">An index.</param>
  1486. /// <value>The value to store at the index.</value>
  1487. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1488. public T this[int index]
  1489. {
  1490. get
  1491. {
  1492. CollectionHelper.CheckIndexInRange(index, length);
  1493. unsafe
  1494. {
  1495. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  1496. }
  1497. }
  1498. set
  1499. {
  1500. CollectionHelper.CheckIndexInRange(index, length);
  1501. unsafe
  1502. {
  1503. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  1504. }
  1505. }
  1506. }
  1507. /// <summary>
  1508. /// Returns the element at a given index.
  1509. /// </summary>
  1510. /// <param name="index">An index.</param>
  1511. /// <returns>The list element at the index.</returns>
  1512. public ref T ElementAt(int index)
  1513. {
  1514. CollectionHelper.CheckIndexInRange(index, length);
  1515. unsafe
  1516. {
  1517. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  1518. }
  1519. }
  1520. /// <summary>
  1521. /// Returns the hash code of this list.
  1522. /// </summary>
  1523. /// <remarks>
  1524. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  1525. /// <returns>The hash code of this list.</returns>
  1526. public override int GetHashCode()
  1527. {
  1528. unsafe
  1529. {
  1530. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  1531. }
  1532. }
  1533. /// <summary>
  1534. /// Appends an element to the end of this list. Increments the length by 1.
  1535. /// </summary>
  1536. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  1537. /// <param name="item">The element to append at the end of the list.</param>
  1538. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  1539. public void Add(in T item)
  1540. {
  1541. this[Length++] = item;
  1542. }
  1543. /// <summary>
  1544. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  1545. /// </summary>
  1546. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  1547. /// <param name="ptr">A buffer.</param>
  1548. /// <param name="length">The number of elements from the buffer to append.</param>
  1549. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  1550. public unsafe void AddRange(void* ptr, int length)
  1551. {
  1552. T* data = (T*)ptr;
  1553. for (var i = 0; i < length; ++i)
  1554. {
  1555. this[Length++] = data[i];
  1556. }
  1557. }
  1558. /// <summary>
  1559. /// Appends an element to the end of this list. Increments the length by 1.
  1560. /// </summary>
  1561. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  1562. /// <param name="item">The element to append at the end of the list.</param>
  1563. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  1564. public void AddNoResize(in T item) => Add(item);
  1565. /// <summary>
  1566. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  1567. /// </summary>
  1568. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  1569. /// <param name="ptr">A buffer.</param>
  1570. /// <param name="length">The number of elements from the buffer to append.</param>
  1571. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  1572. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  1573. /// <summary>
  1574. /// Sets the length to 0.
  1575. /// </summary>
  1576. /// <remarks> Does *not* zero out the bytes.</remarks>
  1577. public void Clear()
  1578. {
  1579. Length = 0;
  1580. }
  1581. /// <summary>
  1582. /// Shifts elements toward the end of this list, increasing its length.
  1583. /// </summary>
  1584. /// <remarks>
  1585. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  1586. ///
  1587. /// The length is increased by `end - begin`.
  1588. ///
  1589. /// If `end` equals `begin`, the method does nothing.
  1590. ///
  1591. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  1592. ///
  1593. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  1594. /// </remarks>
  1595. /// <param name="begin">The index of the first element that will be shifted up.</param>
  1596. /// <param name="end">The index where the first shifted element will end up.</param>
  1597. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  1598. public void InsertRangeWithBeginEnd(int begin, int end)
  1599. {
  1600. int items = end - begin;
  1601. if(items < 1)
  1602. return;
  1603. int itemsToCopy = length - begin;
  1604. Length += items;
  1605. if(itemsToCopy < 1)
  1606. return;
  1607. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  1608. unsafe
  1609. {
  1610. byte *b = Buffer;
  1611. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  1612. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  1613. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  1614. }
  1615. }
  1616. /// <summary>
  1617. /// Inserts a single element at an index. Increments the length by 1.
  1618. /// </summary>
  1619. /// <param name="index">The index at which to insert the element.</param>
  1620. /// <param name="item">The element to insert.</param>
  1621. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1622. public void Insert(int index, in T item)
  1623. {
  1624. InsertRangeWithBeginEnd(index, index+1);
  1625. this[index] = item;
  1626. }
  1627. /// <summary>
  1628. /// Copies the last element of this list to an index. Decrements the length by 1.
  1629. /// </summary>
  1630. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  1631. /// <param name="index">The index to overwrite with the last element.</param>
  1632. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1633. public void RemoveAtSwapBack(int index)
  1634. {
  1635. RemoveRangeSwapBack(index, 1);
  1636. }
  1637. /// <summary>
  1638. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  1639. /// </summary>
  1640. /// <remarks>
  1641. /// Copies the last `count`-numbered elements to the range starting at `index`.
  1642. ///
  1643. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  1644. ///
  1645. /// Does nothing if the count is less than 1.
  1646. /// </remarks>
  1647. /// <param name="index">The first index of the destination range.</param>
  1648. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  1649. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1650. public void RemoveRangeSwapBack(int index, int count)
  1651. {
  1652. if (count > 0)
  1653. {
  1654. int copyFrom = math.max(Length - count, index + count);
  1655. unsafe
  1656. {
  1657. var sizeOf = UnsafeUtility.SizeOf<T>();
  1658. void* dst = Buffer + index * sizeOf;
  1659. void* src = Buffer + copyFrom * sizeOf;
  1660. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  1661. }
  1662. Length -= count;
  1663. }
  1664. }
  1665. /// <summary>
  1666. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  1667. /// </summary>
  1668. /// <param name="begin">The first index of the item to remove.</param>
  1669. /// <param name="end">The index past-the-last item to remove.</param>
  1670. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  1671. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  1672. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  1673. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  1674. /// <summary>
  1675. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  1676. /// </summary>
  1677. /// <param name="index">The index of the element to remove.</param>
  1678. /// <remarks>
  1679. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  1680. /// </remarks>
  1681. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1682. public void RemoveAt(int index)
  1683. {
  1684. RemoveRange(index, 1);
  1685. }
  1686. /// <summary>
  1687. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  1688. /// </summary>
  1689. /// <remarks>
  1690. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  1691. /// </remarks>
  1692. /// <param name="index">The first index of the range to remove.</param>
  1693. /// <param name="count">The number of elements to remove.</param>
  1694. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  1695. public void RemoveRange(int index, int count)
  1696. {
  1697. if (count > 0)
  1698. {
  1699. int copyFrom = math.min(index + count, Length);
  1700. unsafe
  1701. {
  1702. var sizeOf = UnsafeUtility.SizeOf<T>();
  1703. void* dst = Buffer + index * sizeOf;
  1704. void* src = Buffer + copyFrom * sizeOf;
  1705. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  1706. }
  1707. Length -= count;
  1708. }
  1709. }
  1710. /// <summary>
  1711. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  1712. /// </summary>
  1713. /// <param name="begin">The first index of the item to remove.</param>
  1714. /// <param name="end">The index past-the-last item to remove.</param>
  1715. /// <remarks>
  1716. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  1717. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  1718. /// </remarks>
  1719. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  1720. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  1721. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  1722. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  1723. /// <summary>
  1724. /// Returns a managed array that is a copy of this list.
  1725. /// </summary>
  1726. /// <returns>A managed array that is a copy of this list.</returns>
  1727. [NotBurstCompatible]
  1728. public T[] ToArray()
  1729. {
  1730. var result = new T[Length];
  1731. unsafe
  1732. {
  1733. byte* s = Buffer;
  1734. fixed(T* d = result)
  1735. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  1736. }
  1737. return result;
  1738. }
  1739. /// <summary>
  1740. /// Returns an array that is a copy of this list.
  1741. /// </summary>
  1742. /// <param name="allocator">The allocator to use.</param>
  1743. /// <returns>An array that is a copy of this list.</returns>
  1744. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  1745. {
  1746. unsafe
  1747. {
  1748. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  1749. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  1750. return copy;
  1751. }
  1752. }
  1753. /// <summary>
  1754. /// Returns true if two lists are equal.
  1755. /// </summary>
  1756. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1757. /// <param name="a">The first list to compare for equality.</param>
  1758. /// <param name="b">The second list to compare for equality.</param>
  1759. /// <returns>True if the two lists are equal.</returns>
  1760. public static bool operator ==(in FixedList64Bytes<T> a, in FixedList32Bytes<T> b)
  1761. {
  1762. unsafe
  1763. {
  1764. if(a.length != b.length)
  1765. return false;
  1766. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  1767. }
  1768. }
  1769. /// <summary>
  1770. /// Returns true if two lists are unequal.
  1771. /// </summary>
  1772. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1773. /// <param name="a">The first list to compare for inequality.</param>
  1774. /// <param name="b">The second list to compare for inequality.</param>
  1775. /// <returns>True if the two lists are unequal.</returns>
  1776. public static bool operator !=(in FixedList64Bytes<T> a, in FixedList32Bytes<T> b)
  1777. {
  1778. return !(a == b);
  1779. }
  1780. /// <summary>
  1781. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  1782. /// </summary>
  1783. /// <param name="other">A list to to compare with.</param>
  1784. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  1785. ///
  1786. /// 0 denotes that both lists should have the same position in a sort.
  1787. /// -1 denotes that this list should precede the other list in a sort.
  1788. /// +1 denotes that this list should follow the other list in a sort.
  1789. /// </returns>
  1790. public int CompareTo(FixedList32Bytes<T> other)
  1791. {
  1792. unsafe
  1793. {
  1794. fixed(byte* a = &buffer.offset0000.byte0000)
  1795. {
  1796. byte* b = &other.buffer.offset0000.byte0000;
  1797. var aa = a + FixedList.PaddingBytes<T>();
  1798. var bb = b + FixedList.PaddingBytes<T>();
  1799. var mini = math.min(Length, other.Length);
  1800. for(var i = 0; i < mini; ++i)
  1801. {
  1802. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  1803. if(j != 0)
  1804. return j;
  1805. }
  1806. return Length.CompareTo(other.Length);
  1807. }
  1808. }
  1809. }
  1810. /// <summary>
  1811. /// Returns true if this list and another list are equal.
  1812. /// </summary>
  1813. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1814. /// <param name="other">The list to compare for equality.</param>
  1815. /// <returns>True if the two lists are equal.</returns>
  1816. public bool Equals(FixedList32Bytes<T> other)
  1817. {
  1818. return CompareTo(other) == 0;
  1819. }
  1820. /// <summary>
  1821. /// Initializes and returns an instance of FixedList64Bytes with content copied from another list.
  1822. /// </summary>
  1823. /// <param name="other">The list to copy.</param>
  1824. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  1825. public FixedList64Bytes(in FixedList32Bytes<T> other)
  1826. {
  1827. this = default;
  1828. var error = Initialize(other);
  1829. if(error != 0)
  1830. FixedList.CheckResize<FixedBytes62,T>(other.Length);
  1831. }
  1832. /// <summary>
  1833. /// Initializes an instance of FixedList64Bytes with content copied from another list.
  1834. /// </summary>
  1835. /// <param name="other">The list to copy.</param>
  1836. /// <returns>zero on success, or non-zero on error.</returns>
  1837. internal int Initialize(in FixedList32Bytes<T> other)
  1838. {
  1839. if(other.Length > Capacity)
  1840. return (int)CopyError.Truncation;
  1841. length = other.length;
  1842. buffer = new FixedBytes62();
  1843. unsafe
  1844. {
  1845. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  1846. }
  1847. return 0;
  1848. }
  1849. /// <summary>
  1850. /// Returns a new list that is a copy of another list.
  1851. /// </summary>
  1852. /// <param name="other">The list to copy.</param>
  1853. /// <returns>A new list that is a copy of the other.</returns>
  1854. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  1855. public static implicit operator FixedList64Bytes<T>(in FixedList32Bytes<T> other)
  1856. {
  1857. return new FixedList64Bytes<T>(other);
  1858. }
  1859. /// <summary>
  1860. /// Returns true if two lists are equal.
  1861. /// </summary>
  1862. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1863. /// <param name="a">The first list to compare for equality.</param>
  1864. /// <param name="b">The second list to compare for equality.</param>
  1865. /// <returns>True if the two lists are equal.</returns>
  1866. public static bool operator ==(in FixedList64Bytes<T> a, in FixedList64Bytes<T> b)
  1867. {
  1868. unsafe
  1869. {
  1870. if(a.length != b.length)
  1871. return false;
  1872. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  1873. }
  1874. }
  1875. /// <summary>
  1876. /// Returns true if two lists are unequal.
  1877. /// </summary>
  1878. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1879. /// <param name="a">The first list to compare for inequality.</param>
  1880. /// <param name="b">The second list to compare for inequality.</param>
  1881. /// <returns>True if the two lists are unequal.</returns>
  1882. public static bool operator !=(in FixedList64Bytes<T> a, in FixedList64Bytes<T> b)
  1883. {
  1884. return !(a == b);
  1885. }
  1886. /// <summary>
  1887. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  1888. /// </summary>
  1889. /// <param name="other">A list to to compare with.</param>
  1890. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  1891. ///
  1892. /// 0 denotes that both lists should have the same position in a sort.
  1893. /// -1 denotes that this list should precede the other list in a sort.
  1894. /// +1 denotes that this list should follow the other list in a sort.
  1895. /// </returns>
  1896. public int CompareTo(FixedList64Bytes<T> other)
  1897. {
  1898. unsafe
  1899. {
  1900. fixed(byte* a = &buffer.offset0000.byte0000)
  1901. {
  1902. byte* b = &other.buffer.offset0000.byte0000;
  1903. var aa = a + FixedList.PaddingBytes<T>();
  1904. var bb = b + FixedList.PaddingBytes<T>();
  1905. var mini = math.min(Length, other.Length);
  1906. for(var i = 0; i < mini; ++i)
  1907. {
  1908. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  1909. if(j != 0)
  1910. return j;
  1911. }
  1912. return Length.CompareTo(other.Length);
  1913. }
  1914. }
  1915. }
  1916. /// <summary>
  1917. /// Returns true if this list and another list are equal.
  1918. /// </summary>
  1919. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1920. /// <param name="other">The list to compare for equality.</param>
  1921. /// <returns>True if the two lists are equal.</returns>
  1922. public bool Equals(FixedList64Bytes<T> other)
  1923. {
  1924. return CompareTo(other) == 0;
  1925. }
  1926. /// <summary>
  1927. /// Returns true if two lists are equal.
  1928. /// </summary>
  1929. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1930. /// <param name="a">The first list to compare for equality.</param>
  1931. /// <param name="b">The second list to compare for equality.</param>
  1932. /// <returns>True if the two lists are equal.</returns>
  1933. public static bool operator ==(in FixedList64Bytes<T> a, in FixedList128Bytes<T> b)
  1934. {
  1935. unsafe
  1936. {
  1937. if(a.length != b.length)
  1938. return false;
  1939. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  1940. }
  1941. }
  1942. /// <summary>
  1943. /// Returns true if two lists are unequal.
  1944. /// </summary>
  1945. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1946. /// <param name="a">The first list to compare for inequality.</param>
  1947. /// <param name="b">The second list to compare for inequality.</param>
  1948. /// <returns>True if the two lists are unequal.</returns>
  1949. public static bool operator !=(in FixedList64Bytes<T> a, in FixedList128Bytes<T> b)
  1950. {
  1951. return !(a == b);
  1952. }
  1953. /// <summary>
  1954. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  1955. /// </summary>
  1956. /// <param name="other">A list to to compare with.</param>
  1957. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  1958. ///
  1959. /// 0 denotes that both lists should have the same position in a sort.
  1960. /// -1 denotes that this list should precede the other list in a sort.
  1961. /// +1 denotes that this list should follow the other list in a sort.
  1962. /// </returns>
  1963. public int CompareTo(FixedList128Bytes<T> other)
  1964. {
  1965. unsafe
  1966. {
  1967. fixed(byte* a = &buffer.offset0000.byte0000)
  1968. {
  1969. byte* b = &other.buffer.offset0000.byte0000;
  1970. var aa = a + FixedList.PaddingBytes<T>();
  1971. var bb = b + FixedList.PaddingBytes<T>();
  1972. var mini = math.min(Length, other.Length);
  1973. for(var i = 0; i < mini; ++i)
  1974. {
  1975. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  1976. if(j != 0)
  1977. return j;
  1978. }
  1979. return Length.CompareTo(other.Length);
  1980. }
  1981. }
  1982. }
  1983. /// <summary>
  1984. /// Returns true if this list and another list are equal.
  1985. /// </summary>
  1986. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  1987. /// <param name="other">The list to compare for equality.</param>
  1988. /// <returns>True if the two lists are equal.</returns>
  1989. public bool Equals(FixedList128Bytes<T> other)
  1990. {
  1991. return CompareTo(other) == 0;
  1992. }
  1993. /// <summary>
  1994. /// Initializes and returns an instance of FixedList64Bytes with content copied from another list.
  1995. /// </summary>
  1996. /// <param name="other">The list to copy.</param>
  1997. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  1998. public FixedList64Bytes(in FixedList128Bytes<T> other)
  1999. {
  2000. this = default;
  2001. var error = Initialize(other);
  2002. if(error != 0)
  2003. FixedList.CheckResize<FixedBytes62,T>(other.Length);
  2004. }
  2005. /// <summary>
  2006. /// Initializes an instance of FixedList64Bytes with content copied from another list.
  2007. /// </summary>
  2008. /// <param name="other">The list to copy.</param>
  2009. /// <returns>zero on success, or non-zero on error.</returns>
  2010. internal int Initialize(in FixedList128Bytes<T> other)
  2011. {
  2012. if(other.Length > Capacity)
  2013. return (int)CopyError.Truncation;
  2014. length = other.length;
  2015. buffer = new FixedBytes62();
  2016. unsafe
  2017. {
  2018. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  2019. }
  2020. return 0;
  2021. }
  2022. /// <summary>
  2023. /// Returns a new list that is a copy of another list.
  2024. /// </summary>
  2025. /// <param name="other">The list to copy.</param>
  2026. /// <returns>A new list that is a copy of the other.</returns>
  2027. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  2028. public static implicit operator FixedList64Bytes<T>(in FixedList128Bytes<T> other)
  2029. {
  2030. return new FixedList64Bytes<T>(other);
  2031. }
  2032. /// <summary>
  2033. /// Returns true if two lists are equal.
  2034. /// </summary>
  2035. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2036. /// <param name="a">The first list to compare for equality.</param>
  2037. /// <param name="b">The second list to compare for equality.</param>
  2038. /// <returns>True if the two lists are equal.</returns>
  2039. public static bool operator ==(in FixedList64Bytes<T> a, in FixedList512Bytes<T> b)
  2040. {
  2041. unsafe
  2042. {
  2043. if(a.length != b.length)
  2044. return false;
  2045. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  2046. }
  2047. }
  2048. /// <summary>
  2049. /// Returns true if two lists are unequal.
  2050. /// </summary>
  2051. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2052. /// <param name="a">The first list to compare for inequality.</param>
  2053. /// <param name="b">The second list to compare for inequality.</param>
  2054. /// <returns>True if the two lists are unequal.</returns>
  2055. public static bool operator !=(in FixedList64Bytes<T> a, in FixedList512Bytes<T> b)
  2056. {
  2057. return !(a == b);
  2058. }
  2059. /// <summary>
  2060. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  2061. /// </summary>
  2062. /// <param name="other">A list to to compare with.</param>
  2063. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  2064. ///
  2065. /// 0 denotes that both lists should have the same position in a sort.
  2066. /// -1 denotes that this list should precede the other list in a sort.
  2067. /// +1 denotes that this list should follow the other list in a sort.
  2068. /// </returns>
  2069. public int CompareTo(FixedList512Bytes<T> other)
  2070. {
  2071. unsafe
  2072. {
  2073. fixed(byte* a = &buffer.offset0000.byte0000)
  2074. {
  2075. byte* b = &other.buffer.offset0000.byte0000;
  2076. var aa = a + FixedList.PaddingBytes<T>();
  2077. var bb = b + FixedList.PaddingBytes<T>();
  2078. var mini = math.min(Length, other.Length);
  2079. for(var i = 0; i < mini; ++i)
  2080. {
  2081. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  2082. if(j != 0)
  2083. return j;
  2084. }
  2085. return Length.CompareTo(other.Length);
  2086. }
  2087. }
  2088. }
  2089. /// <summary>
  2090. /// Returns true if this list and another list are equal.
  2091. /// </summary>
  2092. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2093. /// <param name="other">The list to compare for equality.</param>
  2094. /// <returns>True if the two lists are equal.</returns>
  2095. public bool Equals(FixedList512Bytes<T> other)
  2096. {
  2097. return CompareTo(other) == 0;
  2098. }
  2099. /// <summary>
  2100. /// Initializes and returns an instance of FixedList64Bytes with content copied from another list.
  2101. /// </summary>
  2102. /// <param name="other">The list to copy.</param>
  2103. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  2104. public FixedList64Bytes(in FixedList512Bytes<T> other)
  2105. {
  2106. this = default;
  2107. var error = Initialize(other);
  2108. if(error != 0)
  2109. FixedList.CheckResize<FixedBytes62,T>(other.Length);
  2110. }
  2111. /// <summary>
  2112. /// Initializes an instance of FixedList64Bytes with content copied from another list.
  2113. /// </summary>
  2114. /// <param name="other">The list to copy.</param>
  2115. /// <returns>zero on success, or non-zero on error.</returns>
  2116. internal int Initialize(in FixedList512Bytes<T> other)
  2117. {
  2118. if(other.Length > Capacity)
  2119. return (int)CopyError.Truncation;
  2120. length = other.length;
  2121. buffer = new FixedBytes62();
  2122. unsafe
  2123. {
  2124. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  2125. }
  2126. return 0;
  2127. }
  2128. /// <summary>
  2129. /// Returns a new list that is a copy of another list.
  2130. /// </summary>
  2131. /// <param name="other">The list to copy.</param>
  2132. /// <returns>A new list that is a copy of the other.</returns>
  2133. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  2134. public static implicit operator FixedList64Bytes<T>(in FixedList512Bytes<T> other)
  2135. {
  2136. return new FixedList64Bytes<T>(other);
  2137. }
  2138. /// <summary>
  2139. /// Returns true if two lists are equal.
  2140. /// </summary>
  2141. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2142. /// <param name="a">The first list to compare for equality.</param>
  2143. /// <param name="b">The second list to compare for equality.</param>
  2144. /// <returns>True if the two lists are equal.</returns>
  2145. public static bool operator ==(in FixedList64Bytes<T> a, in FixedList4096Bytes<T> b)
  2146. {
  2147. unsafe
  2148. {
  2149. if(a.length != b.length)
  2150. return false;
  2151. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  2152. }
  2153. }
  2154. /// <summary>
  2155. /// Returns true if two lists are unequal.
  2156. /// </summary>
  2157. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2158. /// <param name="a">The first list to compare for inequality.</param>
  2159. /// <param name="b">The second list to compare for inequality.</param>
  2160. /// <returns>True if the two lists are unequal.</returns>
  2161. public static bool operator !=(in FixedList64Bytes<T> a, in FixedList4096Bytes<T> b)
  2162. {
  2163. return !(a == b);
  2164. }
  2165. /// <summary>
  2166. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  2167. /// </summary>
  2168. /// <param name="other">A list to to compare with.</param>
  2169. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  2170. ///
  2171. /// 0 denotes that both lists should have the same position in a sort.
  2172. /// -1 denotes that this list should precede the other list in a sort.
  2173. /// +1 denotes that this list should follow the other list in a sort.
  2174. /// </returns>
  2175. public int CompareTo(FixedList4096Bytes<T> other)
  2176. {
  2177. unsafe
  2178. {
  2179. fixed(byte* a = &buffer.offset0000.byte0000)
  2180. {
  2181. byte* b = &other.buffer.offset0000.byte0000;
  2182. var aa = a + FixedList.PaddingBytes<T>();
  2183. var bb = b + FixedList.PaddingBytes<T>();
  2184. var mini = math.min(Length, other.Length);
  2185. for(var i = 0; i < mini; ++i)
  2186. {
  2187. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  2188. if(j != 0)
  2189. return j;
  2190. }
  2191. return Length.CompareTo(other.Length);
  2192. }
  2193. }
  2194. }
  2195. /// <summary>
  2196. /// Returns true if this list and another list are equal.
  2197. /// </summary>
  2198. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2199. /// <param name="other">The list to compare for equality.</param>
  2200. /// <returns>True if the two lists are equal.</returns>
  2201. public bool Equals(FixedList4096Bytes<T> other)
  2202. {
  2203. return CompareTo(other) == 0;
  2204. }
  2205. /// <summary>
  2206. /// Initializes and returns an instance of FixedList64Bytes with content copied from another list.
  2207. /// </summary>
  2208. /// <param name="other">The list to copy.</param>
  2209. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  2210. public FixedList64Bytes(in FixedList4096Bytes<T> other)
  2211. {
  2212. this = default;
  2213. var error = Initialize(other);
  2214. if(error != 0)
  2215. FixedList.CheckResize<FixedBytes62,T>(other.Length);
  2216. }
  2217. /// <summary>
  2218. /// Initializes an instance of FixedList64Bytes with content copied from another list.
  2219. /// </summary>
  2220. /// <param name="other">The list to copy.</param>
  2221. /// <returns>zero on success, or non-zero on error.</returns>
  2222. internal int Initialize(in FixedList4096Bytes<T> other)
  2223. {
  2224. if(other.Length > Capacity)
  2225. return (int)CopyError.Truncation;
  2226. length = other.length;
  2227. buffer = new FixedBytes62();
  2228. unsafe
  2229. {
  2230. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  2231. }
  2232. return 0;
  2233. }
  2234. /// <summary>
  2235. /// Returns a new list that is a copy of another list.
  2236. /// </summary>
  2237. /// <param name="other">The list to copy.</param>
  2238. /// <returns>A new list that is a copy of the other.</returns>
  2239. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList64Bytes&lt;T&gt;.</exception>
  2240. public static implicit operator FixedList64Bytes<T>(in FixedList4096Bytes<T> other)
  2241. {
  2242. return new FixedList64Bytes<T>(other);
  2243. }
  2244. /// <summary>
  2245. /// Returns true if the list is equal to an object.
  2246. /// </summary>
  2247. /// <remarks>Two lists are equal if their length and bytes are equal.
  2248. ///
  2249. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  2250. /// </remarks>
  2251. /// <param name="obj">An object to compare for equality.</param>
  2252. /// <returns>True if the list is equal to the object.</returns>
  2253. [NotBurstCompatible]
  2254. public override bool Equals(object obj)
  2255. {
  2256. if(obj is FixedList32Bytes<T> aFixedList32Bytes) return Equals(aFixedList32Bytes);
  2257. if(obj is FixedList64Bytes<T> aFixedList64Bytes) return Equals(aFixedList64Bytes);
  2258. if(obj is FixedList128Bytes<T> aFixedList128Bytes) return Equals(aFixedList128Bytes);
  2259. if(obj is FixedList512Bytes<T> aFixedList512Bytes) return Equals(aFixedList512Bytes);
  2260. if(obj is FixedList4096Bytes<T> aFixedList4096Bytes) return Equals(aFixedList4096Bytes);
  2261. return false;
  2262. }
  2263. /// <summary>
  2264. /// An enumerator over the elements of a FixedList64Bytes&lt;T&gt;.
  2265. /// </summary>
  2266. /// <remarks>
  2267. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  2268. /// </remarks>
  2269. public struct Enumerator : IEnumerator<T>
  2270. {
  2271. FixedList64Bytes<T> m_List;
  2272. int m_Index;
  2273. /// <summary>
  2274. /// Initializes and returns an instance of FixedList64Bytes&lt;T&gt;.
  2275. /// </summary>
  2276. /// <param name="list">The list for which to create an enumerator.</param>
  2277. public Enumerator(ref FixedList64Bytes<T> list)
  2278. {
  2279. m_List = list;
  2280. m_Index = -1;
  2281. }
  2282. /// <summary>
  2283. /// Does nothing.
  2284. /// </summary>
  2285. public void Dispose()
  2286. {
  2287. }
  2288. /// <summary>
  2289. /// Advances the enumerator to the next element.
  2290. /// </summary>
  2291. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  2292. public bool MoveNext()
  2293. {
  2294. m_Index++;
  2295. return m_Index < m_List.Length;
  2296. }
  2297. /// <summary>
  2298. /// Resets the enumerator to its initial state.
  2299. /// </summary>
  2300. public void Reset()
  2301. {
  2302. m_Index = -1;
  2303. }
  2304. /// <summary>
  2305. /// The current element.
  2306. /// </summary>
  2307. /// <value>The current element.</value>
  2308. public T Current => m_List[m_Index]; // Let FixedList64Bytes<T> indexer check for out of range.
  2309. object IEnumerator.Current => Current;
  2310. }
  2311. /// <summary>
  2312. /// Returns an enumerator for iterating over the elements of this list.
  2313. /// </summary>
  2314. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  2315. public Enumerator GetEnumerator()
  2316. {
  2317. return new Enumerator(ref this);
  2318. }
  2319. /// <summary>
  2320. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  2321. /// </summary>
  2322. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  2323. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  2324. IEnumerator IEnumerable.GetEnumerator()
  2325. {
  2326. throw new NotImplementedException();
  2327. }
  2328. /// <summary>
  2329. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  2330. /// </summary>
  2331. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  2332. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  2333. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  2334. {
  2335. throw new NotImplementedException();
  2336. }
  2337. }
  2338. /// <summary>
  2339. /// Provides extension methods for FixedList64Bytes.
  2340. /// </summary>
  2341. [BurstCompatible]
  2342. public unsafe static class FixedList64BytesExtensions
  2343. {
  2344. /// <summary>
  2345. /// Finds the index of the first occurrence of a particular value in this list.
  2346. /// </summary>
  2347. /// <typeparam name="T">The type of elements in this list.</typeparam>
  2348. /// <typeparam name="U">The value type.</typeparam>
  2349. /// <param name="list">The list to search.</param>
  2350. /// <param name="value">The value to locate.</param>
  2351. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  2352. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  2353. public static int IndexOf<T, U>(this ref FixedList64Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  2354. {
  2355. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  2356. }
  2357. /// <summary>
  2358. /// Returns true if a particular value is present in this list.
  2359. /// </summary>
  2360. /// <typeparam name="T">The type of elements in this list.</typeparam>
  2361. /// <typeparam name="U">The value type.</typeparam>
  2362. /// <param name="list">The list to search.</param>
  2363. /// <param name="value">The value to locate.</param>
  2364. /// <returns>True if the value is present in this list.</returns>
  2365. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  2366. public static bool Contains<T, U>(this ref FixedList64Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  2367. {
  2368. return list.IndexOf(value) != -1;
  2369. }
  2370. /// <summary>
  2371. /// Removes the first occurrence of a particular value in this list.
  2372. /// </summary>
  2373. /// <remarks>
  2374. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  2375. ///
  2376. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.FixedList64BytesExtensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  2377. /// </remarks>
  2378. /// <typeparam name="T">The type of elements in this list.</typeparam>
  2379. /// <typeparam name="U">The value type.</typeparam>
  2380. /// <param name="list">The list to search.</param>
  2381. /// <param name="value">The value to locate and remove.</param>
  2382. /// <returns>True if the value was found and removed.</returns>
  2383. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  2384. public static bool Remove<T, U>(this ref FixedList64Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  2385. {
  2386. int index = list.IndexOf(value);
  2387. if (index < 0)
  2388. {
  2389. return false;
  2390. }
  2391. list.RemoveAt(index);
  2392. return true;
  2393. }
  2394. /// <summary>
  2395. /// Removes the first occurrence of a particular value in this list.
  2396. /// </summary>
  2397. /// <remarks>
  2398. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  2399. ///
  2400. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  2401. /// </remarks>
  2402. /// <typeparam name="T">The type of elements in this list.</typeparam>
  2403. /// <typeparam name="U">The value type.</typeparam>
  2404. /// <param name="list">The list to search.</param>
  2405. /// <param name="value">The value to locate and remove.</param>
  2406. /// <returns>Returns true if the item is removed.</returns>
  2407. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  2408. public static bool RemoveSwapBack<T, U>(this ref FixedList64Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  2409. {
  2410. var index = list.IndexOf(value);
  2411. if (index == -1)
  2412. {
  2413. return false;
  2414. }
  2415. list.RemoveAtSwapBack(index);
  2416. return true;
  2417. }
  2418. }
  2419. sealed class FixedList64BytesDebugView<T> where T : unmanaged
  2420. {
  2421. FixedList64Bytes<T> m_List;
  2422. public FixedList64BytesDebugView(FixedList64Bytes<T> list)
  2423. {
  2424. m_List = list;
  2425. }
  2426. public T[] Items => m_List.ToArray();
  2427. }
  2428. [Obsolete("Renamed to FixedList128Bytes<T> (UnityUpgradable) -> FixedList128Bytes<T>", true)]
  2429. public struct FixedList128<T> where T : unmanaged {}
  2430. /// <summary>
  2431. /// An unmanaged, resizable list whose content is all stored directly in the 128-byte struct. Useful for small lists.
  2432. /// </summary>
  2433. /// <typeparam name="T">The type of the elements.</typeparam>
  2434. [Serializable]
  2435. [DebuggerTypeProxy(typeof(FixedList128BytesDebugView<>))]
  2436. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  2437. public struct FixedList128Bytes<T>
  2438. : INativeList<T>
  2439. , IEnumerable<T> // Used by collection initializers.
  2440. , IEquatable<FixedList32Bytes<T>>
  2441. , IComparable<FixedList32Bytes<T>>
  2442. , IEquatable<FixedList64Bytes<T>>
  2443. , IComparable<FixedList64Bytes<T>>
  2444. , IEquatable<FixedList128Bytes<T>>
  2445. , IComparable<FixedList128Bytes<T>>
  2446. , IEquatable<FixedList512Bytes<T>>
  2447. , IComparable<FixedList512Bytes<T>>
  2448. , IEquatable<FixedList4096Bytes<T>>
  2449. , IComparable<FixedList4096Bytes<T>>
  2450. where T : unmanaged
  2451. {
  2452. [SerializeField] internal ushort length;
  2453. [SerializeField] internal FixedBytes126 buffer;
  2454. /// <summary>
  2455. /// The current number of items in this list.
  2456. /// </summary>
  2457. /// <value>The current number of items in this list.</value>
  2458. [CreateProperty]
  2459. public int Length
  2460. {
  2461. get => length;
  2462. set
  2463. {
  2464. FixedList.CheckResize<FixedBytes126,T>(value);
  2465. length = (ushort)value;
  2466. }
  2467. }
  2468. /// <summary>
  2469. /// A property in order to display items in the Entity Inspector.
  2470. /// </summary>
  2471. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  2472. /// <summary>
  2473. /// Whether this list is empty.
  2474. /// </summary>
  2475. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  2476. public bool IsEmpty => Length == 0;
  2477. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  2478. unsafe internal byte* Buffer
  2479. {
  2480. get
  2481. {
  2482. fixed(byte* b = &buffer.offset0000.byte0000)
  2483. return b + FixedList.PaddingBytes<T>();
  2484. }
  2485. }
  2486. /// <summary>
  2487. /// The number of elements that can fit in this list.
  2488. /// </summary>
  2489. /// <value>The number of elements that can fit in this list.</value>
  2490. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  2491. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  2492. public int Capacity
  2493. {
  2494. get
  2495. {
  2496. return FixedList.Capacity<FixedBytes126,T>();
  2497. }
  2498. set
  2499. {
  2500. CollectionHelper.CheckCapacityInRange(value, Length);
  2501. }
  2502. }
  2503. /// <summary>
  2504. /// The element at a given index.
  2505. /// </summary>
  2506. /// <param name="index">An index.</param>
  2507. /// <value>The value to store at the index.</value>
  2508. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2509. public T this[int index]
  2510. {
  2511. get
  2512. {
  2513. CollectionHelper.CheckIndexInRange(index, length);
  2514. unsafe
  2515. {
  2516. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  2517. }
  2518. }
  2519. set
  2520. {
  2521. CollectionHelper.CheckIndexInRange(index, length);
  2522. unsafe
  2523. {
  2524. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  2525. }
  2526. }
  2527. }
  2528. /// <summary>
  2529. /// Returns the element at a given index.
  2530. /// </summary>
  2531. /// <param name="index">An index.</param>
  2532. /// <returns>The list element at the index.</returns>
  2533. public ref T ElementAt(int index)
  2534. {
  2535. CollectionHelper.CheckIndexInRange(index, length);
  2536. unsafe
  2537. {
  2538. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  2539. }
  2540. }
  2541. /// <summary>
  2542. /// Returns the hash code of this list.
  2543. /// </summary>
  2544. /// <remarks>
  2545. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  2546. /// <returns>The hash code of this list.</returns>
  2547. public override int GetHashCode()
  2548. {
  2549. unsafe
  2550. {
  2551. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  2552. }
  2553. }
  2554. /// <summary>
  2555. /// Appends an element to the end of this list. Increments the length by 1.
  2556. /// </summary>
  2557. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  2558. /// <param name="item">The element to append at the end of the list.</param>
  2559. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  2560. public void Add(in T item)
  2561. {
  2562. this[Length++] = item;
  2563. }
  2564. /// <summary>
  2565. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  2566. /// </summary>
  2567. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  2568. /// <param name="ptr">A buffer.</param>
  2569. /// <param name="length">The number of elements from the buffer to append.</param>
  2570. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  2571. public unsafe void AddRange(void* ptr, int length)
  2572. {
  2573. T* data = (T*)ptr;
  2574. for (var i = 0; i < length; ++i)
  2575. {
  2576. this[Length++] = data[i];
  2577. }
  2578. }
  2579. /// <summary>
  2580. /// Appends an element to the end of this list. Increments the length by 1.
  2581. /// </summary>
  2582. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  2583. /// <param name="item">The element to append at the end of the list.</param>
  2584. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  2585. public void AddNoResize(in T item) => Add(item);
  2586. /// <summary>
  2587. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  2588. /// </summary>
  2589. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  2590. /// <param name="ptr">A buffer.</param>
  2591. /// <param name="length">The number of elements from the buffer to append.</param>
  2592. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  2593. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  2594. /// <summary>
  2595. /// Sets the length to 0.
  2596. /// </summary>
  2597. /// <remarks> Does *not* zero out the bytes.</remarks>
  2598. public void Clear()
  2599. {
  2600. Length = 0;
  2601. }
  2602. /// <summary>
  2603. /// Shifts elements toward the end of this list, increasing its length.
  2604. /// </summary>
  2605. /// <remarks>
  2606. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  2607. ///
  2608. /// The length is increased by `end - begin`.
  2609. ///
  2610. /// If `end` equals `begin`, the method does nothing.
  2611. ///
  2612. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  2613. ///
  2614. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  2615. /// </remarks>
  2616. /// <param name="begin">The index of the first element that will be shifted up.</param>
  2617. /// <param name="end">The index where the first shifted element will end up.</param>
  2618. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  2619. public void InsertRangeWithBeginEnd(int begin, int end)
  2620. {
  2621. int items = end - begin;
  2622. if(items < 1)
  2623. return;
  2624. int itemsToCopy = length - begin;
  2625. Length += items;
  2626. if(itemsToCopy < 1)
  2627. return;
  2628. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  2629. unsafe
  2630. {
  2631. byte *b = Buffer;
  2632. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  2633. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  2634. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  2635. }
  2636. }
  2637. /// <summary>
  2638. /// Inserts a single element at an index. Increments the length by 1.
  2639. /// </summary>
  2640. /// <param name="index">The index at which to insert the element.</param>
  2641. /// <param name="item">The element to insert.</param>
  2642. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2643. public void Insert(int index, in T item)
  2644. {
  2645. InsertRangeWithBeginEnd(index, index+1);
  2646. this[index] = item;
  2647. }
  2648. /// <summary>
  2649. /// Copies the last element of this list to an index. Decrements the length by 1.
  2650. /// </summary>
  2651. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  2652. /// <param name="index">The index to overwrite with the last element.</param>
  2653. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2654. public void RemoveAtSwapBack(int index)
  2655. {
  2656. RemoveRangeSwapBack(index, 1);
  2657. }
  2658. /// <summary>
  2659. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  2660. /// </summary>
  2661. /// <remarks>
  2662. /// Copies the last `count`-numbered elements to the range starting at `index`.
  2663. ///
  2664. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  2665. ///
  2666. /// Does nothing if the count is less than 1.
  2667. /// </remarks>
  2668. /// <param name="index">The first index of the destination range.</param>
  2669. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  2670. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2671. public void RemoveRangeSwapBack(int index, int count)
  2672. {
  2673. if (count > 0)
  2674. {
  2675. int copyFrom = math.max(Length - count, index + count);
  2676. unsafe
  2677. {
  2678. var sizeOf = UnsafeUtility.SizeOf<T>();
  2679. void* dst = Buffer + index * sizeOf;
  2680. void* src = Buffer + copyFrom * sizeOf;
  2681. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  2682. }
  2683. Length -= count;
  2684. }
  2685. }
  2686. /// <summary>
  2687. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  2688. /// </summary>
  2689. /// <param name="begin">The first index of the item to remove.</param>
  2690. /// <param name="end">The index past-the-last item to remove.</param>
  2691. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  2692. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  2693. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  2694. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  2695. /// <summary>
  2696. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  2697. /// </summary>
  2698. /// <param name="index">The index of the element to remove.</param>
  2699. /// <remarks>
  2700. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  2701. /// </remarks>
  2702. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2703. public void RemoveAt(int index)
  2704. {
  2705. RemoveRange(index, 1);
  2706. }
  2707. /// <summary>
  2708. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  2709. /// </summary>
  2710. /// <remarks>
  2711. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  2712. /// </remarks>
  2713. /// <param name="index">The first index of the range to remove.</param>
  2714. /// <param name="count">The number of elements to remove.</param>
  2715. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  2716. public void RemoveRange(int index, int count)
  2717. {
  2718. if (count > 0)
  2719. {
  2720. int copyFrom = math.min(index + count, Length);
  2721. unsafe
  2722. {
  2723. var sizeOf = UnsafeUtility.SizeOf<T>();
  2724. void* dst = Buffer + index * sizeOf;
  2725. void* src = Buffer + copyFrom * sizeOf;
  2726. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  2727. }
  2728. Length -= count;
  2729. }
  2730. }
  2731. /// <summary>
  2732. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  2733. /// </summary>
  2734. /// <param name="begin">The first index of the item to remove.</param>
  2735. /// <param name="end">The index past-the-last item to remove.</param>
  2736. /// <remarks>
  2737. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  2738. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  2739. /// </remarks>
  2740. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  2741. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  2742. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  2743. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  2744. /// <summary>
  2745. /// Returns a managed array that is a copy of this list.
  2746. /// </summary>
  2747. /// <returns>A managed array that is a copy of this list.</returns>
  2748. [NotBurstCompatible]
  2749. public T[] ToArray()
  2750. {
  2751. var result = new T[Length];
  2752. unsafe
  2753. {
  2754. byte* s = Buffer;
  2755. fixed(T* d = result)
  2756. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  2757. }
  2758. return result;
  2759. }
  2760. /// <summary>
  2761. /// Returns an array that is a copy of this list.
  2762. /// </summary>
  2763. /// <param name="allocator">The allocator to use.</param>
  2764. /// <returns>An array that is a copy of this list.</returns>
  2765. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  2766. {
  2767. unsafe
  2768. {
  2769. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  2770. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  2771. return copy;
  2772. }
  2773. }
  2774. /// <summary>
  2775. /// Returns true if two lists are equal.
  2776. /// </summary>
  2777. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2778. /// <param name="a">The first list to compare for equality.</param>
  2779. /// <param name="b">The second list to compare for equality.</param>
  2780. /// <returns>True if the two lists are equal.</returns>
  2781. public static bool operator ==(in FixedList128Bytes<T> a, in FixedList32Bytes<T> b)
  2782. {
  2783. unsafe
  2784. {
  2785. if(a.length != b.length)
  2786. return false;
  2787. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  2788. }
  2789. }
  2790. /// <summary>
  2791. /// Returns true if two lists are unequal.
  2792. /// </summary>
  2793. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2794. /// <param name="a">The first list to compare for inequality.</param>
  2795. /// <param name="b">The second list to compare for inequality.</param>
  2796. /// <returns>True if the two lists are unequal.</returns>
  2797. public static bool operator !=(in FixedList128Bytes<T> a, in FixedList32Bytes<T> b)
  2798. {
  2799. return !(a == b);
  2800. }
  2801. /// <summary>
  2802. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  2803. /// </summary>
  2804. /// <param name="other">A list to to compare with.</param>
  2805. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  2806. ///
  2807. /// 0 denotes that both lists should have the same position in a sort.
  2808. /// -1 denotes that this list should precede the other list in a sort.
  2809. /// +1 denotes that this list should follow the other list in a sort.
  2810. /// </returns>
  2811. public int CompareTo(FixedList32Bytes<T> other)
  2812. {
  2813. unsafe
  2814. {
  2815. fixed(byte* a = &buffer.offset0000.byte0000)
  2816. {
  2817. byte* b = &other.buffer.offset0000.byte0000;
  2818. var aa = a + FixedList.PaddingBytes<T>();
  2819. var bb = b + FixedList.PaddingBytes<T>();
  2820. var mini = math.min(Length, other.Length);
  2821. for(var i = 0; i < mini; ++i)
  2822. {
  2823. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  2824. if(j != 0)
  2825. return j;
  2826. }
  2827. return Length.CompareTo(other.Length);
  2828. }
  2829. }
  2830. }
  2831. /// <summary>
  2832. /// Returns true if this list and another list are equal.
  2833. /// </summary>
  2834. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2835. /// <param name="other">The list to compare for equality.</param>
  2836. /// <returns>True if the two lists are equal.</returns>
  2837. public bool Equals(FixedList32Bytes<T> other)
  2838. {
  2839. return CompareTo(other) == 0;
  2840. }
  2841. /// <summary>
  2842. /// Initializes and returns an instance of FixedList128Bytes with content copied from another list.
  2843. /// </summary>
  2844. /// <param name="other">The list to copy.</param>
  2845. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  2846. public FixedList128Bytes(in FixedList32Bytes<T> other)
  2847. {
  2848. this = default;
  2849. var error = Initialize(other);
  2850. if(error != 0)
  2851. FixedList.CheckResize<FixedBytes126,T>(other.Length);
  2852. }
  2853. /// <summary>
  2854. /// Initializes an instance of FixedList128Bytes with content copied from another list.
  2855. /// </summary>
  2856. /// <param name="other">The list to copy.</param>
  2857. /// <returns>zero on success, or non-zero on error.</returns>
  2858. internal int Initialize(in FixedList32Bytes<T> other)
  2859. {
  2860. if(other.Length > Capacity)
  2861. return (int)CopyError.Truncation;
  2862. length = other.length;
  2863. buffer = new FixedBytes126();
  2864. unsafe
  2865. {
  2866. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  2867. }
  2868. return 0;
  2869. }
  2870. /// <summary>
  2871. /// Returns a new list that is a copy of another list.
  2872. /// </summary>
  2873. /// <param name="other">The list to copy.</param>
  2874. /// <returns>A new list that is a copy of the other.</returns>
  2875. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  2876. public static implicit operator FixedList128Bytes<T>(in FixedList32Bytes<T> other)
  2877. {
  2878. return new FixedList128Bytes<T>(other);
  2879. }
  2880. /// <summary>
  2881. /// Returns true if two lists are equal.
  2882. /// </summary>
  2883. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2884. /// <param name="a">The first list to compare for equality.</param>
  2885. /// <param name="b">The second list to compare for equality.</param>
  2886. /// <returns>True if the two lists are equal.</returns>
  2887. public static bool operator ==(in FixedList128Bytes<T> a, in FixedList64Bytes<T> b)
  2888. {
  2889. unsafe
  2890. {
  2891. if(a.length != b.length)
  2892. return false;
  2893. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  2894. }
  2895. }
  2896. /// <summary>
  2897. /// Returns true if two lists are unequal.
  2898. /// </summary>
  2899. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2900. /// <param name="a">The first list to compare for inequality.</param>
  2901. /// <param name="b">The second list to compare for inequality.</param>
  2902. /// <returns>True if the two lists are unequal.</returns>
  2903. public static bool operator !=(in FixedList128Bytes<T> a, in FixedList64Bytes<T> b)
  2904. {
  2905. return !(a == b);
  2906. }
  2907. /// <summary>
  2908. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  2909. /// </summary>
  2910. /// <param name="other">A list to to compare with.</param>
  2911. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  2912. ///
  2913. /// 0 denotes that both lists should have the same position in a sort.
  2914. /// -1 denotes that this list should precede the other list in a sort.
  2915. /// +1 denotes that this list should follow the other list in a sort.
  2916. /// </returns>
  2917. public int CompareTo(FixedList64Bytes<T> other)
  2918. {
  2919. unsafe
  2920. {
  2921. fixed(byte* a = &buffer.offset0000.byte0000)
  2922. {
  2923. byte* b = &other.buffer.offset0000.byte0000;
  2924. var aa = a + FixedList.PaddingBytes<T>();
  2925. var bb = b + FixedList.PaddingBytes<T>();
  2926. var mini = math.min(Length, other.Length);
  2927. for(var i = 0; i < mini; ++i)
  2928. {
  2929. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  2930. if(j != 0)
  2931. return j;
  2932. }
  2933. return Length.CompareTo(other.Length);
  2934. }
  2935. }
  2936. }
  2937. /// <summary>
  2938. /// Returns true if this list and another list are equal.
  2939. /// </summary>
  2940. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2941. /// <param name="other">The list to compare for equality.</param>
  2942. /// <returns>True if the two lists are equal.</returns>
  2943. public bool Equals(FixedList64Bytes<T> other)
  2944. {
  2945. return CompareTo(other) == 0;
  2946. }
  2947. /// <summary>
  2948. /// Initializes and returns an instance of FixedList128Bytes with content copied from another list.
  2949. /// </summary>
  2950. /// <param name="other">The list to copy.</param>
  2951. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  2952. public FixedList128Bytes(in FixedList64Bytes<T> other)
  2953. {
  2954. this = default;
  2955. var error = Initialize(other);
  2956. if(error != 0)
  2957. FixedList.CheckResize<FixedBytes126,T>(other.Length);
  2958. }
  2959. /// <summary>
  2960. /// Initializes an instance of FixedList128Bytes with content copied from another list.
  2961. /// </summary>
  2962. /// <param name="other">The list to copy.</param>
  2963. /// <returns>zero on success, or non-zero on error.</returns>
  2964. internal int Initialize(in FixedList64Bytes<T> other)
  2965. {
  2966. if(other.Length > Capacity)
  2967. return (int)CopyError.Truncation;
  2968. length = other.length;
  2969. buffer = new FixedBytes126();
  2970. unsafe
  2971. {
  2972. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  2973. }
  2974. return 0;
  2975. }
  2976. /// <summary>
  2977. /// Returns a new list that is a copy of another list.
  2978. /// </summary>
  2979. /// <param name="other">The list to copy.</param>
  2980. /// <returns>A new list that is a copy of the other.</returns>
  2981. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  2982. public static implicit operator FixedList128Bytes<T>(in FixedList64Bytes<T> other)
  2983. {
  2984. return new FixedList128Bytes<T>(other);
  2985. }
  2986. /// <summary>
  2987. /// Returns true if two lists are equal.
  2988. /// </summary>
  2989. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  2990. /// <param name="a">The first list to compare for equality.</param>
  2991. /// <param name="b">The second list to compare for equality.</param>
  2992. /// <returns>True if the two lists are equal.</returns>
  2993. public static bool operator ==(in FixedList128Bytes<T> a, in FixedList128Bytes<T> b)
  2994. {
  2995. unsafe
  2996. {
  2997. if(a.length != b.length)
  2998. return false;
  2999. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  3000. }
  3001. }
  3002. /// <summary>
  3003. /// Returns true if two lists are unequal.
  3004. /// </summary>
  3005. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3006. /// <param name="a">The first list to compare for inequality.</param>
  3007. /// <param name="b">The second list to compare for inequality.</param>
  3008. /// <returns>True if the two lists are unequal.</returns>
  3009. public static bool operator !=(in FixedList128Bytes<T> a, in FixedList128Bytes<T> b)
  3010. {
  3011. return !(a == b);
  3012. }
  3013. /// <summary>
  3014. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  3015. /// </summary>
  3016. /// <param name="other">A list to to compare with.</param>
  3017. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  3018. ///
  3019. /// 0 denotes that both lists should have the same position in a sort.
  3020. /// -1 denotes that this list should precede the other list in a sort.
  3021. /// +1 denotes that this list should follow the other list in a sort.
  3022. /// </returns>
  3023. public int CompareTo(FixedList128Bytes<T> other)
  3024. {
  3025. unsafe
  3026. {
  3027. fixed(byte* a = &buffer.offset0000.byte0000)
  3028. {
  3029. byte* b = &other.buffer.offset0000.byte0000;
  3030. var aa = a + FixedList.PaddingBytes<T>();
  3031. var bb = b + FixedList.PaddingBytes<T>();
  3032. var mini = math.min(Length, other.Length);
  3033. for(var i = 0; i < mini; ++i)
  3034. {
  3035. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  3036. if(j != 0)
  3037. return j;
  3038. }
  3039. return Length.CompareTo(other.Length);
  3040. }
  3041. }
  3042. }
  3043. /// <summary>
  3044. /// Returns true if this list and another list are equal.
  3045. /// </summary>
  3046. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3047. /// <param name="other">The list to compare for equality.</param>
  3048. /// <returns>True if the two lists are equal.</returns>
  3049. public bool Equals(FixedList128Bytes<T> other)
  3050. {
  3051. return CompareTo(other) == 0;
  3052. }
  3053. /// <summary>
  3054. /// Returns true if two lists are equal.
  3055. /// </summary>
  3056. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3057. /// <param name="a">The first list to compare for equality.</param>
  3058. /// <param name="b">The second list to compare for equality.</param>
  3059. /// <returns>True if the two lists are equal.</returns>
  3060. public static bool operator ==(in FixedList128Bytes<T> a, in FixedList512Bytes<T> b)
  3061. {
  3062. unsafe
  3063. {
  3064. if(a.length != b.length)
  3065. return false;
  3066. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  3067. }
  3068. }
  3069. /// <summary>
  3070. /// Returns true if two lists are unequal.
  3071. /// </summary>
  3072. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3073. /// <param name="a">The first list to compare for inequality.</param>
  3074. /// <param name="b">The second list to compare for inequality.</param>
  3075. /// <returns>True if the two lists are unequal.</returns>
  3076. public static bool operator !=(in FixedList128Bytes<T> a, in FixedList512Bytes<T> b)
  3077. {
  3078. return !(a == b);
  3079. }
  3080. /// <summary>
  3081. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  3082. /// </summary>
  3083. /// <param name="other">A list to to compare with.</param>
  3084. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  3085. ///
  3086. /// 0 denotes that both lists should have the same position in a sort.
  3087. /// -1 denotes that this list should precede the other list in a sort.
  3088. /// +1 denotes that this list should follow the other list in a sort.
  3089. /// </returns>
  3090. public int CompareTo(FixedList512Bytes<T> other)
  3091. {
  3092. unsafe
  3093. {
  3094. fixed(byte* a = &buffer.offset0000.byte0000)
  3095. {
  3096. byte* b = &other.buffer.offset0000.byte0000;
  3097. var aa = a + FixedList.PaddingBytes<T>();
  3098. var bb = b + FixedList.PaddingBytes<T>();
  3099. var mini = math.min(Length, other.Length);
  3100. for(var i = 0; i < mini; ++i)
  3101. {
  3102. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  3103. if(j != 0)
  3104. return j;
  3105. }
  3106. return Length.CompareTo(other.Length);
  3107. }
  3108. }
  3109. }
  3110. /// <summary>
  3111. /// Returns true if this list and another list are equal.
  3112. /// </summary>
  3113. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3114. /// <param name="other">The list to compare for equality.</param>
  3115. /// <returns>True if the two lists are equal.</returns>
  3116. public bool Equals(FixedList512Bytes<T> other)
  3117. {
  3118. return CompareTo(other) == 0;
  3119. }
  3120. /// <summary>
  3121. /// Initializes and returns an instance of FixedList128Bytes with content copied from another list.
  3122. /// </summary>
  3123. /// <param name="other">The list to copy.</param>
  3124. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  3125. public FixedList128Bytes(in FixedList512Bytes<T> other)
  3126. {
  3127. this = default;
  3128. var error = Initialize(other);
  3129. if(error != 0)
  3130. FixedList.CheckResize<FixedBytes126,T>(other.Length);
  3131. }
  3132. /// <summary>
  3133. /// Initializes an instance of FixedList128Bytes with content copied from another list.
  3134. /// </summary>
  3135. /// <param name="other">The list to copy.</param>
  3136. /// <returns>zero on success, or non-zero on error.</returns>
  3137. internal int Initialize(in FixedList512Bytes<T> other)
  3138. {
  3139. if(other.Length > Capacity)
  3140. return (int)CopyError.Truncation;
  3141. length = other.length;
  3142. buffer = new FixedBytes126();
  3143. unsafe
  3144. {
  3145. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  3146. }
  3147. return 0;
  3148. }
  3149. /// <summary>
  3150. /// Returns a new list that is a copy of another list.
  3151. /// </summary>
  3152. /// <param name="other">The list to copy.</param>
  3153. /// <returns>A new list that is a copy of the other.</returns>
  3154. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  3155. public static implicit operator FixedList128Bytes<T>(in FixedList512Bytes<T> other)
  3156. {
  3157. return new FixedList128Bytes<T>(other);
  3158. }
  3159. /// <summary>
  3160. /// Returns true if two lists are equal.
  3161. /// </summary>
  3162. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3163. /// <param name="a">The first list to compare for equality.</param>
  3164. /// <param name="b">The second list to compare for equality.</param>
  3165. /// <returns>True if the two lists are equal.</returns>
  3166. public static bool operator ==(in FixedList128Bytes<T> a, in FixedList4096Bytes<T> b)
  3167. {
  3168. unsafe
  3169. {
  3170. if(a.length != b.length)
  3171. return false;
  3172. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  3173. }
  3174. }
  3175. /// <summary>
  3176. /// Returns true if two lists are unequal.
  3177. /// </summary>
  3178. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3179. /// <param name="a">The first list to compare for inequality.</param>
  3180. /// <param name="b">The second list to compare for inequality.</param>
  3181. /// <returns>True if the two lists are unequal.</returns>
  3182. public static bool operator !=(in FixedList128Bytes<T> a, in FixedList4096Bytes<T> b)
  3183. {
  3184. return !(a == b);
  3185. }
  3186. /// <summary>
  3187. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  3188. /// </summary>
  3189. /// <param name="other">A list to to compare with.</param>
  3190. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  3191. ///
  3192. /// 0 denotes that both lists should have the same position in a sort.
  3193. /// -1 denotes that this list should precede the other list in a sort.
  3194. /// +1 denotes that this list should follow the other list in a sort.
  3195. /// </returns>
  3196. public int CompareTo(FixedList4096Bytes<T> other)
  3197. {
  3198. unsafe
  3199. {
  3200. fixed(byte* a = &buffer.offset0000.byte0000)
  3201. {
  3202. byte* b = &other.buffer.offset0000.byte0000;
  3203. var aa = a + FixedList.PaddingBytes<T>();
  3204. var bb = b + FixedList.PaddingBytes<T>();
  3205. var mini = math.min(Length, other.Length);
  3206. for(var i = 0; i < mini; ++i)
  3207. {
  3208. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  3209. if(j != 0)
  3210. return j;
  3211. }
  3212. return Length.CompareTo(other.Length);
  3213. }
  3214. }
  3215. }
  3216. /// <summary>
  3217. /// Returns true if this list and another list are equal.
  3218. /// </summary>
  3219. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3220. /// <param name="other">The list to compare for equality.</param>
  3221. /// <returns>True if the two lists are equal.</returns>
  3222. public bool Equals(FixedList4096Bytes<T> other)
  3223. {
  3224. return CompareTo(other) == 0;
  3225. }
  3226. /// <summary>
  3227. /// Initializes and returns an instance of FixedList128Bytes with content copied from another list.
  3228. /// </summary>
  3229. /// <param name="other">The list to copy.</param>
  3230. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  3231. public FixedList128Bytes(in FixedList4096Bytes<T> other)
  3232. {
  3233. this = default;
  3234. var error = Initialize(other);
  3235. if(error != 0)
  3236. FixedList.CheckResize<FixedBytes126,T>(other.Length);
  3237. }
  3238. /// <summary>
  3239. /// Initializes an instance of FixedList128Bytes with content copied from another list.
  3240. /// </summary>
  3241. /// <param name="other">The list to copy.</param>
  3242. /// <returns>zero on success, or non-zero on error.</returns>
  3243. internal int Initialize(in FixedList4096Bytes<T> other)
  3244. {
  3245. if(other.Length > Capacity)
  3246. return (int)CopyError.Truncation;
  3247. length = other.length;
  3248. buffer = new FixedBytes126();
  3249. unsafe
  3250. {
  3251. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  3252. }
  3253. return 0;
  3254. }
  3255. /// <summary>
  3256. /// Returns a new list that is a copy of another list.
  3257. /// </summary>
  3258. /// <param name="other">The list to copy.</param>
  3259. /// <returns>A new list that is a copy of the other.</returns>
  3260. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList128Bytes&lt;T&gt;.</exception>
  3261. public static implicit operator FixedList128Bytes<T>(in FixedList4096Bytes<T> other)
  3262. {
  3263. return new FixedList128Bytes<T>(other);
  3264. }
  3265. /// <summary>
  3266. /// Returns true if the list is equal to an object.
  3267. /// </summary>
  3268. /// <remarks>Two lists are equal if their length and bytes are equal.
  3269. ///
  3270. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  3271. /// </remarks>
  3272. /// <param name="obj">An object to compare for equality.</param>
  3273. /// <returns>True if the list is equal to the object.</returns>
  3274. [NotBurstCompatible]
  3275. public override bool Equals(object obj)
  3276. {
  3277. if(obj is FixedList32Bytes<T> aFixedList32Bytes) return Equals(aFixedList32Bytes);
  3278. if(obj is FixedList64Bytes<T> aFixedList64Bytes) return Equals(aFixedList64Bytes);
  3279. if(obj is FixedList128Bytes<T> aFixedList128Bytes) return Equals(aFixedList128Bytes);
  3280. if(obj is FixedList512Bytes<T> aFixedList512Bytes) return Equals(aFixedList512Bytes);
  3281. if(obj is FixedList4096Bytes<T> aFixedList4096Bytes) return Equals(aFixedList4096Bytes);
  3282. return false;
  3283. }
  3284. /// <summary>
  3285. /// An enumerator over the elements of a FixedList128Bytes&lt;T&gt;.
  3286. /// </summary>
  3287. /// <remarks>
  3288. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  3289. /// </remarks>
  3290. public struct Enumerator : IEnumerator<T>
  3291. {
  3292. FixedList128Bytes<T> m_List;
  3293. int m_Index;
  3294. /// <summary>
  3295. /// Initializes and returns an instance of FixedList128Bytes&lt;T&gt;.
  3296. /// </summary>
  3297. /// <param name="list">The list for which to create an enumerator.</param>
  3298. public Enumerator(ref FixedList128Bytes<T> list)
  3299. {
  3300. m_List = list;
  3301. m_Index = -1;
  3302. }
  3303. /// <summary>
  3304. /// Does nothing.
  3305. /// </summary>
  3306. public void Dispose()
  3307. {
  3308. }
  3309. /// <summary>
  3310. /// Advances the enumerator to the next element.
  3311. /// </summary>
  3312. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  3313. public bool MoveNext()
  3314. {
  3315. m_Index++;
  3316. return m_Index < m_List.Length;
  3317. }
  3318. /// <summary>
  3319. /// Resets the enumerator to its initial state.
  3320. /// </summary>
  3321. public void Reset()
  3322. {
  3323. m_Index = -1;
  3324. }
  3325. /// <summary>
  3326. /// The current element.
  3327. /// </summary>
  3328. /// <value>The current element.</value>
  3329. public T Current => m_List[m_Index]; // Let FixedList128Bytes<T> indexer check for out of range.
  3330. object IEnumerator.Current => Current;
  3331. }
  3332. /// <summary>
  3333. /// Returns an enumerator for iterating over the elements of this list.
  3334. /// </summary>
  3335. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  3336. public Enumerator GetEnumerator()
  3337. {
  3338. return new Enumerator(ref this);
  3339. }
  3340. /// <summary>
  3341. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  3342. /// </summary>
  3343. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  3344. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  3345. IEnumerator IEnumerable.GetEnumerator()
  3346. {
  3347. throw new NotImplementedException();
  3348. }
  3349. /// <summary>
  3350. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  3351. /// </summary>
  3352. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  3353. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  3354. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  3355. {
  3356. throw new NotImplementedException();
  3357. }
  3358. }
  3359. /// <summary>
  3360. /// Provides extension methods for FixedList128Bytes.
  3361. /// </summary>
  3362. [BurstCompatible]
  3363. public unsafe static class FixedList128BytesExtensions
  3364. {
  3365. /// <summary>
  3366. /// Finds the index of the first occurrence of a particular value in this list.
  3367. /// </summary>
  3368. /// <typeparam name="T">The type of elements in this list.</typeparam>
  3369. /// <typeparam name="U">The value type.</typeparam>
  3370. /// <param name="list">The list to search.</param>
  3371. /// <param name="value">The value to locate.</param>
  3372. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  3373. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  3374. public static int IndexOf<T, U>(this ref FixedList128Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  3375. {
  3376. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  3377. }
  3378. /// <summary>
  3379. /// Returns true if a particular value is present in this list.
  3380. /// </summary>
  3381. /// <typeparam name="T">The type of elements in this list.</typeparam>
  3382. /// <typeparam name="U">The value type.</typeparam>
  3383. /// <param name="list">The list to search.</param>
  3384. /// <param name="value">The value to locate.</param>
  3385. /// <returns>True if the value is present in this list.</returns>
  3386. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  3387. public static bool Contains<T, U>(this ref FixedList128Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  3388. {
  3389. return list.IndexOf(value) != -1;
  3390. }
  3391. /// <summary>
  3392. /// Removes the first occurrence of a particular value in this list.
  3393. /// </summary>
  3394. /// <remarks>
  3395. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  3396. ///
  3397. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.FixedList128BytesExtensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  3398. /// </remarks>
  3399. /// <typeparam name="T">The type of elements in this list.</typeparam>
  3400. /// <typeparam name="U">The value type.</typeparam>
  3401. /// <param name="list">The list to search.</param>
  3402. /// <param name="value">The value to locate and remove.</param>
  3403. /// <returns>True if the value was found and removed.</returns>
  3404. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  3405. public static bool Remove<T, U>(this ref FixedList128Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  3406. {
  3407. int index = list.IndexOf(value);
  3408. if (index < 0)
  3409. {
  3410. return false;
  3411. }
  3412. list.RemoveAt(index);
  3413. return true;
  3414. }
  3415. /// <summary>
  3416. /// Removes the first occurrence of a particular value in this list.
  3417. /// </summary>
  3418. /// <remarks>
  3419. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  3420. ///
  3421. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  3422. /// </remarks>
  3423. /// <typeparam name="T">The type of elements in this list.</typeparam>
  3424. /// <typeparam name="U">The value type.</typeparam>
  3425. /// <param name="list">The list to search.</param>
  3426. /// <param name="value">The value to locate and remove.</param>
  3427. /// <returns>Returns true if the item is removed.</returns>
  3428. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  3429. public static bool RemoveSwapBack<T, U>(this ref FixedList128Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  3430. {
  3431. var index = list.IndexOf(value);
  3432. if (index == -1)
  3433. {
  3434. return false;
  3435. }
  3436. list.RemoveAtSwapBack(index);
  3437. return true;
  3438. }
  3439. }
  3440. sealed class FixedList128BytesDebugView<T> where T : unmanaged
  3441. {
  3442. FixedList128Bytes<T> m_List;
  3443. public FixedList128BytesDebugView(FixedList128Bytes<T> list)
  3444. {
  3445. m_List = list;
  3446. }
  3447. public T[] Items => m_List.ToArray();
  3448. }
  3449. [Obsolete("Renamed to FixedList512Bytes<T> (UnityUpgradable) -> FixedList512Bytes<T>", true)]
  3450. public struct FixedList512<T> where T : unmanaged {}
  3451. /// <summary>
  3452. /// An unmanaged, resizable list whose content is all stored directly in the 512-byte struct. Useful for small lists.
  3453. /// </summary>
  3454. /// <typeparam name="T">The type of the elements.</typeparam>
  3455. [Serializable]
  3456. [DebuggerTypeProxy(typeof(FixedList512BytesDebugView<>))]
  3457. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  3458. public struct FixedList512Bytes<T>
  3459. : INativeList<T>
  3460. , IEnumerable<T> // Used by collection initializers.
  3461. , IEquatable<FixedList32Bytes<T>>
  3462. , IComparable<FixedList32Bytes<T>>
  3463. , IEquatable<FixedList64Bytes<T>>
  3464. , IComparable<FixedList64Bytes<T>>
  3465. , IEquatable<FixedList128Bytes<T>>
  3466. , IComparable<FixedList128Bytes<T>>
  3467. , IEquatable<FixedList512Bytes<T>>
  3468. , IComparable<FixedList512Bytes<T>>
  3469. , IEquatable<FixedList4096Bytes<T>>
  3470. , IComparable<FixedList4096Bytes<T>>
  3471. where T : unmanaged
  3472. {
  3473. [SerializeField] internal ushort length;
  3474. [SerializeField] internal FixedBytes510 buffer;
  3475. /// <summary>
  3476. /// The current number of items in this list.
  3477. /// </summary>
  3478. /// <value>The current number of items in this list.</value>
  3479. [CreateProperty]
  3480. public int Length
  3481. {
  3482. get => length;
  3483. set
  3484. {
  3485. FixedList.CheckResize<FixedBytes510,T>(value);
  3486. length = (ushort)value;
  3487. }
  3488. }
  3489. /// <summary>
  3490. /// A property in order to display items in the Entity Inspector.
  3491. /// </summary>
  3492. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  3493. /// <summary>
  3494. /// Whether this list is empty.
  3495. /// </summary>
  3496. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  3497. public bool IsEmpty => Length == 0;
  3498. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  3499. unsafe internal byte* Buffer
  3500. {
  3501. get
  3502. {
  3503. fixed(byte* b = &buffer.offset0000.byte0000)
  3504. return b + FixedList.PaddingBytes<T>();
  3505. }
  3506. }
  3507. /// <summary>
  3508. /// The number of elements that can fit in this list.
  3509. /// </summary>
  3510. /// <value>The number of elements that can fit in this list.</value>
  3511. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  3512. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  3513. public int Capacity
  3514. {
  3515. get
  3516. {
  3517. return FixedList.Capacity<FixedBytes510,T>();
  3518. }
  3519. set
  3520. {
  3521. CollectionHelper.CheckCapacityInRange(value, Length);
  3522. }
  3523. }
  3524. /// <summary>
  3525. /// The element at a given index.
  3526. /// </summary>
  3527. /// <param name="index">An index.</param>
  3528. /// <value>The value to store at the index.</value>
  3529. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3530. public T this[int index]
  3531. {
  3532. get
  3533. {
  3534. CollectionHelper.CheckIndexInRange(index, length);
  3535. unsafe
  3536. {
  3537. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  3538. }
  3539. }
  3540. set
  3541. {
  3542. CollectionHelper.CheckIndexInRange(index, length);
  3543. unsafe
  3544. {
  3545. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  3546. }
  3547. }
  3548. }
  3549. /// <summary>
  3550. /// Returns the element at a given index.
  3551. /// </summary>
  3552. /// <param name="index">An index.</param>
  3553. /// <returns>The list element at the index.</returns>
  3554. public ref T ElementAt(int index)
  3555. {
  3556. CollectionHelper.CheckIndexInRange(index, length);
  3557. unsafe
  3558. {
  3559. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  3560. }
  3561. }
  3562. /// <summary>
  3563. /// Returns the hash code of this list.
  3564. /// </summary>
  3565. /// <remarks>
  3566. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  3567. /// <returns>The hash code of this list.</returns>
  3568. public override int GetHashCode()
  3569. {
  3570. unsafe
  3571. {
  3572. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  3573. }
  3574. }
  3575. /// <summary>
  3576. /// Appends an element to the end of this list. Increments the length by 1.
  3577. /// </summary>
  3578. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  3579. /// <param name="item">The element to append at the end of the list.</param>
  3580. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  3581. public void Add(in T item)
  3582. {
  3583. this[Length++] = item;
  3584. }
  3585. /// <summary>
  3586. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  3587. /// </summary>
  3588. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  3589. /// <param name="ptr">A buffer.</param>
  3590. /// <param name="length">The number of elements from the buffer to append.</param>
  3591. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  3592. public unsafe void AddRange(void* ptr, int length)
  3593. {
  3594. T* data = (T*)ptr;
  3595. for (var i = 0; i < length; ++i)
  3596. {
  3597. this[Length++] = data[i];
  3598. }
  3599. }
  3600. /// <summary>
  3601. /// Appends an element to the end of this list. Increments the length by 1.
  3602. /// </summary>
  3603. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  3604. /// <param name="item">The element to append at the end of the list.</param>
  3605. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  3606. public void AddNoResize(in T item) => Add(item);
  3607. /// <summary>
  3608. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  3609. /// </summary>
  3610. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  3611. /// <param name="ptr">A buffer.</param>
  3612. /// <param name="length">The number of elements from the buffer to append.</param>
  3613. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  3614. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  3615. /// <summary>
  3616. /// Sets the length to 0.
  3617. /// </summary>
  3618. /// <remarks> Does *not* zero out the bytes.</remarks>
  3619. public void Clear()
  3620. {
  3621. Length = 0;
  3622. }
  3623. /// <summary>
  3624. /// Shifts elements toward the end of this list, increasing its length.
  3625. /// </summary>
  3626. /// <remarks>
  3627. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  3628. ///
  3629. /// The length is increased by `end - begin`.
  3630. ///
  3631. /// If `end` equals `begin`, the method does nothing.
  3632. ///
  3633. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  3634. ///
  3635. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  3636. /// </remarks>
  3637. /// <param name="begin">The index of the first element that will be shifted up.</param>
  3638. /// <param name="end">The index where the first shifted element will end up.</param>
  3639. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  3640. public void InsertRangeWithBeginEnd(int begin, int end)
  3641. {
  3642. int items = end - begin;
  3643. if(items < 1)
  3644. return;
  3645. int itemsToCopy = length - begin;
  3646. Length += items;
  3647. if(itemsToCopy < 1)
  3648. return;
  3649. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  3650. unsafe
  3651. {
  3652. byte *b = Buffer;
  3653. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  3654. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  3655. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  3656. }
  3657. }
  3658. /// <summary>
  3659. /// Inserts a single element at an index. Increments the length by 1.
  3660. /// </summary>
  3661. /// <param name="index">The index at which to insert the element.</param>
  3662. /// <param name="item">The element to insert.</param>
  3663. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3664. public void Insert(int index, in T item)
  3665. {
  3666. InsertRangeWithBeginEnd(index, index+1);
  3667. this[index] = item;
  3668. }
  3669. /// <summary>
  3670. /// Copies the last element of this list to an index. Decrements the length by 1.
  3671. /// </summary>
  3672. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  3673. /// <param name="index">The index to overwrite with the last element.</param>
  3674. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3675. public void RemoveAtSwapBack(int index)
  3676. {
  3677. RemoveRangeSwapBack(index, 1);
  3678. }
  3679. /// <summary>
  3680. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  3681. /// </summary>
  3682. /// <remarks>
  3683. /// Copies the last `count`-numbered elements to the range starting at `index`.
  3684. ///
  3685. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  3686. ///
  3687. /// Does nothing if the count is less than 1.
  3688. /// </remarks>
  3689. /// <param name="index">The first index of the destination range.</param>
  3690. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  3691. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3692. public void RemoveRangeSwapBack(int index, int count)
  3693. {
  3694. if (count > 0)
  3695. {
  3696. int copyFrom = math.max(Length - count, index + count);
  3697. unsafe
  3698. {
  3699. var sizeOf = UnsafeUtility.SizeOf<T>();
  3700. void* dst = Buffer + index * sizeOf;
  3701. void* src = Buffer + copyFrom * sizeOf;
  3702. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  3703. }
  3704. Length -= count;
  3705. }
  3706. }
  3707. /// <summary>
  3708. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  3709. /// </summary>
  3710. /// <param name="begin">The first index of the item to remove.</param>
  3711. /// <param name="end">The index past-the-last item to remove.</param>
  3712. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  3713. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  3714. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  3715. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  3716. /// <summary>
  3717. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  3718. /// </summary>
  3719. /// <param name="index">The index of the element to remove.</param>
  3720. /// <remarks>
  3721. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  3722. /// </remarks>
  3723. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3724. public void RemoveAt(int index)
  3725. {
  3726. RemoveRange(index, 1);
  3727. }
  3728. /// <summary>
  3729. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  3730. /// </summary>
  3731. /// <remarks>
  3732. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  3733. /// </remarks>
  3734. /// <param name="index">The first index of the range to remove.</param>
  3735. /// <param name="count">The number of elements to remove.</param>
  3736. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  3737. public void RemoveRange(int index, int count)
  3738. {
  3739. if (count > 0)
  3740. {
  3741. int copyFrom = math.min(index + count, Length);
  3742. unsafe
  3743. {
  3744. var sizeOf = UnsafeUtility.SizeOf<T>();
  3745. void* dst = Buffer + index * sizeOf;
  3746. void* src = Buffer + copyFrom * sizeOf;
  3747. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  3748. }
  3749. Length -= count;
  3750. }
  3751. }
  3752. /// <summary>
  3753. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  3754. /// </summary>
  3755. /// <param name="begin">The first index of the item to remove.</param>
  3756. /// <param name="end">The index past-the-last item to remove.</param>
  3757. /// <remarks>
  3758. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  3759. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  3760. /// </remarks>
  3761. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  3762. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  3763. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  3764. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  3765. /// <summary>
  3766. /// Returns a managed array that is a copy of this list.
  3767. /// </summary>
  3768. /// <returns>A managed array that is a copy of this list.</returns>
  3769. [NotBurstCompatible]
  3770. public T[] ToArray()
  3771. {
  3772. var result = new T[Length];
  3773. unsafe
  3774. {
  3775. byte* s = Buffer;
  3776. fixed(T* d = result)
  3777. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  3778. }
  3779. return result;
  3780. }
  3781. /// <summary>
  3782. /// Returns an array that is a copy of this list.
  3783. /// </summary>
  3784. /// <param name="allocator">The allocator to use.</param>
  3785. /// <returns>An array that is a copy of this list.</returns>
  3786. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  3787. {
  3788. unsafe
  3789. {
  3790. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  3791. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  3792. return copy;
  3793. }
  3794. }
  3795. /// <summary>
  3796. /// Returns true if two lists are equal.
  3797. /// </summary>
  3798. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3799. /// <param name="a">The first list to compare for equality.</param>
  3800. /// <param name="b">The second list to compare for equality.</param>
  3801. /// <returns>True if the two lists are equal.</returns>
  3802. public static bool operator ==(in FixedList512Bytes<T> a, in FixedList32Bytes<T> b)
  3803. {
  3804. unsafe
  3805. {
  3806. if(a.length != b.length)
  3807. return false;
  3808. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  3809. }
  3810. }
  3811. /// <summary>
  3812. /// Returns true if two lists are unequal.
  3813. /// </summary>
  3814. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3815. /// <param name="a">The first list to compare for inequality.</param>
  3816. /// <param name="b">The second list to compare for inequality.</param>
  3817. /// <returns>True if the two lists are unequal.</returns>
  3818. public static bool operator !=(in FixedList512Bytes<T> a, in FixedList32Bytes<T> b)
  3819. {
  3820. return !(a == b);
  3821. }
  3822. /// <summary>
  3823. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  3824. /// </summary>
  3825. /// <param name="other">A list to to compare with.</param>
  3826. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  3827. ///
  3828. /// 0 denotes that both lists should have the same position in a sort.
  3829. /// -1 denotes that this list should precede the other list in a sort.
  3830. /// +1 denotes that this list should follow the other list in a sort.
  3831. /// </returns>
  3832. public int CompareTo(FixedList32Bytes<T> other)
  3833. {
  3834. unsafe
  3835. {
  3836. fixed(byte* a = &buffer.offset0000.byte0000)
  3837. {
  3838. byte* b = &other.buffer.offset0000.byte0000;
  3839. var aa = a + FixedList.PaddingBytes<T>();
  3840. var bb = b + FixedList.PaddingBytes<T>();
  3841. var mini = math.min(Length, other.Length);
  3842. for(var i = 0; i < mini; ++i)
  3843. {
  3844. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  3845. if(j != 0)
  3846. return j;
  3847. }
  3848. return Length.CompareTo(other.Length);
  3849. }
  3850. }
  3851. }
  3852. /// <summary>
  3853. /// Returns true if this list and another list are equal.
  3854. /// </summary>
  3855. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3856. /// <param name="other">The list to compare for equality.</param>
  3857. /// <returns>True if the two lists are equal.</returns>
  3858. public bool Equals(FixedList32Bytes<T> other)
  3859. {
  3860. return CompareTo(other) == 0;
  3861. }
  3862. /// <summary>
  3863. /// Initializes and returns an instance of FixedList512Bytes with content copied from another list.
  3864. /// </summary>
  3865. /// <param name="other">The list to copy.</param>
  3866. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  3867. public FixedList512Bytes(in FixedList32Bytes<T> other)
  3868. {
  3869. this = default;
  3870. var error = Initialize(other);
  3871. if(error != 0)
  3872. FixedList.CheckResize<FixedBytes510,T>(other.Length);
  3873. }
  3874. /// <summary>
  3875. /// Initializes an instance of FixedList512Bytes with content copied from another list.
  3876. /// </summary>
  3877. /// <param name="other">The list to copy.</param>
  3878. /// <returns>zero on success, or non-zero on error.</returns>
  3879. internal int Initialize(in FixedList32Bytes<T> other)
  3880. {
  3881. if(other.Length > Capacity)
  3882. return (int)CopyError.Truncation;
  3883. length = other.length;
  3884. buffer = new FixedBytes510();
  3885. unsafe
  3886. {
  3887. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  3888. }
  3889. return 0;
  3890. }
  3891. /// <summary>
  3892. /// Returns a new list that is a copy of another list.
  3893. /// </summary>
  3894. /// <param name="other">The list to copy.</param>
  3895. /// <returns>A new list that is a copy of the other.</returns>
  3896. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  3897. public static implicit operator FixedList512Bytes<T>(in FixedList32Bytes<T> other)
  3898. {
  3899. return new FixedList512Bytes<T>(other);
  3900. }
  3901. /// <summary>
  3902. /// Returns true if two lists are equal.
  3903. /// </summary>
  3904. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3905. /// <param name="a">The first list to compare for equality.</param>
  3906. /// <param name="b">The second list to compare for equality.</param>
  3907. /// <returns>True if the two lists are equal.</returns>
  3908. public static bool operator ==(in FixedList512Bytes<T> a, in FixedList64Bytes<T> b)
  3909. {
  3910. unsafe
  3911. {
  3912. if(a.length != b.length)
  3913. return false;
  3914. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  3915. }
  3916. }
  3917. /// <summary>
  3918. /// Returns true if two lists are unequal.
  3919. /// </summary>
  3920. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3921. /// <param name="a">The first list to compare for inequality.</param>
  3922. /// <param name="b">The second list to compare for inequality.</param>
  3923. /// <returns>True if the two lists are unequal.</returns>
  3924. public static bool operator !=(in FixedList512Bytes<T> a, in FixedList64Bytes<T> b)
  3925. {
  3926. return !(a == b);
  3927. }
  3928. /// <summary>
  3929. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  3930. /// </summary>
  3931. /// <param name="other">A list to to compare with.</param>
  3932. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  3933. ///
  3934. /// 0 denotes that both lists should have the same position in a sort.
  3935. /// -1 denotes that this list should precede the other list in a sort.
  3936. /// +1 denotes that this list should follow the other list in a sort.
  3937. /// </returns>
  3938. public int CompareTo(FixedList64Bytes<T> other)
  3939. {
  3940. unsafe
  3941. {
  3942. fixed(byte* a = &buffer.offset0000.byte0000)
  3943. {
  3944. byte* b = &other.buffer.offset0000.byte0000;
  3945. var aa = a + FixedList.PaddingBytes<T>();
  3946. var bb = b + FixedList.PaddingBytes<T>();
  3947. var mini = math.min(Length, other.Length);
  3948. for(var i = 0; i < mini; ++i)
  3949. {
  3950. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  3951. if(j != 0)
  3952. return j;
  3953. }
  3954. return Length.CompareTo(other.Length);
  3955. }
  3956. }
  3957. }
  3958. /// <summary>
  3959. /// Returns true if this list and another list are equal.
  3960. /// </summary>
  3961. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  3962. /// <param name="other">The list to compare for equality.</param>
  3963. /// <returns>True if the two lists are equal.</returns>
  3964. public bool Equals(FixedList64Bytes<T> other)
  3965. {
  3966. return CompareTo(other) == 0;
  3967. }
  3968. /// <summary>
  3969. /// Initializes and returns an instance of FixedList512Bytes with content copied from another list.
  3970. /// </summary>
  3971. /// <param name="other">The list to copy.</param>
  3972. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  3973. public FixedList512Bytes(in FixedList64Bytes<T> other)
  3974. {
  3975. this = default;
  3976. var error = Initialize(other);
  3977. if(error != 0)
  3978. FixedList.CheckResize<FixedBytes510,T>(other.Length);
  3979. }
  3980. /// <summary>
  3981. /// Initializes an instance of FixedList512Bytes with content copied from another list.
  3982. /// </summary>
  3983. /// <param name="other">The list to copy.</param>
  3984. /// <returns>zero on success, or non-zero on error.</returns>
  3985. internal int Initialize(in FixedList64Bytes<T> other)
  3986. {
  3987. if(other.Length > Capacity)
  3988. return (int)CopyError.Truncation;
  3989. length = other.length;
  3990. buffer = new FixedBytes510();
  3991. unsafe
  3992. {
  3993. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  3994. }
  3995. return 0;
  3996. }
  3997. /// <summary>
  3998. /// Returns a new list that is a copy of another list.
  3999. /// </summary>
  4000. /// <param name="other">The list to copy.</param>
  4001. /// <returns>A new list that is a copy of the other.</returns>
  4002. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  4003. public static implicit operator FixedList512Bytes<T>(in FixedList64Bytes<T> other)
  4004. {
  4005. return new FixedList512Bytes<T>(other);
  4006. }
  4007. /// <summary>
  4008. /// Returns true if two lists are equal.
  4009. /// </summary>
  4010. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4011. /// <param name="a">The first list to compare for equality.</param>
  4012. /// <param name="b">The second list to compare for equality.</param>
  4013. /// <returns>True if the two lists are equal.</returns>
  4014. public static bool operator ==(in FixedList512Bytes<T> a, in FixedList128Bytes<T> b)
  4015. {
  4016. unsafe
  4017. {
  4018. if(a.length != b.length)
  4019. return false;
  4020. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  4021. }
  4022. }
  4023. /// <summary>
  4024. /// Returns true if two lists are unequal.
  4025. /// </summary>
  4026. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4027. /// <param name="a">The first list to compare for inequality.</param>
  4028. /// <param name="b">The second list to compare for inequality.</param>
  4029. /// <returns>True if the two lists are unequal.</returns>
  4030. public static bool operator !=(in FixedList512Bytes<T> a, in FixedList128Bytes<T> b)
  4031. {
  4032. return !(a == b);
  4033. }
  4034. /// <summary>
  4035. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  4036. /// </summary>
  4037. /// <param name="other">A list to to compare with.</param>
  4038. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  4039. ///
  4040. /// 0 denotes that both lists should have the same position in a sort.
  4041. /// -1 denotes that this list should precede the other list in a sort.
  4042. /// +1 denotes that this list should follow the other list in a sort.
  4043. /// </returns>
  4044. public int CompareTo(FixedList128Bytes<T> other)
  4045. {
  4046. unsafe
  4047. {
  4048. fixed(byte* a = &buffer.offset0000.byte0000)
  4049. {
  4050. byte* b = &other.buffer.offset0000.byte0000;
  4051. var aa = a + FixedList.PaddingBytes<T>();
  4052. var bb = b + FixedList.PaddingBytes<T>();
  4053. var mini = math.min(Length, other.Length);
  4054. for(var i = 0; i < mini; ++i)
  4055. {
  4056. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  4057. if(j != 0)
  4058. return j;
  4059. }
  4060. return Length.CompareTo(other.Length);
  4061. }
  4062. }
  4063. }
  4064. /// <summary>
  4065. /// Returns true if this list and another list are equal.
  4066. /// </summary>
  4067. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4068. /// <param name="other">The list to compare for equality.</param>
  4069. /// <returns>True if the two lists are equal.</returns>
  4070. public bool Equals(FixedList128Bytes<T> other)
  4071. {
  4072. return CompareTo(other) == 0;
  4073. }
  4074. /// <summary>
  4075. /// Initializes and returns an instance of FixedList512Bytes with content copied from another list.
  4076. /// </summary>
  4077. /// <param name="other">The list to copy.</param>
  4078. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  4079. public FixedList512Bytes(in FixedList128Bytes<T> other)
  4080. {
  4081. this = default;
  4082. var error = Initialize(other);
  4083. if(error != 0)
  4084. FixedList.CheckResize<FixedBytes510,T>(other.Length);
  4085. }
  4086. /// <summary>
  4087. /// Initializes an instance of FixedList512Bytes with content copied from another list.
  4088. /// </summary>
  4089. /// <param name="other">The list to copy.</param>
  4090. /// <returns>zero on success, or non-zero on error.</returns>
  4091. internal int Initialize(in FixedList128Bytes<T> other)
  4092. {
  4093. if(other.Length > Capacity)
  4094. return (int)CopyError.Truncation;
  4095. length = other.length;
  4096. buffer = new FixedBytes510();
  4097. unsafe
  4098. {
  4099. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  4100. }
  4101. return 0;
  4102. }
  4103. /// <summary>
  4104. /// Returns a new list that is a copy of another list.
  4105. /// </summary>
  4106. /// <param name="other">The list to copy.</param>
  4107. /// <returns>A new list that is a copy of the other.</returns>
  4108. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  4109. public static implicit operator FixedList512Bytes<T>(in FixedList128Bytes<T> other)
  4110. {
  4111. return new FixedList512Bytes<T>(other);
  4112. }
  4113. /// <summary>
  4114. /// Returns true if two lists are equal.
  4115. /// </summary>
  4116. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4117. /// <param name="a">The first list to compare for equality.</param>
  4118. /// <param name="b">The second list to compare for equality.</param>
  4119. /// <returns>True if the two lists are equal.</returns>
  4120. public static bool operator ==(in FixedList512Bytes<T> a, in FixedList512Bytes<T> b)
  4121. {
  4122. unsafe
  4123. {
  4124. if(a.length != b.length)
  4125. return false;
  4126. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  4127. }
  4128. }
  4129. /// <summary>
  4130. /// Returns true if two lists are unequal.
  4131. /// </summary>
  4132. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4133. /// <param name="a">The first list to compare for inequality.</param>
  4134. /// <param name="b">The second list to compare for inequality.</param>
  4135. /// <returns>True if the two lists are unequal.</returns>
  4136. public static bool operator !=(in FixedList512Bytes<T> a, in FixedList512Bytes<T> b)
  4137. {
  4138. return !(a == b);
  4139. }
  4140. /// <summary>
  4141. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  4142. /// </summary>
  4143. /// <param name="other">A list to to compare with.</param>
  4144. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  4145. ///
  4146. /// 0 denotes that both lists should have the same position in a sort.
  4147. /// -1 denotes that this list should precede the other list in a sort.
  4148. /// +1 denotes that this list should follow the other list in a sort.
  4149. /// </returns>
  4150. public int CompareTo(FixedList512Bytes<T> other)
  4151. {
  4152. unsafe
  4153. {
  4154. fixed(byte* a = &buffer.offset0000.byte0000)
  4155. {
  4156. byte* b = &other.buffer.offset0000.byte0000;
  4157. var aa = a + FixedList.PaddingBytes<T>();
  4158. var bb = b + FixedList.PaddingBytes<T>();
  4159. var mini = math.min(Length, other.Length);
  4160. for(var i = 0; i < mini; ++i)
  4161. {
  4162. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  4163. if(j != 0)
  4164. return j;
  4165. }
  4166. return Length.CompareTo(other.Length);
  4167. }
  4168. }
  4169. }
  4170. /// <summary>
  4171. /// Returns true if this list and another list are equal.
  4172. /// </summary>
  4173. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4174. /// <param name="other">The list to compare for equality.</param>
  4175. /// <returns>True if the two lists are equal.</returns>
  4176. public bool Equals(FixedList512Bytes<T> other)
  4177. {
  4178. return CompareTo(other) == 0;
  4179. }
  4180. /// <summary>
  4181. /// Returns true if two lists are equal.
  4182. /// </summary>
  4183. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4184. /// <param name="a">The first list to compare for equality.</param>
  4185. /// <param name="b">The second list to compare for equality.</param>
  4186. /// <returns>True if the two lists are equal.</returns>
  4187. public static bool operator ==(in FixedList512Bytes<T> a, in FixedList4096Bytes<T> b)
  4188. {
  4189. unsafe
  4190. {
  4191. if(a.length != b.length)
  4192. return false;
  4193. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  4194. }
  4195. }
  4196. /// <summary>
  4197. /// Returns true if two lists are unequal.
  4198. /// </summary>
  4199. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4200. /// <param name="a">The first list to compare for inequality.</param>
  4201. /// <param name="b">The second list to compare for inequality.</param>
  4202. /// <returns>True if the two lists are unequal.</returns>
  4203. public static bool operator !=(in FixedList512Bytes<T> a, in FixedList4096Bytes<T> b)
  4204. {
  4205. return !(a == b);
  4206. }
  4207. /// <summary>
  4208. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  4209. /// </summary>
  4210. /// <param name="other">A list to to compare with.</param>
  4211. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  4212. ///
  4213. /// 0 denotes that both lists should have the same position in a sort.
  4214. /// -1 denotes that this list should precede the other list in a sort.
  4215. /// +1 denotes that this list should follow the other list in a sort.
  4216. /// </returns>
  4217. public int CompareTo(FixedList4096Bytes<T> other)
  4218. {
  4219. unsafe
  4220. {
  4221. fixed(byte* a = &buffer.offset0000.byte0000)
  4222. {
  4223. byte* b = &other.buffer.offset0000.byte0000;
  4224. var aa = a + FixedList.PaddingBytes<T>();
  4225. var bb = b + FixedList.PaddingBytes<T>();
  4226. var mini = math.min(Length, other.Length);
  4227. for(var i = 0; i < mini; ++i)
  4228. {
  4229. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  4230. if(j != 0)
  4231. return j;
  4232. }
  4233. return Length.CompareTo(other.Length);
  4234. }
  4235. }
  4236. }
  4237. /// <summary>
  4238. /// Returns true if this list and another list are equal.
  4239. /// </summary>
  4240. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4241. /// <param name="other">The list to compare for equality.</param>
  4242. /// <returns>True if the two lists are equal.</returns>
  4243. public bool Equals(FixedList4096Bytes<T> other)
  4244. {
  4245. return CompareTo(other) == 0;
  4246. }
  4247. /// <summary>
  4248. /// Initializes and returns an instance of FixedList512Bytes with content copied from another list.
  4249. /// </summary>
  4250. /// <param name="other">The list to copy.</param>
  4251. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  4252. public FixedList512Bytes(in FixedList4096Bytes<T> other)
  4253. {
  4254. this = default;
  4255. var error = Initialize(other);
  4256. if(error != 0)
  4257. FixedList.CheckResize<FixedBytes510,T>(other.Length);
  4258. }
  4259. /// <summary>
  4260. /// Initializes an instance of FixedList512Bytes with content copied from another list.
  4261. /// </summary>
  4262. /// <param name="other">The list to copy.</param>
  4263. /// <returns>zero on success, or non-zero on error.</returns>
  4264. internal int Initialize(in FixedList4096Bytes<T> other)
  4265. {
  4266. if(other.Length > Capacity)
  4267. return (int)CopyError.Truncation;
  4268. length = other.length;
  4269. buffer = new FixedBytes510();
  4270. unsafe
  4271. {
  4272. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  4273. }
  4274. return 0;
  4275. }
  4276. /// <summary>
  4277. /// Returns a new list that is a copy of another list.
  4278. /// </summary>
  4279. /// <param name="other">The list to copy.</param>
  4280. /// <returns>A new list that is a copy of the other.</returns>
  4281. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList512Bytes&lt;T&gt;.</exception>
  4282. public static implicit operator FixedList512Bytes<T>(in FixedList4096Bytes<T> other)
  4283. {
  4284. return new FixedList512Bytes<T>(other);
  4285. }
  4286. /// <summary>
  4287. /// Returns true if the list is equal to an object.
  4288. /// </summary>
  4289. /// <remarks>Two lists are equal if their length and bytes are equal.
  4290. ///
  4291. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  4292. /// </remarks>
  4293. /// <param name="obj">An object to compare for equality.</param>
  4294. /// <returns>True if the list is equal to the object.</returns>
  4295. [NotBurstCompatible]
  4296. public override bool Equals(object obj)
  4297. {
  4298. if(obj is FixedList32Bytes<T> aFixedList32Bytes) return Equals(aFixedList32Bytes);
  4299. if(obj is FixedList64Bytes<T> aFixedList64Bytes) return Equals(aFixedList64Bytes);
  4300. if(obj is FixedList128Bytes<T> aFixedList128Bytes) return Equals(aFixedList128Bytes);
  4301. if(obj is FixedList512Bytes<T> aFixedList512Bytes) return Equals(aFixedList512Bytes);
  4302. if(obj is FixedList4096Bytes<T> aFixedList4096Bytes) return Equals(aFixedList4096Bytes);
  4303. return false;
  4304. }
  4305. /// <summary>
  4306. /// An enumerator over the elements of a FixedList512Bytes&lt;T&gt;.
  4307. /// </summary>
  4308. /// <remarks>
  4309. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  4310. /// </remarks>
  4311. public struct Enumerator : IEnumerator<T>
  4312. {
  4313. FixedList512Bytes<T> m_List;
  4314. int m_Index;
  4315. /// <summary>
  4316. /// Initializes and returns an instance of FixedList512Bytes&lt;T&gt;.
  4317. /// </summary>
  4318. /// <param name="list">The list for which to create an enumerator.</param>
  4319. public Enumerator(ref FixedList512Bytes<T> list)
  4320. {
  4321. m_List = list;
  4322. m_Index = -1;
  4323. }
  4324. /// <summary>
  4325. /// Does nothing.
  4326. /// </summary>
  4327. public void Dispose()
  4328. {
  4329. }
  4330. /// <summary>
  4331. /// Advances the enumerator to the next element.
  4332. /// </summary>
  4333. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  4334. public bool MoveNext()
  4335. {
  4336. m_Index++;
  4337. return m_Index < m_List.Length;
  4338. }
  4339. /// <summary>
  4340. /// Resets the enumerator to its initial state.
  4341. /// </summary>
  4342. public void Reset()
  4343. {
  4344. m_Index = -1;
  4345. }
  4346. /// <summary>
  4347. /// The current element.
  4348. /// </summary>
  4349. /// <value>The current element.</value>
  4350. public T Current => m_List[m_Index]; // Let FixedList512Bytes<T> indexer check for out of range.
  4351. object IEnumerator.Current => Current;
  4352. }
  4353. /// <summary>
  4354. /// Returns an enumerator for iterating over the elements of this list.
  4355. /// </summary>
  4356. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  4357. public Enumerator GetEnumerator()
  4358. {
  4359. return new Enumerator(ref this);
  4360. }
  4361. /// <summary>
  4362. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  4363. /// </summary>
  4364. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  4365. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  4366. IEnumerator IEnumerable.GetEnumerator()
  4367. {
  4368. throw new NotImplementedException();
  4369. }
  4370. /// <summary>
  4371. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  4372. /// </summary>
  4373. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  4374. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  4375. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  4376. {
  4377. throw new NotImplementedException();
  4378. }
  4379. }
  4380. /// <summary>
  4381. /// Provides extension methods for FixedList512Bytes.
  4382. /// </summary>
  4383. [BurstCompatible]
  4384. public unsafe static class FixedList512BytesExtensions
  4385. {
  4386. /// <summary>
  4387. /// Finds the index of the first occurrence of a particular value in this list.
  4388. /// </summary>
  4389. /// <typeparam name="T">The type of elements in this list.</typeparam>
  4390. /// <typeparam name="U">The value type.</typeparam>
  4391. /// <param name="list">The list to search.</param>
  4392. /// <param name="value">The value to locate.</param>
  4393. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  4394. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  4395. public static int IndexOf<T, U>(this ref FixedList512Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  4396. {
  4397. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  4398. }
  4399. /// <summary>
  4400. /// Returns true if a particular value is present in this list.
  4401. /// </summary>
  4402. /// <typeparam name="T">The type of elements in this list.</typeparam>
  4403. /// <typeparam name="U">The value type.</typeparam>
  4404. /// <param name="list">The list to search.</param>
  4405. /// <param name="value">The value to locate.</param>
  4406. /// <returns>True if the value is present in this list.</returns>
  4407. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  4408. public static bool Contains<T, U>(this ref FixedList512Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  4409. {
  4410. return list.IndexOf(value) != -1;
  4411. }
  4412. /// <summary>
  4413. /// Removes the first occurrence of a particular value in this list.
  4414. /// </summary>
  4415. /// <remarks>
  4416. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  4417. ///
  4418. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.FixedList512BytesExtensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  4419. /// </remarks>
  4420. /// <typeparam name="T">The type of elements in this list.</typeparam>
  4421. /// <typeparam name="U">The value type.</typeparam>
  4422. /// <param name="list">The list to search.</param>
  4423. /// <param name="value">The value to locate and remove.</param>
  4424. /// <returns>True if the value was found and removed.</returns>
  4425. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  4426. public static bool Remove<T, U>(this ref FixedList512Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  4427. {
  4428. int index = list.IndexOf(value);
  4429. if (index < 0)
  4430. {
  4431. return false;
  4432. }
  4433. list.RemoveAt(index);
  4434. return true;
  4435. }
  4436. /// <summary>
  4437. /// Removes the first occurrence of a particular value in this list.
  4438. /// </summary>
  4439. /// <remarks>
  4440. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  4441. ///
  4442. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  4443. /// </remarks>
  4444. /// <typeparam name="T">The type of elements in this list.</typeparam>
  4445. /// <typeparam name="U">The value type.</typeparam>
  4446. /// <param name="list">The list to search.</param>
  4447. /// <param name="value">The value to locate and remove.</param>
  4448. /// <returns>Returns true if the item is removed.</returns>
  4449. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  4450. public static bool RemoveSwapBack<T, U>(this ref FixedList512Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  4451. {
  4452. var index = list.IndexOf(value);
  4453. if (index == -1)
  4454. {
  4455. return false;
  4456. }
  4457. list.RemoveAtSwapBack(index);
  4458. return true;
  4459. }
  4460. }
  4461. sealed class FixedList512BytesDebugView<T> where T : unmanaged
  4462. {
  4463. FixedList512Bytes<T> m_List;
  4464. public FixedList512BytesDebugView(FixedList512Bytes<T> list)
  4465. {
  4466. m_List = list;
  4467. }
  4468. public T[] Items => m_List.ToArray();
  4469. }
  4470. [Obsolete("Renamed to FixedList4096Bytes<T> (UnityUpgradable) -> FixedList4096Bytes<T>", true)]
  4471. public struct FixedList4096<T> where T : unmanaged {}
  4472. /// <summary>
  4473. /// An unmanaged, resizable list whose content is all stored directly in the 4096-byte struct. Useful for small lists.
  4474. /// </summary>
  4475. /// <typeparam name="T">The type of the elements.</typeparam>
  4476. [Serializable]
  4477. [DebuggerTypeProxy(typeof(FixedList4096BytesDebugView<>))]
  4478. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  4479. public struct FixedList4096Bytes<T>
  4480. : INativeList<T>
  4481. , IEnumerable<T> // Used by collection initializers.
  4482. , IEquatable<FixedList32Bytes<T>>
  4483. , IComparable<FixedList32Bytes<T>>
  4484. , IEquatable<FixedList64Bytes<T>>
  4485. , IComparable<FixedList64Bytes<T>>
  4486. , IEquatable<FixedList128Bytes<T>>
  4487. , IComparable<FixedList128Bytes<T>>
  4488. , IEquatable<FixedList512Bytes<T>>
  4489. , IComparable<FixedList512Bytes<T>>
  4490. , IEquatable<FixedList4096Bytes<T>>
  4491. , IComparable<FixedList4096Bytes<T>>
  4492. where T : unmanaged
  4493. {
  4494. [SerializeField] internal ushort length;
  4495. [SerializeField] internal FixedBytes4094 buffer;
  4496. /// <summary>
  4497. /// The current number of items in this list.
  4498. /// </summary>
  4499. /// <value>The current number of items in this list.</value>
  4500. [CreateProperty]
  4501. public int Length
  4502. {
  4503. get => length;
  4504. set
  4505. {
  4506. FixedList.CheckResize<FixedBytes4094,T>(value);
  4507. length = (ushort)value;
  4508. }
  4509. }
  4510. /// <summary>
  4511. /// A property in order to display items in the Entity Inspector.
  4512. /// </summary>
  4513. [CreateProperty] IEnumerable<T> Elements => this.ToArray();
  4514. /// <summary>
  4515. /// Whether this list is empty.
  4516. /// </summary>
  4517. /// <value>True if this string has no characters or if the container has not been constructed.</value>
  4518. public bool IsEmpty => Length == 0;
  4519. internal int LengthInBytes => Length * UnsafeUtility.SizeOf<T>();
  4520. unsafe internal byte* Buffer
  4521. {
  4522. get
  4523. {
  4524. fixed(byte* b = &buffer.offset0000.byte0000)
  4525. return b + FixedList.PaddingBytes<T>();
  4526. }
  4527. }
  4528. /// <summary>
  4529. /// The number of elements that can fit in this list.
  4530. /// </summary>
  4531. /// <value>The number of elements that can fit in this list.</value>
  4532. /// <remarks>The capacity of a FixedList cannot be changed. The setter is included only for conformity with <see cref="INativeList{T}"/>.</remarks>
  4533. /// <exception cref="ArgumentOutOfRangeException">Thrown if the new value does not match the current capacity.</exception>
  4534. public int Capacity
  4535. {
  4536. get
  4537. {
  4538. return FixedList.Capacity<FixedBytes4094,T>();
  4539. }
  4540. set
  4541. {
  4542. CollectionHelper.CheckCapacityInRange(value, Length);
  4543. }
  4544. }
  4545. /// <summary>
  4546. /// The element at a given index.
  4547. /// </summary>
  4548. /// <param name="index">An index.</param>
  4549. /// <value>The value to store at the index.</value>
  4550. /// <exception cref="IndexOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4551. public T this[int index]
  4552. {
  4553. get
  4554. {
  4555. CollectionHelper.CheckIndexInRange(index, length);
  4556. unsafe
  4557. {
  4558. return UnsafeUtility.ReadArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index));
  4559. }
  4560. }
  4561. set
  4562. {
  4563. CollectionHelper.CheckIndexInRange(index, length);
  4564. unsafe
  4565. {
  4566. UnsafeUtility.WriteArrayElement<T>(Buffer, CollectionHelper.AssumePositive(index), value);
  4567. }
  4568. }
  4569. }
  4570. /// <summary>
  4571. /// Returns the element at a given index.
  4572. /// </summary>
  4573. /// <param name="index">An index.</param>
  4574. /// <returns>The list element at the index.</returns>
  4575. public ref T ElementAt(int index)
  4576. {
  4577. CollectionHelper.CheckIndexInRange(index, length);
  4578. unsafe
  4579. {
  4580. return ref UnsafeUtility.ArrayElementAsRef<T>(Buffer, index);
  4581. }
  4582. }
  4583. /// <summary>
  4584. /// Returns the hash code of this list.
  4585. /// </summary>
  4586. /// <remarks>
  4587. /// Only the content of the list (the bytes of the elements) are included in the hash. Any bytes beyond the length are not part of the hash.</remarks>
  4588. /// <returns>The hash code of this list.</returns>
  4589. public override int GetHashCode()
  4590. {
  4591. unsafe
  4592. {
  4593. return (int)CollectionHelper.Hash(Buffer, LengthInBytes);
  4594. }
  4595. }
  4596. /// <summary>
  4597. /// Appends an element to the end of this list. Increments the length by 1.
  4598. /// </summary>
  4599. /// <remarks>The same as <see cref="AddNoResize"/>. Remember that a fixed list is never resized.</remarks>
  4600. /// <param name="item">The element to append at the end of the list.</param>
  4601. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  4602. public void Add(in T item)
  4603. {
  4604. this[Length++] = item;
  4605. }
  4606. /// <summary>
  4607. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  4608. /// </summary>
  4609. /// <remarks>The same as <see cref="AddRangeNoResize"/>. Remember that a fixed list is never resized.</remarks>
  4610. /// <param name="ptr">A buffer.</param>
  4611. /// <param name="length">The number of elements from the buffer to append.</param>
  4612. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  4613. public unsafe void AddRange(void* ptr, int length)
  4614. {
  4615. T* data = (T*)ptr;
  4616. for (var i = 0; i < length; ++i)
  4617. {
  4618. this[Length++] = data[i];
  4619. }
  4620. }
  4621. /// <summary>
  4622. /// Appends an element to the end of this list. Increments the length by 1.
  4623. /// </summary>
  4624. /// <remarks>The same as <see cref="Add"/>. Included only for consistency with the other list types.</remarks>
  4625. /// <param name="item">The element to append at the end of the list.</param>
  4626. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  4627. public void AddNoResize(in T item) => Add(item);
  4628. /// <summary>
  4629. /// Appends elements from a buffer to the end of this list. Increments the length by the number of appended elements.
  4630. /// </summary>
  4631. /// <remarks>The same as <see cref="AddRange"/>. Included only for consistency with the other list types.</remarks>
  4632. /// <param name="ptr">A buffer.</param>
  4633. /// <param name="length">The number of elements from the buffer to append.</param>
  4634. /// <exception cref="IndexOutOfRangeException">Thrown if the append exceeds the capacity.</exception>
  4635. public unsafe void AddRangeNoResize(void* ptr, int length) => AddRange(ptr, length);
  4636. /// <summary>
  4637. /// Sets the length to 0.
  4638. /// </summary>
  4639. /// <remarks> Does *not* zero out the bytes.</remarks>
  4640. public void Clear()
  4641. {
  4642. Length = 0;
  4643. }
  4644. /// <summary>
  4645. /// Shifts elements toward the end of this list, increasing its length.
  4646. /// </summary>
  4647. /// <remarks>
  4648. /// Right-shifts elements in the list so as to create 'free' slots at the beginning or in the middle.
  4649. ///
  4650. /// The length is increased by `end - begin`.
  4651. ///
  4652. /// If `end` equals `begin`, the method does nothing.
  4653. ///
  4654. /// The element at index `begin` will be copied to index `end`, the element at index `begin + 1` will be copied to `end + 1`, and so forth.
  4655. ///
  4656. /// The indexes `begin` up to `end` are not cleared: they will contain whatever values they held prior.
  4657. /// </remarks>
  4658. /// <param name="begin">The index of the first element that will be shifted up.</param>
  4659. /// <param name="end">The index where the first shifted element will end up.</param>
  4660. /// <exception cref="IndexOutOfRangeException">Thrown if the new length exceeds the capacity.</exception>
  4661. public void InsertRangeWithBeginEnd(int begin, int end)
  4662. {
  4663. int items = end - begin;
  4664. if(items < 1)
  4665. return;
  4666. int itemsToCopy = length - begin;
  4667. Length += items;
  4668. if(itemsToCopy < 1)
  4669. return;
  4670. int bytesToCopy = itemsToCopy * UnsafeUtility.SizeOf<T>();
  4671. unsafe
  4672. {
  4673. byte *b = Buffer;
  4674. byte *dest = b + end * UnsafeUtility.SizeOf<T>();
  4675. byte *src = b + begin * UnsafeUtility.SizeOf<T>();
  4676. UnsafeUtility.MemMove(dest, src, bytesToCopy);
  4677. }
  4678. }
  4679. /// <summary>
  4680. /// Inserts a single element at an index. Increments the length by 1.
  4681. /// </summary>
  4682. /// <param name="index">The index at which to insert the element.</param>
  4683. /// <param name="item">The element to insert.</param>
  4684. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4685. public void Insert(int index, in T item)
  4686. {
  4687. InsertRangeWithBeginEnd(index, index+1);
  4688. this[index] = item;
  4689. }
  4690. /// <summary>
  4691. /// Copies the last element of this list to an index. Decrements the length by 1.
  4692. /// </summary>
  4693. /// <remarks>Useful as a cheap way to remove elements from a list when you don't care about preserving order.</remarks>
  4694. /// <param name="index">The index to overwrite with the last element.</param>
  4695. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4696. public void RemoveAtSwapBack(int index)
  4697. {
  4698. RemoveRangeSwapBack(index, 1);
  4699. }
  4700. /// <summary>
  4701. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  4702. /// </summary>
  4703. /// <remarks>
  4704. /// Copies the last `count`-numbered elements to the range starting at `index`.
  4705. ///
  4706. /// Useful as a cheap way to remove elements from a list when you don't care about preserving order.
  4707. ///
  4708. /// Does nothing if the count is less than 1.
  4709. /// </remarks>
  4710. /// <param name="index">The first index of the destination range.</param>
  4711. /// <param name="count">The number of elements to copy and the amount by which to decrement the length.</param>
  4712. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4713. public void RemoveRangeSwapBack(int index, int count)
  4714. {
  4715. if (count > 0)
  4716. {
  4717. int copyFrom = math.max(Length - count, index + count);
  4718. unsafe
  4719. {
  4720. var sizeOf = UnsafeUtility.SizeOf<T>();
  4721. void* dst = Buffer + index * sizeOf;
  4722. void* src = Buffer + copyFrom * sizeOf;
  4723. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  4724. }
  4725. Length -= count;
  4726. }
  4727. }
  4728. /// <summary>
  4729. /// Copies the last *N* elements of this list to a range in this list. Decrements the length by *N*.
  4730. /// </summary>
  4731. /// <param name="begin">The first index of the item to remove.</param>
  4732. /// <param name="end">The index past-the-last item to remove.</param>
  4733. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  4734. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  4735. [Obsolete("RemoveRangeSwapBackWithBeginEnd(begin, end) is deprecated, use RemoveRangeSwapBack(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  4736. public void RemoveRangeSwapBackWithBeginEnd(int begin, int end) => RemoveRangeSwapBack(begin, end - begin);
  4737. /// <summary>
  4738. /// Removes the element at an index. Shifts everything above the index down by one and decrements the length by 1.
  4739. /// </summary>
  4740. /// <param name="index">The index of the element to remove.</param>
  4741. /// <remarks>
  4742. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove an element.
  4743. /// </remarks>
  4744. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4745. public void RemoveAt(int index)
  4746. {
  4747. RemoveRange(index, 1);
  4748. }
  4749. /// <summary>
  4750. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  4751. /// </summary>
  4752. /// <remarks>
  4753. /// If you don't care about preserving the order of the elements, `RemoveAtSwapBack` is a more efficient way to remove elements.
  4754. /// </remarks>
  4755. /// <param name="index">The first index of the range to remove.</param>
  4756. /// <param name="count">The number of elements to remove.</param>
  4757. /// <exception cref="ArgumentOutOfRangeException">Thrown if the index is out of bounds.</exception>
  4758. public void RemoveRange(int index, int count)
  4759. {
  4760. if (count > 0)
  4761. {
  4762. int copyFrom = math.min(index + count, Length);
  4763. unsafe
  4764. {
  4765. var sizeOf = UnsafeUtility.SizeOf<T>();
  4766. void* dst = Buffer + index * sizeOf;
  4767. void* src = Buffer + copyFrom * sizeOf;
  4768. UnsafeUtility.MemCpy(dst, src, (Length - copyFrom) * sizeOf);
  4769. }
  4770. Length -= count;
  4771. }
  4772. }
  4773. /// <summary>
  4774. /// Removes *N* elements of a range. Shifts everything above the range down by *N* and decrements the length by *N*.
  4775. /// </summary>
  4776. /// <param name="begin">The first index of the item to remove.</param>
  4777. /// <param name="end">The index past-the-last item to remove.</param>
  4778. /// <remarks>
  4779. /// This method of removing item(s) is useful only in case when list is ordered and user wants to preserve order
  4780. /// in list after removal In majority of cases is not important and user should use more performant `RemoveRangeSwapBackWithBeginEnd`.
  4781. /// </remarks>
  4782. /// <exception cref="ArgumentException">Thrown if end argument is less than begin argument.</exception>
  4783. /// <exception cref="ArgumentOutOfRangeException">Thrown if begin or end arguments are not positive or out of bounds.</exception>
  4784. [Obsolete("RemoveRangeWithBeginEnd(begin, end) is deprecated, use RemoveRange(index, count) instead. (RemovedAfter 2021-06-02)", false)]
  4785. public void RemoveRangeWithBeginEnd(int begin, int end) => RemoveRange(begin, end - begin);
  4786. /// <summary>
  4787. /// Returns a managed array that is a copy of this list.
  4788. /// </summary>
  4789. /// <returns>A managed array that is a copy of this list.</returns>
  4790. [NotBurstCompatible]
  4791. public T[] ToArray()
  4792. {
  4793. var result = new T[Length];
  4794. unsafe
  4795. {
  4796. byte* s = Buffer;
  4797. fixed(T* d = result)
  4798. UnsafeUtility.MemCpy(d, s, LengthInBytes);
  4799. }
  4800. return result;
  4801. }
  4802. /// <summary>
  4803. /// Returns an array that is a copy of this list.
  4804. /// </summary>
  4805. /// <param name="allocator">The allocator to use.</param>
  4806. /// <returns>An array that is a copy of this list.</returns>
  4807. public NativeArray<T> ToNativeArray(AllocatorManager.AllocatorHandle allocator)
  4808. {
  4809. unsafe
  4810. {
  4811. var copy = CollectionHelper.CreateNativeArray<T>(Length, allocator, NativeArrayOptions.UninitializedMemory);
  4812. UnsafeUtility.MemCpy(copy.GetUnsafePtr(), Buffer, LengthInBytes);
  4813. return copy;
  4814. }
  4815. }
  4816. /// <summary>
  4817. /// Returns true if two lists are equal.
  4818. /// </summary>
  4819. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4820. /// <param name="a">The first list to compare for equality.</param>
  4821. /// <param name="b">The second list to compare for equality.</param>
  4822. /// <returns>True if the two lists are equal.</returns>
  4823. public static bool operator ==(in FixedList4096Bytes<T> a, in FixedList32Bytes<T> b)
  4824. {
  4825. unsafe
  4826. {
  4827. if(a.length != b.length)
  4828. return false;
  4829. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  4830. }
  4831. }
  4832. /// <summary>
  4833. /// Returns true if two lists are unequal.
  4834. /// </summary>
  4835. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4836. /// <param name="a">The first list to compare for inequality.</param>
  4837. /// <param name="b">The second list to compare for inequality.</param>
  4838. /// <returns>True if the two lists are unequal.</returns>
  4839. public static bool operator !=(in FixedList4096Bytes<T> a, in FixedList32Bytes<T> b)
  4840. {
  4841. return !(a == b);
  4842. }
  4843. /// <summary>
  4844. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  4845. /// </summary>
  4846. /// <param name="other">A list to to compare with.</param>
  4847. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  4848. ///
  4849. /// 0 denotes that both lists should have the same position in a sort.
  4850. /// -1 denotes that this list should precede the other list in a sort.
  4851. /// +1 denotes that this list should follow the other list in a sort.
  4852. /// </returns>
  4853. public int CompareTo(FixedList32Bytes<T> other)
  4854. {
  4855. unsafe
  4856. {
  4857. fixed(byte* a = &buffer.offset0000.byte0000)
  4858. {
  4859. byte* b = &other.buffer.offset0000.byte0000;
  4860. var aa = a + FixedList.PaddingBytes<T>();
  4861. var bb = b + FixedList.PaddingBytes<T>();
  4862. var mini = math.min(Length, other.Length);
  4863. for(var i = 0; i < mini; ++i)
  4864. {
  4865. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  4866. if(j != 0)
  4867. return j;
  4868. }
  4869. return Length.CompareTo(other.Length);
  4870. }
  4871. }
  4872. }
  4873. /// <summary>
  4874. /// Returns true if this list and another list are equal.
  4875. /// </summary>
  4876. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4877. /// <param name="other">The list to compare for equality.</param>
  4878. /// <returns>True if the two lists are equal.</returns>
  4879. public bool Equals(FixedList32Bytes<T> other)
  4880. {
  4881. return CompareTo(other) == 0;
  4882. }
  4883. /// <summary>
  4884. /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list.
  4885. /// </summary>
  4886. /// <param name="other">The list to copy.</param>
  4887. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  4888. public FixedList4096Bytes(in FixedList32Bytes<T> other)
  4889. {
  4890. this = default;
  4891. var error = Initialize(other);
  4892. if(error != 0)
  4893. FixedList.CheckResize<FixedBytes4094,T>(other.Length);
  4894. }
  4895. /// <summary>
  4896. /// Initializes an instance of FixedList4096Bytes with content copied from another list.
  4897. /// </summary>
  4898. /// <param name="other">The list to copy.</param>
  4899. /// <returns>zero on success, or non-zero on error.</returns>
  4900. internal int Initialize(in FixedList32Bytes<T> other)
  4901. {
  4902. if(other.Length > Capacity)
  4903. return (int)CopyError.Truncation;
  4904. length = other.length;
  4905. buffer = new FixedBytes4094();
  4906. unsafe
  4907. {
  4908. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  4909. }
  4910. return 0;
  4911. }
  4912. /// <summary>
  4913. /// Returns a new list that is a copy of another list.
  4914. /// </summary>
  4915. /// <param name="other">The list to copy.</param>
  4916. /// <returns>A new list that is a copy of the other.</returns>
  4917. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  4918. public static implicit operator FixedList4096Bytes<T>(in FixedList32Bytes<T> other)
  4919. {
  4920. return new FixedList4096Bytes<T>(other);
  4921. }
  4922. /// <summary>
  4923. /// Returns true if two lists are equal.
  4924. /// </summary>
  4925. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4926. /// <param name="a">The first list to compare for equality.</param>
  4927. /// <param name="b">The second list to compare for equality.</param>
  4928. /// <returns>True if the two lists are equal.</returns>
  4929. public static bool operator ==(in FixedList4096Bytes<T> a, in FixedList64Bytes<T> b)
  4930. {
  4931. unsafe
  4932. {
  4933. if(a.length != b.length)
  4934. return false;
  4935. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  4936. }
  4937. }
  4938. /// <summary>
  4939. /// Returns true if two lists are unequal.
  4940. /// </summary>
  4941. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4942. /// <param name="a">The first list to compare for inequality.</param>
  4943. /// <param name="b">The second list to compare for inequality.</param>
  4944. /// <returns>True if the two lists are unequal.</returns>
  4945. public static bool operator !=(in FixedList4096Bytes<T> a, in FixedList64Bytes<T> b)
  4946. {
  4947. return !(a == b);
  4948. }
  4949. /// <summary>
  4950. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  4951. /// </summary>
  4952. /// <param name="other">A list to to compare with.</param>
  4953. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  4954. ///
  4955. /// 0 denotes that both lists should have the same position in a sort.
  4956. /// -1 denotes that this list should precede the other list in a sort.
  4957. /// +1 denotes that this list should follow the other list in a sort.
  4958. /// </returns>
  4959. public int CompareTo(FixedList64Bytes<T> other)
  4960. {
  4961. unsafe
  4962. {
  4963. fixed(byte* a = &buffer.offset0000.byte0000)
  4964. {
  4965. byte* b = &other.buffer.offset0000.byte0000;
  4966. var aa = a + FixedList.PaddingBytes<T>();
  4967. var bb = b + FixedList.PaddingBytes<T>();
  4968. var mini = math.min(Length, other.Length);
  4969. for(var i = 0; i < mini; ++i)
  4970. {
  4971. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  4972. if(j != 0)
  4973. return j;
  4974. }
  4975. return Length.CompareTo(other.Length);
  4976. }
  4977. }
  4978. }
  4979. /// <summary>
  4980. /// Returns true if this list and another list are equal.
  4981. /// </summary>
  4982. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  4983. /// <param name="other">The list to compare for equality.</param>
  4984. /// <returns>True if the two lists are equal.</returns>
  4985. public bool Equals(FixedList64Bytes<T> other)
  4986. {
  4987. return CompareTo(other) == 0;
  4988. }
  4989. /// <summary>
  4990. /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list.
  4991. /// </summary>
  4992. /// <param name="other">The list to copy.</param>
  4993. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  4994. public FixedList4096Bytes(in FixedList64Bytes<T> other)
  4995. {
  4996. this = default;
  4997. var error = Initialize(other);
  4998. if(error != 0)
  4999. FixedList.CheckResize<FixedBytes4094,T>(other.Length);
  5000. }
  5001. /// <summary>
  5002. /// Initializes an instance of FixedList4096Bytes with content copied from another list.
  5003. /// </summary>
  5004. /// <param name="other">The list to copy.</param>
  5005. /// <returns>zero on success, or non-zero on error.</returns>
  5006. internal int Initialize(in FixedList64Bytes<T> other)
  5007. {
  5008. if(other.Length > Capacity)
  5009. return (int)CopyError.Truncation;
  5010. length = other.length;
  5011. buffer = new FixedBytes4094();
  5012. unsafe
  5013. {
  5014. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  5015. }
  5016. return 0;
  5017. }
  5018. /// <summary>
  5019. /// Returns a new list that is a copy of another list.
  5020. /// </summary>
  5021. /// <param name="other">The list to copy.</param>
  5022. /// <returns>A new list that is a copy of the other.</returns>
  5023. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  5024. public static implicit operator FixedList4096Bytes<T>(in FixedList64Bytes<T> other)
  5025. {
  5026. return new FixedList4096Bytes<T>(other);
  5027. }
  5028. /// <summary>
  5029. /// Returns true if two lists are equal.
  5030. /// </summary>
  5031. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5032. /// <param name="a">The first list to compare for equality.</param>
  5033. /// <param name="b">The second list to compare for equality.</param>
  5034. /// <returns>True if the two lists are equal.</returns>
  5035. public static bool operator ==(in FixedList4096Bytes<T> a, in FixedList128Bytes<T> b)
  5036. {
  5037. unsafe
  5038. {
  5039. if(a.length != b.length)
  5040. return false;
  5041. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  5042. }
  5043. }
  5044. /// <summary>
  5045. /// Returns true if two lists are unequal.
  5046. /// </summary>
  5047. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5048. /// <param name="a">The first list to compare for inequality.</param>
  5049. /// <param name="b">The second list to compare for inequality.</param>
  5050. /// <returns>True if the two lists are unequal.</returns>
  5051. public static bool operator !=(in FixedList4096Bytes<T> a, in FixedList128Bytes<T> b)
  5052. {
  5053. return !(a == b);
  5054. }
  5055. /// <summary>
  5056. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  5057. /// </summary>
  5058. /// <param name="other">A list to to compare with.</param>
  5059. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  5060. ///
  5061. /// 0 denotes that both lists should have the same position in a sort.
  5062. /// -1 denotes that this list should precede the other list in a sort.
  5063. /// +1 denotes that this list should follow the other list in a sort.
  5064. /// </returns>
  5065. public int CompareTo(FixedList128Bytes<T> other)
  5066. {
  5067. unsafe
  5068. {
  5069. fixed(byte* a = &buffer.offset0000.byte0000)
  5070. {
  5071. byte* b = &other.buffer.offset0000.byte0000;
  5072. var aa = a + FixedList.PaddingBytes<T>();
  5073. var bb = b + FixedList.PaddingBytes<T>();
  5074. var mini = math.min(Length, other.Length);
  5075. for(var i = 0; i < mini; ++i)
  5076. {
  5077. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  5078. if(j != 0)
  5079. return j;
  5080. }
  5081. return Length.CompareTo(other.Length);
  5082. }
  5083. }
  5084. }
  5085. /// <summary>
  5086. /// Returns true if this list and another list are equal.
  5087. /// </summary>
  5088. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5089. /// <param name="other">The list to compare for equality.</param>
  5090. /// <returns>True if the two lists are equal.</returns>
  5091. public bool Equals(FixedList128Bytes<T> other)
  5092. {
  5093. return CompareTo(other) == 0;
  5094. }
  5095. /// <summary>
  5096. /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list.
  5097. /// </summary>
  5098. /// <param name="other">The list to copy.</param>
  5099. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  5100. public FixedList4096Bytes(in FixedList128Bytes<T> other)
  5101. {
  5102. this = default;
  5103. var error = Initialize(other);
  5104. if(error != 0)
  5105. FixedList.CheckResize<FixedBytes4094,T>(other.Length);
  5106. }
  5107. /// <summary>
  5108. /// Initializes an instance of FixedList4096Bytes with content copied from another list.
  5109. /// </summary>
  5110. /// <param name="other">The list to copy.</param>
  5111. /// <returns>zero on success, or non-zero on error.</returns>
  5112. internal int Initialize(in FixedList128Bytes<T> other)
  5113. {
  5114. if(other.Length > Capacity)
  5115. return (int)CopyError.Truncation;
  5116. length = other.length;
  5117. buffer = new FixedBytes4094();
  5118. unsafe
  5119. {
  5120. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  5121. }
  5122. return 0;
  5123. }
  5124. /// <summary>
  5125. /// Returns a new list that is a copy of another list.
  5126. /// </summary>
  5127. /// <param name="other">The list to copy.</param>
  5128. /// <returns>A new list that is a copy of the other.</returns>
  5129. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  5130. public static implicit operator FixedList4096Bytes<T>(in FixedList128Bytes<T> other)
  5131. {
  5132. return new FixedList4096Bytes<T>(other);
  5133. }
  5134. /// <summary>
  5135. /// Returns true if two lists are equal.
  5136. /// </summary>
  5137. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5138. /// <param name="a">The first list to compare for equality.</param>
  5139. /// <param name="b">The second list to compare for equality.</param>
  5140. /// <returns>True if the two lists are equal.</returns>
  5141. public static bool operator ==(in FixedList4096Bytes<T> a, in FixedList512Bytes<T> b)
  5142. {
  5143. unsafe
  5144. {
  5145. if(a.length != b.length)
  5146. return false;
  5147. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  5148. }
  5149. }
  5150. /// <summary>
  5151. /// Returns true if two lists are unequal.
  5152. /// </summary>
  5153. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5154. /// <param name="a">The first list to compare for inequality.</param>
  5155. /// <param name="b">The second list to compare for inequality.</param>
  5156. /// <returns>True if the two lists are unequal.</returns>
  5157. public static bool operator !=(in FixedList4096Bytes<T> a, in FixedList512Bytes<T> b)
  5158. {
  5159. return !(a == b);
  5160. }
  5161. /// <summary>
  5162. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  5163. /// </summary>
  5164. /// <param name="other">A list to to compare with.</param>
  5165. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  5166. ///
  5167. /// 0 denotes that both lists should have the same position in a sort.
  5168. /// -1 denotes that this list should precede the other list in a sort.
  5169. /// +1 denotes that this list should follow the other list in a sort.
  5170. /// </returns>
  5171. public int CompareTo(FixedList512Bytes<T> other)
  5172. {
  5173. unsafe
  5174. {
  5175. fixed(byte* a = &buffer.offset0000.byte0000)
  5176. {
  5177. byte* b = &other.buffer.offset0000.byte0000;
  5178. var aa = a + FixedList.PaddingBytes<T>();
  5179. var bb = b + FixedList.PaddingBytes<T>();
  5180. var mini = math.min(Length, other.Length);
  5181. for(var i = 0; i < mini; ++i)
  5182. {
  5183. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  5184. if(j != 0)
  5185. return j;
  5186. }
  5187. return Length.CompareTo(other.Length);
  5188. }
  5189. }
  5190. }
  5191. /// <summary>
  5192. /// Returns true if this list and another list are equal.
  5193. /// </summary>
  5194. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5195. /// <param name="other">The list to compare for equality.</param>
  5196. /// <returns>True if the two lists are equal.</returns>
  5197. public bool Equals(FixedList512Bytes<T> other)
  5198. {
  5199. return CompareTo(other) == 0;
  5200. }
  5201. /// <summary>
  5202. /// Initializes and returns an instance of FixedList4096Bytes with content copied from another list.
  5203. /// </summary>
  5204. /// <param name="other">The list to copy.</param>
  5205. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  5206. public FixedList4096Bytes(in FixedList512Bytes<T> other)
  5207. {
  5208. this = default;
  5209. var error = Initialize(other);
  5210. if(error != 0)
  5211. FixedList.CheckResize<FixedBytes4094,T>(other.Length);
  5212. }
  5213. /// <summary>
  5214. /// Initializes an instance of FixedList4096Bytes with content copied from another list.
  5215. /// </summary>
  5216. /// <param name="other">The list to copy.</param>
  5217. /// <returns>zero on success, or non-zero on error.</returns>
  5218. internal int Initialize(in FixedList512Bytes<T> other)
  5219. {
  5220. if(other.Length > Capacity)
  5221. return (int)CopyError.Truncation;
  5222. length = other.length;
  5223. buffer = new FixedBytes4094();
  5224. unsafe
  5225. {
  5226. UnsafeUtility.MemCpy(Buffer, other.Buffer, LengthInBytes);
  5227. }
  5228. return 0;
  5229. }
  5230. /// <summary>
  5231. /// Returns a new list that is a copy of another list.
  5232. /// </summary>
  5233. /// <param name="other">The list to copy.</param>
  5234. /// <returns>A new list that is a copy of the other.</returns>
  5235. /// <exception cref="IndexOutOfRangeException">Throws if the other list's length exceeds the capacity of FixedList4096Bytes&lt;T&gt;.</exception>
  5236. public static implicit operator FixedList4096Bytes<T>(in FixedList512Bytes<T> other)
  5237. {
  5238. return new FixedList4096Bytes<T>(other);
  5239. }
  5240. /// <summary>
  5241. /// Returns true if two lists are equal.
  5242. /// </summary>
  5243. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5244. /// <param name="a">The first list to compare for equality.</param>
  5245. /// <param name="b">The second list to compare for equality.</param>
  5246. /// <returns>True if the two lists are equal.</returns>
  5247. public static bool operator ==(in FixedList4096Bytes<T> a, in FixedList4096Bytes<T> b)
  5248. {
  5249. unsafe
  5250. {
  5251. if(a.length != b.length)
  5252. return false;
  5253. return UnsafeUtility.MemCmp(a.Buffer, b.Buffer, a.LengthInBytes) == 0;
  5254. }
  5255. }
  5256. /// <summary>
  5257. /// Returns true if two lists are unequal.
  5258. /// </summary>
  5259. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5260. /// <param name="a">The first list to compare for inequality.</param>
  5261. /// <param name="b">The second list to compare for inequality.</param>
  5262. /// <returns>True if the two lists are unequal.</returns>
  5263. public static bool operator !=(in FixedList4096Bytes<T> a, in FixedList4096Bytes<T> b)
  5264. {
  5265. return !(a == b);
  5266. }
  5267. /// <summary>
  5268. /// Returns a number denoting whether this list should be placed before or after another list in a sort.
  5269. /// </summary>
  5270. /// <param name="other">A list to to compare with.</param>
  5271. /// <returns>An integer denoting the respective sort position of the list relative to the other:
  5272. ///
  5273. /// 0 denotes that both lists should have the same position in a sort.
  5274. /// -1 denotes that this list should precede the other list in a sort.
  5275. /// +1 denotes that this list should follow the other list in a sort.
  5276. /// </returns>
  5277. public int CompareTo(FixedList4096Bytes<T> other)
  5278. {
  5279. unsafe
  5280. {
  5281. fixed(byte* a = &buffer.offset0000.byte0000)
  5282. {
  5283. byte* b = &other.buffer.offset0000.byte0000;
  5284. var aa = a + FixedList.PaddingBytes<T>();
  5285. var bb = b + FixedList.PaddingBytes<T>();
  5286. var mini = math.min(Length, other.Length);
  5287. for(var i = 0; i < mini; ++i)
  5288. {
  5289. var j = UnsafeUtility.MemCmp(aa + sizeof(T) * i, bb + sizeof(T) * i, sizeof(T));
  5290. if(j != 0)
  5291. return j;
  5292. }
  5293. return Length.CompareTo(other.Length);
  5294. }
  5295. }
  5296. }
  5297. /// <summary>
  5298. /// Returns true if this list and another list are equal.
  5299. /// </summary>
  5300. /// <remarks>Two lists are equal if their length and bytes are equal.</remarks>
  5301. /// <param name="other">The list to compare for equality.</param>
  5302. /// <returns>True if the two lists are equal.</returns>
  5303. public bool Equals(FixedList4096Bytes<T> other)
  5304. {
  5305. return CompareTo(other) == 0;
  5306. }
  5307. /// <summary>
  5308. /// Returns true if the list is equal to an object.
  5309. /// </summary>
  5310. /// <remarks>Two lists are equal if their length and bytes are equal.
  5311. ///
  5312. /// A FixedList*N*&lt;T&gt; can only be equal to another FixedList*N*&lt;T&gt; with the same *N* and T.
  5313. /// </remarks>
  5314. /// <param name="obj">An object to compare for equality.</param>
  5315. /// <returns>True if the list is equal to the object.</returns>
  5316. [NotBurstCompatible]
  5317. public override bool Equals(object obj)
  5318. {
  5319. if(obj is FixedList32Bytes<T> aFixedList32Bytes) return Equals(aFixedList32Bytes);
  5320. if(obj is FixedList64Bytes<T> aFixedList64Bytes) return Equals(aFixedList64Bytes);
  5321. if(obj is FixedList128Bytes<T> aFixedList128Bytes) return Equals(aFixedList128Bytes);
  5322. if(obj is FixedList512Bytes<T> aFixedList512Bytes) return Equals(aFixedList512Bytes);
  5323. if(obj is FixedList4096Bytes<T> aFixedList4096Bytes) return Equals(aFixedList4096Bytes);
  5324. return false;
  5325. }
  5326. /// <summary>
  5327. /// An enumerator over the elements of a FixedList4096Bytes&lt;T&gt;.
  5328. /// </summary>
  5329. /// <remarks>
  5330. /// In an enumerator's initial state, `Current` cannot be read. The first <see cref="MoveNext"/> call advances the enumerator to the first element.
  5331. /// </remarks>
  5332. public struct Enumerator : IEnumerator<T>
  5333. {
  5334. FixedList4096Bytes<T> m_List;
  5335. int m_Index;
  5336. /// <summary>
  5337. /// Initializes and returns an instance of FixedList4096Bytes&lt;T&gt;.
  5338. /// </summary>
  5339. /// <param name="list">The list for which to create an enumerator.</param>
  5340. public Enumerator(ref FixedList4096Bytes<T> list)
  5341. {
  5342. m_List = list;
  5343. m_Index = -1;
  5344. }
  5345. /// <summary>
  5346. /// Does nothing.
  5347. /// </summary>
  5348. public void Dispose()
  5349. {
  5350. }
  5351. /// <summary>
  5352. /// Advances the enumerator to the next element.
  5353. /// </summary>
  5354. /// <returns>True if <see cref="Current"/> is valid to read after the call.</returns>
  5355. public bool MoveNext()
  5356. {
  5357. m_Index++;
  5358. return m_Index < m_List.Length;
  5359. }
  5360. /// <summary>
  5361. /// Resets the enumerator to its initial state.
  5362. /// </summary>
  5363. public void Reset()
  5364. {
  5365. m_Index = -1;
  5366. }
  5367. /// <summary>
  5368. /// The current element.
  5369. /// </summary>
  5370. /// <value>The current element.</value>
  5371. public T Current => m_List[m_Index]; // Let FixedList4096Bytes<T> indexer check for out of range.
  5372. object IEnumerator.Current => Current;
  5373. }
  5374. /// <summary>
  5375. /// Returns an enumerator for iterating over the elements of this list.
  5376. /// </summary>
  5377. /// <returns>An enumerator for iterating over the elements of this list.</returns>
  5378. public Enumerator GetEnumerator()
  5379. {
  5380. return new Enumerator(ref this);
  5381. }
  5382. /// <summary>
  5383. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  5384. /// </summary>
  5385. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  5386. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  5387. IEnumerator IEnumerable.GetEnumerator()
  5388. {
  5389. throw new NotImplementedException();
  5390. }
  5391. /// <summary>
  5392. /// This method is not implemented. Use <see cref="GetEnumerator"/> instead.
  5393. /// </summary>
  5394. /// <returns>Nothing because it always throws <see cref="NotImplementedException"/>.</returns>
  5395. /// <exception cref="NotImplementedException">Method is not implemented.</exception>
  5396. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  5397. {
  5398. throw new NotImplementedException();
  5399. }
  5400. }
  5401. /// <summary>
  5402. /// Provides extension methods for FixedList4096Bytes.
  5403. /// </summary>
  5404. [BurstCompatible]
  5405. public unsafe static class FixedList4096BytesExtensions
  5406. {
  5407. /// <summary>
  5408. /// Finds the index of the first occurrence of a particular value in this list.
  5409. /// </summary>
  5410. /// <typeparam name="T">The type of elements in this list.</typeparam>
  5411. /// <typeparam name="U">The value type.</typeparam>
  5412. /// <param name="list">The list to search.</param>
  5413. /// <param name="value">The value to locate.</param>
  5414. /// <returns>The index of the first occurrence of the value. Returns -1 if no occurrence is found.</returns>
  5415. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  5416. public static int IndexOf<T, U>(this ref FixedList4096Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  5417. {
  5418. return NativeArrayExtensions.IndexOf<T, U>(list.Buffer, list.Length, value);
  5419. }
  5420. /// <summary>
  5421. /// Returns true if a particular value is present in this list.
  5422. /// </summary>
  5423. /// <typeparam name="T">The type of elements in this list.</typeparam>
  5424. /// <typeparam name="U">The value type.</typeparam>
  5425. /// <param name="list">The list to search.</param>
  5426. /// <param name="value">The value to locate.</param>
  5427. /// <returns>True if the value is present in this list.</returns>
  5428. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  5429. public static bool Contains<T, U>(this ref FixedList4096Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  5430. {
  5431. return list.IndexOf(value) != -1;
  5432. }
  5433. /// <summary>
  5434. /// Removes the first occurrence of a particular value in this list.
  5435. /// </summary>
  5436. /// <remarks>
  5437. /// If a value is removed, all elements after it are shifted down by one, and the list's length is decremented by one.
  5438. ///
  5439. /// If you don't need to preserve the order of the remaining elements, <see cref="Unity.Collections.FixedList4096BytesExtensions.RemoveSwapBack{T, U}"/> is a cheaper alternative.
  5440. /// </remarks>
  5441. /// <typeparam name="T">The type of elements in this list.</typeparam>
  5442. /// <typeparam name="U">The value type.</typeparam>
  5443. /// <param name="list">The list to search.</param>
  5444. /// <param name="value">The value to locate and remove.</param>
  5445. /// <returns>True if the value was found and removed.</returns>
  5446. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  5447. public static bool Remove<T, U>(this ref FixedList4096Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  5448. {
  5449. int index = list.IndexOf(value);
  5450. if (index < 0)
  5451. {
  5452. return false;
  5453. }
  5454. list.RemoveAt(index);
  5455. return true;
  5456. }
  5457. /// <summary>
  5458. /// Removes the first occurrence of a particular value in this list.
  5459. /// </summary>
  5460. /// <remarks>
  5461. /// If a value is removed, the last element of the list is copied to overwrite the removed value, and the list's length is decremented by one.
  5462. ///
  5463. /// This is cheaper than <see cref="Remove"/>, but the order of the remaining elements is not preserved.
  5464. /// </remarks>
  5465. /// <typeparam name="T">The type of elements in this list.</typeparam>
  5466. /// <typeparam name="U">The value type.</typeparam>
  5467. /// <param name="list">The list to search.</param>
  5468. /// <param name="value">The value to locate and remove.</param>
  5469. /// <returns>Returns true if the item is removed.</returns>
  5470. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(int) })]
  5471. public static bool RemoveSwapBack<T, U>(this ref FixedList4096Bytes<T> list, U value) where T : unmanaged, IEquatable<U>
  5472. {
  5473. var index = list.IndexOf(value);
  5474. if (index == -1)
  5475. {
  5476. return false;
  5477. }
  5478. list.RemoveAtSwapBack(index);
  5479. return true;
  5480. }
  5481. }
  5482. sealed class FixedList4096BytesDebugView<T> where T : unmanaged
  5483. {
  5484. FixedList4096Bytes<T> m_List;
  5485. public FixedList4096BytesDebugView(FixedList4096Bytes<T> list)
  5486. {
  5487. m_List = list;
  5488. }
  5489. public T[] Items => m_List.ToArray();
  5490. }
  5491. /// <summary>
  5492. /// An unmanaged, resizable list of byte that does not allocate memory.
  5493. /// It is 32 bytes in size, and contains all the memory it needs.
  5494. /// </summary>
  5495. [Serializable]
  5496. [StructLayout(LayoutKind.Explicit, Size=32)]
  5497. [Obsolete("FixedListByte32 is deprecated, please use FixedList32Bytes<byte> instead. (UnityUpgradable) -> FixedList32Bytes<byte>", true)]
  5498. public struct FixedListByte32 {}
  5499. [Obsolete("FixedListByte32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView<byte>", true)]
  5500. sealed class FixedListByte32DebugView
  5501. {
  5502. FixedList32Bytes<byte> m_List;
  5503. public FixedListByte32DebugView(FixedList32Bytes<byte> list)
  5504. {
  5505. m_List = list;
  5506. }
  5507. public byte[] Items => m_List.ToArray();
  5508. }
  5509. /// <summary>
  5510. /// An unmanaged, resizable list of byte that does not allocate memory.
  5511. /// It is 64 bytes in size, and contains all the memory it needs.
  5512. /// </summary>
  5513. [Serializable]
  5514. [StructLayout(LayoutKind.Explicit, Size=64)]
  5515. [Obsolete("FixedListByte64 is deprecated, please use FixedList64Bytes<byte> instead. (UnityUpgradable) -> FixedList64Bytes<byte>", true)]
  5516. public struct FixedListByte64 {}
  5517. [Obsolete("FixedListByte64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView<byte>", true)]
  5518. sealed class FixedListByte64DebugView
  5519. {
  5520. FixedList64Bytes<byte> m_List;
  5521. public FixedListByte64DebugView(FixedList64Bytes<byte> list)
  5522. {
  5523. m_List = list;
  5524. }
  5525. public byte[] Items => m_List.ToArray();
  5526. }
  5527. /// <summary>
  5528. /// An unmanaged, resizable list of byte that does not allocate memory.
  5529. /// It is 128 bytes in size, and contains all the memory it needs.
  5530. /// </summary>
  5531. [Serializable]
  5532. [StructLayout(LayoutKind.Explicit, Size=128)]
  5533. [Obsolete("FixedListByte128 is deprecated, please use FixedList128Bytes<byte> instead. (UnityUpgradable) -> FixedList128Bytes<byte>", true)]
  5534. public struct FixedListByte128 {}
  5535. [Obsolete("FixedListByte128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView<byte>", true)]
  5536. sealed class FixedListByte128DebugView
  5537. {
  5538. FixedList128Bytes<byte> m_List;
  5539. public FixedListByte128DebugView(FixedList128Bytes<byte> list)
  5540. {
  5541. m_List = list;
  5542. }
  5543. public byte[] Items => m_List.ToArray();
  5544. }
  5545. /// <summary>
  5546. /// An unmanaged, resizable list of byte that does not allocate memory.
  5547. /// It is 512 bytes in size, and contains all the memory it needs.
  5548. /// </summary>
  5549. [Serializable]
  5550. [StructLayout(LayoutKind.Explicit, Size=512)]
  5551. [Obsolete("FixedListByte512 is deprecated, please use FixedList512Bytes<byte> instead. (UnityUpgradable) -> FixedList512Bytes<byte>", true)]
  5552. public struct FixedListByte512 {}
  5553. [Obsolete("FixedListByte512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView<byte>", true)]
  5554. sealed class FixedListByte512DebugView
  5555. {
  5556. FixedList512Bytes<byte> m_List;
  5557. public FixedListByte512DebugView(FixedList512Bytes<byte> list)
  5558. {
  5559. m_List = list;
  5560. }
  5561. public byte[] Items => m_List.ToArray();
  5562. }
  5563. /// <summary>
  5564. /// An unmanaged, resizable list of byte that does not allocate memory.
  5565. /// It is 4096 bytes in size, and contains all the memory it needs.
  5566. /// </summary>
  5567. [Serializable]
  5568. [StructLayout(LayoutKind.Explicit, Size=4096)]
  5569. [Obsolete("FixedListByte4096 is deprecated, please use FixedList4096Bytes<byte> instead. (UnityUpgradable) -> FixedList4096Bytes<byte>", true)]
  5570. public struct FixedListByte4096 {}
  5571. [Obsolete("FixedListByte4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView<byte>", true)]
  5572. sealed class FixedListByte4096DebugView
  5573. {
  5574. FixedList4096Bytes<byte> m_List;
  5575. public FixedListByte4096DebugView(FixedList4096Bytes<byte> list)
  5576. {
  5577. m_List = list;
  5578. }
  5579. public byte[] Items => m_List.ToArray();
  5580. }
  5581. /// <summary>
  5582. /// An unmanaged, resizable list of int that does not allocate memory.
  5583. /// It is 32 bytes in size, and contains all the memory it needs.
  5584. /// </summary>
  5585. [Serializable]
  5586. [StructLayout(LayoutKind.Explicit, Size=32)]
  5587. [Obsolete("FixedListInt32 is deprecated, please use FixedList32Bytes<int> instead. (UnityUpgradable) -> FixedList32Bytes<int>", true)]
  5588. public struct FixedListInt32 {}
  5589. [Obsolete("FixedListInt32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView<int>", true)]
  5590. sealed class FixedListInt32DebugView
  5591. {
  5592. FixedList32Bytes<int> m_List;
  5593. public FixedListInt32DebugView(FixedList32Bytes<int> list)
  5594. {
  5595. m_List = list;
  5596. }
  5597. public int[] Items => m_List.ToArray();
  5598. }
  5599. /// <summary>
  5600. /// An unmanaged, resizable list of int that does not allocate memory.
  5601. /// It is 64 bytes in size, and contains all the memory it needs.
  5602. /// </summary>
  5603. [Serializable]
  5604. [StructLayout(LayoutKind.Explicit, Size=64)]
  5605. [Obsolete("FixedListInt64 is deprecated, please use FixedList64Bytes<int> instead. (UnityUpgradable) -> FixedList64Bytes<int>", true)]
  5606. public struct FixedListInt64 {}
  5607. [Obsolete("FixedListInt64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView<int>", true)]
  5608. sealed class FixedListInt64DebugView
  5609. {
  5610. FixedList64Bytes<int> m_List;
  5611. public FixedListInt64DebugView(FixedList64Bytes<int> list)
  5612. {
  5613. m_List = list;
  5614. }
  5615. public int[] Items => m_List.ToArray();
  5616. }
  5617. /// <summary>
  5618. /// An unmanaged, resizable list of int that does not allocate memory.
  5619. /// It is 128 bytes in size, and contains all the memory it needs.
  5620. /// </summary>
  5621. [Serializable]
  5622. [StructLayout(LayoutKind.Explicit, Size=128)]
  5623. [Obsolete("FixedListInt128 is deprecated, please use FixedList128Bytes<int> instead. (UnityUpgradable) -> FixedList128Bytes<int>", true)]
  5624. public struct FixedListInt128 {}
  5625. [Obsolete("FixedListInt128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView<int>", true)]
  5626. sealed class FixedListInt128DebugView
  5627. {
  5628. FixedList128Bytes<int> m_List;
  5629. public FixedListInt128DebugView(FixedList128Bytes<int> list)
  5630. {
  5631. m_List = list;
  5632. }
  5633. public int[] Items => m_List.ToArray();
  5634. }
  5635. /// <summary>
  5636. /// An unmanaged, resizable list of int that does not allocate memory.
  5637. /// It is 512 bytes in size, and contains all the memory it needs.
  5638. /// </summary>
  5639. [Serializable]
  5640. [StructLayout(LayoutKind.Explicit, Size=512)]
  5641. [Obsolete("FixedListInt512 is deprecated, please use FixedList512Bytes<int> instead. (UnityUpgradable) -> FixedList512Bytes<int>", true)]
  5642. public struct FixedListInt512 {}
  5643. [Obsolete("FixedListInt512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView<int>", true)]
  5644. sealed class FixedListInt512DebugView
  5645. {
  5646. FixedList512Bytes<int> m_List;
  5647. public FixedListInt512DebugView(FixedList512Bytes<int> list)
  5648. {
  5649. m_List = list;
  5650. }
  5651. public int[] Items => m_List.ToArray();
  5652. }
  5653. /// <summary>
  5654. /// An unmanaged, resizable list of int that does not allocate memory.
  5655. /// It is 4096 bytes in size, and contains all the memory it needs.
  5656. /// </summary>
  5657. [Serializable]
  5658. [StructLayout(LayoutKind.Explicit, Size=4096)]
  5659. [Obsolete("FixedListInt4096 is deprecated, please use FixedList4096Bytes<int> instead. (UnityUpgradable) -> FixedList4096Bytes<int>", true)]
  5660. public struct FixedListInt4096 {}
  5661. [Obsolete("FixedListInt4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView<int>", true)]
  5662. sealed class FixedListInt4096DebugView
  5663. {
  5664. FixedList4096Bytes<int> m_List;
  5665. public FixedListInt4096DebugView(FixedList4096Bytes<int> list)
  5666. {
  5667. m_List = list;
  5668. }
  5669. public int[] Items => m_List.ToArray();
  5670. }
  5671. /// <summary>
  5672. /// An unmanaged, resizable list of float that does not allocate memory.
  5673. /// It is 32 bytes in size, and contains all the memory it needs.
  5674. /// </summary>
  5675. [Serializable]
  5676. [StructLayout(LayoutKind.Explicit, Size=32)]
  5677. [Obsolete("FixedListFloat32 is deprecated, please use FixedList32Bytes<float> instead. (UnityUpgradable) -> FixedList32Bytes<float>", true)]
  5678. public struct FixedListFloat32 {}
  5679. [Obsolete("FixedListFloat32DebugView is deprecated. (UnityUpgradable) -> FixedList32BytesDebugView<float>", true)]
  5680. sealed class FixedListFloat32DebugView
  5681. {
  5682. FixedList32Bytes<float> m_List;
  5683. public FixedListFloat32DebugView(FixedList32Bytes<float> list)
  5684. {
  5685. m_List = list;
  5686. }
  5687. public float[] Items => m_List.ToArray();
  5688. }
  5689. /// <summary>
  5690. /// An unmanaged, resizable list of float that does not allocate memory.
  5691. /// It is 64 bytes in size, and contains all the memory it needs.
  5692. /// </summary>
  5693. [Serializable]
  5694. [StructLayout(LayoutKind.Explicit, Size=64)]
  5695. [Obsolete("FixedListFloat64 is deprecated, please use FixedList64Bytes<float> instead. (UnityUpgradable) -> FixedList64Bytes<float>", true)]
  5696. public struct FixedListFloat64 {}
  5697. [Obsolete("FixedListFloat64DebugView is deprecated. (UnityUpgradable) -> FixedList64BytesDebugView<float>", true)]
  5698. sealed class FixedListFloat64DebugView
  5699. {
  5700. FixedList64Bytes<float> m_List;
  5701. public FixedListFloat64DebugView(FixedList64Bytes<float> list)
  5702. {
  5703. m_List = list;
  5704. }
  5705. public float[] Items => m_List.ToArray();
  5706. }
  5707. /// <summary>
  5708. /// An unmanaged, resizable list of float that does not allocate memory.
  5709. /// It is 128 bytes in size, and contains all the memory it needs.
  5710. /// </summary>
  5711. [Serializable]
  5712. [StructLayout(LayoutKind.Explicit, Size=128)]
  5713. [Obsolete("FixedListFloat128 is deprecated, please use FixedList128Bytes<float> instead. (UnityUpgradable) -> FixedList128Bytes<float>", true)]
  5714. public struct FixedListFloat128 {}
  5715. [Obsolete("FixedListFloat128DebugView is deprecated. (UnityUpgradable) -> FixedList128BytesDebugView<float>", true)]
  5716. sealed class FixedListFloat128DebugView
  5717. {
  5718. FixedList128Bytes<float> m_List;
  5719. public FixedListFloat128DebugView(FixedList128Bytes<float> list)
  5720. {
  5721. m_List = list;
  5722. }
  5723. public float[] Items => m_List.ToArray();
  5724. }
  5725. /// <summary>
  5726. /// An unmanaged, resizable list of float that does not allocate memory.
  5727. /// It is 512 bytes in size, and contains all the memory it needs.
  5728. /// </summary>
  5729. [Serializable]
  5730. [StructLayout(LayoutKind.Explicit, Size=512)]
  5731. [Obsolete("FixedListFloat512 is deprecated, please use FixedList512Bytes<float> instead. (UnityUpgradable) -> FixedList512Bytes<float>", true)]
  5732. public struct FixedListFloat512 {}
  5733. [Obsolete("FixedListFloat512DebugView is deprecated. (UnityUpgradable) -> FixedList512BytesDebugView<float>", true)]
  5734. sealed class FixedListFloat512DebugView
  5735. {
  5736. FixedList512Bytes<float> m_List;
  5737. public FixedListFloat512DebugView(FixedList512Bytes<float> list)
  5738. {
  5739. m_List = list;
  5740. }
  5741. public float[] Items => m_List.ToArray();
  5742. }
  5743. /// <summary>
  5744. /// An unmanaged, resizable list of float that does not allocate memory.
  5745. /// It is 4096 bytes in size, and contains all the memory it needs.
  5746. /// </summary>
  5747. [Serializable]
  5748. [StructLayout(LayoutKind.Explicit, Size=4096)]
  5749. [Obsolete("FixedListFloat4096 is deprecated, please use FixedList4096Bytes<float> instead. (UnityUpgradable) -> FixedList4096Bytes<float>", true)]
  5750. public struct FixedListFloat4096 {}
  5751. [Obsolete("FixedListFloat4096DebugView is deprecated. (UnityUpgradable) -> FixedList4096BytesDebugView<float>", true)]
  5752. sealed class FixedListFloat4096DebugView
  5753. {
  5754. FixedList4096Bytes<float> m_List;
  5755. public FixedListFloat4096DebugView(FixedList4096Bytes<float> list)
  5756. {
  5757. m_List = list;
  5758. }
  5759. public float[] Items => m_List.ToArray();
  5760. }
  5761. /// <summary>
  5762. /// Provides extension methods for FixedList*N*.
  5763. /// </summary>
  5764. public static class FixedListExtensions
  5765. {
  5766. /// <summary>
  5767. /// Sorts the elements in this list in ascending order.
  5768. /// </summary>
  5769. /// <typeparam name="T">The type of the elements.</typeparam>
  5770. /// <param name="list">The list to sort.</param>
  5771. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  5772. public static void Sort<T>(this ref FixedList32Bytes<T> list)
  5773. where T : unmanaged, IComparable<T>
  5774. {
  5775. unsafe
  5776. {
  5777. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5778. {
  5779. var c = b + FixedList.PaddingBytes<T>();
  5780. NativeSortExtension.Sort((T*)c, list.Length);
  5781. }
  5782. }
  5783. }
  5784. /// <summary>
  5785. /// Sorts the elements in this list using a custom comparison.
  5786. /// </summary>
  5787. /// <typeparam name="T">The type of the elements.</typeparam>
  5788. /// <typeparam name="U">The type of the comparer.</typeparam>
  5789. /// <param name="list">The list to sort.</param>
  5790. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  5791. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  5792. public static void Sort<T, U>(this ref FixedList32Bytes<T> list, U comp)
  5793. where T : unmanaged, IComparable<T>
  5794. where U : IComparer<T>
  5795. {
  5796. unsafe
  5797. {
  5798. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5799. {
  5800. var c = b + FixedList.PaddingBytes<T>();
  5801. NativeSortExtension.Sort((T*)c, list.Length, comp);
  5802. }
  5803. }
  5804. }
  5805. /// <summary>
  5806. /// Sorts the elements in this list in ascending order.
  5807. /// </summary>
  5808. /// <typeparam name="T">The type of the elements.</typeparam>
  5809. /// <param name="list">The list to sort.</param>
  5810. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  5811. public static void Sort<T>(this ref FixedList64Bytes<T> list)
  5812. where T : unmanaged, IComparable<T>
  5813. {
  5814. unsafe
  5815. {
  5816. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5817. {
  5818. var c = b + FixedList.PaddingBytes<T>();
  5819. NativeSortExtension.Sort((T*)c, list.Length);
  5820. }
  5821. }
  5822. }
  5823. /// <summary>
  5824. /// Sorts the elements in this list using a custom comparison.
  5825. /// </summary>
  5826. /// <typeparam name="T">The type of the elements.</typeparam>
  5827. /// <typeparam name="U">The type of the comparer.</typeparam>
  5828. /// <param name="list">The list to sort.</param>
  5829. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  5830. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  5831. public static void Sort<T, U>(this ref FixedList64Bytes<T> list, U comp)
  5832. where T : unmanaged, IComparable<T>
  5833. where U : IComparer<T>
  5834. {
  5835. unsafe
  5836. {
  5837. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5838. {
  5839. var c = b + FixedList.PaddingBytes<T>();
  5840. NativeSortExtension.Sort((T*)c, list.Length, comp);
  5841. }
  5842. }
  5843. }
  5844. /// <summary>
  5845. /// Sorts the elements in this list in ascending order.
  5846. /// </summary>
  5847. /// <typeparam name="T">The type of the elements.</typeparam>
  5848. /// <param name="list">The list to sort.</param>
  5849. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  5850. public static void Sort<T>(this ref FixedList128Bytes<T> list)
  5851. where T : unmanaged, IComparable<T>
  5852. {
  5853. unsafe
  5854. {
  5855. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5856. {
  5857. var c = b + FixedList.PaddingBytes<T>();
  5858. NativeSortExtension.Sort((T*)c, list.Length);
  5859. }
  5860. }
  5861. }
  5862. /// <summary>
  5863. /// Sorts the elements in this list using a custom comparison.
  5864. /// </summary>
  5865. /// <typeparam name="T">The type of the elements.</typeparam>
  5866. /// <typeparam name="U">The type of the comparer.</typeparam>
  5867. /// <param name="list">The list to sort.</param>
  5868. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  5869. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  5870. public static void Sort<T, U>(this ref FixedList128Bytes<T> list, U comp)
  5871. where T : unmanaged, IComparable<T>
  5872. where U : IComparer<T>
  5873. {
  5874. unsafe
  5875. {
  5876. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5877. {
  5878. var c = b + FixedList.PaddingBytes<T>();
  5879. NativeSortExtension.Sort((T*)c, list.Length, comp);
  5880. }
  5881. }
  5882. }
  5883. /// <summary>
  5884. /// Sorts the elements in this list in ascending order.
  5885. /// </summary>
  5886. /// <typeparam name="T">The type of the elements.</typeparam>
  5887. /// <param name="list">The list to sort.</param>
  5888. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  5889. public static void Sort<T>(this ref FixedList512Bytes<T> list)
  5890. where T : unmanaged, IComparable<T>
  5891. {
  5892. unsafe
  5893. {
  5894. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5895. {
  5896. var c = b + FixedList.PaddingBytes<T>();
  5897. NativeSortExtension.Sort((T*)c, list.Length);
  5898. }
  5899. }
  5900. }
  5901. /// <summary>
  5902. /// Sorts the elements in this list using a custom comparison.
  5903. /// </summary>
  5904. /// <typeparam name="T">The type of the elements.</typeparam>
  5905. /// <typeparam name="U">The type of the comparer.</typeparam>
  5906. /// <param name="list">The list to sort.</param>
  5907. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  5908. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  5909. public static void Sort<T, U>(this ref FixedList512Bytes<T> list, U comp)
  5910. where T : unmanaged, IComparable<T>
  5911. where U : IComparer<T>
  5912. {
  5913. unsafe
  5914. {
  5915. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5916. {
  5917. var c = b + FixedList.PaddingBytes<T>();
  5918. NativeSortExtension.Sort((T*)c, list.Length, comp);
  5919. }
  5920. }
  5921. }
  5922. /// <summary>
  5923. /// Sorts the elements in this list in ascending order.
  5924. /// </summary>
  5925. /// <typeparam name="T">The type of the elements.</typeparam>
  5926. /// <param name="list">The list to sort.</param>
  5927. [BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
  5928. public static void Sort<T>(this ref FixedList4096Bytes<T> list)
  5929. where T : unmanaged, IComparable<T>
  5930. {
  5931. unsafe
  5932. {
  5933. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5934. {
  5935. var c = b + FixedList.PaddingBytes<T>();
  5936. NativeSortExtension.Sort((T*)c, list.Length);
  5937. }
  5938. }
  5939. }
  5940. /// <summary>
  5941. /// Sorts the elements in this list using a custom comparison.
  5942. /// </summary>
  5943. /// <typeparam name="T">The type of the elements.</typeparam>
  5944. /// <typeparam name="U">The type of the comparer.</typeparam>
  5945. /// <param name="list">The list to sort.</param>
  5946. /// <param name="comp">The comparison function used to determine the relative order of the elements.</param>
  5947. [BurstCompatible(GenericTypeArguments = new [] { typeof(int), typeof(NativeSortExtension.DefaultComparer<int>) })]
  5948. public static void Sort<T, U>(this ref FixedList4096Bytes<T> list, U comp)
  5949. where T : unmanaged, IComparable<T>
  5950. where U : IComparer<T>
  5951. {
  5952. unsafe
  5953. {
  5954. fixed(byte* b = &list.buffer.offset0000.byte0000)
  5955. {
  5956. var c = b + FixedList.PaddingBytes<T>();
  5957. NativeSortExtension.Sort((T*)c, list.Length, comp);
  5958. }
  5959. }
  5960. }
  5961. }
  5962. }