123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035 |
- /* Arrays.java -- Utility class with methods to operate on arrays
- Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
- Free Software Foundation, Inc.
- This file is part of GNU Classpath.
- GNU Classpath is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
- GNU Classpath is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNU Classpath; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301 USA.
- Linking this library statically or dynamically with other modules is
- making a combined work based on this library. Thus, the terms and
- conditions of the GNU General Public License cover the whole
- combination.
- As a special exception, the copyright holders of this library give you
- permission to link this library with independent modules to produce an
- executable, regardless of the license terms of these independent
- modules, and to copy and distribute the resulting executable under
- terms of your choice, provided that you also meet, for each linked
- independent module, the terms and conditions of the license of that
- module. An independent module is a module which is not derived from
- or based on this library. If you modify this library, you may extend
- this exception to your version of the library, but you are not
- obligated to do so. If you do not wish to do so, delete this
- exception statement from your version. */
- package java.util;
- import gnu.java.lang.CPStringBuilder;
- import java.io.Serializable;
- import java.lang.reflect.Array;
- /**
- * This class contains various static utility methods performing operations on
- * arrays, and a method to provide a List "view" of an array to facilitate
- * using arrays with Collection-based APIs. All methods throw a
- * {@link NullPointerException} if the parameter array is null.
- * <p>
- *
- * Implementations may use their own algorithms, but must obey the general
- * properties; for example, the sort must be stable and n*log(n) complexity.
- * Sun's implementation of sort, and therefore ours, is a tuned quicksort,
- * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort
- * Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265
- * (November 1993). This algorithm offers n*log(n) performance on many data
- * sets that cause other quicksorts to degrade to quadratic performance.
- *
- * @author Original author unknown
- * @author Bryce McKinlay
- * @author Eric Blake (ebb9@email.byu.edu)
- * @see Comparable
- * @see Comparator
- * @since 1.2
- * @status updated to 1.4
- */
- public class Arrays
- {
- /**
- * This class is non-instantiable.
- */
- private Arrays()
- {
- }
- // binarySearch
- /**
- * Perform a binary search of a byte array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(byte[] a, byte key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a byte array for a key. The range
- * must be sorted (as by the <code>sort(byte[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(byte[] a, int low, int hi, byte key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final byte d = a[mid];
- if (d == key)
- return mid;
- else if (d > key)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop.
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of a char array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(char[] a, char key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a char array for a key. The range
- * must be sorted (as by the <code>sort(char[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(char[] a, int low, int hi, char key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final char d = a[mid];
- if (d == key)
- return mid;
- else if (d > key)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop.
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of a short array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(short[] a, short key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a short array for a key. The range
- * must be sorted (as by the <code>sort(short[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(short[] a, int low, int hi, short key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final short d = a[mid];
- if (d == key)
- return mid;
- else if (d > key)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop.
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of an int array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(int[] a, int key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of an integer array for a key. The range
- * must be sorted (as by the <code>sort(int[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(int[] a, int low, int hi, int key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final int d = a[mid];
- if (d == key)
- return mid;
- else if (d > key)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop.
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of a long array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(long[] a, long key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a long array for a key. The range
- * must be sorted (as by the <code>sort(long[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(long[] a, int low, int hi, long key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final long d = a[mid];
- if (d == key)
- return mid;
- else if (d > key)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop.
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of a float array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(float[] a, float key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a float array for a key. The range
- * must be sorted (as by the <code>sort(float[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(float[] a, int low, int hi, float key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- // Must use Float.compare to take into account NaN, +-0.
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final int r = Float.compare(a[mid], key);
- if (r == 0)
- return mid;
- else if (r > 0)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of a double array for a key. The array must be
- * sorted (as by the sort() method) - if it is not, the behaviour of this
- * method is undefined, and may be an infinite loop. If the array contains
- * the key more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(double[] a, double key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key);
- }
- /**
- * Perform a binary search of a range of a double array for a key. The range
- * must be sorted (as by the <code>sort(double[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static int binarySearch(double[] a, int low, int hi, double key)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- // Must use Double.compare to take into account NaN, +-0.
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- final int r = Double.compare(a[mid], key);
- if (r == 0)
- return mid;
- else if (r > 0)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop
- low = ++mid;
- }
- return -mid - 1;
- }
- /**
- * Perform a binary search of an Object array for a key, using the natural
- * ordering of the elements. The array must be sorted (as by the sort()
- * method) - if it is not, the behaviour of this method is undefined, and may
- * be an infinite loop. Further, the key must be comparable with every item
- * in the array. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this (JCL)
- * implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws ClassCastException if key could not be compared with one of the
- * elements of a
- * @throws NullPointerException if a null element in a is compared
- */
- public static int binarySearch(Object[] a, Object key)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, key, null);
- }
- /**
- * Perform a binary search of a range of an Object array for a key. The range
- * must be sorted (as by the <code>sort(Object[], int, int)</code> method) -
- * if it is not, the behaviour of this method is undefined, and may be an
- * infinite loop. If the array contains the key more than once, any one of
- * them may be found. Note: although the specification allows for an infinite
- * loop if the array is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- */
- public static int binarySearch(Object[] a, int low, int hi, Object key)
- {
- return binarySearch(a, low, hi, key, null);
- }
- /**
- * Perform a binary search of an Object array for a key, using a supplied
- * Comparator. The array must be sorted (as by the sort() method with the
- * same Comparator) - if it is not, the behaviour of this method is
- * undefined, and may be an infinite loop. Further, the key must be
- * comparable with every item in the array. If the array contains the key
- * more than once, any one of them may be found. Note: although the
- * specification allows for an infinite loop if the array is unsorted, it
- * will not happen in this (JCL) implementation.
- *
- * @param a the array to search (must be sorted)
- * @param key the value to search for
- * @param c the comparator by which the array is sorted; or null to
- * use the elements' natural order
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws ClassCastException if key could not be compared with one of the
- * elements of a
- * @throws NullPointerException if a null element is compared with natural
- * ordering (only possible when c is null)
- */
- public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
- {
- if (a.length == 0)
- return -1;
- return binarySearch(a, 0, a.length - 1, key, c);
- }
- /**
- * Perform a binary search of a range of an Object array for a key using
- * a {@link Comparator}. The range must be sorted (as by the
- * <code>sort(Object[], int, int)</code> method) - if it is not, the
- * behaviour of this method is undefined, and may be an infinite loop. If
- * the array contains the key more than once, any one of them may be found.
- * Note: although the specification allows for an infinite loop if the array
- * is unsorted, it will not happen in this implementation.
- *
- * @param a the array to search (must be sorted)
- * @param low the lowest index to search from.
- * @param hi the highest index to search to.
- * @param key the value to search for
- * @param c the comparator by which the array is sorted; or null to
- * use the elements' natural order
- * @return the index at which the key was found, or -n-1 if it was not
- * found, where n is the index of the first value higher than key or
- * a.length if there is no such value.
- * @throws ClassCastException if key could not be compared with one of the
- * elements of a
- * @throws IllegalArgumentException if <code>low > hi</code>
- * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
- * <code>hi > a.length</code>.
- */
- public static <T> int binarySearch(T[] a, int low, int hi, T key,
- Comparator<? super T> c)
- {
- if (low > hi)
- throw new IllegalArgumentException("The start index is higher than " +
- "the finish index.");
- if (low < 0 || hi > a.length)
- throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
- "of bounds.");
- int mid = 0;
- while (low <= hi)
- {
- mid = (low + hi) >>> 1;
- // NOTE: Please keep the order of a[mid] and key. Although
- // not required by the specs, the RI has it in this order as
- // well, and real programs (erroneously) depend on it.
- final int d = Collections.compare(a[mid], key, c);
- if (d == 0)
- return mid;
- else if (d > 0)
- hi = mid - 1;
- else
- // This gets the insertion point right on the last loop
- low = ++mid;
- }
- return -mid - 1;
- }
- // equals
- /**
- * Compare two boolean arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(boolean[] a1, boolean[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two byte arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(byte[] a1, byte[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two char arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(char[] a1, char[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two short arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(short[] a1, short[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two int arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(int[] a1, int[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two long arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(long[] a1, long[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (a1[i] != a2[i])
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two float arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(float[] a1, float[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // Must use Float.compare to take into account NaN, +-0.
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (Float.compare(a1[i], a2[i]) != 0)
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two double arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a2 is of the same length
- * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
- */
- public static boolean equals(double[] a1, double[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // Must use Double.compare to take into account NaN, +-0.
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (Double.compare(a1[i], a2[i]) != 0)
- return false;
- return true;
- }
- return false;
- }
- /**
- * Compare two Object arrays for equality.
- *
- * @param a1 the first array to compare
- * @param a2 the second array to compare
- * @return true if a1 and a2 are both null, or if a1 is of the same length
- * as a2, and for each 0 <= i < a.length, a1[i] == null ?
- * a2[i] == null : a1[i].equals(a2[i]).
- */
- public static boolean equals(Object[] a1, Object[] a2)
- {
- // Quick test which saves comparing elements of the same array, and also
- // catches the case that both are null.
- if (a1 == a2)
- return true;
- if (null == a1 || null == a2)
- return false;
- // If they're the same length, test each element
- if (a1.length == a2.length)
- {
- int i = a1.length;
- while (--i >= 0)
- if (! AbstractCollection.equals(a1[i], a2[i]))
- return false;
- return true;
- }
- return false;
- }
- // fill
- /**
- * Fill an array with a boolean value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(boolean[] a, boolean val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a boolean value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a byte value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(byte[] a, byte val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a byte value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a char value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(char[] a, char val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a char value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(char[] a, int fromIndex, int toIndex, char val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a short value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(short[] a, short val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a short value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(short[] a, int fromIndex, int toIndex, short val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with an int value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(int[] a, int val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with an int value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(int[] a, int fromIndex, int toIndex, int val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a long value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(long[] a, long val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a long value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(long[] a, int fromIndex, int toIndex, long val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a float value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(float[] a, float val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a float value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(float[] a, int fromIndex, int toIndex, float val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with a double value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- */
- public static void fill(double[] a, double val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with a double value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(double[] a, int fromIndex, int toIndex, double val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- /**
- * Fill an array with an Object value.
- *
- * @param a the array to fill
- * @param val the value to fill it with
- * @throws ClassCastException if val is not an instance of the element
- * type of a.
- */
- public static void fill(Object[] a, Object val)
- {
- fill(a, 0, a.length, val);
- }
- /**
- * Fill a range of an array with an Object value.
- *
- * @param a the array to fill
- * @param fromIndex the index to fill from, inclusive
- * @param toIndex the index to fill to, exclusive
- * @param val the value to fill with
- * @throws ClassCastException if val is not an instance of the element
- * type of a.
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- for (int i = fromIndex; i < toIndex; i++)
- a[i] = val;
- }
- // sort
- // Thanks to Paul Fisher (rao@gnu.org) for finding this quicksort algorithm
- // as specified by Sun and porting it to Java. The algorithm is an optimised
- // quicksort, as described in Jon L. Bentley and M. Douglas McIlroy's
- // "Engineering a Sort Function", Software-Practice and Experience, Vol.
- // 23(11) P. 1249-1265 (November 1993). This algorithm gives n*log(n)
- // performance on many arrays that would take quadratic time with a standard
- // quicksort.
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the byte array to sort
- */
- public static void sort(byte[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the byte array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(byte[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, byte[] d)
- {
- return (d[a] < d[b]
- ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
- : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, byte[] a)
- {
- byte c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, byte[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(byte[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i; j > from && array[j - 1] > array[j]; j--)
- swap(j, j - 1, array);
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = array[b] - array[from]) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = array[c] - array[from]) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the char array to sort
- */
- public static void sort(char[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the char array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(char[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, char[] d)
- {
- return (d[a] < d[b]
- ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
- : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, char[] a)
- {
- char c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, char[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(char[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i; j > from && array[j - 1] > array[j]; j--)
- swap(j, j - 1, array);
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = array[b] - array[from]) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = array[c] - array[from]) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the short array to sort
- */
- public static void sort(short[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the short array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(short[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, short[] d)
- {
- return (d[a] < d[b]
- ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
- : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, short[] a)
- {
- short c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, short[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(short[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i; j > from && array[j - 1] > array[j]; j--)
- swap(j, j - 1, array);
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = array[b] - array[from]) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = array[c] - array[from]) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the int array to sort
- */
- public static void sort(int[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the int array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(int[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, int[] d)
- {
- return (d[a] < d[b]
- ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
- : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, int[] a)
- {
- int c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, int[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Compares two integers in natural order, since a - b is inadequate.
- *
- * @param a the first int
- * @param b the second int
- * @return < 0, 0, or > 0 accorting to the comparison
- */
- private static int compare(int a, int b)
- {
- return a < b ? -1 : a == b ? 0 : 1;
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(int[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i; j > from && array[j - 1] > array[j]; j--)
- swap(j, j - 1, array);
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = compare(array[b], array[from])) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = compare(array[c], array[from])) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the long array to sort
- */
- public static void sort(long[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the long array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(long[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, long[] d)
- {
- return (d[a] < d[b]
- ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
- : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, long[] a)
- {
- long c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, long[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Compares two longs in natural order, since a - b is inadequate.
- *
- * @param a the first long
- * @param b the second long
- * @return < 0, 0, or > 0 accorting to the comparison
- */
- private static int compare(long a, long b)
- {
- return a < b ? -1 : a == b ? 0 : 1;
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(long[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i; j > from && array[j - 1] > array[j]; j--)
- swap(j, j - 1, array);
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = compare(array[b], array[from])) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = compare(array[c], array[from])) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the float array to sort
- */
- public static void sort(float[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the float array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(float[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, float[] d)
- {
- return (Float.compare(d[a], d[b]) < 0
- ? (Float.compare(d[b], d[c]) < 0 ? b
- : Float.compare(d[a], d[c]) < 0 ? c : a)
- : (Float.compare(d[b], d[c]) > 0 ? b
- : Float.compare(d[a], d[c]) > 0 ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, float[] a)
- {
- float c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, float[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(float[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i;
- j > from && Float.compare(array[j - 1], array[j]) > 0;
- j--)
- {
- swap(j, j - 1, array);
- }
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = Float.compare(array[b], array[from])) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = Float.compare(array[c], array[from])) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the double array to sort
- */
- public static void sort(double[] a)
- {
- qsort(a, 0, a.length);
- }
- /**
- * Performs a stable sort on the elements, arranging them according to their
- * natural order.
- *
- * @param a the double array to sort
- * @param fromIndex the first index to sort (inclusive)
- * @param toIndex the last index to sort (exclusive)
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
- * || toIndex > a.length
- */
- public static void sort(double[] a, int fromIndex, int toIndex)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException();
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- qsort(a, fromIndex, toIndex - fromIndex);
- }
- /**
- * Finds the index of the median of three array elements.
- *
- * @param a the first index
- * @param b the second index
- * @param c the third index
- * @param d the array
- * @return the index (a, b, or c) which has the middle value of the three
- */
- private static int med3(int a, int b, int c, double[] d)
- {
- return (Double.compare(d[a], d[b]) < 0
- ? (Double.compare(d[b], d[c]) < 0 ? b
- : Double.compare(d[a], d[c]) < 0 ? c : a)
- : (Double.compare(d[b], d[c]) > 0 ? b
- : Double.compare(d[a], d[c]) > 0 ? c : a));
- }
- /**
- * Swaps the elements at two locations of an array
- *
- * @param i the first index
- * @param j the second index
- * @param a the array
- */
- private static void swap(int i, int j, double[] a)
- {
- double c = a[i];
- a[i] = a[j];
- a[j] = c;
- }
- /**
- * Swaps two ranges of an array.
- *
- * @param i the first range start
- * @param j the second range start
- * @param n the element count
- * @param a the array
- */
- private static void vecswap(int i, int j, int n, double[] a)
- {
- for ( ; n > 0; i++, j++, n--)
- swap(i, j, a);
- }
- /**
- * Performs a recursive modified quicksort.
- *
- * @param array the array to sort
- * @param from the start index (inclusive)
- * @param count the number of elements to sort
- */
- private static void qsort(double[] array, int from, int count)
- {
- // Use an insertion sort on small arrays.
- if (count <= 7)
- {
- for (int i = from + 1; i < from + count; i++)
- for (int j = i;
- j > from && Double.compare(array[j - 1], array[j]) > 0;
- j--)
- {
- swap(j, j - 1, array);
- }
- return;
- }
- // Determine a good median element.
- int mid = from + count / 2;
- int lo = from;
- int hi = from + count - 1;
- if (count > 40)
- { // big arrays, pseudomedian of 9
- int s = count / 8;
- lo = med3(lo, lo + s, lo + 2 * s, array);
- mid = med3(mid - s, mid, mid + s, array);
- hi = med3(hi - 2 * s, hi - s, hi, array);
- }
- mid = med3(lo, mid, hi, array);
- int a, b, c, d;
- int comp;
- // Pull the median element out of the fray, and use it as a pivot.
- swap(from, mid, array);
- a = b = from;
- c = d = from + count - 1;
- // Repeatedly move b and c to each other, swapping elements so
- // that all elements before index b are less than the pivot, and all
- // elements after index c are greater than the pivot. a and b track
- // the elements equal to the pivot.
- while (true)
- {
- while (b <= c && (comp = Double.compare(array[b], array[from])) <= 0)
- {
- if (comp == 0)
- {
- swap(a, b, array);
- a++;
- }
- b++;
- }
- while (c >= b && (comp = Double.compare(array[c], array[from])) >= 0)
- {
- if (comp == 0)
- {
- swap(c, d, array);
- d--;
- }
- c--;
- }
- if (b > c)
- break;
- swap(b, c, array);
- b++;
- c--;
- }
- // Swap pivot(s) back in place, the recurse on left and right sections.
- hi = from + count;
- int span;
- span = Math.min(a - from, b - a);
- vecswap(from, b - span, span, array);
- span = Math.min(d - c, hi - d - 1);
- vecswap(b, hi - span, span, array);
- span = b - a;
- if (span > 1)
- qsort(array, from, span);
- span = d - c;
- if (span > 1)
- qsort(array, hi - span, span);
- }
- /**
- * Sort an array of Objects according to their natural ordering. The sort is
- * guaranteed to be stable, that is, equal elements will not be reordered.
- * The sort algorithm is a mergesort with the merge omitted if the last
- * element of one half comes before the first element of the other half. This
- * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
- * copy of the array.
- *
- * @param a the array to be sorted
- * @throws ClassCastException if any two elements are not mutually
- * comparable
- * @throws NullPointerException if an element is null (since
- * null.compareTo cannot work)
- * @see Comparable
- */
- public static void sort(Object[] a)
- {
- sort(a, 0, a.length, null);
- }
- /**
- * Sort an array of Objects according to a Comparator. The sort is
- * guaranteed to be stable, that is, equal elements will not be reordered.
- * The sort algorithm is a mergesort with the merge omitted if the last
- * element of one half comes before the first element of the other half. This
- * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
- * copy of the array.
- *
- * @param a the array to be sorted
- * @param c a Comparator to use in sorting the array; or null to indicate
- * the elements' natural order
- * @throws ClassCastException if any two elements are not mutually
- * comparable by the Comparator provided
- * @throws NullPointerException if a null element is compared with natural
- * ordering (only possible when c is null)
- */
- public static <T> void sort(T[] a, Comparator<? super T> c)
- {
- sort(a, 0, a.length, c);
- }
- /**
- * Sort an array of Objects according to their natural ordering. The sort is
- * guaranteed to be stable, that is, equal elements will not be reordered.
- * The sort algorithm is a mergesort with the merge omitted if the last
- * element of one half comes before the first element of the other half. This
- * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
- * copy of the array.
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element to be sorted
- * @param toIndex the index of the last element to be sorted plus one
- * @throws ClassCastException if any two elements are not mutually
- * comparable
- * @throws NullPointerException if an element is null (since
- * null.compareTo cannot work)
- * @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
- * are not in range.
- * @throws IllegalArgumentException if fromIndex > toIndex
- */
- public static void sort(Object[] a, int fromIndex, int toIndex)
- {
- sort(a, fromIndex, toIndex, null);
- }
- /**
- * Sort an array of Objects according to a Comparator. The sort is
- * guaranteed to be stable, that is, equal elements will not be reordered.
- * The sort algorithm is a mergesort with the merge omitted if the last
- * element of one half comes before the first element of the other half. This
- * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
- * copy of the array.
- *
- * @param a the array to be sorted
- * @param fromIndex the index of the first element to be sorted
- * @param toIndex the index of the last element to be sorted plus one
- * @param c a Comparator to use in sorting the array; or null to indicate
- * the elements' natural order
- * @throws ClassCastException if any two elements are not mutually
- * comparable by the Comparator provided
- * @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
- * are not in range.
- * @throws IllegalArgumentException if fromIndex > toIndex
- * @throws NullPointerException if a null element is compared with natural
- * ordering (only possible when c is null)
- */
- public static <T> void sort(T[] a, int fromIndex, int toIndex,
- Comparator<? super T> c)
- {
- if (fromIndex > toIndex)
- throw new IllegalArgumentException("fromIndex " + fromIndex
- + " > toIndex " + toIndex);
- if (fromIndex < 0)
- throw new ArrayIndexOutOfBoundsException();
- // In general, the code attempts to be simple rather than fast, the
- // idea being that a good optimising JIT will be able to optimise it
- // better than I can, and if I try it will make it more confusing for
- // the JIT. First presort the array in chunks of length 6 with insertion
- // sort. A mergesort would give too much overhead for this length.
- for (int chunk = fromIndex; chunk < toIndex; chunk += 6)
- {
- int end = Math.min(chunk + 6, toIndex);
- for (int i = chunk + 1; i < end; i++)
- {
- if (Collections.compare(a[i - 1], a[i], c) > 0)
- {
- // not already sorted
- int j = i;
- T elem = a[j];
- do
- {
- a[j] = a[j - 1];
- j--;
- }
- while (j > chunk
- && Collections.compare(a[j - 1], elem, c) > 0);
- a[j] = elem;
- }
- }
- }
- int len = toIndex - fromIndex;
- // If length is smaller or equal 6 we are done.
- if (len <= 6)
- return;
- T[] src = a;
- T[] dest = (T[]) new Object[len];
- T[] t = null; // t is used for swapping src and dest
- // The difference of the fromIndex of the src and dest array.
- int srcDestDiff = -fromIndex;
- // The merges are done in this loop
- for (int size = 6; size < len; size <<= 1)
- {
- for (int start = fromIndex; start < toIndex; start += size << 1)
- {
- // mid is the start of the second sublist;
- // end the start of the next sublist (or end of array).
- int mid = start + size;
- int end = Math.min(toIndex, mid + size);
- // The second list is empty or the elements are already in
- // order - no need to merge
- if (mid >= end
- || Collections.compare(src[mid - 1], src[mid], c) <= 0)
- {
- System.arraycopy(src, start,
- dest, start + srcDestDiff, end - start);
- // The two halves just need swapping - no need to merge
- }
- else if (Collections.compare(src[start], src[end - 1], c) > 0)
- {
- System.arraycopy(src, start,
- dest, end - size + srcDestDiff, size);
- System.arraycopy(src, mid,
- dest, start + srcDestDiff, end - mid);
- }
- else
- {
- // Declare a lot of variables to save repeating
- // calculations. Hopefully a decent JIT will put these
- // in registers and make this fast
- int p1 = start;
- int p2 = mid;
- int i = start + srcDestDiff;
- // The main merge loop; terminates as soon as either
- // half is ended
- while (p1 < mid && p2 < end)
- {
- dest[i++] =
- src[(Collections.compare(src[p1], src[p2], c) <= 0
- ? p1++ : p2++)];
- }
- // Finish up by copying the remainder of whichever half
- // wasn't finished.
- if (p1 < mid)
- System.arraycopy(src, p1, dest, i, mid - p1);
- else
- System.arraycopy(src, p2, dest, i, end - p2);
- }
- }
- // swap src and dest ready for the next merge
- t = src;
- src = dest;
- dest = t;
- fromIndex += srcDestDiff;
- toIndex += srcDestDiff;
- srcDestDiff = -srcDestDiff;
- }
- // make sure the result ends up back in the right place. Note
- // that src and dest may have been swapped above, so src
- // contains the sorted array.
- if (src != a)
- {
- // Note that fromIndex == 0.
- System.arraycopy(src, 0, a, srcDestDiff, toIndex);
- }
- }
- /**
- * Returns a list "view" of the specified array. This method is intended to
- * make it easy to use the Collections API with existing array-based APIs and
- * programs. Changes in the list or the array show up in both places. The
- * list does not support element addition or removal, but does permit
- * value modification. The returned list implements both Serializable and
- * RandomAccess.
- *
- * @param a the array to return a view of (<code>null</code> not permitted)
- * @return a fixed-size list, changes to which "write through" to the array
- *
- * @throws NullPointerException if <code>a</code> is <code>null</code>.
- * @see Serializable
- * @see RandomAccess
- * @see Arrays.ArrayList
- */
- public static <T> List<T> asList(final T... a)
- {
- return new Arrays.ArrayList(a);
- }
- /**
- * Returns the hashcode of an array of long numbers. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents longs in their wrapper class, <code>Long</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of long numbers for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(long[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- {
- int elt = (int) (v[i] ^ (v[i] >>> 32));
- result = 31 * result + elt;
- }
- return result;
- }
- /**
- * Returns the hashcode of an array of integer numbers. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents ints in their wrapper class, <code>Integer</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of integer numbers for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(int[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + v[i];
- return result;
- }
- /**
- * Returns the hashcode of an array of short numbers. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents shorts in their wrapper class, <code>Short</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of short numbers for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(short[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + v[i];
- return result;
- }
- /**
- * Returns the hashcode of an array of characters. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents chars in their wrapper class, <code>Character</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of characters for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(char[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + v[i];
- return result;
- }
- /**
- * Returns the hashcode of an array of bytes. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents bytes in their wrapper class, <code>Byte</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of bytes for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(byte[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + v[i];
- return result;
- }
- /**
- * Returns the hashcode of an array of booleans. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents booleans in their wrapper class,
- * <code>Boolean</code>. For <code>null</code>, 0 is returned.
- *
- * @param v an array of booleans for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(boolean[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + (v[i] ? 1231 : 1237);
- return result;
- }
- /**
- * Returns the hashcode of an array of floats. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents floats in their wrapper class, <code>Float</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of floats for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(float[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- result = 31 * result + Float.floatToIntBits(v[i]);
- return result;
- }
- /**
- * Returns the hashcode of an array of doubles. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object. This has the same
- * data, but represents doubles in their wrapper class, <code>Double</code>.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of doubles for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(double[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- {
- long l = Double.doubleToLongBits(v[i]);
- int elt = (int) (l ^ (l >>> 32));
- result = 31 * result + elt;
- }
- return result;
- }
- /**
- * Returns the hashcode of an array of objects. If two arrays
- * are equal, according to <code>equals()</code>, they should have the
- * same hashcode. The hashcode returned by the method is equal to that
- * obtained by the corresponding <code>List</code> object.
- * For <code>null</code>, 0 is returned.
- *
- * @param v an array of integer numbers for which the hash code should be
- * computed.
- * @return the hash code of the array, or 0 if null was given.
- * @since 1.5
- */
- public static int hashCode(Object[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- {
- int elt = v[i] == null ? 0 : v[i].hashCode();
- result = 31 * result + elt;
- }
- return result;
- }
- public static int deepHashCode(Object[] v)
- {
- if (v == null)
- return 0;
- int result = 1;
- for (int i = 0; i < v.length; ++i)
- {
- int elt;
- if (v[i] == null)
- elt = 0;
- else if (v[i] instanceof boolean[])
- elt = hashCode((boolean[]) v[i]);
- else if (v[i] instanceof byte[])
- elt = hashCode((byte[]) v[i]);
- else if (v[i] instanceof char[])
- elt = hashCode((char[]) v[i]);
- else if (v[i] instanceof short[])
- elt = hashCode((short[]) v[i]);
- else if (v[i] instanceof int[])
- elt = hashCode((int[]) v[i]);
- else if (v[i] instanceof long[])
- elt = hashCode((long[]) v[i]);
- else if (v[i] instanceof float[])
- elt = hashCode((float[]) v[i]);
- else if (v[i] instanceof double[])
- elt = hashCode((double[]) v[i]);
- else if (v[i] instanceof Object[])
- elt = hashCode((Object[]) v[i]);
- else
- elt = v[i].hashCode();
- result = 31 * result + elt;
- }
- return result;
- }
- /** @since 1.5 */
- public static boolean deepEquals(Object[] v1, Object[] v2)
- {
- if (v1 == null)
- return v2 == null;
- if (v2 == null || v1.length != v2.length)
- return false;
- for (int i = 0; i < v1.length; ++i)
- {
- Object e1 = v1[i];
- Object e2 = v2[i];
- if (e1 == e2)
- continue;
- if (e1 == null || e2 == null)
- return false;
- boolean check;
- if (e1 instanceof boolean[] && e2 instanceof boolean[])
- check = equals((boolean[]) e1, (boolean[]) e2);
- else if (e1 instanceof byte[] && e2 instanceof byte[])
- check = equals((byte[]) e1, (byte[]) e2);
- else if (e1 instanceof char[] && e2 instanceof char[])
- check = equals((char[]) e1, (char[]) e2);
- else if (e1 instanceof short[] && e2 instanceof short[])
- check = equals((short[]) e1, (short[]) e2);
- else if (e1 instanceof int[] && e2 instanceof int[])
- check = equals((int[]) e1, (int[]) e2);
- else if (e1 instanceof long[] && e2 instanceof long[])
- check = equals((long[]) e1, (long[]) e2);
- else if (e1 instanceof float[] && e2 instanceof float[])
- check = equals((float[]) e1, (float[]) e2);
- else if (e1 instanceof double[] && e2 instanceof double[])
- check = equals((double[]) e1, (double[]) e2);
- else if (e1 instanceof Object[] && e2 instanceof Object[])
- check = equals((Object[]) e1, (Object[]) e2);
- else
- check = e1.equals(e2);
- if (! check)
- return false;
- }
- return true;
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(boolean[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(byte[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(char[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(short[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(int[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(long[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(float[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(double[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- /**
- * Returns a String representation of the argument array. Returns "null"
- * if <code>a</code> is null.
- * @param v the array to represent
- * @return a String representing this array
- * @since 1.5
- */
- public static String toString(Object[] v)
- {
- if (v == null)
- return "null";
- CPStringBuilder b = new CPStringBuilder("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- b.append(v[i]);
- }
- b.append("]");
- return b.toString();
- }
- private static void deepToString(Object[] v, CPStringBuilder b, HashSet seen)
- {
- b.append("[");
- for (int i = 0; i < v.length; ++i)
- {
- if (i > 0)
- b.append(", ");
- Object elt = v[i];
- if (elt == null)
- b.append("null");
- else if (elt instanceof boolean[])
- b.append(toString((boolean[]) elt));
- else if (elt instanceof byte[])
- b.append(toString((byte[]) elt));
- else if (elt instanceof char[])
- b.append(toString((char[]) elt));
- else if (elt instanceof short[])
- b.append(toString((short[]) elt));
- else if (elt instanceof int[])
- b.append(toString((int[]) elt));
- else if (elt instanceof long[])
- b.append(toString((long[]) elt));
- else if (elt instanceof float[])
- b.append(toString((float[]) elt));
- else if (elt instanceof double[])
- b.append(toString((double[]) elt));
- else if (elt instanceof Object[])
- {
- Object[] os = (Object[]) elt;
- if (seen.contains(os))
- b.append("[...]");
- else
- {
- seen.add(os);
- deepToString(os, b, seen);
- }
- }
- else
- b.append(elt);
- }
- b.append("]");
- }
- /** @since 1.5 */
- public static String deepToString(Object[] v)
- {
- if (v == null)
- return "null";
- HashSet seen = new HashSet();
- CPStringBuilder b = new CPStringBuilder();
- deepToString(v, b, seen);
- return b.toString();
- }
- /**
- * Inner class used by {@link #asList(Object[])} to provide a list interface
- * to an array. The name, though it clashes with java.util.ArrayList, is
- * Sun's choice for Serialization purposes. Element addition and removal
- * is prohibited, but values can be modified.
- *
- * @author Eric Blake (ebb9@email.byu.edu)
- * @status updated to 1.4
- */
- private static final class ArrayList<E> extends AbstractList<E>
- implements Serializable, RandomAccess
- {
- // We override the necessary methods, plus others which will be much
- // more efficient with direct iteration rather than relying on iterator().
- /**
- * Compatible with JDK 1.4.
- */
- private static final long serialVersionUID = -2764017481108945198L;
- /**
- * The array we are viewing.
- * @serial the array
- */
- private final E[] a;
- /**
- * Construct a list view of the array.
- * @param a the array to view
- * @throws NullPointerException if a is null
- */
- ArrayList(E[] a)
- {
- // We have to explicitly check.
- if (a == null)
- throw new NullPointerException();
- this.a = a;
- }
- /**
- * Returns the object at the specified index in
- * the array.
- *
- * @param index The index to retrieve an object from.
- * @return The object at the array index specified.
- */
- public E get(int index)
- {
- return a[index];
- }
- /**
- * Returns the size of the array.
- *
- * @return The size.
- */
- public int size()
- {
- return a.length;
- }
- /**
- * Replaces the object at the specified index
- * with the supplied element.
- *
- * @param index The index at which to place the new object.
- * @param element The new object.
- * @return The object replaced by this operation.
- */
- public E set(int index, E element)
- {
- E old = a[index];
- a[index] = element;
- return old;
- }
- /**
- * Returns true if the array contains the
- * supplied object.
- *
- * @param o The object to look for.
- * @return True if the object was found.
- */
- public boolean contains(Object o)
- {
- return lastIndexOf(o) >= 0;
- }
- /**
- * Returns the first index at which the
- * object, o, occurs in the array.
- *
- * @param o The object to search for.
- * @return The first relevant index.
- */
- public int indexOf(Object o)
- {
- int size = a.length;
- for (int i = 0; i < size; i++)
- if (ArrayList.equals(o, a[i]))
- return i;
- return -1;
- }
- /**
- * Returns the last index at which the
- * object, o, occurs in the array.
- *
- * @param o The object to search for.
- * @return The last relevant index.
- */
- public int lastIndexOf(Object o)
- {
- int i = a.length;
- while (--i >= 0)
- if (ArrayList.equals(o, a[i]))
- return i;
- return -1;
- }
- /**
- * Transforms the list into an array of
- * objects, by simplying cloning the array
- * wrapped by this list.
- *
- * @return A clone of the internal array.
- */
- public Object[] toArray()
- {
- return (Object[]) a.clone();
- }
- /**
- * Copies the objects from this list into
- * the supplied array. The supplied array
- * is shrunk or enlarged to the size of the
- * internal array, and filled with its objects.
- *
- * @param array The array to fill with the objects in this list.
- * @return The array containing the objects in this list,
- * which may or may not be == to array.
- */
- public <T> T[] toArray(T[] array)
- {
- int size = a.length;
- if (array.length < size)
- array = (T[]) Array.newInstance(array.getClass().getComponentType(),
- size);
- else if (array.length > size)
- array[size] = null;
- System.arraycopy(a, 0, array, 0, size);
- return array;
- }
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>false</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>false</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>false</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(boolean[],int,int)
- */
- public static boolean[] copyOf(boolean[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>false</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>false</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(boolean[],int)
- */
- public static boolean[] copyOfRange(boolean[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- boolean[] newArray = new boolean[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, false);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>(byte)0</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>(byte)0</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>(byte)0</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(byte[],int,int)
- */
- public static byte[] copyOf(byte[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>(byte)0</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>(byte)0</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(byte[],int)
- */
- public static byte[] copyOfRange(byte[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- byte[] newArray = new byte[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, (byte)0);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>'\0'</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>'\0'</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>'\0'</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(char[],int,int)
- */
- public static char[] copyOf(char[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>'\0'</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>'\0'</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(char[],int)
- */
- public static char[] copyOfRange(char[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- char[] newArray = new char[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, '\0');
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>0d</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>0d</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>0d</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(double[],int,int)
- */
- public static double[] copyOf(double[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>0d</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>0d</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(double[],int)
- */
- public static double[] copyOfRange(double[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- double[] newArray = new double[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, 0d);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>0f</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>0f</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>0f</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(float[],int,int)
- */
- public static float[] copyOf(float[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>0f</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>0f</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(float[],int)
- */
- public static float[] copyOfRange(float[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- float[] newArray = new float[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, 0f);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>0</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>0</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>0</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(int[],int,int)
- */
- public static int[] copyOf(int[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>0</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>0</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(int[],int)
- */
- public static int[] copyOfRange(int[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- int[] newArray = new int[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, 0);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>0L</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>0L</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>0L</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(long[],int,int)
- */
- public static long[] copyOf(long[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>0L</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>0L</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(long[],int)
- */
- public static long[] copyOfRange(long[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- long[] newArray = new long[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, 0L);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>(short)0</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>(short)0</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>(short)0</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(short[],int,int)
- */
- public static short[] copyOf(short[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>(short)0</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>(short)0</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(short[],int)
- */
- public static short[] copyOfRange(short[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- short[] newArray = new short[to - from];
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, (short)0);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>null</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>null</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength)</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>null</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(T[],int,int)
- */
- public static <T> T[] copyOf(T[] original, int newLength)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>null</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>null</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(T[],int)
- */
- public static <T> T[] copyOfRange(T[] original, int from, int to)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- Class elemType = original.getClass().getComponentType();
- T[] newArray = (T[]) Array.newInstance(elemType, to - from);
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, null);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- /**
- * Returns a copy of the supplied array, truncating or padding as
- * necessary with <code>null</code> to obtain the specified length.
- * Indices that are valid for both arrays will return the same value.
- * Indices that only exist in the returned array (due to the new length
- * being greater than the original length) will return <code>null</code>.
- * This is equivalent to calling
- * <code>copyOfRange(original, 0, newLength, newType)</code>. The returned
- * array will be of the specified type, <code>newType</code>.
- *
- * @param original the original array to be copied.
- * @param newLength the length of the returned array.
- * @param newType the type of the returned array.
- * @return a copy of the original array, truncated or padded with
- * <code>null</code> to obtain the required length.
- * @throws NegativeArraySizeException if <code>newLength</code> is negative.
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOfRange(U[],int,int,Class)
- */
- public static <T,U> T[] copyOf(U[] original, int newLength,
- Class<? extends T[]> newType)
- {
- if (newLength < 0)
- throw new NegativeArraySizeException("The array size is negative.");
- return copyOfRange(original, 0, newLength, newType);
- }
- /**
- * Copies the specified range of the supplied array to a new
- * array, padding as necessary with <code>null</code>
- * if <code>to</code> is greater than the length of the original
- * array. <code>from</code> must be in the range zero to
- * <code>original.length</code> and can not be greater than
- * <code>to</code>. The initial element of the
- * returned array will be equal to <code>original[from]</code>,
- * except where <code>from</code> is equal to <code>to</code>
- * (where a zero-length array will be returned) or <code>
- * <code>from</code> is equal to <code>original.length</code>
- * (where an array padded with <code>null</code> will be
- * returned). The returned array is always of length
- * <code>to - from</code> and will be of the specified type,
- * <code>newType</code>.
- *
- * @param original the array from which to copy.
- * @param from the initial index of the range, inclusive.
- * @param to the final index of the range, exclusive.
- * @param newType the type of the returned array.
- * @return a copy of the specified range, with padding to
- * obtain the required length.
- * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
- * or <code>from > original.length</code>
- * @throws IllegalArgumentException if <code>from > to</code>
- * @throws NullPointerException if <code>original</code> is <code>null</code>.
- * @since 1.6
- * @see #copyOf(T[],int)
- */
- public static <T,U> T[] copyOfRange(U[] original, int from, int to,
- Class<? extends T[]> newType)
- {
- if (from > to)
- throw new IllegalArgumentException("The initial index is after " +
- "the final index.");
- T[] newArray = (T[]) Array.newInstance(newType.getComponentType(),
- to - from);
- if (to > original.length)
- {
- System.arraycopy(original, from, newArray, 0,
- original.length - from);
- fill(newArray, original.length, newArray.length, null);
- }
- else
- System.arraycopy(original, from, newArray, 0, to - from);
- return newArray;
- }
- }
|