Arrays.java 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035
  1. /* Arrays.java -- Utility class with methods to operate on arrays
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. import gnu.java.lang.CPStringBuilder;
  34. import java.io.Serializable;
  35. import java.lang.reflect.Array;
  36. /**
  37. * This class contains various static utility methods performing operations on
  38. * arrays, and a method to provide a List "view" of an array to facilitate
  39. * using arrays with Collection-based APIs. All methods throw a
  40. * {@link NullPointerException} if the parameter array is null.
  41. * <p>
  42. *
  43. * Implementations may use their own algorithms, but must obey the general
  44. * properties; for example, the sort must be stable and n*log(n) complexity.
  45. * Sun's implementation of sort, and therefore ours, is a tuned quicksort,
  46. * adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort
  47. * Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265
  48. * (November 1993). This algorithm offers n*log(n) performance on many data
  49. * sets that cause other quicksorts to degrade to quadratic performance.
  50. *
  51. * @author Original author unknown
  52. * @author Bryce McKinlay
  53. * @author Eric Blake (ebb9@email.byu.edu)
  54. * @see Comparable
  55. * @see Comparator
  56. * @since 1.2
  57. * @status updated to 1.4
  58. */
  59. public class Arrays
  60. {
  61. /**
  62. * This class is non-instantiable.
  63. */
  64. private Arrays()
  65. {
  66. }
  67. // binarySearch
  68. /**
  69. * Perform a binary search of a byte array for a key. The array must be
  70. * sorted (as by the sort() method) - if it is not, the behaviour of this
  71. * method is undefined, and may be an infinite loop. If the array contains
  72. * the key more than once, any one of them may be found. Note: although the
  73. * specification allows for an infinite loop if the array is unsorted, it
  74. * will not happen in this implementation.
  75. *
  76. * @param a the array to search (must be sorted)
  77. * @param key the value to search for
  78. * @return the index at which the key was found, or -n-1 if it was not
  79. * found, where n is the index of the first value higher than key or
  80. * a.length if there is no such value.
  81. */
  82. public static int binarySearch(byte[] a, byte key)
  83. {
  84. if (a.length == 0)
  85. return -1;
  86. return binarySearch(a, 0, a.length - 1, key);
  87. }
  88. /**
  89. * Perform a binary search of a range of a byte array for a key. The range
  90. * must be sorted (as by the <code>sort(byte[], int, int)</code> method) -
  91. * if it is not, the behaviour of this method is undefined, and may be an
  92. * infinite loop. If the array contains the key more than once, any one of
  93. * them may be found. Note: although the specification allows for an infinite
  94. * loop if the array is unsorted, it will not happen in this implementation.
  95. *
  96. * @param a the array to search (must be sorted)
  97. * @param low the lowest index to search from.
  98. * @param hi the highest index to search to.
  99. * @param key the value to search for
  100. * @return the index at which the key was found, or -n-1 if it was not
  101. * found, where n is the index of the first value higher than key or
  102. * a.length if there is no such value.
  103. * @throws IllegalArgumentException if <code>low > hi</code>
  104. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  105. * <code>hi > a.length</code>.
  106. */
  107. public static int binarySearch(byte[] a, int low, int hi, byte key)
  108. {
  109. if (low > hi)
  110. throw new IllegalArgumentException("The start index is higher than " +
  111. "the finish index.");
  112. if (low < 0 || hi > a.length)
  113. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  114. "of bounds.");
  115. int mid = 0;
  116. while (low <= hi)
  117. {
  118. mid = (low + hi) >>> 1;
  119. final byte d = a[mid];
  120. if (d == key)
  121. return mid;
  122. else if (d > key)
  123. hi = mid - 1;
  124. else
  125. // This gets the insertion point right on the last loop.
  126. low = ++mid;
  127. }
  128. return -mid - 1;
  129. }
  130. /**
  131. * Perform a binary search of a char array for a key. The array must be
  132. * sorted (as by the sort() method) - if it is not, the behaviour of this
  133. * method is undefined, and may be an infinite loop. If the array contains
  134. * the key more than once, any one of them may be found. Note: although the
  135. * specification allows for an infinite loop if the array is unsorted, it
  136. * will not happen in this implementation.
  137. *
  138. * @param a the array to search (must be sorted)
  139. * @param key the value to search for
  140. * @return the index at which the key was found, or -n-1 if it was not
  141. * found, where n is the index of the first value higher than key or
  142. * a.length if there is no such value.
  143. */
  144. public static int binarySearch(char[] a, char key)
  145. {
  146. if (a.length == 0)
  147. return -1;
  148. return binarySearch(a, 0, a.length - 1, key);
  149. }
  150. /**
  151. * Perform a binary search of a range of a char array for a key. The range
  152. * must be sorted (as by the <code>sort(char[], int, int)</code> method) -
  153. * if it is not, the behaviour of this method is undefined, and may be an
  154. * infinite loop. If the array contains the key more than once, any one of
  155. * them may be found. Note: although the specification allows for an infinite
  156. * loop if the array is unsorted, it will not happen in this implementation.
  157. *
  158. * @param a the array to search (must be sorted)
  159. * @param low the lowest index to search from.
  160. * @param hi the highest index to search to.
  161. * @param key the value to search for
  162. * @return the index at which the key was found, or -n-1 if it was not
  163. * found, where n is the index of the first value higher than key or
  164. * a.length if there is no such value.
  165. * @throws IllegalArgumentException if <code>low > hi</code>
  166. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  167. * <code>hi > a.length</code>.
  168. */
  169. public static int binarySearch(char[] a, int low, int hi, char key)
  170. {
  171. if (low > hi)
  172. throw new IllegalArgumentException("The start index is higher than " +
  173. "the finish index.");
  174. if (low < 0 || hi > a.length)
  175. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  176. "of bounds.");
  177. int mid = 0;
  178. while (low <= hi)
  179. {
  180. mid = (low + hi) >>> 1;
  181. final char d = a[mid];
  182. if (d == key)
  183. return mid;
  184. else if (d > key)
  185. hi = mid - 1;
  186. else
  187. // This gets the insertion point right on the last loop.
  188. low = ++mid;
  189. }
  190. return -mid - 1;
  191. }
  192. /**
  193. * Perform a binary search of a short array for a key. The array must be
  194. * sorted (as by the sort() method) - if it is not, the behaviour of this
  195. * method is undefined, and may be an infinite loop. If the array contains
  196. * the key more than once, any one of them may be found. Note: although the
  197. * specification allows for an infinite loop if the array is unsorted, it
  198. * will not happen in this implementation.
  199. *
  200. * @param a the array to search (must be sorted)
  201. * @param key the value to search for
  202. * @return the index at which the key was found, or -n-1 if it was not
  203. * found, where n is the index of the first value higher than key or
  204. * a.length if there is no such value.
  205. */
  206. public static int binarySearch(short[] a, short key)
  207. {
  208. if (a.length == 0)
  209. return -1;
  210. return binarySearch(a, 0, a.length - 1, key);
  211. }
  212. /**
  213. * Perform a binary search of a range of a short array for a key. The range
  214. * must be sorted (as by the <code>sort(short[], int, int)</code> method) -
  215. * if it is not, the behaviour of this method is undefined, and may be an
  216. * infinite loop. If the array contains the key more than once, any one of
  217. * them may be found. Note: although the specification allows for an infinite
  218. * loop if the array is unsorted, it will not happen in this implementation.
  219. *
  220. * @param a the array to search (must be sorted)
  221. * @param low the lowest index to search from.
  222. * @param hi the highest index to search to.
  223. * @param key the value to search for
  224. * @return the index at which the key was found, or -n-1 if it was not
  225. * found, where n is the index of the first value higher than key or
  226. * a.length if there is no such value.
  227. * @throws IllegalArgumentException if <code>low > hi</code>
  228. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  229. * <code>hi > a.length</code>.
  230. */
  231. public static int binarySearch(short[] a, int low, int hi, short key)
  232. {
  233. if (low > hi)
  234. throw new IllegalArgumentException("The start index is higher than " +
  235. "the finish index.");
  236. if (low < 0 || hi > a.length)
  237. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  238. "of bounds.");
  239. int mid = 0;
  240. while (low <= hi)
  241. {
  242. mid = (low + hi) >>> 1;
  243. final short d = a[mid];
  244. if (d == key)
  245. return mid;
  246. else if (d > key)
  247. hi = mid - 1;
  248. else
  249. // This gets the insertion point right on the last loop.
  250. low = ++mid;
  251. }
  252. return -mid - 1;
  253. }
  254. /**
  255. * Perform a binary search of an int array for a key. The array must be
  256. * sorted (as by the sort() method) - if it is not, the behaviour of this
  257. * method is undefined, and may be an infinite loop. If the array contains
  258. * the key more than once, any one of them may be found. Note: although the
  259. * specification allows for an infinite loop if the array is unsorted, it
  260. * will not happen in this implementation.
  261. *
  262. * @param a the array to search (must be sorted)
  263. * @param key the value to search for
  264. * @return the index at which the key was found, or -n-1 if it was not
  265. * found, where n is the index of the first value higher than key or
  266. * a.length if there is no such value.
  267. */
  268. public static int binarySearch(int[] a, int key)
  269. {
  270. if (a.length == 0)
  271. return -1;
  272. return binarySearch(a, 0, a.length - 1, key);
  273. }
  274. /**
  275. * Perform a binary search of a range of an integer array for a key. The range
  276. * must be sorted (as by the <code>sort(int[], int, int)</code> method) -
  277. * if it is not, the behaviour of this method is undefined, and may be an
  278. * infinite loop. If the array contains the key more than once, any one of
  279. * them may be found. Note: although the specification allows for an infinite
  280. * loop if the array is unsorted, it will not happen in this implementation.
  281. *
  282. * @param a the array to search (must be sorted)
  283. * @param low the lowest index to search from.
  284. * @param hi the highest index to search to.
  285. * @param key the value to search for
  286. * @return the index at which the key was found, or -n-1 if it was not
  287. * found, where n is the index of the first value higher than key or
  288. * a.length if there is no such value.
  289. * @throws IllegalArgumentException if <code>low > hi</code>
  290. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  291. * <code>hi > a.length</code>.
  292. */
  293. public static int binarySearch(int[] a, int low, int hi, int key)
  294. {
  295. if (low > hi)
  296. throw new IllegalArgumentException("The start index is higher than " +
  297. "the finish index.");
  298. if (low < 0 || hi > a.length)
  299. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  300. "of bounds.");
  301. int mid = 0;
  302. while (low <= hi)
  303. {
  304. mid = (low + hi) >>> 1;
  305. final int d = a[mid];
  306. if (d == key)
  307. return mid;
  308. else if (d > key)
  309. hi = mid - 1;
  310. else
  311. // This gets the insertion point right on the last loop.
  312. low = ++mid;
  313. }
  314. return -mid - 1;
  315. }
  316. /**
  317. * Perform a binary search of a long array for a key. The array must be
  318. * sorted (as by the sort() method) - if it is not, the behaviour of this
  319. * method is undefined, and may be an infinite loop. If the array contains
  320. * the key more than once, any one of them may be found. Note: although the
  321. * specification allows for an infinite loop if the array is unsorted, it
  322. * will not happen in this implementation.
  323. *
  324. * @param a the array to search (must be sorted)
  325. * @param key the value to search for
  326. * @return the index at which the key was found, or -n-1 if it was not
  327. * found, where n is the index of the first value higher than key or
  328. * a.length if there is no such value.
  329. */
  330. public static int binarySearch(long[] a, long key)
  331. {
  332. if (a.length == 0)
  333. return -1;
  334. return binarySearch(a, 0, a.length - 1, key);
  335. }
  336. /**
  337. * Perform a binary search of a range of a long array for a key. The range
  338. * must be sorted (as by the <code>sort(long[], int, int)</code> method) -
  339. * if it is not, the behaviour of this method is undefined, and may be an
  340. * infinite loop. If the array contains the key more than once, any one of
  341. * them may be found. Note: although the specification allows for an infinite
  342. * loop if the array is unsorted, it will not happen in this implementation.
  343. *
  344. * @param a the array to search (must be sorted)
  345. * @param low the lowest index to search from.
  346. * @param hi the highest index to search to.
  347. * @param key the value to search for
  348. * @return the index at which the key was found, or -n-1 if it was not
  349. * found, where n is the index of the first value higher than key or
  350. * a.length if there is no such value.
  351. * @throws IllegalArgumentException if <code>low > hi</code>
  352. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  353. * <code>hi > a.length</code>.
  354. */
  355. public static int binarySearch(long[] a, int low, int hi, long key)
  356. {
  357. if (low > hi)
  358. throw new IllegalArgumentException("The start index is higher than " +
  359. "the finish index.");
  360. if (low < 0 || hi > a.length)
  361. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  362. "of bounds.");
  363. int mid = 0;
  364. while (low <= hi)
  365. {
  366. mid = (low + hi) >>> 1;
  367. final long d = a[mid];
  368. if (d == key)
  369. return mid;
  370. else if (d > key)
  371. hi = mid - 1;
  372. else
  373. // This gets the insertion point right on the last loop.
  374. low = ++mid;
  375. }
  376. return -mid - 1;
  377. }
  378. /**
  379. * Perform a binary search of a float array for a key. The array must be
  380. * sorted (as by the sort() method) - if it is not, the behaviour of this
  381. * method is undefined, and may be an infinite loop. If the array contains
  382. * the key more than once, any one of them may be found. Note: although the
  383. * specification allows for an infinite loop if the array is unsorted, it
  384. * will not happen in this implementation.
  385. *
  386. * @param a the array to search (must be sorted)
  387. * @param key the value to search for
  388. * @return the index at which the key was found, or -n-1 if it was not
  389. * found, where n is the index of the first value higher than key or
  390. * a.length if there is no such value.
  391. */
  392. public static int binarySearch(float[] a, float key)
  393. {
  394. if (a.length == 0)
  395. return -1;
  396. return binarySearch(a, 0, a.length - 1, key);
  397. }
  398. /**
  399. * Perform a binary search of a range of a float array for a key. The range
  400. * must be sorted (as by the <code>sort(float[], int, int)</code> method) -
  401. * if it is not, the behaviour of this method is undefined, and may be an
  402. * infinite loop. If the array contains the key more than once, any one of
  403. * them may be found. Note: although the specification allows for an infinite
  404. * loop if the array is unsorted, it will not happen in this implementation.
  405. *
  406. * @param a the array to search (must be sorted)
  407. * @param low the lowest index to search from.
  408. * @param hi the highest index to search to.
  409. * @param key the value to search for
  410. * @return the index at which the key was found, or -n-1 if it was not
  411. * found, where n is the index of the first value higher than key or
  412. * a.length if there is no such value.
  413. * @throws IllegalArgumentException if <code>low > hi</code>
  414. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  415. * <code>hi > a.length</code>.
  416. */
  417. public static int binarySearch(float[] a, int low, int hi, float key)
  418. {
  419. if (low > hi)
  420. throw new IllegalArgumentException("The start index is higher than " +
  421. "the finish index.");
  422. if (low < 0 || hi > a.length)
  423. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  424. "of bounds.");
  425. // Must use Float.compare to take into account NaN, +-0.
  426. int mid = 0;
  427. while (low <= hi)
  428. {
  429. mid = (low + hi) >>> 1;
  430. final int r = Float.compare(a[mid], key);
  431. if (r == 0)
  432. return mid;
  433. else if (r > 0)
  434. hi = mid - 1;
  435. else
  436. // This gets the insertion point right on the last loop
  437. low = ++mid;
  438. }
  439. return -mid - 1;
  440. }
  441. /**
  442. * Perform a binary search of a double array for a key. The array must be
  443. * sorted (as by the sort() method) - if it is not, the behaviour of this
  444. * method is undefined, and may be an infinite loop. If the array contains
  445. * the key more than once, any one of them may be found. Note: although the
  446. * specification allows for an infinite loop if the array is unsorted, it
  447. * will not happen in this implementation.
  448. *
  449. * @param a the array to search (must be sorted)
  450. * @param key the value to search for
  451. * @return the index at which the key was found, or -n-1 if it was not
  452. * found, where n is the index of the first value higher than key or
  453. * a.length if there is no such value.
  454. */
  455. public static int binarySearch(double[] a, double key)
  456. {
  457. if (a.length == 0)
  458. return -1;
  459. return binarySearch(a, 0, a.length - 1, key);
  460. }
  461. /**
  462. * Perform a binary search of a range of a double array for a key. The range
  463. * must be sorted (as by the <code>sort(double[], int, int)</code> method) -
  464. * if it is not, the behaviour of this method is undefined, and may be an
  465. * infinite loop. If the array contains the key more than once, any one of
  466. * them may be found. Note: although the specification allows for an infinite
  467. * loop if the array is unsorted, it will not happen in this implementation.
  468. *
  469. * @param a the array to search (must be sorted)
  470. * @param low the lowest index to search from.
  471. * @param hi the highest index to search to.
  472. * @param key the value to search for
  473. * @return the index at which the key was found, or -n-1 if it was not
  474. * found, where n is the index of the first value higher than key or
  475. * a.length if there is no such value.
  476. * @throws IllegalArgumentException if <code>low > hi</code>
  477. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  478. * <code>hi > a.length</code>.
  479. */
  480. public static int binarySearch(double[] a, int low, int hi, double key)
  481. {
  482. if (low > hi)
  483. throw new IllegalArgumentException("The start index is higher than " +
  484. "the finish index.");
  485. if (low < 0 || hi > a.length)
  486. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  487. "of bounds.");
  488. // Must use Double.compare to take into account NaN, +-0.
  489. int mid = 0;
  490. while (low <= hi)
  491. {
  492. mid = (low + hi) >>> 1;
  493. final int r = Double.compare(a[mid], key);
  494. if (r == 0)
  495. return mid;
  496. else if (r > 0)
  497. hi = mid - 1;
  498. else
  499. // This gets the insertion point right on the last loop
  500. low = ++mid;
  501. }
  502. return -mid - 1;
  503. }
  504. /**
  505. * Perform a binary search of an Object array for a key, using the natural
  506. * ordering of the elements. The array must be sorted (as by the sort()
  507. * method) - if it is not, the behaviour of this method is undefined, and may
  508. * be an infinite loop. Further, the key must be comparable with every item
  509. * in the array. If the array contains the key more than once, any one of
  510. * them may be found. Note: although the specification allows for an infinite
  511. * loop if the array is unsorted, it will not happen in this (JCL)
  512. * implementation.
  513. *
  514. * @param a the array to search (must be sorted)
  515. * @param key the value to search for
  516. * @return the index at which the key was found, or -n-1 if it was not
  517. * found, where n is the index of the first value higher than key or
  518. * a.length if there is no such value.
  519. * @throws ClassCastException if key could not be compared with one of the
  520. * elements of a
  521. * @throws NullPointerException if a null element in a is compared
  522. */
  523. public static int binarySearch(Object[] a, Object key)
  524. {
  525. if (a.length == 0)
  526. return -1;
  527. return binarySearch(a, key, null);
  528. }
  529. /**
  530. * Perform a binary search of a range of an Object array for a key. The range
  531. * must be sorted (as by the <code>sort(Object[], int, int)</code> method) -
  532. * if it is not, the behaviour of this method is undefined, and may be an
  533. * infinite loop. If the array contains the key more than once, any one of
  534. * them may be found. Note: although the specification allows for an infinite
  535. * loop if the array is unsorted, it will not happen in this implementation.
  536. *
  537. * @param a the array to search (must be sorted)
  538. * @param low the lowest index to search from.
  539. * @param hi the highest index to search to.
  540. * @param key the value to search for
  541. * @return the index at which the key was found, or -n-1 if it was not
  542. * found, where n is the index of the first value higher than key or
  543. * a.length if there is no such value.
  544. */
  545. public static int binarySearch(Object[] a, int low, int hi, Object key)
  546. {
  547. return binarySearch(a, low, hi, key, null);
  548. }
  549. /**
  550. * Perform a binary search of an Object array for a key, using a supplied
  551. * Comparator. The array must be sorted (as by the sort() method with the
  552. * same Comparator) - if it is not, the behaviour of this method is
  553. * undefined, and may be an infinite loop. Further, the key must be
  554. * comparable with every item in the array. If the array contains the key
  555. * more than once, any one of them may be found. Note: although the
  556. * specification allows for an infinite loop if the array is unsorted, it
  557. * will not happen in this (JCL) implementation.
  558. *
  559. * @param a the array to search (must be sorted)
  560. * @param key the value to search for
  561. * @param c the comparator by which the array is sorted; or null to
  562. * use the elements' natural order
  563. * @return the index at which the key was found, or -n-1 if it was not
  564. * found, where n is the index of the first value higher than key or
  565. * a.length if there is no such value.
  566. * @throws ClassCastException if key could not be compared with one of the
  567. * elements of a
  568. * @throws NullPointerException if a null element is compared with natural
  569. * ordering (only possible when c is null)
  570. */
  571. public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
  572. {
  573. if (a.length == 0)
  574. return -1;
  575. return binarySearch(a, 0, a.length - 1, key, c);
  576. }
  577. /**
  578. * Perform a binary search of a range of an Object array for a key using
  579. * a {@link Comparator}. The range must be sorted (as by the
  580. * <code>sort(Object[], int, int)</code> method) - if it is not, the
  581. * behaviour of this method is undefined, and may be an infinite loop. If
  582. * the array contains the key more than once, any one of them may be found.
  583. * Note: although the specification allows for an infinite loop if the array
  584. * is unsorted, it will not happen in this implementation.
  585. *
  586. * @param a the array to search (must be sorted)
  587. * @param low the lowest index to search from.
  588. * @param hi the highest index to search to.
  589. * @param key the value to search for
  590. * @param c the comparator by which the array is sorted; or null to
  591. * use the elements' natural order
  592. * @return the index at which the key was found, or -n-1 if it was not
  593. * found, where n is the index of the first value higher than key or
  594. * a.length if there is no such value.
  595. * @throws ClassCastException if key could not be compared with one of the
  596. * elements of a
  597. * @throws IllegalArgumentException if <code>low > hi</code>
  598. * @throws ArrayIndexOutOfBoundsException if <code>low < 0</code> or
  599. * <code>hi > a.length</code>.
  600. */
  601. public static <T> int binarySearch(T[] a, int low, int hi, T key,
  602. Comparator<? super T> c)
  603. {
  604. if (low > hi)
  605. throw new IllegalArgumentException("The start index is higher than " +
  606. "the finish index.");
  607. if (low < 0 || hi > a.length)
  608. throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
  609. "of bounds.");
  610. int mid = 0;
  611. while (low <= hi)
  612. {
  613. mid = (low + hi) >>> 1;
  614. // NOTE: Please keep the order of a[mid] and key. Although
  615. // not required by the specs, the RI has it in this order as
  616. // well, and real programs (erroneously) depend on it.
  617. final int d = Collections.compare(a[mid], key, c);
  618. if (d == 0)
  619. return mid;
  620. else if (d > 0)
  621. hi = mid - 1;
  622. else
  623. // This gets the insertion point right on the last loop
  624. low = ++mid;
  625. }
  626. return -mid - 1;
  627. }
  628. // equals
  629. /**
  630. * Compare two boolean arrays for equality.
  631. *
  632. * @param a1 the first array to compare
  633. * @param a2 the second array to compare
  634. * @return true if a1 and a2 are both null, or if a2 is of the same length
  635. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  636. */
  637. public static boolean equals(boolean[] a1, boolean[] a2)
  638. {
  639. // Quick test which saves comparing elements of the same array, and also
  640. // catches the case that both are null.
  641. if (a1 == a2)
  642. return true;
  643. if (null == a1 || null == a2)
  644. return false;
  645. // If they're the same length, test each element
  646. if (a1.length == a2.length)
  647. {
  648. int i = a1.length;
  649. while (--i >= 0)
  650. if (a1[i] != a2[i])
  651. return false;
  652. return true;
  653. }
  654. return false;
  655. }
  656. /**
  657. * Compare two byte arrays for equality.
  658. *
  659. * @param a1 the first array to compare
  660. * @param a2 the second array to compare
  661. * @return true if a1 and a2 are both null, or if a2 is of the same length
  662. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  663. */
  664. public static boolean equals(byte[] a1, byte[] a2)
  665. {
  666. // Quick test which saves comparing elements of the same array, and also
  667. // catches the case that both are null.
  668. if (a1 == a2)
  669. return true;
  670. if (null == a1 || null == a2)
  671. return false;
  672. // If they're the same length, test each element
  673. if (a1.length == a2.length)
  674. {
  675. int i = a1.length;
  676. while (--i >= 0)
  677. if (a1[i] != a2[i])
  678. return false;
  679. return true;
  680. }
  681. return false;
  682. }
  683. /**
  684. * Compare two char arrays for equality.
  685. *
  686. * @param a1 the first array to compare
  687. * @param a2 the second array to compare
  688. * @return true if a1 and a2 are both null, or if a2 is of the same length
  689. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  690. */
  691. public static boolean equals(char[] a1, char[] a2)
  692. {
  693. // Quick test which saves comparing elements of the same array, and also
  694. // catches the case that both are null.
  695. if (a1 == a2)
  696. return true;
  697. if (null == a1 || null == a2)
  698. return false;
  699. // If they're the same length, test each element
  700. if (a1.length == a2.length)
  701. {
  702. int i = a1.length;
  703. while (--i >= 0)
  704. if (a1[i] != a2[i])
  705. return false;
  706. return true;
  707. }
  708. return false;
  709. }
  710. /**
  711. * Compare two short arrays for equality.
  712. *
  713. * @param a1 the first array to compare
  714. * @param a2 the second array to compare
  715. * @return true if a1 and a2 are both null, or if a2 is of the same length
  716. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  717. */
  718. public static boolean equals(short[] a1, short[] a2)
  719. {
  720. // Quick test which saves comparing elements of the same array, and also
  721. // catches the case that both are null.
  722. if (a1 == a2)
  723. return true;
  724. if (null == a1 || null == a2)
  725. return false;
  726. // If they're the same length, test each element
  727. if (a1.length == a2.length)
  728. {
  729. int i = a1.length;
  730. while (--i >= 0)
  731. if (a1[i] != a2[i])
  732. return false;
  733. return true;
  734. }
  735. return false;
  736. }
  737. /**
  738. * Compare two int arrays for equality.
  739. *
  740. * @param a1 the first array to compare
  741. * @param a2 the second array to compare
  742. * @return true if a1 and a2 are both null, or if a2 is of the same length
  743. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  744. */
  745. public static boolean equals(int[] a1, int[] a2)
  746. {
  747. // Quick test which saves comparing elements of the same array, and also
  748. // catches the case that both are null.
  749. if (a1 == a2)
  750. return true;
  751. if (null == a1 || null == a2)
  752. return false;
  753. // If they're the same length, test each element
  754. if (a1.length == a2.length)
  755. {
  756. int i = a1.length;
  757. while (--i >= 0)
  758. if (a1[i] != a2[i])
  759. return false;
  760. return true;
  761. }
  762. return false;
  763. }
  764. /**
  765. * Compare two long arrays for equality.
  766. *
  767. * @param a1 the first array to compare
  768. * @param a2 the second array to compare
  769. * @return true if a1 and a2 are both null, or if a2 is of the same length
  770. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  771. */
  772. public static boolean equals(long[] a1, long[] a2)
  773. {
  774. // Quick test which saves comparing elements of the same array, and also
  775. // catches the case that both are null.
  776. if (a1 == a2)
  777. return true;
  778. if (null == a1 || null == a2)
  779. return false;
  780. // If they're the same length, test each element
  781. if (a1.length == a2.length)
  782. {
  783. int i = a1.length;
  784. while (--i >= 0)
  785. if (a1[i] != a2[i])
  786. return false;
  787. return true;
  788. }
  789. return false;
  790. }
  791. /**
  792. * Compare two float arrays for equality.
  793. *
  794. * @param a1 the first array to compare
  795. * @param a2 the second array to compare
  796. * @return true if a1 and a2 are both null, or if a2 is of the same length
  797. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  798. */
  799. public static boolean equals(float[] a1, float[] a2)
  800. {
  801. // Quick test which saves comparing elements of the same array, and also
  802. // catches the case that both are null.
  803. if (a1 == a2)
  804. return true;
  805. if (null == a1 || null == a2)
  806. return false;
  807. // Must use Float.compare to take into account NaN, +-0.
  808. // If they're the same length, test each element
  809. if (a1.length == a2.length)
  810. {
  811. int i = a1.length;
  812. while (--i >= 0)
  813. if (Float.compare(a1[i], a2[i]) != 0)
  814. return false;
  815. return true;
  816. }
  817. return false;
  818. }
  819. /**
  820. * Compare two double arrays for equality.
  821. *
  822. * @param a1 the first array to compare
  823. * @param a2 the second array to compare
  824. * @return true if a1 and a2 are both null, or if a2 is of the same length
  825. * as a1, and for each 0 <= i < a1.length, a1[i] == a2[i]
  826. */
  827. public static boolean equals(double[] a1, double[] a2)
  828. {
  829. // Quick test which saves comparing elements of the same array, and also
  830. // catches the case that both are null.
  831. if (a1 == a2)
  832. return true;
  833. if (null == a1 || null == a2)
  834. return false;
  835. // Must use Double.compare to take into account NaN, +-0.
  836. // If they're the same length, test each element
  837. if (a1.length == a2.length)
  838. {
  839. int i = a1.length;
  840. while (--i >= 0)
  841. if (Double.compare(a1[i], a2[i]) != 0)
  842. return false;
  843. return true;
  844. }
  845. return false;
  846. }
  847. /**
  848. * Compare two Object arrays for equality.
  849. *
  850. * @param a1 the first array to compare
  851. * @param a2 the second array to compare
  852. * @return true if a1 and a2 are both null, or if a1 is of the same length
  853. * as a2, and for each 0 <= i < a.length, a1[i] == null ?
  854. * a2[i] == null : a1[i].equals(a2[i]).
  855. */
  856. public static boolean equals(Object[] a1, Object[] a2)
  857. {
  858. // Quick test which saves comparing elements of the same array, and also
  859. // catches the case that both are null.
  860. if (a1 == a2)
  861. return true;
  862. if (null == a1 || null == a2)
  863. return false;
  864. // If they're the same length, test each element
  865. if (a1.length == a2.length)
  866. {
  867. int i = a1.length;
  868. while (--i >= 0)
  869. if (! AbstractCollection.equals(a1[i], a2[i]))
  870. return false;
  871. return true;
  872. }
  873. return false;
  874. }
  875. // fill
  876. /**
  877. * Fill an array with a boolean value.
  878. *
  879. * @param a the array to fill
  880. * @param val the value to fill it with
  881. */
  882. public static void fill(boolean[] a, boolean val)
  883. {
  884. fill(a, 0, a.length, val);
  885. }
  886. /**
  887. * Fill a range of an array with a boolean value.
  888. *
  889. * @param a the array to fill
  890. * @param fromIndex the index to fill from, inclusive
  891. * @param toIndex the index to fill to, exclusive
  892. * @param val the value to fill with
  893. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  894. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  895. * || toIndex &gt; a.length
  896. */
  897. public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
  898. {
  899. if (fromIndex > toIndex)
  900. throw new IllegalArgumentException();
  901. for (int i = fromIndex; i < toIndex; i++)
  902. a[i] = val;
  903. }
  904. /**
  905. * Fill an array with a byte value.
  906. *
  907. * @param a the array to fill
  908. * @param val the value to fill it with
  909. */
  910. public static void fill(byte[] a, byte val)
  911. {
  912. fill(a, 0, a.length, val);
  913. }
  914. /**
  915. * Fill a range of an array with a byte value.
  916. *
  917. * @param a the array to fill
  918. * @param fromIndex the index to fill from, inclusive
  919. * @param toIndex the index to fill to, exclusive
  920. * @param val the value to fill with
  921. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  922. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  923. * || toIndex &gt; a.length
  924. */
  925. public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
  926. {
  927. if (fromIndex > toIndex)
  928. throw new IllegalArgumentException();
  929. for (int i = fromIndex; i < toIndex; i++)
  930. a[i] = val;
  931. }
  932. /**
  933. * Fill an array with a char value.
  934. *
  935. * @param a the array to fill
  936. * @param val the value to fill it with
  937. */
  938. public static void fill(char[] a, char val)
  939. {
  940. fill(a, 0, a.length, val);
  941. }
  942. /**
  943. * Fill a range of an array with a char value.
  944. *
  945. * @param a the array to fill
  946. * @param fromIndex the index to fill from, inclusive
  947. * @param toIndex the index to fill to, exclusive
  948. * @param val the value to fill with
  949. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  950. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  951. * || toIndex &gt; a.length
  952. */
  953. public static void fill(char[] a, int fromIndex, int toIndex, char val)
  954. {
  955. if (fromIndex > toIndex)
  956. throw new IllegalArgumentException();
  957. for (int i = fromIndex; i < toIndex; i++)
  958. a[i] = val;
  959. }
  960. /**
  961. * Fill an array with a short value.
  962. *
  963. * @param a the array to fill
  964. * @param val the value to fill it with
  965. */
  966. public static void fill(short[] a, short val)
  967. {
  968. fill(a, 0, a.length, val);
  969. }
  970. /**
  971. * Fill a range of an array with a short value.
  972. *
  973. * @param a the array to fill
  974. * @param fromIndex the index to fill from, inclusive
  975. * @param toIndex the index to fill to, exclusive
  976. * @param val the value to fill with
  977. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  978. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  979. * || toIndex &gt; a.length
  980. */
  981. public static void fill(short[] a, int fromIndex, int toIndex, short val)
  982. {
  983. if (fromIndex > toIndex)
  984. throw new IllegalArgumentException();
  985. for (int i = fromIndex; i < toIndex; i++)
  986. a[i] = val;
  987. }
  988. /**
  989. * Fill an array with an int value.
  990. *
  991. * @param a the array to fill
  992. * @param val the value to fill it with
  993. */
  994. public static void fill(int[] a, int val)
  995. {
  996. fill(a, 0, a.length, val);
  997. }
  998. /**
  999. * Fill a range of an array with an int value.
  1000. *
  1001. * @param a the array to fill
  1002. * @param fromIndex the index to fill from, inclusive
  1003. * @param toIndex the index to fill to, exclusive
  1004. * @param val the value to fill with
  1005. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1006. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1007. * || toIndex &gt; a.length
  1008. */
  1009. public static void fill(int[] a, int fromIndex, int toIndex, int val)
  1010. {
  1011. if (fromIndex > toIndex)
  1012. throw new IllegalArgumentException();
  1013. for (int i = fromIndex; i < toIndex; i++)
  1014. a[i] = val;
  1015. }
  1016. /**
  1017. * Fill an array with a long value.
  1018. *
  1019. * @param a the array to fill
  1020. * @param val the value to fill it with
  1021. */
  1022. public static void fill(long[] a, long val)
  1023. {
  1024. fill(a, 0, a.length, val);
  1025. }
  1026. /**
  1027. * Fill a range of an array with a long value.
  1028. *
  1029. * @param a the array to fill
  1030. * @param fromIndex the index to fill from, inclusive
  1031. * @param toIndex the index to fill to, exclusive
  1032. * @param val the value to fill with
  1033. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1034. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1035. * || toIndex &gt; a.length
  1036. */
  1037. public static void fill(long[] a, int fromIndex, int toIndex, long val)
  1038. {
  1039. if (fromIndex > toIndex)
  1040. throw new IllegalArgumentException();
  1041. for (int i = fromIndex; i < toIndex; i++)
  1042. a[i] = val;
  1043. }
  1044. /**
  1045. * Fill an array with a float value.
  1046. *
  1047. * @param a the array to fill
  1048. * @param val the value to fill it with
  1049. */
  1050. public static void fill(float[] a, float val)
  1051. {
  1052. fill(a, 0, a.length, val);
  1053. }
  1054. /**
  1055. * Fill a range of an array with a float value.
  1056. *
  1057. * @param a the array to fill
  1058. * @param fromIndex the index to fill from, inclusive
  1059. * @param toIndex the index to fill to, exclusive
  1060. * @param val the value to fill with
  1061. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1062. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1063. * || toIndex &gt; a.length
  1064. */
  1065. public static void fill(float[] a, int fromIndex, int toIndex, float val)
  1066. {
  1067. if (fromIndex > toIndex)
  1068. throw new IllegalArgumentException();
  1069. for (int i = fromIndex; i < toIndex; i++)
  1070. a[i] = val;
  1071. }
  1072. /**
  1073. * Fill an array with a double value.
  1074. *
  1075. * @param a the array to fill
  1076. * @param val the value to fill it with
  1077. */
  1078. public static void fill(double[] a, double val)
  1079. {
  1080. fill(a, 0, a.length, val);
  1081. }
  1082. /**
  1083. * Fill a range of an array with a double value.
  1084. *
  1085. * @param a the array to fill
  1086. * @param fromIndex the index to fill from, inclusive
  1087. * @param toIndex the index to fill to, exclusive
  1088. * @param val the value to fill with
  1089. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1090. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1091. * || toIndex &gt; a.length
  1092. */
  1093. public static void fill(double[] a, int fromIndex, int toIndex, double val)
  1094. {
  1095. if (fromIndex > toIndex)
  1096. throw new IllegalArgumentException();
  1097. for (int i = fromIndex; i < toIndex; i++)
  1098. a[i] = val;
  1099. }
  1100. /**
  1101. * Fill an array with an Object value.
  1102. *
  1103. * @param a the array to fill
  1104. * @param val the value to fill it with
  1105. * @throws ClassCastException if val is not an instance of the element
  1106. * type of a.
  1107. */
  1108. public static void fill(Object[] a, Object val)
  1109. {
  1110. fill(a, 0, a.length, val);
  1111. }
  1112. /**
  1113. * Fill a range of an array with an Object value.
  1114. *
  1115. * @param a the array to fill
  1116. * @param fromIndex the index to fill from, inclusive
  1117. * @param toIndex the index to fill to, exclusive
  1118. * @param val the value to fill with
  1119. * @throws ClassCastException if val is not an instance of the element
  1120. * type of a.
  1121. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1122. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1123. * || toIndex &gt; a.length
  1124. */
  1125. public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
  1126. {
  1127. if (fromIndex > toIndex)
  1128. throw new IllegalArgumentException();
  1129. for (int i = fromIndex; i < toIndex; i++)
  1130. a[i] = val;
  1131. }
  1132. // sort
  1133. // Thanks to Paul Fisher (rao@gnu.org) for finding this quicksort algorithm
  1134. // as specified by Sun and porting it to Java. The algorithm is an optimised
  1135. // quicksort, as described in Jon L. Bentley and M. Douglas McIlroy's
  1136. // "Engineering a Sort Function", Software-Practice and Experience, Vol.
  1137. // 23(11) P. 1249-1265 (November 1993). This algorithm gives n*log(n)
  1138. // performance on many arrays that would take quadratic time with a standard
  1139. // quicksort.
  1140. /**
  1141. * Performs a stable sort on the elements, arranging them according to their
  1142. * natural order.
  1143. *
  1144. * @param a the byte array to sort
  1145. */
  1146. public static void sort(byte[] a)
  1147. {
  1148. qsort(a, 0, a.length);
  1149. }
  1150. /**
  1151. * Performs a stable sort on the elements, arranging them according to their
  1152. * natural order.
  1153. *
  1154. * @param a the byte array to sort
  1155. * @param fromIndex the first index to sort (inclusive)
  1156. * @param toIndex the last index to sort (exclusive)
  1157. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1158. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1159. * || toIndex &gt; a.length
  1160. */
  1161. public static void sort(byte[] a, int fromIndex, int toIndex)
  1162. {
  1163. if (fromIndex > toIndex)
  1164. throw new IllegalArgumentException();
  1165. if (fromIndex < 0)
  1166. throw new ArrayIndexOutOfBoundsException();
  1167. qsort(a, fromIndex, toIndex - fromIndex);
  1168. }
  1169. /**
  1170. * Finds the index of the median of three array elements.
  1171. *
  1172. * @param a the first index
  1173. * @param b the second index
  1174. * @param c the third index
  1175. * @param d the array
  1176. * @return the index (a, b, or c) which has the middle value of the three
  1177. */
  1178. private static int med3(int a, int b, int c, byte[] d)
  1179. {
  1180. return (d[a] < d[b]
  1181. ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
  1182. : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
  1183. }
  1184. /**
  1185. * Swaps the elements at two locations of an array
  1186. *
  1187. * @param i the first index
  1188. * @param j the second index
  1189. * @param a the array
  1190. */
  1191. private static void swap(int i, int j, byte[] a)
  1192. {
  1193. byte c = a[i];
  1194. a[i] = a[j];
  1195. a[j] = c;
  1196. }
  1197. /**
  1198. * Swaps two ranges of an array.
  1199. *
  1200. * @param i the first range start
  1201. * @param j the second range start
  1202. * @param n the element count
  1203. * @param a the array
  1204. */
  1205. private static void vecswap(int i, int j, int n, byte[] a)
  1206. {
  1207. for ( ; n > 0; i++, j++, n--)
  1208. swap(i, j, a);
  1209. }
  1210. /**
  1211. * Performs a recursive modified quicksort.
  1212. *
  1213. * @param array the array to sort
  1214. * @param from the start index (inclusive)
  1215. * @param count the number of elements to sort
  1216. */
  1217. private static void qsort(byte[] array, int from, int count)
  1218. {
  1219. // Use an insertion sort on small arrays.
  1220. if (count <= 7)
  1221. {
  1222. for (int i = from + 1; i < from + count; i++)
  1223. for (int j = i; j > from && array[j - 1] > array[j]; j--)
  1224. swap(j, j - 1, array);
  1225. return;
  1226. }
  1227. // Determine a good median element.
  1228. int mid = from + count / 2;
  1229. int lo = from;
  1230. int hi = from + count - 1;
  1231. if (count > 40)
  1232. { // big arrays, pseudomedian of 9
  1233. int s = count / 8;
  1234. lo = med3(lo, lo + s, lo + 2 * s, array);
  1235. mid = med3(mid - s, mid, mid + s, array);
  1236. hi = med3(hi - 2 * s, hi - s, hi, array);
  1237. }
  1238. mid = med3(lo, mid, hi, array);
  1239. int a, b, c, d;
  1240. int comp;
  1241. // Pull the median element out of the fray, and use it as a pivot.
  1242. swap(from, mid, array);
  1243. a = b = from;
  1244. c = d = from + count - 1;
  1245. // Repeatedly move b and c to each other, swapping elements so
  1246. // that all elements before index b are less than the pivot, and all
  1247. // elements after index c are greater than the pivot. a and b track
  1248. // the elements equal to the pivot.
  1249. while (true)
  1250. {
  1251. while (b <= c && (comp = array[b] - array[from]) <= 0)
  1252. {
  1253. if (comp == 0)
  1254. {
  1255. swap(a, b, array);
  1256. a++;
  1257. }
  1258. b++;
  1259. }
  1260. while (c >= b && (comp = array[c] - array[from]) >= 0)
  1261. {
  1262. if (comp == 0)
  1263. {
  1264. swap(c, d, array);
  1265. d--;
  1266. }
  1267. c--;
  1268. }
  1269. if (b > c)
  1270. break;
  1271. swap(b, c, array);
  1272. b++;
  1273. c--;
  1274. }
  1275. // Swap pivot(s) back in place, the recurse on left and right sections.
  1276. hi = from + count;
  1277. int span;
  1278. span = Math.min(a - from, b - a);
  1279. vecswap(from, b - span, span, array);
  1280. span = Math.min(d - c, hi - d - 1);
  1281. vecswap(b, hi - span, span, array);
  1282. span = b - a;
  1283. if (span > 1)
  1284. qsort(array, from, span);
  1285. span = d - c;
  1286. if (span > 1)
  1287. qsort(array, hi - span, span);
  1288. }
  1289. /**
  1290. * Performs a stable sort on the elements, arranging them according to their
  1291. * natural order.
  1292. *
  1293. * @param a the char array to sort
  1294. */
  1295. public static void sort(char[] a)
  1296. {
  1297. qsort(a, 0, a.length);
  1298. }
  1299. /**
  1300. * Performs a stable sort on the elements, arranging them according to their
  1301. * natural order.
  1302. *
  1303. * @param a the char array to sort
  1304. * @param fromIndex the first index to sort (inclusive)
  1305. * @param toIndex the last index to sort (exclusive)
  1306. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1307. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1308. * || toIndex &gt; a.length
  1309. */
  1310. public static void sort(char[] a, int fromIndex, int toIndex)
  1311. {
  1312. if (fromIndex > toIndex)
  1313. throw new IllegalArgumentException();
  1314. if (fromIndex < 0)
  1315. throw new ArrayIndexOutOfBoundsException();
  1316. qsort(a, fromIndex, toIndex - fromIndex);
  1317. }
  1318. /**
  1319. * Finds the index of the median of three array elements.
  1320. *
  1321. * @param a the first index
  1322. * @param b the second index
  1323. * @param c the third index
  1324. * @param d the array
  1325. * @return the index (a, b, or c) which has the middle value of the three
  1326. */
  1327. private static int med3(int a, int b, int c, char[] d)
  1328. {
  1329. return (d[a] < d[b]
  1330. ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
  1331. : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
  1332. }
  1333. /**
  1334. * Swaps the elements at two locations of an array
  1335. *
  1336. * @param i the first index
  1337. * @param j the second index
  1338. * @param a the array
  1339. */
  1340. private static void swap(int i, int j, char[] a)
  1341. {
  1342. char c = a[i];
  1343. a[i] = a[j];
  1344. a[j] = c;
  1345. }
  1346. /**
  1347. * Swaps two ranges of an array.
  1348. *
  1349. * @param i the first range start
  1350. * @param j the second range start
  1351. * @param n the element count
  1352. * @param a the array
  1353. */
  1354. private static void vecswap(int i, int j, int n, char[] a)
  1355. {
  1356. for ( ; n > 0; i++, j++, n--)
  1357. swap(i, j, a);
  1358. }
  1359. /**
  1360. * Performs a recursive modified quicksort.
  1361. *
  1362. * @param array the array to sort
  1363. * @param from the start index (inclusive)
  1364. * @param count the number of elements to sort
  1365. */
  1366. private static void qsort(char[] array, int from, int count)
  1367. {
  1368. // Use an insertion sort on small arrays.
  1369. if (count <= 7)
  1370. {
  1371. for (int i = from + 1; i < from + count; i++)
  1372. for (int j = i; j > from && array[j - 1] > array[j]; j--)
  1373. swap(j, j - 1, array);
  1374. return;
  1375. }
  1376. // Determine a good median element.
  1377. int mid = from + count / 2;
  1378. int lo = from;
  1379. int hi = from + count - 1;
  1380. if (count > 40)
  1381. { // big arrays, pseudomedian of 9
  1382. int s = count / 8;
  1383. lo = med3(lo, lo + s, lo + 2 * s, array);
  1384. mid = med3(mid - s, mid, mid + s, array);
  1385. hi = med3(hi - 2 * s, hi - s, hi, array);
  1386. }
  1387. mid = med3(lo, mid, hi, array);
  1388. int a, b, c, d;
  1389. int comp;
  1390. // Pull the median element out of the fray, and use it as a pivot.
  1391. swap(from, mid, array);
  1392. a = b = from;
  1393. c = d = from + count - 1;
  1394. // Repeatedly move b and c to each other, swapping elements so
  1395. // that all elements before index b are less than the pivot, and all
  1396. // elements after index c are greater than the pivot. a and b track
  1397. // the elements equal to the pivot.
  1398. while (true)
  1399. {
  1400. while (b <= c && (comp = array[b] - array[from]) <= 0)
  1401. {
  1402. if (comp == 0)
  1403. {
  1404. swap(a, b, array);
  1405. a++;
  1406. }
  1407. b++;
  1408. }
  1409. while (c >= b && (comp = array[c] - array[from]) >= 0)
  1410. {
  1411. if (comp == 0)
  1412. {
  1413. swap(c, d, array);
  1414. d--;
  1415. }
  1416. c--;
  1417. }
  1418. if (b > c)
  1419. break;
  1420. swap(b, c, array);
  1421. b++;
  1422. c--;
  1423. }
  1424. // Swap pivot(s) back in place, the recurse on left and right sections.
  1425. hi = from + count;
  1426. int span;
  1427. span = Math.min(a - from, b - a);
  1428. vecswap(from, b - span, span, array);
  1429. span = Math.min(d - c, hi - d - 1);
  1430. vecswap(b, hi - span, span, array);
  1431. span = b - a;
  1432. if (span > 1)
  1433. qsort(array, from, span);
  1434. span = d - c;
  1435. if (span > 1)
  1436. qsort(array, hi - span, span);
  1437. }
  1438. /**
  1439. * Performs a stable sort on the elements, arranging them according to their
  1440. * natural order.
  1441. *
  1442. * @param a the short array to sort
  1443. */
  1444. public static void sort(short[] a)
  1445. {
  1446. qsort(a, 0, a.length);
  1447. }
  1448. /**
  1449. * Performs a stable sort on the elements, arranging them according to their
  1450. * natural order.
  1451. *
  1452. * @param a the short array to sort
  1453. * @param fromIndex the first index to sort (inclusive)
  1454. * @param toIndex the last index to sort (exclusive)
  1455. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1456. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1457. * || toIndex &gt; a.length
  1458. */
  1459. public static void sort(short[] a, int fromIndex, int toIndex)
  1460. {
  1461. if (fromIndex > toIndex)
  1462. throw new IllegalArgumentException();
  1463. if (fromIndex < 0)
  1464. throw new ArrayIndexOutOfBoundsException();
  1465. qsort(a, fromIndex, toIndex - fromIndex);
  1466. }
  1467. /**
  1468. * Finds the index of the median of three array elements.
  1469. *
  1470. * @param a the first index
  1471. * @param b the second index
  1472. * @param c the third index
  1473. * @param d the array
  1474. * @return the index (a, b, or c) which has the middle value of the three
  1475. */
  1476. private static int med3(int a, int b, int c, short[] d)
  1477. {
  1478. return (d[a] < d[b]
  1479. ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
  1480. : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
  1481. }
  1482. /**
  1483. * Swaps the elements at two locations of an array
  1484. *
  1485. * @param i the first index
  1486. * @param j the second index
  1487. * @param a the array
  1488. */
  1489. private static void swap(int i, int j, short[] a)
  1490. {
  1491. short c = a[i];
  1492. a[i] = a[j];
  1493. a[j] = c;
  1494. }
  1495. /**
  1496. * Swaps two ranges of an array.
  1497. *
  1498. * @param i the first range start
  1499. * @param j the second range start
  1500. * @param n the element count
  1501. * @param a the array
  1502. */
  1503. private static void vecswap(int i, int j, int n, short[] a)
  1504. {
  1505. for ( ; n > 0; i++, j++, n--)
  1506. swap(i, j, a);
  1507. }
  1508. /**
  1509. * Performs a recursive modified quicksort.
  1510. *
  1511. * @param array the array to sort
  1512. * @param from the start index (inclusive)
  1513. * @param count the number of elements to sort
  1514. */
  1515. private static void qsort(short[] array, int from, int count)
  1516. {
  1517. // Use an insertion sort on small arrays.
  1518. if (count <= 7)
  1519. {
  1520. for (int i = from + 1; i < from + count; i++)
  1521. for (int j = i; j > from && array[j - 1] > array[j]; j--)
  1522. swap(j, j - 1, array);
  1523. return;
  1524. }
  1525. // Determine a good median element.
  1526. int mid = from + count / 2;
  1527. int lo = from;
  1528. int hi = from + count - 1;
  1529. if (count > 40)
  1530. { // big arrays, pseudomedian of 9
  1531. int s = count / 8;
  1532. lo = med3(lo, lo + s, lo + 2 * s, array);
  1533. mid = med3(mid - s, mid, mid + s, array);
  1534. hi = med3(hi - 2 * s, hi - s, hi, array);
  1535. }
  1536. mid = med3(lo, mid, hi, array);
  1537. int a, b, c, d;
  1538. int comp;
  1539. // Pull the median element out of the fray, and use it as a pivot.
  1540. swap(from, mid, array);
  1541. a = b = from;
  1542. c = d = from + count - 1;
  1543. // Repeatedly move b and c to each other, swapping elements so
  1544. // that all elements before index b are less than the pivot, and all
  1545. // elements after index c are greater than the pivot. a and b track
  1546. // the elements equal to the pivot.
  1547. while (true)
  1548. {
  1549. while (b <= c && (comp = array[b] - array[from]) <= 0)
  1550. {
  1551. if (comp == 0)
  1552. {
  1553. swap(a, b, array);
  1554. a++;
  1555. }
  1556. b++;
  1557. }
  1558. while (c >= b && (comp = array[c] - array[from]) >= 0)
  1559. {
  1560. if (comp == 0)
  1561. {
  1562. swap(c, d, array);
  1563. d--;
  1564. }
  1565. c--;
  1566. }
  1567. if (b > c)
  1568. break;
  1569. swap(b, c, array);
  1570. b++;
  1571. c--;
  1572. }
  1573. // Swap pivot(s) back in place, the recurse on left and right sections.
  1574. hi = from + count;
  1575. int span;
  1576. span = Math.min(a - from, b - a);
  1577. vecswap(from, b - span, span, array);
  1578. span = Math.min(d - c, hi - d - 1);
  1579. vecswap(b, hi - span, span, array);
  1580. span = b - a;
  1581. if (span > 1)
  1582. qsort(array, from, span);
  1583. span = d - c;
  1584. if (span > 1)
  1585. qsort(array, hi - span, span);
  1586. }
  1587. /**
  1588. * Performs a stable sort on the elements, arranging them according to their
  1589. * natural order.
  1590. *
  1591. * @param a the int array to sort
  1592. */
  1593. public static void sort(int[] a)
  1594. {
  1595. qsort(a, 0, a.length);
  1596. }
  1597. /**
  1598. * Performs a stable sort on the elements, arranging them according to their
  1599. * natural order.
  1600. *
  1601. * @param a the int array to sort
  1602. * @param fromIndex the first index to sort (inclusive)
  1603. * @param toIndex the last index to sort (exclusive)
  1604. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1605. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1606. * || toIndex &gt; a.length
  1607. */
  1608. public static void sort(int[] a, int fromIndex, int toIndex)
  1609. {
  1610. if (fromIndex > toIndex)
  1611. throw new IllegalArgumentException();
  1612. if (fromIndex < 0)
  1613. throw new ArrayIndexOutOfBoundsException();
  1614. qsort(a, fromIndex, toIndex - fromIndex);
  1615. }
  1616. /**
  1617. * Finds the index of the median of three array elements.
  1618. *
  1619. * @param a the first index
  1620. * @param b the second index
  1621. * @param c the third index
  1622. * @param d the array
  1623. * @return the index (a, b, or c) which has the middle value of the three
  1624. */
  1625. private static int med3(int a, int b, int c, int[] d)
  1626. {
  1627. return (d[a] < d[b]
  1628. ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
  1629. : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
  1630. }
  1631. /**
  1632. * Swaps the elements at two locations of an array
  1633. *
  1634. * @param i the first index
  1635. * @param j the second index
  1636. * @param a the array
  1637. */
  1638. private static void swap(int i, int j, int[] a)
  1639. {
  1640. int c = a[i];
  1641. a[i] = a[j];
  1642. a[j] = c;
  1643. }
  1644. /**
  1645. * Swaps two ranges of an array.
  1646. *
  1647. * @param i the first range start
  1648. * @param j the second range start
  1649. * @param n the element count
  1650. * @param a the array
  1651. */
  1652. private static void vecswap(int i, int j, int n, int[] a)
  1653. {
  1654. for ( ; n > 0; i++, j++, n--)
  1655. swap(i, j, a);
  1656. }
  1657. /**
  1658. * Compares two integers in natural order, since a - b is inadequate.
  1659. *
  1660. * @param a the first int
  1661. * @param b the second int
  1662. * @return &lt; 0, 0, or &gt; 0 accorting to the comparison
  1663. */
  1664. private static int compare(int a, int b)
  1665. {
  1666. return a < b ? -1 : a == b ? 0 : 1;
  1667. }
  1668. /**
  1669. * Performs a recursive modified quicksort.
  1670. *
  1671. * @param array the array to sort
  1672. * @param from the start index (inclusive)
  1673. * @param count the number of elements to sort
  1674. */
  1675. private static void qsort(int[] array, int from, int count)
  1676. {
  1677. // Use an insertion sort on small arrays.
  1678. if (count <= 7)
  1679. {
  1680. for (int i = from + 1; i < from + count; i++)
  1681. for (int j = i; j > from && array[j - 1] > array[j]; j--)
  1682. swap(j, j - 1, array);
  1683. return;
  1684. }
  1685. // Determine a good median element.
  1686. int mid = from + count / 2;
  1687. int lo = from;
  1688. int hi = from + count - 1;
  1689. if (count > 40)
  1690. { // big arrays, pseudomedian of 9
  1691. int s = count / 8;
  1692. lo = med3(lo, lo + s, lo + 2 * s, array);
  1693. mid = med3(mid - s, mid, mid + s, array);
  1694. hi = med3(hi - 2 * s, hi - s, hi, array);
  1695. }
  1696. mid = med3(lo, mid, hi, array);
  1697. int a, b, c, d;
  1698. int comp;
  1699. // Pull the median element out of the fray, and use it as a pivot.
  1700. swap(from, mid, array);
  1701. a = b = from;
  1702. c = d = from + count - 1;
  1703. // Repeatedly move b and c to each other, swapping elements so
  1704. // that all elements before index b are less than the pivot, and all
  1705. // elements after index c are greater than the pivot. a and b track
  1706. // the elements equal to the pivot.
  1707. while (true)
  1708. {
  1709. while (b <= c && (comp = compare(array[b], array[from])) <= 0)
  1710. {
  1711. if (comp == 0)
  1712. {
  1713. swap(a, b, array);
  1714. a++;
  1715. }
  1716. b++;
  1717. }
  1718. while (c >= b && (comp = compare(array[c], array[from])) >= 0)
  1719. {
  1720. if (comp == 0)
  1721. {
  1722. swap(c, d, array);
  1723. d--;
  1724. }
  1725. c--;
  1726. }
  1727. if (b > c)
  1728. break;
  1729. swap(b, c, array);
  1730. b++;
  1731. c--;
  1732. }
  1733. // Swap pivot(s) back in place, the recurse on left and right sections.
  1734. hi = from + count;
  1735. int span;
  1736. span = Math.min(a - from, b - a);
  1737. vecswap(from, b - span, span, array);
  1738. span = Math.min(d - c, hi - d - 1);
  1739. vecswap(b, hi - span, span, array);
  1740. span = b - a;
  1741. if (span > 1)
  1742. qsort(array, from, span);
  1743. span = d - c;
  1744. if (span > 1)
  1745. qsort(array, hi - span, span);
  1746. }
  1747. /**
  1748. * Performs a stable sort on the elements, arranging them according to their
  1749. * natural order.
  1750. *
  1751. * @param a the long array to sort
  1752. */
  1753. public static void sort(long[] a)
  1754. {
  1755. qsort(a, 0, a.length);
  1756. }
  1757. /**
  1758. * Performs a stable sort on the elements, arranging them according to their
  1759. * natural order.
  1760. *
  1761. * @param a the long array to sort
  1762. * @param fromIndex the first index to sort (inclusive)
  1763. * @param toIndex the last index to sort (exclusive)
  1764. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1765. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1766. * || toIndex &gt; a.length
  1767. */
  1768. public static void sort(long[] a, int fromIndex, int toIndex)
  1769. {
  1770. if (fromIndex > toIndex)
  1771. throw new IllegalArgumentException();
  1772. if (fromIndex < 0)
  1773. throw new ArrayIndexOutOfBoundsException();
  1774. qsort(a, fromIndex, toIndex - fromIndex);
  1775. }
  1776. /**
  1777. * Finds the index of the median of three array elements.
  1778. *
  1779. * @param a the first index
  1780. * @param b the second index
  1781. * @param c the third index
  1782. * @param d the array
  1783. * @return the index (a, b, or c) which has the middle value of the three
  1784. */
  1785. private static int med3(int a, int b, int c, long[] d)
  1786. {
  1787. return (d[a] < d[b]
  1788. ? (d[b] < d[c] ? b : d[a] < d[c] ? c : a)
  1789. : (d[b] > d[c] ? b : d[a] > d[c] ? c : a));
  1790. }
  1791. /**
  1792. * Swaps the elements at two locations of an array
  1793. *
  1794. * @param i the first index
  1795. * @param j the second index
  1796. * @param a the array
  1797. */
  1798. private static void swap(int i, int j, long[] a)
  1799. {
  1800. long c = a[i];
  1801. a[i] = a[j];
  1802. a[j] = c;
  1803. }
  1804. /**
  1805. * Swaps two ranges of an array.
  1806. *
  1807. * @param i the first range start
  1808. * @param j the second range start
  1809. * @param n the element count
  1810. * @param a the array
  1811. */
  1812. private static void vecswap(int i, int j, int n, long[] a)
  1813. {
  1814. for ( ; n > 0; i++, j++, n--)
  1815. swap(i, j, a);
  1816. }
  1817. /**
  1818. * Compares two longs in natural order, since a - b is inadequate.
  1819. *
  1820. * @param a the first long
  1821. * @param b the second long
  1822. * @return &lt; 0, 0, or &gt; 0 accorting to the comparison
  1823. */
  1824. private static int compare(long a, long b)
  1825. {
  1826. return a < b ? -1 : a == b ? 0 : 1;
  1827. }
  1828. /**
  1829. * Performs a recursive modified quicksort.
  1830. *
  1831. * @param array the array to sort
  1832. * @param from the start index (inclusive)
  1833. * @param count the number of elements to sort
  1834. */
  1835. private static void qsort(long[] array, int from, int count)
  1836. {
  1837. // Use an insertion sort on small arrays.
  1838. if (count <= 7)
  1839. {
  1840. for (int i = from + 1; i < from + count; i++)
  1841. for (int j = i; j > from && array[j - 1] > array[j]; j--)
  1842. swap(j, j - 1, array);
  1843. return;
  1844. }
  1845. // Determine a good median element.
  1846. int mid = from + count / 2;
  1847. int lo = from;
  1848. int hi = from + count - 1;
  1849. if (count > 40)
  1850. { // big arrays, pseudomedian of 9
  1851. int s = count / 8;
  1852. lo = med3(lo, lo + s, lo + 2 * s, array);
  1853. mid = med3(mid - s, mid, mid + s, array);
  1854. hi = med3(hi - 2 * s, hi - s, hi, array);
  1855. }
  1856. mid = med3(lo, mid, hi, array);
  1857. int a, b, c, d;
  1858. int comp;
  1859. // Pull the median element out of the fray, and use it as a pivot.
  1860. swap(from, mid, array);
  1861. a = b = from;
  1862. c = d = from + count - 1;
  1863. // Repeatedly move b and c to each other, swapping elements so
  1864. // that all elements before index b are less than the pivot, and all
  1865. // elements after index c are greater than the pivot. a and b track
  1866. // the elements equal to the pivot.
  1867. while (true)
  1868. {
  1869. while (b <= c && (comp = compare(array[b], array[from])) <= 0)
  1870. {
  1871. if (comp == 0)
  1872. {
  1873. swap(a, b, array);
  1874. a++;
  1875. }
  1876. b++;
  1877. }
  1878. while (c >= b && (comp = compare(array[c], array[from])) >= 0)
  1879. {
  1880. if (comp == 0)
  1881. {
  1882. swap(c, d, array);
  1883. d--;
  1884. }
  1885. c--;
  1886. }
  1887. if (b > c)
  1888. break;
  1889. swap(b, c, array);
  1890. b++;
  1891. c--;
  1892. }
  1893. // Swap pivot(s) back in place, the recurse on left and right sections.
  1894. hi = from + count;
  1895. int span;
  1896. span = Math.min(a - from, b - a);
  1897. vecswap(from, b - span, span, array);
  1898. span = Math.min(d - c, hi - d - 1);
  1899. vecswap(b, hi - span, span, array);
  1900. span = b - a;
  1901. if (span > 1)
  1902. qsort(array, from, span);
  1903. span = d - c;
  1904. if (span > 1)
  1905. qsort(array, hi - span, span);
  1906. }
  1907. /**
  1908. * Performs a stable sort on the elements, arranging them according to their
  1909. * natural order.
  1910. *
  1911. * @param a the float array to sort
  1912. */
  1913. public static void sort(float[] a)
  1914. {
  1915. qsort(a, 0, a.length);
  1916. }
  1917. /**
  1918. * Performs a stable sort on the elements, arranging them according to their
  1919. * natural order.
  1920. *
  1921. * @param a the float array to sort
  1922. * @param fromIndex the first index to sort (inclusive)
  1923. * @param toIndex the last index to sort (exclusive)
  1924. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  1925. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  1926. * || toIndex &gt; a.length
  1927. */
  1928. public static void sort(float[] a, int fromIndex, int toIndex)
  1929. {
  1930. if (fromIndex > toIndex)
  1931. throw new IllegalArgumentException();
  1932. if (fromIndex < 0)
  1933. throw new ArrayIndexOutOfBoundsException();
  1934. qsort(a, fromIndex, toIndex - fromIndex);
  1935. }
  1936. /**
  1937. * Finds the index of the median of three array elements.
  1938. *
  1939. * @param a the first index
  1940. * @param b the second index
  1941. * @param c the third index
  1942. * @param d the array
  1943. * @return the index (a, b, or c) which has the middle value of the three
  1944. */
  1945. private static int med3(int a, int b, int c, float[] d)
  1946. {
  1947. return (Float.compare(d[a], d[b]) < 0
  1948. ? (Float.compare(d[b], d[c]) < 0 ? b
  1949. : Float.compare(d[a], d[c]) < 0 ? c : a)
  1950. : (Float.compare(d[b], d[c]) > 0 ? b
  1951. : Float.compare(d[a], d[c]) > 0 ? c : a));
  1952. }
  1953. /**
  1954. * Swaps the elements at two locations of an array
  1955. *
  1956. * @param i the first index
  1957. * @param j the second index
  1958. * @param a the array
  1959. */
  1960. private static void swap(int i, int j, float[] a)
  1961. {
  1962. float c = a[i];
  1963. a[i] = a[j];
  1964. a[j] = c;
  1965. }
  1966. /**
  1967. * Swaps two ranges of an array.
  1968. *
  1969. * @param i the first range start
  1970. * @param j the second range start
  1971. * @param n the element count
  1972. * @param a the array
  1973. */
  1974. private static void vecswap(int i, int j, int n, float[] a)
  1975. {
  1976. for ( ; n > 0; i++, j++, n--)
  1977. swap(i, j, a);
  1978. }
  1979. /**
  1980. * Performs a recursive modified quicksort.
  1981. *
  1982. * @param array the array to sort
  1983. * @param from the start index (inclusive)
  1984. * @param count the number of elements to sort
  1985. */
  1986. private static void qsort(float[] array, int from, int count)
  1987. {
  1988. // Use an insertion sort on small arrays.
  1989. if (count <= 7)
  1990. {
  1991. for (int i = from + 1; i < from + count; i++)
  1992. for (int j = i;
  1993. j > from && Float.compare(array[j - 1], array[j]) > 0;
  1994. j--)
  1995. {
  1996. swap(j, j - 1, array);
  1997. }
  1998. return;
  1999. }
  2000. // Determine a good median element.
  2001. int mid = from + count / 2;
  2002. int lo = from;
  2003. int hi = from + count - 1;
  2004. if (count > 40)
  2005. { // big arrays, pseudomedian of 9
  2006. int s = count / 8;
  2007. lo = med3(lo, lo + s, lo + 2 * s, array);
  2008. mid = med3(mid - s, mid, mid + s, array);
  2009. hi = med3(hi - 2 * s, hi - s, hi, array);
  2010. }
  2011. mid = med3(lo, mid, hi, array);
  2012. int a, b, c, d;
  2013. int comp;
  2014. // Pull the median element out of the fray, and use it as a pivot.
  2015. swap(from, mid, array);
  2016. a = b = from;
  2017. c = d = from + count - 1;
  2018. // Repeatedly move b and c to each other, swapping elements so
  2019. // that all elements before index b are less than the pivot, and all
  2020. // elements after index c are greater than the pivot. a and b track
  2021. // the elements equal to the pivot.
  2022. while (true)
  2023. {
  2024. while (b <= c && (comp = Float.compare(array[b], array[from])) <= 0)
  2025. {
  2026. if (comp == 0)
  2027. {
  2028. swap(a, b, array);
  2029. a++;
  2030. }
  2031. b++;
  2032. }
  2033. while (c >= b && (comp = Float.compare(array[c], array[from])) >= 0)
  2034. {
  2035. if (comp == 0)
  2036. {
  2037. swap(c, d, array);
  2038. d--;
  2039. }
  2040. c--;
  2041. }
  2042. if (b > c)
  2043. break;
  2044. swap(b, c, array);
  2045. b++;
  2046. c--;
  2047. }
  2048. // Swap pivot(s) back in place, the recurse on left and right sections.
  2049. hi = from + count;
  2050. int span;
  2051. span = Math.min(a - from, b - a);
  2052. vecswap(from, b - span, span, array);
  2053. span = Math.min(d - c, hi - d - 1);
  2054. vecswap(b, hi - span, span, array);
  2055. span = b - a;
  2056. if (span > 1)
  2057. qsort(array, from, span);
  2058. span = d - c;
  2059. if (span > 1)
  2060. qsort(array, hi - span, span);
  2061. }
  2062. /**
  2063. * Performs a stable sort on the elements, arranging them according to their
  2064. * natural order.
  2065. *
  2066. * @param a the double array to sort
  2067. */
  2068. public static void sort(double[] a)
  2069. {
  2070. qsort(a, 0, a.length);
  2071. }
  2072. /**
  2073. * Performs a stable sort on the elements, arranging them according to their
  2074. * natural order.
  2075. *
  2076. * @param a the double array to sort
  2077. * @param fromIndex the first index to sort (inclusive)
  2078. * @param toIndex the last index to sort (exclusive)
  2079. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  2080. * @throws ArrayIndexOutOfBoundsException if fromIndex &lt; 0
  2081. * || toIndex &gt; a.length
  2082. */
  2083. public static void sort(double[] a, int fromIndex, int toIndex)
  2084. {
  2085. if (fromIndex > toIndex)
  2086. throw new IllegalArgumentException();
  2087. if (fromIndex < 0)
  2088. throw new ArrayIndexOutOfBoundsException();
  2089. qsort(a, fromIndex, toIndex - fromIndex);
  2090. }
  2091. /**
  2092. * Finds the index of the median of three array elements.
  2093. *
  2094. * @param a the first index
  2095. * @param b the second index
  2096. * @param c the third index
  2097. * @param d the array
  2098. * @return the index (a, b, or c) which has the middle value of the three
  2099. */
  2100. private static int med3(int a, int b, int c, double[] d)
  2101. {
  2102. return (Double.compare(d[a], d[b]) < 0
  2103. ? (Double.compare(d[b], d[c]) < 0 ? b
  2104. : Double.compare(d[a], d[c]) < 0 ? c : a)
  2105. : (Double.compare(d[b], d[c]) > 0 ? b
  2106. : Double.compare(d[a], d[c]) > 0 ? c : a));
  2107. }
  2108. /**
  2109. * Swaps the elements at two locations of an array
  2110. *
  2111. * @param i the first index
  2112. * @param j the second index
  2113. * @param a the array
  2114. */
  2115. private static void swap(int i, int j, double[] a)
  2116. {
  2117. double c = a[i];
  2118. a[i] = a[j];
  2119. a[j] = c;
  2120. }
  2121. /**
  2122. * Swaps two ranges of an array.
  2123. *
  2124. * @param i the first range start
  2125. * @param j the second range start
  2126. * @param n the element count
  2127. * @param a the array
  2128. */
  2129. private static void vecswap(int i, int j, int n, double[] a)
  2130. {
  2131. for ( ; n > 0; i++, j++, n--)
  2132. swap(i, j, a);
  2133. }
  2134. /**
  2135. * Performs a recursive modified quicksort.
  2136. *
  2137. * @param array the array to sort
  2138. * @param from the start index (inclusive)
  2139. * @param count the number of elements to sort
  2140. */
  2141. private static void qsort(double[] array, int from, int count)
  2142. {
  2143. // Use an insertion sort on small arrays.
  2144. if (count <= 7)
  2145. {
  2146. for (int i = from + 1; i < from + count; i++)
  2147. for (int j = i;
  2148. j > from && Double.compare(array[j - 1], array[j]) > 0;
  2149. j--)
  2150. {
  2151. swap(j, j - 1, array);
  2152. }
  2153. return;
  2154. }
  2155. // Determine a good median element.
  2156. int mid = from + count / 2;
  2157. int lo = from;
  2158. int hi = from + count - 1;
  2159. if (count > 40)
  2160. { // big arrays, pseudomedian of 9
  2161. int s = count / 8;
  2162. lo = med3(lo, lo + s, lo + 2 * s, array);
  2163. mid = med3(mid - s, mid, mid + s, array);
  2164. hi = med3(hi - 2 * s, hi - s, hi, array);
  2165. }
  2166. mid = med3(lo, mid, hi, array);
  2167. int a, b, c, d;
  2168. int comp;
  2169. // Pull the median element out of the fray, and use it as a pivot.
  2170. swap(from, mid, array);
  2171. a = b = from;
  2172. c = d = from + count - 1;
  2173. // Repeatedly move b and c to each other, swapping elements so
  2174. // that all elements before index b are less than the pivot, and all
  2175. // elements after index c are greater than the pivot. a and b track
  2176. // the elements equal to the pivot.
  2177. while (true)
  2178. {
  2179. while (b <= c && (comp = Double.compare(array[b], array[from])) <= 0)
  2180. {
  2181. if (comp == 0)
  2182. {
  2183. swap(a, b, array);
  2184. a++;
  2185. }
  2186. b++;
  2187. }
  2188. while (c >= b && (comp = Double.compare(array[c], array[from])) >= 0)
  2189. {
  2190. if (comp == 0)
  2191. {
  2192. swap(c, d, array);
  2193. d--;
  2194. }
  2195. c--;
  2196. }
  2197. if (b > c)
  2198. break;
  2199. swap(b, c, array);
  2200. b++;
  2201. c--;
  2202. }
  2203. // Swap pivot(s) back in place, the recurse on left and right sections.
  2204. hi = from + count;
  2205. int span;
  2206. span = Math.min(a - from, b - a);
  2207. vecswap(from, b - span, span, array);
  2208. span = Math.min(d - c, hi - d - 1);
  2209. vecswap(b, hi - span, span, array);
  2210. span = b - a;
  2211. if (span > 1)
  2212. qsort(array, from, span);
  2213. span = d - c;
  2214. if (span > 1)
  2215. qsort(array, hi - span, span);
  2216. }
  2217. /**
  2218. * Sort an array of Objects according to their natural ordering. The sort is
  2219. * guaranteed to be stable, that is, equal elements will not be reordered.
  2220. * The sort algorithm is a mergesort with the merge omitted if the last
  2221. * element of one half comes before the first element of the other half. This
  2222. * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
  2223. * copy of the array.
  2224. *
  2225. * @param a the array to be sorted
  2226. * @throws ClassCastException if any two elements are not mutually
  2227. * comparable
  2228. * @throws NullPointerException if an element is null (since
  2229. * null.compareTo cannot work)
  2230. * @see Comparable
  2231. */
  2232. public static void sort(Object[] a)
  2233. {
  2234. sort(a, 0, a.length, null);
  2235. }
  2236. /**
  2237. * Sort an array of Objects according to a Comparator. The sort is
  2238. * guaranteed to be stable, that is, equal elements will not be reordered.
  2239. * The sort algorithm is a mergesort with the merge omitted if the last
  2240. * element of one half comes before the first element of the other half. This
  2241. * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
  2242. * copy of the array.
  2243. *
  2244. * @param a the array to be sorted
  2245. * @param c a Comparator to use in sorting the array; or null to indicate
  2246. * the elements' natural order
  2247. * @throws ClassCastException if any two elements are not mutually
  2248. * comparable by the Comparator provided
  2249. * @throws NullPointerException if a null element is compared with natural
  2250. * ordering (only possible when c is null)
  2251. */
  2252. public static <T> void sort(T[] a, Comparator<? super T> c)
  2253. {
  2254. sort(a, 0, a.length, c);
  2255. }
  2256. /**
  2257. * Sort an array of Objects according to their natural ordering. The sort is
  2258. * guaranteed to be stable, that is, equal elements will not be reordered.
  2259. * The sort algorithm is a mergesort with the merge omitted if the last
  2260. * element of one half comes before the first element of the other half. This
  2261. * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
  2262. * copy of the array.
  2263. *
  2264. * @param a the array to be sorted
  2265. * @param fromIndex the index of the first element to be sorted
  2266. * @param toIndex the index of the last element to be sorted plus one
  2267. * @throws ClassCastException if any two elements are not mutually
  2268. * comparable
  2269. * @throws NullPointerException if an element is null (since
  2270. * null.compareTo cannot work)
  2271. * @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
  2272. * are not in range.
  2273. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  2274. */
  2275. public static void sort(Object[] a, int fromIndex, int toIndex)
  2276. {
  2277. sort(a, fromIndex, toIndex, null);
  2278. }
  2279. /**
  2280. * Sort an array of Objects according to a Comparator. The sort is
  2281. * guaranteed to be stable, that is, equal elements will not be reordered.
  2282. * The sort algorithm is a mergesort with the merge omitted if the last
  2283. * element of one half comes before the first element of the other half. This
  2284. * algorithm gives guaranteed O(n*log(n)) time, at the expense of making a
  2285. * copy of the array.
  2286. *
  2287. * @param a the array to be sorted
  2288. * @param fromIndex the index of the first element to be sorted
  2289. * @param toIndex the index of the last element to be sorted plus one
  2290. * @param c a Comparator to use in sorting the array; or null to indicate
  2291. * the elements' natural order
  2292. * @throws ClassCastException if any two elements are not mutually
  2293. * comparable by the Comparator provided
  2294. * @throws ArrayIndexOutOfBoundsException if fromIndex and toIndex
  2295. * are not in range.
  2296. * @throws IllegalArgumentException if fromIndex &gt; toIndex
  2297. * @throws NullPointerException if a null element is compared with natural
  2298. * ordering (only possible when c is null)
  2299. */
  2300. public static <T> void sort(T[] a, int fromIndex, int toIndex,
  2301. Comparator<? super T> c)
  2302. {
  2303. if (fromIndex > toIndex)
  2304. throw new IllegalArgumentException("fromIndex " + fromIndex
  2305. + " > toIndex " + toIndex);
  2306. if (fromIndex < 0)
  2307. throw new ArrayIndexOutOfBoundsException();
  2308. // In general, the code attempts to be simple rather than fast, the
  2309. // idea being that a good optimising JIT will be able to optimise it
  2310. // better than I can, and if I try it will make it more confusing for
  2311. // the JIT. First presort the array in chunks of length 6 with insertion
  2312. // sort. A mergesort would give too much overhead for this length.
  2313. for (int chunk = fromIndex; chunk < toIndex; chunk += 6)
  2314. {
  2315. int end = Math.min(chunk + 6, toIndex);
  2316. for (int i = chunk + 1; i < end; i++)
  2317. {
  2318. if (Collections.compare(a[i - 1], a[i], c) > 0)
  2319. {
  2320. // not already sorted
  2321. int j = i;
  2322. T elem = a[j];
  2323. do
  2324. {
  2325. a[j] = a[j - 1];
  2326. j--;
  2327. }
  2328. while (j > chunk
  2329. && Collections.compare(a[j - 1], elem, c) > 0);
  2330. a[j] = elem;
  2331. }
  2332. }
  2333. }
  2334. int len = toIndex - fromIndex;
  2335. // If length is smaller or equal 6 we are done.
  2336. if (len <= 6)
  2337. return;
  2338. T[] src = a;
  2339. T[] dest = (T[]) new Object[len];
  2340. T[] t = null; // t is used for swapping src and dest
  2341. // The difference of the fromIndex of the src and dest array.
  2342. int srcDestDiff = -fromIndex;
  2343. // The merges are done in this loop
  2344. for (int size = 6; size < len; size <<= 1)
  2345. {
  2346. for (int start = fromIndex; start < toIndex; start += size << 1)
  2347. {
  2348. // mid is the start of the second sublist;
  2349. // end the start of the next sublist (or end of array).
  2350. int mid = start + size;
  2351. int end = Math.min(toIndex, mid + size);
  2352. // The second list is empty or the elements are already in
  2353. // order - no need to merge
  2354. if (mid >= end
  2355. || Collections.compare(src[mid - 1], src[mid], c) <= 0)
  2356. {
  2357. System.arraycopy(src, start,
  2358. dest, start + srcDestDiff, end - start);
  2359. // The two halves just need swapping - no need to merge
  2360. }
  2361. else if (Collections.compare(src[start], src[end - 1], c) > 0)
  2362. {
  2363. System.arraycopy(src, start,
  2364. dest, end - size + srcDestDiff, size);
  2365. System.arraycopy(src, mid,
  2366. dest, start + srcDestDiff, end - mid);
  2367. }
  2368. else
  2369. {
  2370. // Declare a lot of variables to save repeating
  2371. // calculations. Hopefully a decent JIT will put these
  2372. // in registers and make this fast
  2373. int p1 = start;
  2374. int p2 = mid;
  2375. int i = start + srcDestDiff;
  2376. // The main merge loop; terminates as soon as either
  2377. // half is ended
  2378. while (p1 < mid && p2 < end)
  2379. {
  2380. dest[i++] =
  2381. src[(Collections.compare(src[p1], src[p2], c) <= 0
  2382. ? p1++ : p2++)];
  2383. }
  2384. // Finish up by copying the remainder of whichever half
  2385. // wasn't finished.
  2386. if (p1 < mid)
  2387. System.arraycopy(src, p1, dest, i, mid - p1);
  2388. else
  2389. System.arraycopy(src, p2, dest, i, end - p2);
  2390. }
  2391. }
  2392. // swap src and dest ready for the next merge
  2393. t = src;
  2394. src = dest;
  2395. dest = t;
  2396. fromIndex += srcDestDiff;
  2397. toIndex += srcDestDiff;
  2398. srcDestDiff = -srcDestDiff;
  2399. }
  2400. // make sure the result ends up back in the right place. Note
  2401. // that src and dest may have been swapped above, so src
  2402. // contains the sorted array.
  2403. if (src != a)
  2404. {
  2405. // Note that fromIndex == 0.
  2406. System.arraycopy(src, 0, a, srcDestDiff, toIndex);
  2407. }
  2408. }
  2409. /**
  2410. * Returns a list "view" of the specified array. This method is intended to
  2411. * make it easy to use the Collections API with existing array-based APIs and
  2412. * programs. Changes in the list or the array show up in both places. The
  2413. * list does not support element addition or removal, but does permit
  2414. * value modification. The returned list implements both Serializable and
  2415. * RandomAccess.
  2416. *
  2417. * @param a the array to return a view of (<code>null</code> not permitted)
  2418. * @return a fixed-size list, changes to which "write through" to the array
  2419. *
  2420. * @throws NullPointerException if <code>a</code> is <code>null</code>.
  2421. * @see Serializable
  2422. * @see RandomAccess
  2423. * @see Arrays.ArrayList
  2424. */
  2425. public static <T> List<T> asList(final T... a)
  2426. {
  2427. return new Arrays.ArrayList(a);
  2428. }
  2429. /**
  2430. * Returns the hashcode of an array of long numbers. If two arrays
  2431. * are equal, according to <code>equals()</code>, they should have the
  2432. * same hashcode. The hashcode returned by the method is equal to that
  2433. * obtained by the corresponding <code>List</code> object. This has the same
  2434. * data, but represents longs in their wrapper class, <code>Long</code>.
  2435. * For <code>null</code>, 0 is returned.
  2436. *
  2437. * @param v an array of long numbers for which the hash code should be
  2438. * computed.
  2439. * @return the hash code of the array, or 0 if null was given.
  2440. * @since 1.5
  2441. */
  2442. public static int hashCode(long[] v)
  2443. {
  2444. if (v == null)
  2445. return 0;
  2446. int result = 1;
  2447. for (int i = 0; i < v.length; ++i)
  2448. {
  2449. int elt = (int) (v[i] ^ (v[i] >>> 32));
  2450. result = 31 * result + elt;
  2451. }
  2452. return result;
  2453. }
  2454. /**
  2455. * Returns the hashcode of an array of integer numbers. If two arrays
  2456. * are equal, according to <code>equals()</code>, they should have the
  2457. * same hashcode. The hashcode returned by the method is equal to that
  2458. * obtained by the corresponding <code>List</code> object. This has the same
  2459. * data, but represents ints in their wrapper class, <code>Integer</code>.
  2460. * For <code>null</code>, 0 is returned.
  2461. *
  2462. * @param v an array of integer numbers for which the hash code should be
  2463. * computed.
  2464. * @return the hash code of the array, or 0 if null was given.
  2465. * @since 1.5
  2466. */
  2467. public static int hashCode(int[] v)
  2468. {
  2469. if (v == null)
  2470. return 0;
  2471. int result = 1;
  2472. for (int i = 0; i < v.length; ++i)
  2473. result = 31 * result + v[i];
  2474. return result;
  2475. }
  2476. /**
  2477. * Returns the hashcode of an array of short numbers. If two arrays
  2478. * are equal, according to <code>equals()</code>, they should have the
  2479. * same hashcode. The hashcode returned by the method is equal to that
  2480. * obtained by the corresponding <code>List</code> object. This has the same
  2481. * data, but represents shorts in their wrapper class, <code>Short</code>.
  2482. * For <code>null</code>, 0 is returned.
  2483. *
  2484. * @param v an array of short numbers for which the hash code should be
  2485. * computed.
  2486. * @return the hash code of the array, or 0 if null was given.
  2487. * @since 1.5
  2488. */
  2489. public static int hashCode(short[] v)
  2490. {
  2491. if (v == null)
  2492. return 0;
  2493. int result = 1;
  2494. for (int i = 0; i < v.length; ++i)
  2495. result = 31 * result + v[i];
  2496. return result;
  2497. }
  2498. /**
  2499. * Returns the hashcode of an array of characters. If two arrays
  2500. * are equal, according to <code>equals()</code>, they should have the
  2501. * same hashcode. The hashcode returned by the method is equal to that
  2502. * obtained by the corresponding <code>List</code> object. This has the same
  2503. * data, but represents chars in their wrapper class, <code>Character</code>.
  2504. * For <code>null</code>, 0 is returned.
  2505. *
  2506. * @param v an array of characters for which the hash code should be
  2507. * computed.
  2508. * @return the hash code of the array, or 0 if null was given.
  2509. * @since 1.5
  2510. */
  2511. public static int hashCode(char[] v)
  2512. {
  2513. if (v == null)
  2514. return 0;
  2515. int result = 1;
  2516. for (int i = 0; i < v.length; ++i)
  2517. result = 31 * result + v[i];
  2518. return result;
  2519. }
  2520. /**
  2521. * Returns the hashcode of an array of bytes. If two arrays
  2522. * are equal, according to <code>equals()</code>, they should have the
  2523. * same hashcode. The hashcode returned by the method is equal to that
  2524. * obtained by the corresponding <code>List</code> object. This has the same
  2525. * data, but represents bytes in their wrapper class, <code>Byte</code>.
  2526. * For <code>null</code>, 0 is returned.
  2527. *
  2528. * @param v an array of bytes for which the hash code should be
  2529. * computed.
  2530. * @return the hash code of the array, or 0 if null was given.
  2531. * @since 1.5
  2532. */
  2533. public static int hashCode(byte[] v)
  2534. {
  2535. if (v == null)
  2536. return 0;
  2537. int result = 1;
  2538. for (int i = 0; i < v.length; ++i)
  2539. result = 31 * result + v[i];
  2540. return result;
  2541. }
  2542. /**
  2543. * Returns the hashcode of an array of booleans. If two arrays
  2544. * are equal, according to <code>equals()</code>, they should have the
  2545. * same hashcode. The hashcode returned by the method is equal to that
  2546. * obtained by the corresponding <code>List</code> object. This has the same
  2547. * data, but represents booleans in their wrapper class,
  2548. * <code>Boolean</code>. For <code>null</code>, 0 is returned.
  2549. *
  2550. * @param v an array of booleans for which the hash code should be
  2551. * computed.
  2552. * @return the hash code of the array, or 0 if null was given.
  2553. * @since 1.5
  2554. */
  2555. public static int hashCode(boolean[] v)
  2556. {
  2557. if (v == null)
  2558. return 0;
  2559. int result = 1;
  2560. for (int i = 0; i < v.length; ++i)
  2561. result = 31 * result + (v[i] ? 1231 : 1237);
  2562. return result;
  2563. }
  2564. /**
  2565. * Returns the hashcode of an array of floats. If two arrays
  2566. * are equal, according to <code>equals()</code>, they should have the
  2567. * same hashcode. The hashcode returned by the method is equal to that
  2568. * obtained by the corresponding <code>List</code> object. This has the same
  2569. * data, but represents floats in their wrapper class, <code>Float</code>.
  2570. * For <code>null</code>, 0 is returned.
  2571. *
  2572. * @param v an array of floats for which the hash code should be
  2573. * computed.
  2574. * @return the hash code of the array, or 0 if null was given.
  2575. * @since 1.5
  2576. */
  2577. public static int hashCode(float[] v)
  2578. {
  2579. if (v == null)
  2580. return 0;
  2581. int result = 1;
  2582. for (int i = 0; i < v.length; ++i)
  2583. result = 31 * result + Float.floatToIntBits(v[i]);
  2584. return result;
  2585. }
  2586. /**
  2587. * Returns the hashcode of an array of doubles. If two arrays
  2588. * are equal, according to <code>equals()</code>, they should have the
  2589. * same hashcode. The hashcode returned by the method is equal to that
  2590. * obtained by the corresponding <code>List</code> object. This has the same
  2591. * data, but represents doubles in their wrapper class, <code>Double</code>.
  2592. * For <code>null</code>, 0 is returned.
  2593. *
  2594. * @param v an array of doubles for which the hash code should be
  2595. * computed.
  2596. * @return the hash code of the array, or 0 if null was given.
  2597. * @since 1.5
  2598. */
  2599. public static int hashCode(double[] v)
  2600. {
  2601. if (v == null)
  2602. return 0;
  2603. int result = 1;
  2604. for (int i = 0; i < v.length; ++i)
  2605. {
  2606. long l = Double.doubleToLongBits(v[i]);
  2607. int elt = (int) (l ^ (l >>> 32));
  2608. result = 31 * result + elt;
  2609. }
  2610. return result;
  2611. }
  2612. /**
  2613. * Returns the hashcode of an array of objects. If two arrays
  2614. * are equal, according to <code>equals()</code>, they should have the
  2615. * same hashcode. The hashcode returned by the method is equal to that
  2616. * obtained by the corresponding <code>List</code> object.
  2617. * For <code>null</code>, 0 is returned.
  2618. *
  2619. * @param v an array of integer numbers for which the hash code should be
  2620. * computed.
  2621. * @return the hash code of the array, or 0 if null was given.
  2622. * @since 1.5
  2623. */
  2624. public static int hashCode(Object[] v)
  2625. {
  2626. if (v == null)
  2627. return 0;
  2628. int result = 1;
  2629. for (int i = 0; i < v.length; ++i)
  2630. {
  2631. int elt = v[i] == null ? 0 : v[i].hashCode();
  2632. result = 31 * result + elt;
  2633. }
  2634. return result;
  2635. }
  2636. public static int deepHashCode(Object[] v)
  2637. {
  2638. if (v == null)
  2639. return 0;
  2640. int result = 1;
  2641. for (int i = 0; i < v.length; ++i)
  2642. {
  2643. int elt;
  2644. if (v[i] == null)
  2645. elt = 0;
  2646. else if (v[i] instanceof boolean[])
  2647. elt = hashCode((boolean[]) v[i]);
  2648. else if (v[i] instanceof byte[])
  2649. elt = hashCode((byte[]) v[i]);
  2650. else if (v[i] instanceof char[])
  2651. elt = hashCode((char[]) v[i]);
  2652. else if (v[i] instanceof short[])
  2653. elt = hashCode((short[]) v[i]);
  2654. else if (v[i] instanceof int[])
  2655. elt = hashCode((int[]) v[i]);
  2656. else if (v[i] instanceof long[])
  2657. elt = hashCode((long[]) v[i]);
  2658. else if (v[i] instanceof float[])
  2659. elt = hashCode((float[]) v[i]);
  2660. else if (v[i] instanceof double[])
  2661. elt = hashCode((double[]) v[i]);
  2662. else if (v[i] instanceof Object[])
  2663. elt = hashCode((Object[]) v[i]);
  2664. else
  2665. elt = v[i].hashCode();
  2666. result = 31 * result + elt;
  2667. }
  2668. return result;
  2669. }
  2670. /** @since 1.5 */
  2671. public static boolean deepEquals(Object[] v1, Object[] v2)
  2672. {
  2673. if (v1 == null)
  2674. return v2 == null;
  2675. if (v2 == null || v1.length != v2.length)
  2676. return false;
  2677. for (int i = 0; i < v1.length; ++i)
  2678. {
  2679. Object e1 = v1[i];
  2680. Object e2 = v2[i];
  2681. if (e1 == e2)
  2682. continue;
  2683. if (e1 == null || e2 == null)
  2684. return false;
  2685. boolean check;
  2686. if (e1 instanceof boolean[] && e2 instanceof boolean[])
  2687. check = equals((boolean[]) e1, (boolean[]) e2);
  2688. else if (e1 instanceof byte[] && e2 instanceof byte[])
  2689. check = equals((byte[]) e1, (byte[]) e2);
  2690. else if (e1 instanceof char[] && e2 instanceof char[])
  2691. check = equals((char[]) e1, (char[]) e2);
  2692. else if (e1 instanceof short[] && e2 instanceof short[])
  2693. check = equals((short[]) e1, (short[]) e2);
  2694. else if (e1 instanceof int[] && e2 instanceof int[])
  2695. check = equals((int[]) e1, (int[]) e2);
  2696. else if (e1 instanceof long[] && e2 instanceof long[])
  2697. check = equals((long[]) e1, (long[]) e2);
  2698. else if (e1 instanceof float[] && e2 instanceof float[])
  2699. check = equals((float[]) e1, (float[]) e2);
  2700. else if (e1 instanceof double[] && e2 instanceof double[])
  2701. check = equals((double[]) e1, (double[]) e2);
  2702. else if (e1 instanceof Object[] && e2 instanceof Object[])
  2703. check = equals((Object[]) e1, (Object[]) e2);
  2704. else
  2705. check = e1.equals(e2);
  2706. if (! check)
  2707. return false;
  2708. }
  2709. return true;
  2710. }
  2711. /**
  2712. * Returns a String representation of the argument array. Returns "null"
  2713. * if <code>a</code> is null.
  2714. * @param v the array to represent
  2715. * @return a String representing this array
  2716. * @since 1.5
  2717. */
  2718. public static String toString(boolean[] v)
  2719. {
  2720. if (v == null)
  2721. return "null";
  2722. CPStringBuilder b = new CPStringBuilder("[");
  2723. for (int i = 0; i < v.length; ++i)
  2724. {
  2725. if (i > 0)
  2726. b.append(", ");
  2727. b.append(v[i]);
  2728. }
  2729. b.append("]");
  2730. return b.toString();
  2731. }
  2732. /**
  2733. * Returns a String representation of the argument array. Returns "null"
  2734. * if <code>a</code> is null.
  2735. * @param v the array to represent
  2736. * @return a String representing this array
  2737. * @since 1.5
  2738. */
  2739. public static String toString(byte[] v)
  2740. {
  2741. if (v == null)
  2742. return "null";
  2743. CPStringBuilder b = new CPStringBuilder("[");
  2744. for (int i = 0; i < v.length; ++i)
  2745. {
  2746. if (i > 0)
  2747. b.append(", ");
  2748. b.append(v[i]);
  2749. }
  2750. b.append("]");
  2751. return b.toString();
  2752. }
  2753. /**
  2754. * Returns a String representation of the argument array. Returns "null"
  2755. * if <code>a</code> is null.
  2756. * @param v the array to represent
  2757. * @return a String representing this array
  2758. * @since 1.5
  2759. */
  2760. public static String toString(char[] v)
  2761. {
  2762. if (v == null)
  2763. return "null";
  2764. CPStringBuilder b = new CPStringBuilder("[");
  2765. for (int i = 0; i < v.length; ++i)
  2766. {
  2767. if (i > 0)
  2768. b.append(", ");
  2769. b.append(v[i]);
  2770. }
  2771. b.append("]");
  2772. return b.toString();
  2773. }
  2774. /**
  2775. * Returns a String representation of the argument array. Returns "null"
  2776. * if <code>a</code> is null.
  2777. * @param v the array to represent
  2778. * @return a String representing this array
  2779. * @since 1.5
  2780. */
  2781. public static String toString(short[] v)
  2782. {
  2783. if (v == null)
  2784. return "null";
  2785. CPStringBuilder b = new CPStringBuilder("[");
  2786. for (int i = 0; i < v.length; ++i)
  2787. {
  2788. if (i > 0)
  2789. b.append(", ");
  2790. b.append(v[i]);
  2791. }
  2792. b.append("]");
  2793. return b.toString();
  2794. }
  2795. /**
  2796. * Returns a String representation of the argument array. Returns "null"
  2797. * if <code>a</code> is null.
  2798. * @param v the array to represent
  2799. * @return a String representing this array
  2800. * @since 1.5
  2801. */
  2802. public static String toString(int[] v)
  2803. {
  2804. if (v == null)
  2805. return "null";
  2806. CPStringBuilder b = new CPStringBuilder("[");
  2807. for (int i = 0; i < v.length; ++i)
  2808. {
  2809. if (i > 0)
  2810. b.append(", ");
  2811. b.append(v[i]);
  2812. }
  2813. b.append("]");
  2814. return b.toString();
  2815. }
  2816. /**
  2817. * Returns a String representation of the argument array. Returns "null"
  2818. * if <code>a</code> is null.
  2819. * @param v the array to represent
  2820. * @return a String representing this array
  2821. * @since 1.5
  2822. */
  2823. public static String toString(long[] v)
  2824. {
  2825. if (v == null)
  2826. return "null";
  2827. CPStringBuilder b = new CPStringBuilder("[");
  2828. for (int i = 0; i < v.length; ++i)
  2829. {
  2830. if (i > 0)
  2831. b.append(", ");
  2832. b.append(v[i]);
  2833. }
  2834. b.append("]");
  2835. return b.toString();
  2836. }
  2837. /**
  2838. * Returns a String representation of the argument array. Returns "null"
  2839. * if <code>a</code> is null.
  2840. * @param v the array to represent
  2841. * @return a String representing this array
  2842. * @since 1.5
  2843. */
  2844. public static String toString(float[] v)
  2845. {
  2846. if (v == null)
  2847. return "null";
  2848. CPStringBuilder b = new CPStringBuilder("[");
  2849. for (int i = 0; i < v.length; ++i)
  2850. {
  2851. if (i > 0)
  2852. b.append(", ");
  2853. b.append(v[i]);
  2854. }
  2855. b.append("]");
  2856. return b.toString();
  2857. }
  2858. /**
  2859. * Returns a String representation of the argument array. Returns "null"
  2860. * if <code>a</code> is null.
  2861. * @param v the array to represent
  2862. * @return a String representing this array
  2863. * @since 1.5
  2864. */
  2865. public static String toString(double[] v)
  2866. {
  2867. if (v == null)
  2868. return "null";
  2869. CPStringBuilder b = new CPStringBuilder("[");
  2870. for (int i = 0; i < v.length; ++i)
  2871. {
  2872. if (i > 0)
  2873. b.append(", ");
  2874. b.append(v[i]);
  2875. }
  2876. b.append("]");
  2877. return b.toString();
  2878. }
  2879. /**
  2880. * Returns a String representation of the argument array. Returns "null"
  2881. * if <code>a</code> is null.
  2882. * @param v the array to represent
  2883. * @return a String representing this array
  2884. * @since 1.5
  2885. */
  2886. public static String toString(Object[] v)
  2887. {
  2888. if (v == null)
  2889. return "null";
  2890. CPStringBuilder b = new CPStringBuilder("[");
  2891. for (int i = 0; i < v.length; ++i)
  2892. {
  2893. if (i > 0)
  2894. b.append(", ");
  2895. b.append(v[i]);
  2896. }
  2897. b.append("]");
  2898. return b.toString();
  2899. }
  2900. private static void deepToString(Object[] v, CPStringBuilder b, HashSet seen)
  2901. {
  2902. b.append("[");
  2903. for (int i = 0; i < v.length; ++i)
  2904. {
  2905. if (i > 0)
  2906. b.append(", ");
  2907. Object elt = v[i];
  2908. if (elt == null)
  2909. b.append("null");
  2910. else if (elt instanceof boolean[])
  2911. b.append(toString((boolean[]) elt));
  2912. else if (elt instanceof byte[])
  2913. b.append(toString((byte[]) elt));
  2914. else if (elt instanceof char[])
  2915. b.append(toString((char[]) elt));
  2916. else if (elt instanceof short[])
  2917. b.append(toString((short[]) elt));
  2918. else if (elt instanceof int[])
  2919. b.append(toString((int[]) elt));
  2920. else if (elt instanceof long[])
  2921. b.append(toString((long[]) elt));
  2922. else if (elt instanceof float[])
  2923. b.append(toString((float[]) elt));
  2924. else if (elt instanceof double[])
  2925. b.append(toString((double[]) elt));
  2926. else if (elt instanceof Object[])
  2927. {
  2928. Object[] os = (Object[]) elt;
  2929. if (seen.contains(os))
  2930. b.append("[...]");
  2931. else
  2932. {
  2933. seen.add(os);
  2934. deepToString(os, b, seen);
  2935. }
  2936. }
  2937. else
  2938. b.append(elt);
  2939. }
  2940. b.append("]");
  2941. }
  2942. /** @since 1.5 */
  2943. public static String deepToString(Object[] v)
  2944. {
  2945. if (v == null)
  2946. return "null";
  2947. HashSet seen = new HashSet();
  2948. CPStringBuilder b = new CPStringBuilder();
  2949. deepToString(v, b, seen);
  2950. return b.toString();
  2951. }
  2952. /**
  2953. * Inner class used by {@link #asList(Object[])} to provide a list interface
  2954. * to an array. The name, though it clashes with java.util.ArrayList, is
  2955. * Sun's choice for Serialization purposes. Element addition and removal
  2956. * is prohibited, but values can be modified.
  2957. *
  2958. * @author Eric Blake (ebb9@email.byu.edu)
  2959. * @status updated to 1.4
  2960. */
  2961. private static final class ArrayList<E> extends AbstractList<E>
  2962. implements Serializable, RandomAccess
  2963. {
  2964. // We override the necessary methods, plus others which will be much
  2965. // more efficient with direct iteration rather than relying on iterator().
  2966. /**
  2967. * Compatible with JDK 1.4.
  2968. */
  2969. private static final long serialVersionUID = -2764017481108945198L;
  2970. /**
  2971. * The array we are viewing.
  2972. * @serial the array
  2973. */
  2974. private final E[] a;
  2975. /**
  2976. * Construct a list view of the array.
  2977. * @param a the array to view
  2978. * @throws NullPointerException if a is null
  2979. */
  2980. ArrayList(E[] a)
  2981. {
  2982. // We have to explicitly check.
  2983. if (a == null)
  2984. throw new NullPointerException();
  2985. this.a = a;
  2986. }
  2987. /**
  2988. * Returns the object at the specified index in
  2989. * the array.
  2990. *
  2991. * @param index The index to retrieve an object from.
  2992. * @return The object at the array index specified.
  2993. */
  2994. public E get(int index)
  2995. {
  2996. return a[index];
  2997. }
  2998. /**
  2999. * Returns the size of the array.
  3000. *
  3001. * @return The size.
  3002. */
  3003. public int size()
  3004. {
  3005. return a.length;
  3006. }
  3007. /**
  3008. * Replaces the object at the specified index
  3009. * with the supplied element.
  3010. *
  3011. * @param index The index at which to place the new object.
  3012. * @param element The new object.
  3013. * @return The object replaced by this operation.
  3014. */
  3015. public E set(int index, E element)
  3016. {
  3017. E old = a[index];
  3018. a[index] = element;
  3019. return old;
  3020. }
  3021. /**
  3022. * Returns true if the array contains the
  3023. * supplied object.
  3024. *
  3025. * @param o The object to look for.
  3026. * @return True if the object was found.
  3027. */
  3028. public boolean contains(Object o)
  3029. {
  3030. return lastIndexOf(o) >= 0;
  3031. }
  3032. /**
  3033. * Returns the first index at which the
  3034. * object, o, occurs in the array.
  3035. *
  3036. * @param o The object to search for.
  3037. * @return The first relevant index.
  3038. */
  3039. public int indexOf(Object o)
  3040. {
  3041. int size = a.length;
  3042. for (int i = 0; i < size; i++)
  3043. if (ArrayList.equals(o, a[i]))
  3044. return i;
  3045. return -1;
  3046. }
  3047. /**
  3048. * Returns the last index at which the
  3049. * object, o, occurs in the array.
  3050. *
  3051. * @param o The object to search for.
  3052. * @return The last relevant index.
  3053. */
  3054. public int lastIndexOf(Object o)
  3055. {
  3056. int i = a.length;
  3057. while (--i >= 0)
  3058. if (ArrayList.equals(o, a[i]))
  3059. return i;
  3060. return -1;
  3061. }
  3062. /**
  3063. * Transforms the list into an array of
  3064. * objects, by simplying cloning the array
  3065. * wrapped by this list.
  3066. *
  3067. * @return A clone of the internal array.
  3068. */
  3069. public Object[] toArray()
  3070. {
  3071. return (Object[]) a.clone();
  3072. }
  3073. /**
  3074. * Copies the objects from this list into
  3075. * the supplied array. The supplied array
  3076. * is shrunk or enlarged to the size of the
  3077. * internal array, and filled with its objects.
  3078. *
  3079. * @param array The array to fill with the objects in this list.
  3080. * @return The array containing the objects in this list,
  3081. * which may or may not be == to array.
  3082. */
  3083. public <T> T[] toArray(T[] array)
  3084. {
  3085. int size = a.length;
  3086. if (array.length < size)
  3087. array = (T[]) Array.newInstance(array.getClass().getComponentType(),
  3088. size);
  3089. else if (array.length > size)
  3090. array[size] = null;
  3091. System.arraycopy(a, 0, array, 0, size);
  3092. return array;
  3093. }
  3094. }
  3095. /**
  3096. * Returns a copy of the supplied array, truncating or padding as
  3097. * necessary with <code>false</code> to obtain the specified length.
  3098. * Indices that are valid for both arrays will return the same value.
  3099. * Indices that only exist in the returned array (due to the new length
  3100. * being greater than the original length) will return <code>false</code>.
  3101. * This is equivalent to calling
  3102. * <code>copyOfRange(original, 0, newLength)</code>.
  3103. *
  3104. * @param original the original array to be copied.
  3105. * @param newLength the length of the returned array.
  3106. * @return a copy of the original array, truncated or padded with
  3107. * <code>false</code> to obtain the required length.
  3108. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3109. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3110. * @since 1.6
  3111. * @see #copyOfRange(boolean[],int,int)
  3112. */
  3113. public static boolean[] copyOf(boolean[] original, int newLength)
  3114. {
  3115. if (newLength < 0)
  3116. throw new NegativeArraySizeException("The array size is negative.");
  3117. return copyOfRange(original, 0, newLength);
  3118. }
  3119. /**
  3120. * Copies the specified range of the supplied array to a new
  3121. * array, padding as necessary with <code>false</code>
  3122. * if <code>to</code> is greater than the length of the original
  3123. * array. <code>from</code> must be in the range zero to
  3124. * <code>original.length</code> and can not be greater than
  3125. * <code>to</code>. The initial element of the
  3126. * returned array will be equal to <code>original[from]</code>,
  3127. * except where <code>from</code> is equal to <code>to</code>
  3128. * (where a zero-length array will be returned) or <code>
  3129. * <code>from</code> is equal to <code>original.length</code>
  3130. * (where an array padded with <code>false</code> will be
  3131. * returned). The returned array is always of length
  3132. * <code>to - from</code>.
  3133. *
  3134. * @param original the array from which to copy.
  3135. * @param from the initial index of the range, inclusive.
  3136. * @param to the final index of the range, exclusive.
  3137. * @return a copy of the specified range, with padding to
  3138. * obtain the required length.
  3139. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3140. * or <code>from > original.length</code>
  3141. * @throws IllegalArgumentException if <code>from > to</code>
  3142. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3143. * @since 1.6
  3144. * @see #copyOf(boolean[],int)
  3145. */
  3146. public static boolean[] copyOfRange(boolean[] original, int from, int to)
  3147. {
  3148. if (from > to)
  3149. throw new IllegalArgumentException("The initial index is after " +
  3150. "the final index.");
  3151. boolean[] newArray = new boolean[to - from];
  3152. if (to > original.length)
  3153. {
  3154. System.arraycopy(original, from, newArray, 0,
  3155. original.length - from);
  3156. fill(newArray, original.length, newArray.length, false);
  3157. }
  3158. else
  3159. System.arraycopy(original, from, newArray, 0, to - from);
  3160. return newArray;
  3161. }
  3162. /**
  3163. * Returns a copy of the supplied array, truncating or padding as
  3164. * necessary with <code>(byte)0</code> to obtain the specified length.
  3165. * Indices that are valid for both arrays will return the same value.
  3166. * Indices that only exist in the returned array (due to the new length
  3167. * being greater than the original length) will return <code>(byte)0</code>.
  3168. * This is equivalent to calling
  3169. * <code>copyOfRange(original, 0, newLength)</code>.
  3170. *
  3171. * @param original the original array to be copied.
  3172. * @param newLength the length of the returned array.
  3173. * @return a copy of the original array, truncated or padded with
  3174. * <code>(byte)0</code> to obtain the required length.
  3175. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3176. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3177. * @since 1.6
  3178. * @see #copyOfRange(byte[],int,int)
  3179. */
  3180. public static byte[] copyOf(byte[] original, int newLength)
  3181. {
  3182. if (newLength < 0)
  3183. throw new NegativeArraySizeException("The array size is negative.");
  3184. return copyOfRange(original, 0, newLength);
  3185. }
  3186. /**
  3187. * Copies the specified range of the supplied array to a new
  3188. * array, padding as necessary with <code>(byte)0</code>
  3189. * if <code>to</code> is greater than the length of the original
  3190. * array. <code>from</code> must be in the range zero to
  3191. * <code>original.length</code> and can not be greater than
  3192. * <code>to</code>. The initial element of the
  3193. * returned array will be equal to <code>original[from]</code>,
  3194. * except where <code>from</code> is equal to <code>to</code>
  3195. * (where a zero-length array will be returned) or <code>
  3196. * <code>from</code> is equal to <code>original.length</code>
  3197. * (where an array padded with <code>(byte)0</code> will be
  3198. * returned). The returned array is always of length
  3199. * <code>to - from</code>.
  3200. *
  3201. * @param original the array from which to copy.
  3202. * @param from the initial index of the range, inclusive.
  3203. * @param to the final index of the range, exclusive.
  3204. * @return a copy of the specified range, with padding to
  3205. * obtain the required length.
  3206. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3207. * or <code>from > original.length</code>
  3208. * @throws IllegalArgumentException if <code>from > to</code>
  3209. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3210. * @since 1.6
  3211. * @see #copyOf(byte[],int)
  3212. */
  3213. public static byte[] copyOfRange(byte[] original, int from, int to)
  3214. {
  3215. if (from > to)
  3216. throw new IllegalArgumentException("The initial index is after " +
  3217. "the final index.");
  3218. byte[] newArray = new byte[to - from];
  3219. if (to > original.length)
  3220. {
  3221. System.arraycopy(original, from, newArray, 0,
  3222. original.length - from);
  3223. fill(newArray, original.length, newArray.length, (byte)0);
  3224. }
  3225. else
  3226. System.arraycopy(original, from, newArray, 0, to - from);
  3227. return newArray;
  3228. }
  3229. /**
  3230. * Returns a copy of the supplied array, truncating or padding as
  3231. * necessary with <code>'\0'</code> to obtain the specified length.
  3232. * Indices that are valid for both arrays will return the same value.
  3233. * Indices that only exist in the returned array (due to the new length
  3234. * being greater than the original length) will return <code>'\0'</code>.
  3235. * This is equivalent to calling
  3236. * <code>copyOfRange(original, 0, newLength)</code>.
  3237. *
  3238. * @param original the original array to be copied.
  3239. * @param newLength the length of the returned array.
  3240. * @return a copy of the original array, truncated or padded with
  3241. * <code>'\0'</code> to obtain the required length.
  3242. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3243. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3244. * @since 1.6
  3245. * @see #copyOfRange(char[],int,int)
  3246. */
  3247. public static char[] copyOf(char[] original, int newLength)
  3248. {
  3249. if (newLength < 0)
  3250. throw new NegativeArraySizeException("The array size is negative.");
  3251. return copyOfRange(original, 0, newLength);
  3252. }
  3253. /**
  3254. * Copies the specified range of the supplied array to a new
  3255. * array, padding as necessary with <code>'\0'</code>
  3256. * if <code>to</code> is greater than the length of the original
  3257. * array. <code>from</code> must be in the range zero to
  3258. * <code>original.length</code> and can not be greater than
  3259. * <code>to</code>. The initial element of the
  3260. * returned array will be equal to <code>original[from]</code>,
  3261. * except where <code>from</code> is equal to <code>to</code>
  3262. * (where a zero-length array will be returned) or <code>
  3263. * <code>from</code> is equal to <code>original.length</code>
  3264. * (where an array padded with <code>'\0'</code> will be
  3265. * returned). The returned array is always of length
  3266. * <code>to - from</code>.
  3267. *
  3268. * @param original the array from which to copy.
  3269. * @param from the initial index of the range, inclusive.
  3270. * @param to the final index of the range, exclusive.
  3271. * @return a copy of the specified range, with padding to
  3272. * obtain the required length.
  3273. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3274. * or <code>from > original.length</code>
  3275. * @throws IllegalArgumentException if <code>from > to</code>
  3276. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3277. * @since 1.6
  3278. * @see #copyOf(char[],int)
  3279. */
  3280. public static char[] copyOfRange(char[] original, int from, int to)
  3281. {
  3282. if (from > to)
  3283. throw new IllegalArgumentException("The initial index is after " +
  3284. "the final index.");
  3285. char[] newArray = new char[to - from];
  3286. if (to > original.length)
  3287. {
  3288. System.arraycopy(original, from, newArray, 0,
  3289. original.length - from);
  3290. fill(newArray, original.length, newArray.length, '\0');
  3291. }
  3292. else
  3293. System.arraycopy(original, from, newArray, 0, to - from);
  3294. return newArray;
  3295. }
  3296. /**
  3297. * Returns a copy of the supplied array, truncating or padding as
  3298. * necessary with <code>0d</code> to obtain the specified length.
  3299. * Indices that are valid for both arrays will return the same value.
  3300. * Indices that only exist in the returned array (due to the new length
  3301. * being greater than the original length) will return <code>0d</code>.
  3302. * This is equivalent to calling
  3303. * <code>copyOfRange(original, 0, newLength)</code>.
  3304. *
  3305. * @param original the original array to be copied.
  3306. * @param newLength the length of the returned array.
  3307. * @return a copy of the original array, truncated or padded with
  3308. * <code>0d</code> to obtain the required length.
  3309. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3310. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3311. * @since 1.6
  3312. * @see #copyOfRange(double[],int,int)
  3313. */
  3314. public static double[] copyOf(double[] original, int newLength)
  3315. {
  3316. if (newLength < 0)
  3317. throw new NegativeArraySizeException("The array size is negative.");
  3318. return copyOfRange(original, 0, newLength);
  3319. }
  3320. /**
  3321. * Copies the specified range of the supplied array to a new
  3322. * array, padding as necessary with <code>0d</code>
  3323. * if <code>to</code> is greater than the length of the original
  3324. * array. <code>from</code> must be in the range zero to
  3325. * <code>original.length</code> and can not be greater than
  3326. * <code>to</code>. The initial element of the
  3327. * returned array will be equal to <code>original[from]</code>,
  3328. * except where <code>from</code> is equal to <code>to</code>
  3329. * (where a zero-length array will be returned) or <code>
  3330. * <code>from</code> is equal to <code>original.length</code>
  3331. * (where an array padded with <code>0d</code> will be
  3332. * returned). The returned array is always of length
  3333. * <code>to - from</code>.
  3334. *
  3335. * @param original the array from which to copy.
  3336. * @param from the initial index of the range, inclusive.
  3337. * @param to the final index of the range, exclusive.
  3338. * @return a copy of the specified range, with padding to
  3339. * obtain the required length.
  3340. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3341. * or <code>from > original.length</code>
  3342. * @throws IllegalArgumentException if <code>from > to</code>
  3343. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3344. * @since 1.6
  3345. * @see #copyOf(double[],int)
  3346. */
  3347. public static double[] copyOfRange(double[] original, int from, int to)
  3348. {
  3349. if (from > to)
  3350. throw new IllegalArgumentException("The initial index is after " +
  3351. "the final index.");
  3352. double[] newArray = new double[to - from];
  3353. if (to > original.length)
  3354. {
  3355. System.arraycopy(original, from, newArray, 0,
  3356. original.length - from);
  3357. fill(newArray, original.length, newArray.length, 0d);
  3358. }
  3359. else
  3360. System.arraycopy(original, from, newArray, 0, to - from);
  3361. return newArray;
  3362. }
  3363. /**
  3364. * Returns a copy of the supplied array, truncating or padding as
  3365. * necessary with <code>0f</code> to obtain the specified length.
  3366. * Indices that are valid for both arrays will return the same value.
  3367. * Indices that only exist in the returned array (due to the new length
  3368. * being greater than the original length) will return <code>0f</code>.
  3369. * This is equivalent to calling
  3370. * <code>copyOfRange(original, 0, newLength)</code>.
  3371. *
  3372. * @param original the original array to be copied.
  3373. * @param newLength the length of the returned array.
  3374. * @return a copy of the original array, truncated or padded with
  3375. * <code>0f</code> to obtain the required length.
  3376. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3377. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3378. * @since 1.6
  3379. * @see #copyOfRange(float[],int,int)
  3380. */
  3381. public static float[] copyOf(float[] original, int newLength)
  3382. {
  3383. if (newLength < 0)
  3384. throw new NegativeArraySizeException("The array size is negative.");
  3385. return copyOfRange(original, 0, newLength);
  3386. }
  3387. /**
  3388. * Copies the specified range of the supplied array to a new
  3389. * array, padding as necessary with <code>0f</code>
  3390. * if <code>to</code> is greater than the length of the original
  3391. * array. <code>from</code> must be in the range zero to
  3392. * <code>original.length</code> and can not be greater than
  3393. * <code>to</code>. The initial element of the
  3394. * returned array will be equal to <code>original[from]</code>,
  3395. * except where <code>from</code> is equal to <code>to</code>
  3396. * (where a zero-length array will be returned) or <code>
  3397. * <code>from</code> is equal to <code>original.length</code>
  3398. * (where an array padded with <code>0f</code> will be
  3399. * returned). The returned array is always of length
  3400. * <code>to - from</code>.
  3401. *
  3402. * @param original the array from which to copy.
  3403. * @param from the initial index of the range, inclusive.
  3404. * @param to the final index of the range, exclusive.
  3405. * @return a copy of the specified range, with padding to
  3406. * obtain the required length.
  3407. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3408. * or <code>from > original.length</code>
  3409. * @throws IllegalArgumentException if <code>from > to</code>
  3410. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3411. * @since 1.6
  3412. * @see #copyOf(float[],int)
  3413. */
  3414. public static float[] copyOfRange(float[] original, int from, int to)
  3415. {
  3416. if (from > to)
  3417. throw new IllegalArgumentException("The initial index is after " +
  3418. "the final index.");
  3419. float[] newArray = new float[to - from];
  3420. if (to > original.length)
  3421. {
  3422. System.arraycopy(original, from, newArray, 0,
  3423. original.length - from);
  3424. fill(newArray, original.length, newArray.length, 0f);
  3425. }
  3426. else
  3427. System.arraycopy(original, from, newArray, 0, to - from);
  3428. return newArray;
  3429. }
  3430. /**
  3431. * Returns a copy of the supplied array, truncating or padding as
  3432. * necessary with <code>0</code> to obtain the specified length.
  3433. * Indices that are valid for both arrays will return the same value.
  3434. * Indices that only exist in the returned array (due to the new length
  3435. * being greater than the original length) will return <code>0</code>.
  3436. * This is equivalent to calling
  3437. * <code>copyOfRange(original, 0, newLength)</code>.
  3438. *
  3439. * @param original the original array to be copied.
  3440. * @param newLength the length of the returned array.
  3441. * @return a copy of the original array, truncated or padded with
  3442. * <code>0</code> to obtain the required length.
  3443. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3444. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3445. * @since 1.6
  3446. * @see #copyOfRange(int[],int,int)
  3447. */
  3448. public static int[] copyOf(int[] original, int newLength)
  3449. {
  3450. if (newLength < 0)
  3451. throw new NegativeArraySizeException("The array size is negative.");
  3452. return copyOfRange(original, 0, newLength);
  3453. }
  3454. /**
  3455. * Copies the specified range of the supplied array to a new
  3456. * array, padding as necessary with <code>0</code>
  3457. * if <code>to</code> is greater than the length of the original
  3458. * array. <code>from</code> must be in the range zero to
  3459. * <code>original.length</code> and can not be greater than
  3460. * <code>to</code>. The initial element of the
  3461. * returned array will be equal to <code>original[from]</code>,
  3462. * except where <code>from</code> is equal to <code>to</code>
  3463. * (where a zero-length array will be returned) or <code>
  3464. * <code>from</code> is equal to <code>original.length</code>
  3465. * (where an array padded with <code>0</code> will be
  3466. * returned). The returned array is always of length
  3467. * <code>to - from</code>.
  3468. *
  3469. * @param original the array from which to copy.
  3470. * @param from the initial index of the range, inclusive.
  3471. * @param to the final index of the range, exclusive.
  3472. * @return a copy of the specified range, with padding to
  3473. * obtain the required length.
  3474. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3475. * or <code>from > original.length</code>
  3476. * @throws IllegalArgumentException if <code>from > to</code>
  3477. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3478. * @since 1.6
  3479. * @see #copyOf(int[],int)
  3480. */
  3481. public static int[] copyOfRange(int[] original, int from, int to)
  3482. {
  3483. if (from > to)
  3484. throw new IllegalArgumentException("The initial index is after " +
  3485. "the final index.");
  3486. int[] newArray = new int[to - from];
  3487. if (to > original.length)
  3488. {
  3489. System.arraycopy(original, from, newArray, 0,
  3490. original.length - from);
  3491. fill(newArray, original.length, newArray.length, 0);
  3492. }
  3493. else
  3494. System.arraycopy(original, from, newArray, 0, to - from);
  3495. return newArray;
  3496. }
  3497. /**
  3498. * Returns a copy of the supplied array, truncating or padding as
  3499. * necessary with <code>0L</code> to obtain the specified length.
  3500. * Indices that are valid for both arrays will return the same value.
  3501. * Indices that only exist in the returned array (due to the new length
  3502. * being greater than the original length) will return <code>0L</code>.
  3503. * This is equivalent to calling
  3504. * <code>copyOfRange(original, 0, newLength)</code>.
  3505. *
  3506. * @param original the original array to be copied.
  3507. * @param newLength the length of the returned array.
  3508. * @return a copy of the original array, truncated or padded with
  3509. * <code>0L</code> to obtain the required length.
  3510. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3511. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3512. * @since 1.6
  3513. * @see #copyOfRange(long[],int,int)
  3514. */
  3515. public static long[] copyOf(long[] original, int newLength)
  3516. {
  3517. if (newLength < 0)
  3518. throw new NegativeArraySizeException("The array size is negative.");
  3519. return copyOfRange(original, 0, newLength);
  3520. }
  3521. /**
  3522. * Copies the specified range of the supplied array to a new
  3523. * array, padding as necessary with <code>0L</code>
  3524. * if <code>to</code> is greater than the length of the original
  3525. * array. <code>from</code> must be in the range zero to
  3526. * <code>original.length</code> and can not be greater than
  3527. * <code>to</code>. The initial element of the
  3528. * returned array will be equal to <code>original[from]</code>,
  3529. * except where <code>from</code> is equal to <code>to</code>
  3530. * (where a zero-length array will be returned) or <code>
  3531. * <code>from</code> is equal to <code>original.length</code>
  3532. * (where an array padded with <code>0L</code> will be
  3533. * returned). The returned array is always of length
  3534. * <code>to - from</code>.
  3535. *
  3536. * @param original the array from which to copy.
  3537. * @param from the initial index of the range, inclusive.
  3538. * @param to the final index of the range, exclusive.
  3539. * @return a copy of the specified range, with padding to
  3540. * obtain the required length.
  3541. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3542. * or <code>from > original.length</code>
  3543. * @throws IllegalArgumentException if <code>from > to</code>
  3544. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3545. * @since 1.6
  3546. * @see #copyOf(long[],int)
  3547. */
  3548. public static long[] copyOfRange(long[] original, int from, int to)
  3549. {
  3550. if (from > to)
  3551. throw new IllegalArgumentException("The initial index is after " +
  3552. "the final index.");
  3553. long[] newArray = new long[to - from];
  3554. if (to > original.length)
  3555. {
  3556. System.arraycopy(original, from, newArray, 0,
  3557. original.length - from);
  3558. fill(newArray, original.length, newArray.length, 0L);
  3559. }
  3560. else
  3561. System.arraycopy(original, from, newArray, 0, to - from);
  3562. return newArray;
  3563. }
  3564. /**
  3565. * Returns a copy of the supplied array, truncating or padding as
  3566. * necessary with <code>(short)0</code> to obtain the specified length.
  3567. * Indices that are valid for both arrays will return the same value.
  3568. * Indices that only exist in the returned array (due to the new length
  3569. * being greater than the original length) will return <code>(short)0</code>.
  3570. * This is equivalent to calling
  3571. * <code>copyOfRange(original, 0, newLength)</code>.
  3572. *
  3573. * @param original the original array to be copied.
  3574. * @param newLength the length of the returned array.
  3575. * @return a copy of the original array, truncated or padded with
  3576. * <code>(short)0</code> to obtain the required length.
  3577. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3578. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3579. * @since 1.6
  3580. * @see #copyOfRange(short[],int,int)
  3581. */
  3582. public static short[] copyOf(short[] original, int newLength)
  3583. {
  3584. if (newLength < 0)
  3585. throw new NegativeArraySizeException("The array size is negative.");
  3586. return copyOfRange(original, 0, newLength);
  3587. }
  3588. /**
  3589. * Copies the specified range of the supplied array to a new
  3590. * array, padding as necessary with <code>(short)0</code>
  3591. * if <code>to</code> is greater than the length of the original
  3592. * array. <code>from</code> must be in the range zero to
  3593. * <code>original.length</code> and can not be greater than
  3594. * <code>to</code>. The initial element of the
  3595. * returned array will be equal to <code>original[from]</code>,
  3596. * except where <code>from</code> is equal to <code>to</code>
  3597. * (where a zero-length array will be returned) or <code>
  3598. * <code>from</code> is equal to <code>original.length</code>
  3599. * (where an array padded with <code>(short)0</code> will be
  3600. * returned). The returned array is always of length
  3601. * <code>to - from</code>.
  3602. *
  3603. * @param original the array from which to copy.
  3604. * @param from the initial index of the range, inclusive.
  3605. * @param to the final index of the range, exclusive.
  3606. * @return a copy of the specified range, with padding to
  3607. * obtain the required length.
  3608. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3609. * or <code>from > original.length</code>
  3610. * @throws IllegalArgumentException if <code>from > to</code>
  3611. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3612. * @since 1.6
  3613. * @see #copyOf(short[],int)
  3614. */
  3615. public static short[] copyOfRange(short[] original, int from, int to)
  3616. {
  3617. if (from > to)
  3618. throw new IllegalArgumentException("The initial index is after " +
  3619. "the final index.");
  3620. short[] newArray = new short[to - from];
  3621. if (to > original.length)
  3622. {
  3623. System.arraycopy(original, from, newArray, 0,
  3624. original.length - from);
  3625. fill(newArray, original.length, newArray.length, (short)0);
  3626. }
  3627. else
  3628. System.arraycopy(original, from, newArray, 0, to - from);
  3629. return newArray;
  3630. }
  3631. /**
  3632. * Returns a copy of the supplied array, truncating or padding as
  3633. * necessary with <code>null</code> to obtain the specified length.
  3634. * Indices that are valid for both arrays will return the same value.
  3635. * Indices that only exist in the returned array (due to the new length
  3636. * being greater than the original length) will return <code>null</code>.
  3637. * This is equivalent to calling
  3638. * <code>copyOfRange(original, 0, newLength)</code>.
  3639. *
  3640. * @param original the original array to be copied.
  3641. * @param newLength the length of the returned array.
  3642. * @return a copy of the original array, truncated or padded with
  3643. * <code>null</code> to obtain the required length.
  3644. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3645. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3646. * @since 1.6
  3647. * @see #copyOfRange(T[],int,int)
  3648. */
  3649. public static <T> T[] copyOf(T[] original, int newLength)
  3650. {
  3651. if (newLength < 0)
  3652. throw new NegativeArraySizeException("The array size is negative.");
  3653. return copyOfRange(original, 0, newLength);
  3654. }
  3655. /**
  3656. * Copies the specified range of the supplied array to a new
  3657. * array, padding as necessary with <code>null</code>
  3658. * if <code>to</code> is greater than the length of the original
  3659. * array. <code>from</code> must be in the range zero to
  3660. * <code>original.length</code> and can not be greater than
  3661. * <code>to</code>. The initial element of the
  3662. * returned array will be equal to <code>original[from]</code>,
  3663. * except where <code>from</code> is equal to <code>to</code>
  3664. * (where a zero-length array will be returned) or <code>
  3665. * <code>from</code> is equal to <code>original.length</code>
  3666. * (where an array padded with <code>null</code> will be
  3667. * returned). The returned array is always of length
  3668. * <code>to - from</code>.
  3669. *
  3670. * @param original the array from which to copy.
  3671. * @param from the initial index of the range, inclusive.
  3672. * @param to the final index of the range, exclusive.
  3673. * @return a copy of the specified range, with padding to
  3674. * obtain the required length.
  3675. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3676. * or <code>from > original.length</code>
  3677. * @throws IllegalArgumentException if <code>from > to</code>
  3678. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3679. * @since 1.6
  3680. * @see #copyOf(T[],int)
  3681. */
  3682. public static <T> T[] copyOfRange(T[] original, int from, int to)
  3683. {
  3684. if (from > to)
  3685. throw new IllegalArgumentException("The initial index is after " +
  3686. "the final index.");
  3687. Class elemType = original.getClass().getComponentType();
  3688. T[] newArray = (T[]) Array.newInstance(elemType, to - from);
  3689. if (to > original.length)
  3690. {
  3691. System.arraycopy(original, from, newArray, 0,
  3692. original.length - from);
  3693. fill(newArray, original.length, newArray.length, null);
  3694. }
  3695. else
  3696. System.arraycopy(original, from, newArray, 0, to - from);
  3697. return newArray;
  3698. }
  3699. /**
  3700. * Returns a copy of the supplied array, truncating or padding as
  3701. * necessary with <code>null</code> to obtain the specified length.
  3702. * Indices that are valid for both arrays will return the same value.
  3703. * Indices that only exist in the returned array (due to the new length
  3704. * being greater than the original length) will return <code>null</code>.
  3705. * This is equivalent to calling
  3706. * <code>copyOfRange(original, 0, newLength, newType)</code>. The returned
  3707. * array will be of the specified type, <code>newType</code>.
  3708. *
  3709. * @param original the original array to be copied.
  3710. * @param newLength the length of the returned array.
  3711. * @param newType the type of the returned array.
  3712. * @return a copy of the original array, truncated or padded with
  3713. * <code>null</code> to obtain the required length.
  3714. * @throws NegativeArraySizeException if <code>newLength</code> is negative.
  3715. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3716. * @since 1.6
  3717. * @see #copyOfRange(U[],int,int,Class)
  3718. */
  3719. public static <T,U> T[] copyOf(U[] original, int newLength,
  3720. Class<? extends T[]> newType)
  3721. {
  3722. if (newLength < 0)
  3723. throw new NegativeArraySizeException("The array size is negative.");
  3724. return copyOfRange(original, 0, newLength, newType);
  3725. }
  3726. /**
  3727. * Copies the specified range of the supplied array to a new
  3728. * array, padding as necessary with <code>null</code>
  3729. * if <code>to</code> is greater than the length of the original
  3730. * array. <code>from</code> must be in the range zero to
  3731. * <code>original.length</code> and can not be greater than
  3732. * <code>to</code>. The initial element of the
  3733. * returned array will be equal to <code>original[from]</code>,
  3734. * except where <code>from</code> is equal to <code>to</code>
  3735. * (where a zero-length array will be returned) or <code>
  3736. * <code>from</code> is equal to <code>original.length</code>
  3737. * (where an array padded with <code>null</code> will be
  3738. * returned). The returned array is always of length
  3739. * <code>to - from</code> and will be of the specified type,
  3740. * <code>newType</code>.
  3741. *
  3742. * @param original the array from which to copy.
  3743. * @param from the initial index of the range, inclusive.
  3744. * @param to the final index of the range, exclusive.
  3745. * @param newType the type of the returned array.
  3746. * @return a copy of the specified range, with padding to
  3747. * obtain the required length.
  3748. * @throws ArrayIndexOutOfBoundsException if <code>from < 0</code>
  3749. * or <code>from > original.length</code>
  3750. * @throws IllegalArgumentException if <code>from > to</code>
  3751. * @throws NullPointerException if <code>original</code> is <code>null</code>.
  3752. * @since 1.6
  3753. * @see #copyOf(T[],int)
  3754. */
  3755. public static <T,U> T[] copyOfRange(U[] original, int from, int to,
  3756. Class<? extends T[]> newType)
  3757. {
  3758. if (from > to)
  3759. throw new IllegalArgumentException("The initial index is after " +
  3760. "the final index.");
  3761. T[] newArray = (T[]) Array.newInstance(newType.getComponentType(),
  3762. to - from);
  3763. if (to > original.length)
  3764. {
  3765. System.arraycopy(original, from, newArray, 0,
  3766. original.length - from);
  3767. fill(newArray, original.length, newArray.length, null);
  3768. }
  3769. else
  3770. System.arraycopy(original, from, newArray, 0, to - from);
  3771. return newArray;
  3772. }
  3773. }