| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671 | /** * The array module provides array manipulation routines in a manner that * balances performance and flexibility. Operations are provided for sorting, * and for processing both sorted and unsorted arrays. * * Copyright: Copyright (C) 2005-2006 Sean Kelly. All rights reserved. * License: BSD style: $(LICENSE) * Authors: Sean Kelly */ module tango.core.Array; private import tango.core.Traits; private import tango.stdc.stdlib : alloca, rand; version( TangoDoc ) { typedef int Num; typedef int Elem; typedef int Elem2; typedef bool function( Elem ) Pred1E; typedef bool function( Elem, Elem ) Pred2E; typedef Elem2 function( Elem, Elem ) Map2E; typedef Elem function( Elem, Elem ) Reduce2E; typedef size_t function( size_t ) Oper1A; } private { struct IsEqual( T ) { static bool opCall( T p1, T p2 ) { // TODO: Fix this if/when opEquals is changed to return a bool. static if( is( T == class ) || is( T == struct ) ) return (p1 == p2) != 0; else return p1 == p2; } } struct IsLess( T ) { static bool opCall( T p1, T p2 ) { return p1 < p2; } } struct RandOper() { static size_t opCall( size_t lim ) { // NOTE: The use of 'max' here is intended to eliminate modulo bias // in this routine. size_t max = size_t.max - (size_t.max % lim); size_t val; do { static if( size_t.sizeof == 4 ) { val = (((cast(size_t)rand()) << 16) & 0xffff0000u) | (((cast(size_t)rand())) & 0x0000ffffu); } else // assume size_t.sizeof == 8 { val = (((cast(size_t)rand()) << 48) & 0xffff000000000000uL) | (((cast(size_t)rand()) << 32) & 0x0000ffff00000000uL) | (((cast(size_t)rand()) << 16) & 0x00000000ffff0000uL) | (((cast(size_t)rand())) & 0x000000000000ffffuL); } } while( val > max ); return val % lim; } } template ElemTypeOf( T ) { alias typeof(T[0]) ElemTypeOf; } } //////////////////////////////////////////////////////////////////////////////// // Find //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t find( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t find( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); } else { template find_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { foreach( size_t pos, Elem cur; buf ) { if( pred( cur, pat ) ) return pos; } return buf.length; } size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) { if( buf.length == 0 || pat.length == 0 || buf.length < pat.length ) { return buf.length; } size_t end = buf.length - pat.length + 1; for( size_t pos = 0; pos < end; ++pos ) { if( pred( buf[pos], pat[0] ) ) { size_t mat = 0; do { if( ++mat >= pat.length ) return pos - pat.length + 1; if( ++pos >= buf.length ) return buf.length; } while( pred( buf[pos], pat[mat] ) ); pos -= mat; } } return buf.length; } } template find( Buf, Pat ) { size_t find( Buf buf, Pat pat ) { return find_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template find( Buf, Pat, Pred ) { size_t find( Buf buf, Pat pat, Pred pred ) { return find_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { // find element assert( find( "", 'a' ) == 0 ); assert( find( "abc", 'a' ) == 0 ); assert( find( "abc", 'b' ) == 1 ); assert( find( "abc", 'c' ) == 2 ); assert( find( "abc", 'd' ) == 3 ); // null parameters assert( find( "", "" ) == 0 ); assert( find( "a", "" ) == 1 ); assert( find( "", "a" ) == 0 ); // exact match assert( find( "abc", "abc" ) == 0 ); // simple substring match assert( find( "abc", "a" ) == 0 ); assert( find( "abca", "a" ) == 0 ); assert( find( "abc", "b" ) == 1 ); assert( find( "abc", "c" ) == 2 ); assert( find( "abc", "d" ) == 3 ); // multi-char substring match assert( find( "abc", "ab" ) == 0 ); assert( find( "abcab", "ab" ) == 0 ); assert( find( "abc", "bc" ) == 1 ); assert( find( "abc", "ac" ) == 3 ); assert( find( "abrabracadabra", "abracadabra" ) == 3 ); } } } //////////////////////////////////////////////////////////////////////////////// // Reverse Find //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from $(LP)buf.length .. 0], returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t rfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from $(LP)buf.length .. 0], returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t rfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); } else { template rfind_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { if( buf.length == 0 ) return buf.length; size_t pos = buf.length; do { if( pred( buf[--pos], pat ) ) return pos; } while( pos > 0 ); return buf.length; } size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) { if( buf.length == 0 || pat.length == 0 || buf.length < pat.length ) { return buf.length; } size_t pos = buf.length - pat.length + 1; do { if( pred( buf[--pos], pat[0] ) ) { size_t mat = 0; do { if( ++mat >= pat.length ) return pos - pat.length + 1; if( ++pos >= buf.length ) return buf.length; } while( pred( buf[pos], pat[mat] ) ); pos -= mat; } } while( pos > 0 ); return buf.length; } } template rfind( Buf, Pat ) { size_t rfind( Buf buf, Pat pat ) { return rfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template rfind( Buf, Pat, Pred ) { size_t rfind( Buf buf, Pat pat, Pred pred ) { return rfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { // rfind element assert( rfind( "", 'a' ) == 0 ); assert( rfind( "abc", 'a' ) == 0 ); assert( rfind( "abc", 'b' ) == 1 ); assert( rfind( "abc", 'c' ) == 2 ); assert( rfind( "abc", 'd' ) == 3 ); // null parameters assert( rfind( "", "" ) == 0 ); assert( rfind( "a", "" ) == 1 ); assert( rfind( "", "a" ) == 0 ); // exact match assert( rfind( "abc", "abc" ) == 0 ); // simple substring match assert( rfind( "abc", "a" ) == 0 ); assert( rfind( "abca", "a" ) == 3 ); assert( rfind( "abc", "b" ) == 1 ); assert( rfind( "abc", "c" ) == 2 ); assert( rfind( "abc", "d" ) == 3 ); // multi-char substring match assert( rfind( "abc", "ab" ) == 0 ); assert( rfind( "abcab", "ab" ) == 3 ); assert( rfind( "abc", "bc" ) == 1 ); assert( rfind( "abc", "ac" ) == 3 ); assert( rfind( "abracadabrabra", "abracadabra" ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // KMP Find //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * This function uses the KMP algorithm and offers O(M+N) performance but * must allocate a temporary buffer of size pat.sizeof to do so. If it is * available on the target system, alloca will be used for the allocation, * otherwise a standard dynamic memory allocation will occur. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t kfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * This function uses the KMP algorithm and offers O(M+N) performance but * must allocate a temporary buffer of size pat.sizeof to do so. If it is * available on the target system, alloca will be used for the allocation, * otherwise a standard dynamic memory allocation will occur. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t kfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); } else { template kfind_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { foreach( size_t pos, Elem cur; buf ) { if( pred( cur, pat ) ) return pos; } return buf.length; } size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) { if( buf.length == 0 || pat.length == 0 || buf.length < pat.length ) { return buf.length; } static if( is( alloca ) ) // always false, alloca usage should be rethought { size_t[] func = (cast(size_t*) alloca( (pat.length + 1) * size_t.sizeof ))[0 .. pat.length + 1]; } else { size_t[] func = new size_t[pat.length + 1]; scope( exit ) delete func; // force cleanup } func[0] = 0; // // building prefix-function // for( size_t m = 0, i = 1 ; i < pat.length ; ++i ) { while( ( m > 0 ) && !pred( pat[m], pat[i] ) ) m = func[m - 1]; if( pred( pat[m], pat[i] ) ) ++m; func[i] = m; } // // searching // for( size_t m = 0, i = 0; i < buf.length; ++i ) { while( ( m > 0 ) && !pred( pat[m], buf[i] ) ) m = func[m - 1]; if( pred( pat[m], buf[i] ) ) { ++m; if( m == pat.length ) { return i - pat.length + 1; } } } return buf.length; } } template kfind( Buf, Pat ) { size_t kfind( Buf buf, Pat pat ) { return kfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template kfind( Buf, Pat, Pred ) { size_t kfind( Buf buf, Pat pat, Pred pred ) { return kfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { // find element assert( kfind( "", 'a' ) == 0 ); assert( kfind( "abc", 'a' ) == 0 ); assert( kfind( "abc", 'b' ) == 1 ); assert( kfind( "abc", 'c' ) == 2 ); assert( kfind( "abc", 'd' ) == 3 ); // null parameters assert( kfind( "", "" ) == 0 ); assert( kfind( "a", "" ) == 1 ); assert( kfind( "", "a" ) == 0 ); // exact match assert( kfind( "abc", "abc" ) == 0 ); // simple substring match assert( kfind( "abc", "a" ) == 0 ); assert( kfind( "abca", "a" ) == 0 ); assert( kfind( "abc", "b" ) == 1 ); assert( kfind( "abc", "c" ) == 2 ); assert( kfind( "abc", "d" ) == 3 ); // multi-char substring match assert( kfind( "abc", "ab" ) == 0 ); assert( kfind( "abcab", "ab" ) == 0 ); assert( kfind( "abc", "bc" ) == 1 ); assert( kfind( "abc", "ac" ) == 3 ); assert( kfind( "abrabracadabra", "abracadabra" ) == 3 ); } } } //////////////////////////////////////////////////////////////////////////////// // KMP Reverse Find //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from $(LP)buf.length .. 0], returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * This function uses the KMP algorithm and offers O(M+N) performance but * must allocate a temporary buffer of size pat.sizeof to do so. If it is * available on the target system, alloca will be used for the allocation, * otherwise a standard dynamic memory allocation will occur. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t krfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from $(LP)buf.length .. 0], returning * the index of the first element matching pat, or buf.length if no match * was found. Comparisons will be performed using the supplied predicate * or '==' if none is supplied. * * This function uses the KMP algorithm and offers O(M+N) performance but * must allocate a temporary buffer of size pat.sizeof to do so. If it is * available on the target system, alloca will be used for the allocation, * otherwise a standard dynamic memory allocation will occur. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t krfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); } else { template krfind_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { if( buf.length == 0 ) return buf.length; size_t pos = buf.length; do { if( pred( buf[--pos], pat ) ) return pos; } while( pos > 0 ); return buf.length; } size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) { if( buf.length == 0 || pat.length == 0 || buf.length < pat.length ) { return buf.length; } static if( is( alloca ) ) // always false, alloca usage should be rethought { size_t[] func = (cast(size_t*) alloca( (pat.length + 1) * size_t.sizeof ))[0 .. pat.length + 1]; } else { size_t[] func = new size_t[pat.length + 1]; scope( exit ) delete func; // force cleanup } func[$ - 1] = 0; // // building prefix-function // for( size_t m = 0, i = pat.length - 1; i > 0; --i ) { while( ( m > 0 ) && !pred( pat[length - m - 1], pat[i - 1] ) ) m = func[length - m]; if( pred( pat[length - m - 1], pat[i - 1] ) ) ++m; func[i - 1] = m; } // // searching // size_t m = 0; size_t i = buf.length; do { --i; while( ( m > 0 ) && !pred( pat[length - m - 1], buf[i] ) ) m = func[length - m - 1]; if( pred( pat[length - m - 1], buf[i] ) ) { ++m; if ( m == pat.length ) { return i; } } } while( i > 0 ); return buf.length; } } template krfind( Buf, Pat ) { size_t krfind( Buf buf, Pat pat ) { return krfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template krfind( Buf, Pat, Pred ) { size_t krfind( Buf buf, Pat pat, Pred pred ) { return krfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { // rfind element assert( krfind( "", 'a' ) == 0 ); assert( krfind( "abc", 'a' ) == 0 ); assert( krfind( "abc", 'b' ) == 1 ); assert( krfind( "abc", 'c' ) == 2 ); assert( krfind( "abc", 'd' ) == 3 ); // null parameters assert( krfind( "", "" ) == 0 ); assert( krfind( "a", "" ) == 1 ); assert( krfind( "", "a" ) == 0 ); // exact match assert( krfind( "abc", "abc" ) == 0 ); // simple substring match assert( krfind( "abc", "a" ) == 0 ); assert( krfind( "abca", "a" ) == 3 ); assert( krfind( "abc", "b" ) == 1 ); assert( krfind( "abc", "c" ) == 2 ); assert( krfind( "abc", "d" ) == 3 ); // multi-char substring match assert( krfind( "abc", "ab" ) == 0 ); assert( krfind( "abcab", "ab" ) == 3 ); assert( krfind( "abc", "bc" ) == 1 ); assert( krfind( "abc", "ac" ) == 3 ); assert( krfind( "abracadabrabra", "abracadabra" ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // Find-If //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element where pred returns true. * * Params: * buf = The array to search. * pred = The evaluation predicate, which should return true if the * element is a valid match and false if not. This predicate * may be any callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t findIf( Elem[] buf, Pred1E pred ); } else { template findIf_( Elem, Pred ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred ) { foreach( size_t pos, Elem cur; buf ) { if( pred( cur ) ) return pos; } return buf.length; } } template findIf( Buf, Pred ) { size_t findIf( Buf buf, Pred pred ) { return findIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { assert( findIf( "bcecg", ( char c ) { return c == 'a'; } ) == 5 ); assert( findIf( "bcecg", ( char c ) { return c == 'b'; } ) == 0 ); assert( findIf( "bcecg", ( char c ) { return c == 'c'; } ) == 1 ); assert( findIf( "bcecg", ( char c ) { return c == 'd'; } ) == 5 ); assert( findIf( "bcecg", ( char c ) { return c == 'g'; } ) == 4 ); assert( findIf( "bcecg", ( char c ) { return c == 'h'; } ) == 5 ); } } } //////////////////////////////////////////////////////////////////////////////// // Reverse Find-If //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from $(LP)buf.length .. 0], returning * the index of the first element where pred returns true. * * Params: * buf = The array to search. * pred = The evaluation predicate, which should return true if the * element is a valid match and false if not. This predicate * may be any callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t rfindIf( Elem[] buf, Pred1E pred ); } else { template rfindIf_( Elem, Pred ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred ) { if( buf.length == 0 ) return buf.length; size_t pos = buf.length; do { if( pred( buf[--pos] ) ) return pos; } while( pos > 0 ); return buf.length; } } template rfindIf( Buf, Pred ) { size_t rfindIf( Buf buf, Pred pred ) { return rfindIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { assert( rfindIf( "bcecg", ( char c ) { return c == 'a'; } ) == 5 ); assert( rfindIf( "bcecg", ( char c ) { return c == 'b'; } ) == 0 ); assert( rfindIf( "bcecg", ( char c ) { return c == 'c'; } ) == 3 ); assert( rfindIf( "bcecg", ( char c ) { return c == 'd'; } ) == 5 ); assert( rfindIf( "bcecg", ( char c ) { return c == 'g'; } ) == 4 ); assert( rfindIf( "bcecg", ( char c ) { return c == 'h'; } ) == 5 ); } } } //////////////////////////////////////////////////////////////////////////////// // Find Adjacent //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * the index of the first element that compares equal to the next element * in the sequence. Comparisons will be performed using the supplied * predicate or '==' if none is supplied. * * Params: * buf = The array to scan. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t findAdj( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); } else { template findAdj_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred = Pred.init ) { if( buf.length < 2 ) return buf.length; Elem sav = buf[0]; foreach( size_t pos, Elem cur; buf[1 .. $] ) { if( pred( cur, sav ) ) return pos; sav = cur; } return buf.length; } } template findAdj( Buf ) { size_t findAdj( Buf buf ) { return findAdj_!(ElemTypeOf!(Buf)).fn( buf ); } } template findAdj( Buf, Pred ) { size_t findAdj( Buf buf, Pred pred ) { return findAdj_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { assert( findAdj( "aabcdef" ) == 0 ); assert( findAdj( "abcddef" ) == 3 ); assert( findAdj( "abcdeff" ) == 5 ); assert( findAdj( "abcdefg" ) == 7 ); } } } //////////////////////////////////////////////////////////////////////////////// // Contains //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * true if an element matching pat is found. Comparisons will be performed * using the supplied predicate or '<' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * True if an element equivalent to pat is found, false if not. */ equals_t contains( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * true if a sequence matching pat is found. Comparisons will be performed * using the supplied predicate or '<' if none is supplied. * * Params: * buf = The array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * True if an element equivalent to pat is found, false if not. */ equals_t contains( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); } else { template contains( Buf, Pat ) { equals_t contains( Buf buf, Pat pat ) { return cast(equals_t)(find( buf, pat ) != buf.length); } } template contains( Buf, Pat, Pred ) { equals_t contains( Buf buf, Pat pat, Pred pred ) { return cast(equals_t)(find( buf, pat, pred ) != buf.length); } } debug( UnitTest ) { unittest { // find element assert( !contains( "", 'a' ) ); assert( contains( "abc", 'a' ) ); assert( contains( "abc", 'b' ) ); assert( contains( "abc", 'c' ) ); assert( !contains( "abc", 'd' ) ); // null parameters assert( !contains( "", "" ) ); assert( !contains( "a", "" ) ); assert( !contains( "", "a" ) ); // exact match assert( contains( "abc", "abc" ) ); // simple substring match assert( contains( "abc", "a" ) ); assert( contains( "abca", "a" ) ); assert( contains( "abc", "b" ) ); assert( contains( "abc", "c" ) ); assert( !contains( "abc", "d" ) ); // multi-char substring match assert( contains( "abc", "ab" ) ); assert( contains( "abcab", "ab" ) ); assert( contains( "abc", "bc" ) ); assert( !contains( "abc", "ac" ) ); assert( contains( "abrabracadabra", "abracadabra" ) ); } } } //////////////////////////////////////////////////////////////////////////////// // Mismatch //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a parallel linear scan of bufA and bufB from [0 .. N$(RP) * where N = min(bufA.length, bufB.length), returning the index of * the first element in bufA which does not match the corresponding element * in bufB or N if no mismatch occurs. Comparisons will be performed using * the supplied predicate or '==' if none is supplied. * * Params: * bufA = The array to evaluate. * bufB = The array to match against. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first mismatch or N if the first N elements of bufA * and bufB match, where N = min$(LP)bufA.length, bufB.length$(RP). */ size_t mismatch( Elem[] bufA, Elem[] bufB, Pred2E pred = Pred2E.init ); } else { template mismatch_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] bufA, Elem[] bufB, Pred pred = Pred.init ) { size_t posA = 0, posB = 0; while( posA < bufA.length && posB < bufB.length ) { if( !pred( bufB[posB], bufA[posA] ) ) break; ++posA, ++posB; } return posA; } } template mismatch( BufA, BufB ) { size_t mismatch( BufA bufA, BufB bufB ) { return mismatch_!(ElemTypeOf!(BufA)).fn( bufA, bufB ); } } template mismatch( BufA, BufB, Pred ) { size_t mismatch( BufA bufA, BufB bufB, Pred pred ) { return mismatch_!(ElemTypeOf!(BufA), Pred).fn( bufA, bufB, pred ); } } debug( UnitTest ) { unittest { assert( mismatch( "a", "abcdefg" ) == 1 ); assert( mismatch( "abcdefg", "a" ) == 1 ); assert( mismatch( "x", "abcdefg" ) == 0 ); assert( mismatch( "abcdefg", "x" ) == 0 ); assert( mismatch( "xbcdefg", "abcdefg" ) == 0 ); assert( mismatch( "abcdefg", "xbcdefg" ) == 0 ); assert( mismatch( "abcxefg", "abcdefg" ) == 3 ); assert( mismatch( "abcdefg", "abcxefg" ) == 3 ); assert( mismatch( "abcdefx", "abcdefg" ) == 6 ); assert( mismatch( "abcdefg", "abcdefx" ) == 6 ); } } } //////////////////////////////////////////////////////////////////////////////// // Count //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * a count of the number of elements matching pat. Comparisons will be * performed using the supplied predicate or '==' if none is supplied. * * Params: * buf = The array to scan. * pat = The pattern to match. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The number of elements matching pat. */ size_t count( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); } else { template count_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { size_t cnt = 0; foreach( size_t pos, Elem cur; buf ) { if( pred( cur, pat ) ) ++cnt; } return cnt; } } template count( Buf, Pat ) { size_t count( Buf buf, Pat pat ) { return count_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template count( Buf, Pat, Pred ) { size_t count( Buf buf, Pat pat, Pred pred ) { return count_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { assert( count( "gbbbi", 'a' ) == 0 ); assert( count( "gbbbi", 'g' ) == 1 ); assert( count( "gbbbi", 'b' ) == 3 ); assert( count( "gbbbi", 'i' ) == 1 ); assert( count( "gbbbi", 'd' ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // Count-If //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), returning * a count of the number of elements where pred returns true. * * Params: * buf = The array to scan. * pred = The evaluation predicate, which should return true if the * element is a valid match and false if not. This predicate * may be any callable type. * * Returns: * The number of elements where pred returns true. */ size_t countIf( Elem[] buf, Pred1E pred = Pred1E.init ); } else { template countIf_( Elem, Pred ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred ) { size_t cnt = 0; foreach( size_t pos, Elem cur; buf ) { if( pred( cur ) ) ++cnt; } return cnt; } } template countIf( Buf, Pred ) { size_t countIf( Buf buf, Pred pred ) { return countIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { assert( countIf( "gbbbi", ( char c ) { return c == 'a'; } ) == 0 ); assert( countIf( "gbbbi", ( char c ) { return c == 'g'; } ) == 1 ); assert( countIf( "gbbbi", ( char c ) { return c == 'b'; } ) == 3 ); assert( countIf( "gbbbi", ( char c ) { return c == 'i'; } ) == 1 ); assert( countIf( "gbbbi", ( char c ) { return c == 'd'; } ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // Replace //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), replacing * occurrences of pat with val. Comparisons will be performed using the * supplied predicate or '==' if none is supplied. * * Params: * buf = The array to scan. * pat = The pattern to match. * val = The value to substitute. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The number of elements replaced. */ size_t replace( Elem[] buf, Elem pat, Elem val, Pred2E pred = Pred2E.init ); } else { template replace_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Elem val, Pred pred = Pred.init ) { size_t cnt = 0; foreach( size_t pos, ref Elem cur; buf ) { if( pred( cur, pat ) ) { cur = val; ++cnt; } } return cnt; } } template replace( Buf, Elem ) { size_t replace( Buf buf, Elem pat, Elem val ) { return replace_!(ElemTypeOf!(Buf)).fn( buf, pat, val ); } } template replace( Buf, Elem, Pred ) { size_t replace( Buf buf, Elem pat, Elem val, Pred pred ) { return replace_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, val, pred ); } } debug( UnitTest ) { unittest { assert( replace( "gbbbi".dup, 'a', 'b' ) == 0 ); assert( replace( "gbbbi".dup, 'g', 'h' ) == 1 ); assert( replace( "gbbbi".dup, 'b', 'c' ) == 3 ); assert( replace( "gbbbi".dup, 'i', 'j' ) == 1 ); assert( replace( "gbbbi".dup, 'd', 'e' ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // Replace-If //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), replacing * elements where pred returns true with val. * * Params: * buf = The array to scan. * val = The value to substitute. * pred = The evaluation predicate, which should return true if the * element is a valid match and false if not. This predicate * may be any callable type. * * Returns: * The number of elements replaced. */ size_t replaceIf( Elem[] buf, Elem val, Pred2E pred = Pred2E.init ); } else { template replaceIf_( Elem, Pred ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem val, Pred pred ) { size_t cnt = 0; foreach( size_t pos, ref Elem cur; buf ) { if( pred( cur ) ) { cur = val; ++cnt; } } return cnt; } } template replaceIf( Buf, Elem, Pred ) { size_t replaceIf( Buf buf, Elem val, Pred pred ) { return replaceIf_!(ElemTypeOf!(Buf), Pred).fn( buf, val, pred ); } } debug( UnitTest ) { unittest { assert( replaceIf( "gbbbi".dup, 'b', ( char c ) { return c == 'a'; } ) == 0 ); assert( replaceIf( "gbbbi".dup, 'h', ( char c ) { return c == 'g'; } ) == 1 ); assert( replaceIf( "gbbbi".dup, 'c', ( char c ) { return c == 'b'; } ) == 3 ); assert( replaceIf( "gbbbi".dup, 'j', ( char c ) { return c == 'i'; } ) == 1 ); assert( replaceIf( "gbbbi".dup, 'e', ( char c ) { return c == 'd'; } ) == 0 ); } } } //////////////////////////////////////////////////////////////////////////////// // Remove //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), moving all * elements matching pat to the end of the sequence. The relative order of * elements not matching pat will be preserved. Comparisons will be * performed using the supplied predicate or '==' if none is supplied. * * Params: * buf = The array to scan. This parameter is not marked 'ref' * to allow temporary slices to be modified. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on the * result of this operation, even though it may be viewed as a * side-effect. * pat = The pattern to match against. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The number of elements that do not match pat. */ size_t remove( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); /** * Performs a linear scan of buf from [0 .. buf.length$(RP), moving all * elements matching pat to the end of the sequence. The relative order of * elements not matching pat will be preserved. Comparisons will be * performed '=='. * * Params: * buf = The array to scan. This parameter is not marked 'ref' * to allow temporary slices to be modified. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on the * result of this operation, even though it may be viewed as a * side-effect. * pat = The pattern to match against. * * Returns: * The number of elements that do not match pat. */ size_t remove( Elem[] buf, Elem pat ); } else { template remove_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } size_t cnt = 0; for( size_t pos = 0, len = buf.length; pos < len; ++pos ) { if( pred( buf[pos], pat ) ) ++cnt; else exch( pos, pos - cnt ); } return buf.length - cnt; } } template remove( Buf, Pat ) { size_t remove( Buf buf, Pat pat ) { return remove_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template remove( Buf, Pat, Pred ) { size_t remove( Buf buf, Pat pat, Pred pred ) { return remove_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { void test( char[] buf, char pat, size_t num ) { assert( remove( buf, pat ) == num ); foreach( pos, cur; buf ) { assert( pos < num ? cur != pat : cur == pat ); } } test( "abcdefghij".dup, 'x', 10 ); test( "xabcdefghi".dup, 'x', 9 ); test( "abcdefghix".dup, 'x', 9 ); test( "abxxcdefgh".dup, 'x', 8 ); test( "xaxbcdxxex".dup, 'x', 5 ); } } } //////////////////////////////////////////////////////////////////////////////// // Remove-If //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), moving all * elements that satisfy pred to the end of the sequence. The relative * order of elements that do not satisfy pred will be preserved. * * Params: * buf = The array to scan. This parameter is not marked 'ref' * to allow temporary slices to be modified. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on the * result of this operation, even though it may be viewed as a * side-effect. * pred = The evaluation predicate, which should return true if the * element satisfies the condition and false if not. This * predicate may be any callable type. * * Returns: * The number of elements that do not satisfy pred. */ size_t removeIf( Elem[] buf, Pred1E pred ); } else { template removeIf_( Elem, Pred ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } size_t cnt = 0; for( size_t pos = 0, len = buf.length; pos < len; ++pos ) { if( pred( buf[pos] ) ) ++cnt; else exch( pos, pos - cnt ); } return buf.length - cnt; } } template removeIf( Buf, Pred ) { size_t removeIf( Buf buf, Pred pred ) { return removeIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { void test( char[] buf, bool delegate( char ) dg, size_t num ) { assert( removeIf( buf, dg ) == num ); foreach( pos, cur; buf ) { assert( pos < num ? !dg( cur ) : dg( cur ) ); } } test( "abcdefghij".dup, ( char c ) { return c == 'x'; }, 10 ); test( "xabcdefghi".dup, ( char c ) { return c == 'x'; }, 9 ); test( "abcdefghix".dup, ( char c ) { return c == 'x'; }, 9 ); test( "abxxcdefgh".dup, ( char c ) { return c == 'x'; }, 8 ); test( "xaxbcdxxex".dup, ( char c ) { return c == 'x'; }, 5 ); } } } //////////////////////////////////////////////////////////////////////////////// // Unique //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [0 .. buf.length$(RP), moving all * but the first element of each consecutive group of duplicate elements to * the end of the sequence. The relative order of all remaining elements * will be preserved. Comparisons will be performed using the supplied * predicate or '==' if none is supplied. * * Params: * buf = The array to scan. This parameter is not marked 'ref' * to allow temporary slices to be modified. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on the * result of this operation, even though it may be viewed as a * side-effect. * pred = The evaluation predicate, which should return true if e1 is * equal to e2 and false if not. This predicate may be any * callable type. * * Returns: * The number of distinct sub-sequences in buf. */ size_t distinct( Elem[] buf, Pred2E pred = Pred2E.init ); } else { template distinct_( Elem, Pred = IsEqual!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Pred pred = Pred.init ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } if( buf.length < 2 ) return buf.length; size_t cnt = 0; Elem pat = buf[0]; for( size_t pos = 1, len = buf.length; pos < len; ++pos ) { if( pred( buf[pos], pat ) ) ++cnt; else { pat = buf[pos]; exch( pos, pos - cnt ); } } return buf.length - cnt; } } template distinct( Buf ) { size_t distinct( Buf buf ) { return distinct_!(ElemTypeOf!(Buf)).fn( buf ); } } template distinct( Buf, Pred ) { size_t distinct( Buf buf, Pred pred ) { return distinct_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { void test( char[] buf, char[] pat ) { assert( distinct( buf ) == pat.length ); foreach( pos, cur; pat ) { assert( buf[pos] == cur ); } } test( "abcdefghij".dup, "abcdefghij" ); test( "aabcdefghi".dup, "abcdefghi" ); test( "bcdefghijj".dup, "bcdefghij" ); test( "abccdefghi".dup, "abcdefghi" ); test( "abccdddefg".dup, "abcdefg" ); } } } //////////////////////////////////////////////////////////////////////////////// // Shuffle //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a linear scan of buf from [2 .. buf.length$(RP), exchanging * each element with an element in the range [0 .. pos$(RP), where pos * represents the current array position. * * Params: * buf = The array to shuffle. * oper = The randomize operation, which should return a number in the * range [0 .. N$(RP) for any supplied value N. This routine * may be any callable type. */ void shuffle( Elem[] buf, Oper1A oper = Oper1A.init ); } else { template shuffle_( Elem, Oper ) { static assert( isCallableType!(Oper) ); void fn( Elem[] buf, Oper oper ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } for( size_t pos = buf.length - 1; pos > 0; --pos ) { exch( pos, oper( pos + 1 ) ); } } } template shuffle( Buf, Oper = RandOper!() ) { void shuffle( Buf buf, Oper oper = Oper.init ) { return shuffle_!(ElemTypeOf!(Buf), Oper).fn( buf, oper ); } } debug( UnitTest ) { unittest { char[] buf = "abcdefghijklmnopqrstuvwxyz"; char[] tmp = buf.dup; assert( tmp == buf ); shuffle( tmp ); assert( tmp != buf ); } } } //////////////////////////////////////////////////////////////////////////////// // Partition //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Partitions buf such that all elements that satisfy pred will be placed * before the elements that do not satisfy pred. The algorithm is not * required to be stable. * * Params: * buf = The array to partition. This parameter is not marked 'ref' * to allow temporary slices to be sorted. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on * the result of this operation, even though it may be viewed * as a side-effect. * pred = The evaluation predicate, which should return true if the * element satisfies the condition and false if not. This * predicate may be any callable type. * * Returns: * The number of elements that satisfy pred. */ size_t partition( Elem[] buf, Pred1E pred ); } else { template partition_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); size_t fn( Elem[] buf, Pred pred ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } if( buf.length < 2 ) return 0; size_t l = 0, r = buf.length, i = l, j = r - 1; while( true ) { while( i < r && pred( buf[i] ) ) ++i; while( j > l && !pred( buf[j] ) ) --j; if( i >= j ) break; exch( i++, j-- ); } return i; } } template partition( Buf, Pred ) { size_t partition( Buf buf, Pred pred ) { return partition_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { void test( char[] buf, bool delegate( char ) dg, size_t num ) { assert( partition( buf, dg ) == num ); for( size_t pos = 0; pos < buf.length; ++pos ) { assert( pos < num ? dg( buf[pos] ) : !dg( buf[pos] ) ); } } test( "abcdefg".dup, ( char c ) { return c < 'a'; }, 0 ); test( "gfedcba".dup, ( char c ) { return c < 'a'; }, 0 ); test( "abcdefg".dup, ( char c ) { return c < 'h'; }, 7 ); test( "gfedcba".dup, ( char c ) { return c < 'h'; }, 7 ); test( "abcdefg".dup, ( char c ) { return c < 'd'; }, 3 ); test( "gfedcba".dup, ( char c ) { return c < 'd'; }, 3 ); test( "bbdaabc".dup, ( char c ) { return c < 'c'; }, 5 ); } } } //////////////////////////////////////////////////////////////////////////////// // Select //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Partitions buf with num - 1 as a pivot such that the first num elements * will be less than or equal to the remaining elements in the array. * Comparisons will be performed using the supplied predicate or '<' if * none is supplied. The algorithm is not required to be stable. * * Params: * buf = The array to partition. This parameter is not marked 'ref' * to allow temporary slices to be sorted. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on * the result of this operation, even though it may be viewed * as a side-effect. * num = The number of elements which are considered significant in * this array, where num - 1 is the pivot around which partial * sorting will occur. For example, if num is buf.length / 2 * then select will effectively partition the array around its * median value, with the elements in the first half of the array * evaluating as less than or equal to the elements in the second * half. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the pivot point, which will be the lesser of num - 1 and * buf.length. */ size_t select( Elem[] buf, Num num, Pred2E pred = Pred2E.init ); } else { template select_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); size_t fn( Elem[] buf, size_t num, Pred pred = Pred.init ) { // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } if( buf.length < 2 ) return buf.length; size_t l = 0, r = buf.length - 1, k = num; while( r > l ) { size_t i = l, j = r - 1; Elem v = buf[r]; while( true ) { while( i < r && pred( buf[i], v ) ) ++i; while( j > l && pred( v, buf[j] ) ) --j; if( i >= j ) break; exch( i++, j-- ); } exch( i, r ); if( i >= k ) r = i - 1; if( i <= k ) l = i + 1; } return num - 1; } } template select( Buf, Num ) { size_t select( Buf buf, Num num ) { return select_!(ElemTypeOf!(Buf)).fn( buf, num ); } } template select( Buf, Num, Pred ) { size_t select( Buf buf, Num num, Pred pred ) { return select_!(ElemTypeOf!(Buf), Pred).fn( buf, num, pred ); } } debug( UnitTest ) { unittest { char[] buf = "efedcaabca".dup; size_t num = buf.length / 2; size_t pos = select( buf, num ); assert( pos == num - 1 ); foreach( cur; buf[0 .. pos] ) assert( cur <= buf[pos] ); foreach( cur; buf[pos .. $] ) assert( cur >= buf[pos] ); } } } //////////////////////////////////////////////////////////////////////////////// // Sort //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Sorts buf using the supplied predicate or '<' if none is supplied. The * algorithm is not required to be stable. The current implementation is * based on quicksort, but uses a three-way partitioning scheme to improve * performance for ranges containing duplicate values (Bentley and McIlroy, * 1993). * * Params: * buf = The array to sort. This parameter is not marked 'ref' to * allow temporary slices to be sorted. As buf is not resized * in any way, omitting the 'ref' qualifier has no effect on * the result of this operation, even though it may be viewed * as a side-effect. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. */ void sort( Elem, Pred2E = IsLess!(Elem) )( Elem[] buf, Pred2E pred = Pred2E.init ); } else { template sort_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); void fn( Elem[] buf, Pred pred = Pred.init ) { bool equiv( Elem p1, Elem p2 ) { return !pred( p1, p2 ) && !pred( p2, p1 ); } // NOTE: Indexes are passed instead of references because DMD does // not inline the reference-based version. void exch( size_t p1, size_t p2 ) { Elem t = buf[p1]; buf[p1] = buf[p2]; buf[p2] = t; } // NOTE: This algorithm operates on the inclusive range [l .. r]. void insertionSort( size_t l, size_t r ) { for( size_t i = r; i > l; --i ) { // swap the min element to buf[0] to act as a sentinel if( pred( buf[i], buf[i - 1] ) ) exch( i, i - 1 ); } for( size_t i = l + 2; i <= r; ++i ) { size_t j = i; Elem v = buf[i]; // don't need to test (j != l) because of the sentinel while( pred( v, buf[j - 1] ) ) { buf[j] = buf[j - 1]; j--; } buf[j] = v; } } size_t medianOf( size_t l, size_t m, size_t r ) { if( pred( buf[m], buf[l] ) ) { if( pred( buf[r], buf[m] ) ) return m; else { if( pred( buf[r], buf[l] ) ) return r; else return l; } } else { if( pred( buf[r], buf[m] ) ) { if( pred( buf[r], buf[l] ) ) return l; else return r; } else return m; } } // NOTE: This algorithm operates on the inclusive range [l .. r]. void quicksort( size_t l, size_t r, size_t d ) { if( r <= l ) return; // HEURISTIC: Use insertion sort for sufficiently small arrays. enum { MIN_LENGTH = 80 } if( r - l < MIN_LENGTH ) return insertionSort( l, r ); // HEURISTIC: If the recursion depth is too great, assume this // is a worst-case array and fail to heap sort. if( d-- == 0 ) { makeHeap( buf[l .. r+1], pred ); sortHeap( buf[l .. r+1], pred ); return; } // HEURISTIC: Use the median-of-3 value as a pivot. Swap this // into r so quicksort remains untouched. exch( r, medianOf( l, l + (r - l) / 2, r ) ); // This implementation of quicksort improves upon the classic // algorithm by partitioning the array into three parts, one // each for keys smaller than, equal to, and larger than the // partitioning element, v: // // |--less than v--|--equal to v--|--greater than v--[v] // l j i r // // This approach sorts ranges containing duplicate elements // more quickly. During processing, the following situation // is maintained: // // |--equal--|--less--|--[###]--|--greater--|--equal--[v] // l p i j q r // // Please note that this implementation varies from the typical // algorithm by replacing the use of signed index values with // unsigned values. Elem v = buf[r]; size_t i = l, j = r, p = l, q = r; while( true ) { while( pred( buf[i], v ) ) ++i; while( pred( v, buf[--j] ) ) if( j == l ) break; if( i >= j ) break; exch( i, j ); if( equiv( buf[i], v ) ) exch( p++, i ); if( equiv( v, buf[j] ) ) exch( --q, j ); ++i; } exch( i, r ); if( p < i ) { j = i - 1; for( size_t k = l; k < p; k++, j-- ) exch( k, j ); quicksort( l, j, d ); } if( ++i < q ) { for( size_t k = r - 1; k >= q; k--, i++ ) exch( k, i ); quicksort( i, r, d ); } } size_t maxDepth( size_t x ) { size_t d = 0; do { ++d; x /= 2; } while( x > 1 ); return d * 2; // same as "floor( log( x ) / log( 2 ) ) * 2" } if( buf.length > 1 ) { quicksort( 0, buf.length - 1, maxDepth( buf.length ) ); } } } template sort( Buf ) { void sort( Buf buf ) { return sort_!(ElemTypeOf!(Buf)).fn( buf ); } } template sort( Buf, Pred ) { void sort( Buf buf, Pred pred ) { return sort_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); } } debug( UnitTest ) { unittest { void test( char[] buf ) { sort( buf ); char sav = buf[0]; foreach( cur; buf ) { assert( cur >= sav ); sav = cur; } } test( "mkcvalsidivjoaisjdvmzlksvdjioawmdsvmsdfefewv".dup ); test( "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf".dup ); test( "the quick brown fox jumped over the lazy dog".dup ); test( "abcdefghijklmnopqrstuvwxyz".dup ); test( "zyxwvutsrqponmlkjihgfedcba".dup ); } } } //////////////////////////////////////////////////////////////////////////////// // Lower Bound //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a binary search of buf, returning the index of the first * location where pat may be inserted without disrupting sort order. If * the sort order of pat precedes all elements in buf then 0 will be * returned. If the sort order of pat succeeds the largest element in buf * then buf.length will be returned. Comparisons will be performed using * the supplied predicate or '<' if none is supplied. * * Params: * buf = The sorted array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t lbound( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); } else { template lbound_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { size_t beg = 0, end = buf.length, mid = end / 2; while( beg < end ) { if( pred( buf[mid], pat ) ) beg = mid + 1; else end = mid; mid = beg + ( end - beg ) / 2; } return mid; } } template lbound( Buf, Pat ) { size_t lbound( Buf buf, Pat pat ) { return lbound_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template lbound( Buf, Pat, Pred ) { size_t lbound( Buf buf, Pat pat, Pred pred ) { return lbound_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { assert( lbound( "bcefg", 'a' ) == 0 ); assert( lbound( "bcefg", 'h' ) == 5 ); assert( lbound( "bcefg", 'd' ) == 2 ); assert( lbound( "bcefg", 'e' ) == 2 ); } } } //////////////////////////////////////////////////////////////////////////////// // Upper Bound //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a binary search of buf, returning the index of the first * location beyond where pat may be inserted without disrupting sort order. * If the sort order of pat precedes all elements in buf then 0 will be * returned. If the sort order of pat succeeds the largest element in buf * then buf.length will be returned. Comparisons will be performed using * the supplied predicate or '<' if none is supplied. * * Params: * buf = The sorted array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * The index of the first match or buf.length if no match was found. */ size_t ubound( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); } else { template ubound_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred) ); size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { size_t beg = 0, end = buf.length, mid = end / 2; while( beg < end ) { if( !pred( pat, buf[mid] ) ) beg = mid + 1; else end = mid; mid = beg + ( end - beg ) / 2; } return mid; } } template ubound( Buf, Pat ) { size_t ubound( Buf buf, Pat pat ) { return ubound_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template ubound( Buf, Pat, Pred ) { size_t ubound( Buf buf, Pat pat, Pred pred ) { return ubound_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { assert( ubound( "bcefg", 'a' ) == 0 ); assert( ubound( "bcefg", 'h' ) == 5 ); assert( ubound( "bcefg", 'd' ) == 2 ); assert( ubound( "bcefg", 'e' ) == 3 ); } } } //////////////////////////////////////////////////////////////////////////////// // Binary Search //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a binary search of buf, returning true if an element equivalent * to pat is found. Comparisons will be performed using the supplied * predicate or '<' if none is supplied. * * Params: * buf = The sorted array to search. * pat = The pattern to search for. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * True if an element equivalent to pat is found, false if not. */ bool bsearch( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); } else { template bsearch_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred) ); bool fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) { size_t pos = lbound( buf, pat, pred ); return pos < buf.length && !( pat < buf[pos] ); } } template bsearch( Buf, Pat ) { bool bsearch( Buf buf, Pat pat ) { return bsearch_!(ElemTypeOf!(Buf)).fn( buf, pat ); } } template bsearch( Buf, Pat, Pred ) { bool bsearch( Buf buf, Pat pat, Pred pred ) { return bsearch_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); } } debug( UnitTest ) { unittest { assert( !bsearch( "bcefg", 'a' ) ); assert( bsearch( "bcefg", 'b' ) ); assert( bsearch( "bcefg", 'c' ) ); assert( !bsearch( "bcefg", 'd' ) ); assert( bsearch( "bcefg", 'e' ) ); assert( bsearch( "bcefg", 'f' ) ); assert( bsearch( "bcefg", 'g' ) ); assert( !bsearch( "bcefg", 'h' ) ); } } } //////////////////////////////////////////////////////////////////////////////// // Includes //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Performs a parallel linear scan of setA and setB from [0 .. N$(RP) * where N = min(setA.length, setB.length), returning true if setA * includes all elements in setB and false if not. Both setA and setB are * required to be sorted, and duplicates in setB require an equal number of * duplicates in setA. Comparisons will be performed using the supplied * predicate or '<' if none is supplied. * * Params: * setA = The sorted array to evaluate. * setB = The sorted array to match against. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * True if setA includes all elements in setB, false if not. */ bool includes( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); } else { template includes_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); bool fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) { size_t posA = 0, posB = 0; while( posA < setA.length && posB < setB.length ) { if( pred( setB[posB], setA[posA] ) ) return false; else if( pred( setA[posA], setB[posB] ) ) ++posA; else ++posA, ++posB; } return posB == setB.length; } } template includes( BufA, BufB ) { bool includes( BufA setA, BufB setB ) { return includes_!(ElemTypeOf!(BufA)).fn( setA, setB ); } } template includes( BufA, BufB, Pred ) { bool includes( BufA setA, BufB setB, Pred pred ) { return includes_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); } } debug( UnitTest ) { unittest { assert( includes( "abcdefg", "a" ) ); assert( includes( "abcdefg", "g" ) ); assert( includes( "abcdefg", "d" ) ); assert( includes( "abcdefg", "abcdefg" ) ); assert( includes( "aaaabbbcdddefgg", "abbbcdefg" ) ); assert( !includes( "abcdefg", "aaabcdefg" ) ); assert( !includes( "abcdefg", "abcdefggg" ) ); assert( !includes( "abbbcdefg", "abbbbcdefg" ) ); } } } //////////////////////////////////////////////////////////////////////////////// // Union Of //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Computes the union of setA and setB as a set operation and returns the * retult in a new sorted array. Both setA and setB are required to be * sorted. If either setA or setB contain duplicates, the result will * contain the larger number of duplicates from setA and setB. When an * overlap occurs, entries will be copied from setA. Comparisons will be * performed using the supplied predicate or '<' if none is supplied. * * Params: * setA = The first sorted array to evaluate. * setB = The second sorted array to evaluate. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * A new array containing the union of setA and setB. */ Elem[] unionOf( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); } else { template unionOf_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) { size_t posA = 0, posB = 0; Elem[] setU; while( posA < setA.length && posB < setB.length ) { if( pred( setA[posA], setB[posB] ) ) setU ~= setA[posA++]; else if( pred( setB[posB], setA[posA] ) ) setU ~= setB[posB++]; else setU ~= setA[posA++], posB++; } setU ~= setA[posA .. $]; setU ~= setB[posB .. $]; return setU; } } template unionOf( BufA, BufB ) { ElemTypeOf!(BufA)[] unionOf( BufA setA, BufB setB ) { return unionOf_!(ElemTypeOf!(BufA)).fn( setA, setB ); } } template unionOf( BufA, BufB, Pred ) { ElemTypeOf!(BufA)[] unionOf( BufA setA, BufB setB, Pred pred ) { return unionOf_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); } } debug( UnitTest ) { unittest { assert( unionOf( "", "" ) == "" ); assert( unionOf( "abc", "def" ) == "abcdef" ); assert( unionOf( "abbbcd", "aadeefg" ) == "aabbbcdeefg" ); } } } //////////////////////////////////////////////////////////////////////////////// // Intersection Of //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Computes the intersection of setA and setB as a set operation and * returns the retult in a new sorted array. Both setA and setB are * required to be sorted. If either setA or setB contain duplicates, the * result will contain the smaller number of duplicates from setA and setB. * All entries will be copied from setA. Comparisons will be performed * using the supplied predicate or '<' if none is supplied. * * Params: * setA = The first sorted array to evaluate. * setB = The second sorted array to evaluate. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * A new array containing the intersection of setA and setB. */ Elem[] intersectionOf( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); } else { template intersectionOf_( Elem, Pred = IsLess!(Elem) ) { static assert( isCallableType!(Pred ) ); Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) { size_t posA = 0, posB = 0; Elem[] setI; while( posA < setA.length && posB < setB.length ) { if( pred( setA[posA], setB[posB] ) ) ++posA; else if( pred( setB[posB], setA[posA] ) ) ++posB; else setI ~= setA[posA++], posB++; } return setI; } } template intersectionOf( BufA, BufB ) { ElemTypeOf!(BufA)[] intersectionOf( BufA setA, BufB setB ) { return intersectionOf_!(ElemTypeOf!(BufA)).fn( setA, setB ); } } template intersectionOf( BufA, BufB, Pred ) { ElemTypeOf!(BufA)[] intersectionOf( BufA setA, BufB setB, Pred pred ) { return intersectionOf_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); } } debug( UnitTest ) { unittest { assert( intersectionOf( "", "" ) == "" ); assert( intersectionOf( "abc", "def" ) == "" ); assert( intersectionOf( "abbbcd", "aabdddeefg" ) == "abd" ); } } } //////////////////////////////////////////////////////////////////////////////// // Missing From //////////////////////////////////////////////////////////////////////////////// version( TangoDoc ) { /** * Returns a new array containing all elements in setA which are not * present in setB. Both setA and setB are required to be sorted. * Comparisons will be performed using the supplied predicate or '<' * if none is supplied. * * Params: * setA = The first sorted array to evaluate. * setB = The second sorted array to evaluate. * pred = The evaluation predicate, which should return true if e1 is * less than e2 and false if not. This predicate may be any * callable type. * * Returns: * A new array containing the elements in setA that are not in setB. */ Elem[] missingFrom( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); } e |