暫無描述
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 272KB

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