ArrayList.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  1. // ArrayList.cs
  2. //
  3. // Implementation of the ECMA ArrayList.
  4. //
  5. // Copyright (c) 2003 Thong (Tum) Nguyen [[email protected]]
  6. //
  7. // http://www.opensource.org/licenses/mit-license.html
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  10. // software and associated documentation files (the "Software"), to deal in the
  11. // Software without restriction, including without limitation the rights to use, copy,
  12. // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  13. // and to permit persons to whom the Software is furnished to do so, subject to the
  14. // following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in all copies
  17. // or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  28. //
  29. // Permission is hereby granted, free of charge, to any person obtaining
  30. // a copy of this software and associated documentation files (the
  31. // "Software"), to deal in the Software without restriction, including
  32. // without limitation the rights to use, copy, modify, merge, publish,
  33. // distribute, sublicense, and/or sell copies of the Software, and to
  34. // permit persons to whom the Software is furnished to do so, subject to
  35. // the following conditions:
  36. //
  37. // The above copyright notice and this permission notice shall be
  38. // included in all copies or substantial portions of the Software.
  39. //
  40. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  41. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  42. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  43. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  44. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  45. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  46. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. //
  48. using System;
  49. using System.Collections;
  50. namespace System.Collections
  51. {
  52. [Serializable]
  53. public class ArrayList
  54. : IList, ICloneable, ICollection, IEnumerable
  55. {
  56. #region Enumerator
  57. private sealed class ArrayListEnumerator
  58. : IEnumerator, ICloneable
  59. {
  60. private int m_Pos;
  61. private int m_Index;
  62. private int m_Count;
  63. private object m_Current;
  64. private ArrayList m_List;
  65. private int m_ExpectedStateChanges;
  66. public ArrayListEnumerator(ArrayList list)
  67. : this(list, 0, list.Count)
  68. {
  69. }
  70. public object Clone()
  71. {
  72. return this.MemberwiseClone();
  73. }
  74. public ArrayListEnumerator(ArrayList list, int index, int count)
  75. {
  76. m_List = list;
  77. m_Index = index;
  78. m_Count = count;
  79. m_Pos = m_Index - 1;
  80. m_Current = null;
  81. m_ExpectedStateChanges = list._version;
  82. }
  83. public object Current
  84. {
  85. get
  86. {
  87. if (m_Pos == m_Index - 1) {
  88. throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
  89. }
  90. return m_Current;
  91. }
  92. }
  93. public bool MoveNext()
  94. {
  95. if (m_List._version != m_ExpectedStateChanges)
  96. {
  97. throw new InvalidOperationException("List has changed.");
  98. }
  99. m_Pos++;
  100. if (m_Pos - m_Index < m_Count)
  101. {
  102. m_Current = m_List[m_Pos];
  103. return true;
  104. }
  105. return false;
  106. }
  107. public void Reset()
  108. {
  109. m_Current = null;
  110. m_Pos = m_Index - 1;
  111. }
  112. }
  113. sealed class SimpleEnumerator : IEnumerator, ICloneable
  114. {
  115. ArrayList list;
  116. int index;
  117. int version;
  118. object currentElement;
  119. static object endFlag = new object ();
  120. public SimpleEnumerator (ArrayList list)
  121. {
  122. this.list = list;
  123. index = -1;
  124. version = list._version;
  125. currentElement = endFlag;
  126. }
  127. public object Clone ()
  128. {
  129. return MemberwiseClone ();
  130. }
  131. public bool MoveNext ()
  132. {
  133. if (version != list._version)
  134. throw new InvalidOperationException("List has changed.");
  135. if (++index < list.Count) {
  136. currentElement = list [index];
  137. return true;
  138. } else {
  139. currentElement = endFlag;
  140. return false;
  141. }
  142. }
  143. public object Current {
  144. get {
  145. if (currentElement == endFlag) {
  146. if (index == -1)
  147. throw new InvalidOperationException ("Enumerator not started");
  148. else
  149. throw new InvalidOperationException ("Enumerator ended");
  150. }
  151. return currentElement;
  152. }
  153. }
  154. public void Reset ()
  155. {
  156. if (version != list._version)
  157. throw new InvalidOperationException ("List has changed.");
  158. currentElement = endFlag;
  159. index = -1;
  160. }
  161. }
  162. #endregion
  163. #region ArrayListAdapter
  164. /// <summary>
  165. /// Adapts various ILists into an ArrayList.
  166. /// </summary>
  167. [Serializable]
  168. private sealed class ArrayListAdapter
  169. : ArrayList
  170. {
  171. private sealed class EnumeratorWithRange
  172. : IEnumerator, ICloneable
  173. {
  174. private int m_StartIndex;
  175. private int m_Count;
  176. private int m_MaxCount;
  177. private IEnumerator m_Enumerator;
  178. public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
  179. {
  180. m_Count = 0;
  181. m_StartIndex = index;
  182. m_MaxCount = count;
  183. m_Enumerator = enumerator;
  184. Reset();
  185. }
  186. public object Clone()
  187. {
  188. return this.MemberwiseClone();
  189. }
  190. public object Current
  191. {
  192. get
  193. {
  194. return m_Enumerator.Current;
  195. }
  196. }
  197. public bool MoveNext()
  198. {
  199. if (m_Count >= m_MaxCount)
  200. {
  201. return false;
  202. }
  203. m_Count++;
  204. return m_Enumerator.MoveNext();
  205. }
  206. public void Reset()
  207. {
  208. m_Count = 0;
  209. m_Enumerator.Reset();
  210. for (int i = 0; i < m_StartIndex; i++)
  211. {
  212. m_Enumerator.MoveNext();
  213. }
  214. }
  215. }
  216. private IList m_Adaptee;
  217. public ArrayListAdapter(IList adaptee)
  218. : base(0, true)
  219. {
  220. m_Adaptee = adaptee;
  221. }
  222. public override object this[int index]
  223. {
  224. get
  225. {
  226. return m_Adaptee[index];
  227. }
  228. set
  229. {
  230. m_Adaptee[index] = value;
  231. }
  232. }
  233. public override int Count
  234. {
  235. get
  236. {
  237. return m_Adaptee.Count;
  238. }
  239. }
  240. public override int Capacity
  241. {
  242. get
  243. {
  244. return m_Adaptee.Count;
  245. }
  246. set
  247. {
  248. if (value < m_Adaptee.Count)
  249. {
  250. throw new ArgumentException("capacity");
  251. }
  252. }
  253. }
  254. public override bool IsFixedSize
  255. {
  256. get
  257. {
  258. return m_Adaptee.IsFixedSize;
  259. }
  260. }
  261. public override bool IsReadOnly
  262. {
  263. get
  264. {
  265. return m_Adaptee.IsReadOnly;
  266. }
  267. }
  268. public override object SyncRoot
  269. {
  270. get
  271. {
  272. return m_Adaptee.SyncRoot;
  273. }
  274. }
  275. public override int Add(object value)
  276. {
  277. return m_Adaptee.Add(value);
  278. }
  279. public override void Clear()
  280. {
  281. m_Adaptee.Clear();
  282. }
  283. public override bool Contains(object value)
  284. {
  285. return m_Adaptee.Contains(value);
  286. }
  287. public override int IndexOf(object value)
  288. {
  289. return m_Adaptee.IndexOf(value);
  290. }
  291. public override int IndexOf(object value, int startIndex)
  292. {
  293. return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
  294. }
  295. public override int IndexOf(object value, int startIndex, int count)
  296. {
  297. if (startIndex < 0 || startIndex > m_Adaptee.Count)
  298. {
  299. throw new ArgumentOutOfRangeException("startIndex", startIndex,
  300. "Does not specify valid index.");
  301. }
  302. if (count < 0)
  303. {
  304. throw new ArgumentOutOfRangeException("count", count,
  305. "Can't be less than 0.");
  306. }
  307. // re-ordered to avoid possible integer overflow
  308. if (startIndex > m_Adaptee.Count - count) {
  309. // LAMESPEC: Every other method throws ArgumentException
  310. throw new ArgumentOutOfRangeException("count",
  311. "Start index and count do not specify a valid range.");
  312. }
  313. if (value == null)
  314. {
  315. for (int i = startIndex; i < startIndex + count; i++)
  316. {
  317. if (m_Adaptee[i] == null)
  318. {
  319. return i;
  320. }
  321. }
  322. }
  323. else
  324. {
  325. for (int i = startIndex; i < startIndex + count; i++)
  326. {
  327. if (value.Equals(m_Adaptee[i]))
  328. {
  329. return i;
  330. }
  331. }
  332. }
  333. return -1;
  334. }
  335. public override int LastIndexOf(object value)
  336. {
  337. return LastIndexOf(value, m_Adaptee.Count - 1);
  338. }
  339. public override int LastIndexOf(object value, int startIndex)
  340. {
  341. return LastIndexOf(value, startIndex, startIndex + 1);
  342. }
  343. public override int LastIndexOf(object value, int startIndex, int count)
  344. {
  345. if (startIndex < 0)
  346. {
  347. throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
  348. }
  349. if (count < 0)
  350. {
  351. throw new ArgumentOutOfRangeException("count", count, "count is negative.");
  352. }
  353. if (startIndex - count + 1 < 0)
  354. {
  355. throw new ArgumentOutOfRangeException("count", count, "count is too large.");
  356. }
  357. if (value == null)
  358. {
  359. for (int i = startIndex; i > startIndex - count; i--)
  360. {
  361. if (m_Adaptee[i] == null)
  362. {
  363. return i;
  364. }
  365. }
  366. }
  367. else
  368. {
  369. for (int i = startIndex; i > startIndex - count; i--)
  370. {
  371. if (value.Equals(m_Adaptee[i]))
  372. {
  373. return i;
  374. }
  375. }
  376. }
  377. return -1;
  378. }
  379. public override void Insert(int index, object value)
  380. {
  381. m_Adaptee.Insert(index, value);
  382. }
  383. public override void InsertRange(int index, ICollection c)
  384. {
  385. if (c == null)
  386. {
  387. throw new ArgumentNullException("c");
  388. }
  389. if (index > m_Adaptee.Count)
  390. {
  391. throw new ArgumentOutOfRangeException("index", index,
  392. "Index must be >= 0 and <= Count.");
  393. }
  394. foreach (object value in c)
  395. {
  396. m_Adaptee.Insert(index++, value);
  397. }
  398. }
  399. public override void Remove(object value)
  400. {
  401. m_Adaptee.Remove(value);
  402. }
  403. public override void RemoveAt(int index)
  404. {
  405. m_Adaptee.RemoveAt(index);
  406. }
  407. public override void RemoveRange(int index, int count)
  408. {
  409. CheckRange(index, count, m_Adaptee.Count);
  410. for (int i = 0; i < count; i++)
  411. {
  412. m_Adaptee.RemoveAt(index);
  413. }
  414. }
  415. public override void Reverse()
  416. {
  417. Reverse(0, m_Adaptee.Count);
  418. }
  419. public override void Reverse(int index, int count)
  420. {
  421. object tmp;
  422. CheckRange(index, count, m_Adaptee.Count);
  423. for (int i = 0; i < count / 2; i++)
  424. {
  425. tmp = m_Adaptee[i + index];
  426. m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
  427. m_Adaptee[(index + count) - i + index - 1] = tmp;
  428. }
  429. }
  430. public override void SetRange(int index, ICollection c)
  431. {
  432. if (c == null)
  433. {
  434. throw new ArgumentNullException("c");
  435. }
  436. if (index < 0 || index + c.Count > m_Adaptee.Count)
  437. {
  438. throw new ArgumentOutOfRangeException("index");
  439. }
  440. int x = index;
  441. foreach (object value in c)
  442. {
  443. m_Adaptee[x++] = value;
  444. }
  445. }
  446. public override void CopyTo(System.Array array)
  447. {
  448. m_Adaptee.CopyTo(array, 0);
  449. }
  450. public override void CopyTo(System.Array array, int index)
  451. {
  452. m_Adaptee.CopyTo(array, index);
  453. }
  454. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  455. {
  456. if (index < 0)
  457. {
  458. throw new ArgumentOutOfRangeException("index", index,
  459. "Can't be less than zero.");
  460. }
  461. if (arrayIndex < 0)
  462. {
  463. throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
  464. "Can't be less than zero.");
  465. }
  466. if (count < 0)
  467. {
  468. throw new ArgumentOutOfRangeException("index", index,
  469. "Can't be less than zero.");
  470. }
  471. if (index >= m_Adaptee.Count)
  472. {
  473. throw new ArgumentException("Can't be more or equal to list count.",
  474. "index");
  475. }
  476. if (array.Rank > 1)
  477. {
  478. throw new ArgumentException("Can't copy into multi-dimensional array.");
  479. }
  480. if (arrayIndex >= array.Length)
  481. {
  482. throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
  483. }
  484. if (array.Length - arrayIndex + 1 < count)
  485. {
  486. throw new ArgumentException("Destination array is too small.");
  487. }
  488. // re-ordered to avoid possible integer overflow
  489. if (index > m_Adaptee.Count - count) {
  490. throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
  491. }
  492. for (int i = 0; i < count; i++)
  493. {
  494. array.SetValue(m_Adaptee[index + i], arrayIndex + i);
  495. }
  496. }
  497. public override bool IsSynchronized
  498. {
  499. get
  500. {
  501. return m_Adaptee.IsSynchronized;
  502. }
  503. }
  504. public override IEnumerator GetEnumerator()
  505. {
  506. return m_Adaptee.GetEnumerator();
  507. }
  508. public override IEnumerator GetEnumerator(int index, int count)
  509. {
  510. CheckRange(index, count, m_Adaptee.Count);
  511. return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
  512. }
  513. public override void AddRange(ICollection c)
  514. {
  515. foreach (object value in c)
  516. {
  517. m_Adaptee.Add(value);
  518. }
  519. }
  520. public override int BinarySearch(object value)
  521. {
  522. return BinarySearch(value, null);
  523. }
  524. public override int BinarySearch(object value, IComparer comparer)
  525. {
  526. return BinarySearch(0, m_Adaptee.Count, value, comparer);
  527. }
  528. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  529. {
  530. int r, x, y, z;
  531. // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
  532. // Alternatives include copying the adaptee to a temporary array first.
  533. CheckRange(index, count, m_Adaptee.Count);
  534. if (comparer == null)
  535. {
  536. comparer = Comparer.Default;
  537. }
  538. x = index;
  539. y = index + count - 1;
  540. while (x <= y)
  541. {
  542. z = (x + y) / 2;
  543. r = comparer.Compare(value, m_Adaptee[z]);
  544. if (r < 0)
  545. {
  546. y = z - 1;
  547. }
  548. else if (r > 0)
  549. {
  550. x = z + 1;
  551. }
  552. else
  553. {
  554. return z;
  555. }
  556. }
  557. return ~x;
  558. }
  559. public override object Clone()
  560. {
  561. return new ArrayList.ArrayListAdapter(m_Adaptee);
  562. }
  563. public override ArrayList GetRange(int index, int count)
  564. {
  565. CheckRange(index, count, m_Adaptee.Count);
  566. return new RangedArrayList(this, index, count);
  567. }
  568. public override void TrimToSize()
  569. {
  570. // N/A
  571. }
  572. public override void Sort()
  573. {
  574. Sort(Comparer.Default);
  575. }
  576. public override void Sort(IComparer comparer)
  577. {
  578. Sort(0, m_Adaptee.Count, comparer);
  579. }
  580. public override void Sort(int index, int count, IComparer comparer)
  581. {
  582. CheckRange(index, count, m_Adaptee.Count);
  583. if (comparer == null)
  584. {
  585. comparer = Comparer.Default;
  586. }
  587. // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
  588. // Alternatives include copying the adaptee into a temporary array first.
  589. QuickSort(m_Adaptee, index, index + count - 1, comparer);
  590. }
  591. /// <summary>
  592. /// Swaps two items in a list at the specified indexes.
  593. /// </summary>
  594. private static void Swap(IList list, int x, int y)
  595. {
  596. object tmp;
  597. tmp = list[x];
  598. list[x] = list[y];
  599. list[y] = tmp;
  600. }
  601. /// <summary>
  602. /// Quicksort for lists.
  603. /// </summary>
  604. /// <remarks>
  605. /// This function acts as both qsort() and partition().
  606. /// </remarks>
  607. internal static void QuickSort(IList list, int left, int right, IComparer comparer)
  608. {
  609. int i, j, middle;
  610. object pivot;
  611. if (left >= right)
  612. {
  613. return;
  614. }
  615. // Pick the pivot using the median-of-three strategy.
  616. middle = (left + right) / 2;
  617. if (comparer.Compare(list[middle], list[left]) < 0)
  618. {
  619. Swap(list, middle, left);
  620. }
  621. if (comparer.Compare(list[right], list[left]) < 0)
  622. {
  623. Swap(list, right, left);
  624. }
  625. if (comparer.Compare(list[right], list[middle]) < 0)
  626. {
  627. Swap(list, right, middle);
  628. }
  629. if (right - left + 1 <= 3)
  630. {
  631. return;
  632. }
  633. // Put the pivot in right - 1.
  634. Swap(list, right - 1, middle);
  635. // List should look like:
  636. //
  637. // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
  638. pivot = list[right - 1];
  639. // Sort from (left + 1) to (right - 2).
  640. i = left;
  641. j = right - 1;
  642. for (;;)
  643. {
  644. while (comparer.Compare(list[++i], pivot) < 0);
  645. while (comparer.Compare(list[--j], pivot) > 0);
  646. if (i < j)
  647. {
  648. Swap(list, i, j);
  649. }
  650. else
  651. {
  652. break;
  653. }
  654. }
  655. // Put pivot into the right position (real middle).
  656. Swap(list, right - 1, i);
  657. // Recursively sort the left and right sub lists.
  658. QuickSort(list, left, i - 1, comparer);
  659. QuickSort(list, i + 1, right, comparer);
  660. }
  661. public override object[] ToArray()
  662. {
  663. object[] retval;
  664. retval = new object[m_Adaptee.Count];
  665. m_Adaptee.CopyTo(retval, 0);
  666. return retval;
  667. }
  668. public override Array ToArray(Type elementType)
  669. {
  670. Array retval;
  671. retval = Array.CreateInstance(elementType, m_Adaptee.Count);
  672. m_Adaptee.CopyTo(retval, 0);
  673. return retval;
  674. }
  675. }
  676. #endregion // ArrayListAdapter
  677. //
  678. // ArrayList wrappers
  679. //
  680. #region ArrayListWrapper
  681. /// <summary>
  682. /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
  683. /// the underlying wrappee.
  684. /// </summary>
  685. [Serializable]
  686. private class ArrayListWrapper
  687. : ArrayList
  688. {
  689. protected ArrayList m_InnerArrayList;
  690. #region Constructors
  691. public ArrayListWrapper(ArrayList innerArrayList)
  692. {
  693. m_InnerArrayList = innerArrayList;
  694. }
  695. #endregion
  696. #region Indexers
  697. public override object this[int index]
  698. {
  699. get
  700. {
  701. return m_InnerArrayList[index];
  702. }
  703. set
  704. {
  705. m_InnerArrayList[index] = value;
  706. }
  707. }
  708. #endregion
  709. #region Properties
  710. public override int Count
  711. {
  712. get
  713. {
  714. return m_InnerArrayList.Count;
  715. }
  716. }
  717. public override int Capacity
  718. {
  719. get
  720. {
  721. return m_InnerArrayList.Capacity;
  722. }
  723. set
  724. {
  725. m_InnerArrayList.Capacity = value;
  726. }
  727. }
  728. public override bool IsFixedSize
  729. {
  730. get
  731. {
  732. return m_InnerArrayList.IsFixedSize;
  733. }
  734. }
  735. public override bool IsReadOnly
  736. {
  737. get
  738. {
  739. return m_InnerArrayList.IsReadOnly;
  740. }
  741. }
  742. public override bool IsSynchronized
  743. {
  744. get
  745. {
  746. return m_InnerArrayList.IsSynchronized;
  747. }
  748. }
  749. public override object SyncRoot
  750. {
  751. get
  752. {
  753. return m_InnerArrayList.SyncRoot;
  754. }
  755. }
  756. #endregion
  757. #region Methods
  758. public override int Add(object value)
  759. {
  760. return m_InnerArrayList.Add(value);
  761. }
  762. public override void Clear()
  763. {
  764. m_InnerArrayList.Clear();
  765. }
  766. public override bool Contains(object value)
  767. {
  768. return m_InnerArrayList.Contains(value);
  769. }
  770. public override int IndexOf(object value)
  771. {
  772. return m_InnerArrayList.IndexOf(value);
  773. }
  774. public override int IndexOf(object value, int startIndex)
  775. {
  776. return m_InnerArrayList.IndexOf(value, startIndex);
  777. }
  778. public override int IndexOf(object value, int startIndex, int count)
  779. {
  780. return m_InnerArrayList.IndexOf(value, startIndex, count);
  781. }
  782. public override int LastIndexOf(object value)
  783. {
  784. return m_InnerArrayList.LastIndexOf(value);
  785. }
  786. public override int LastIndexOf(object value, int startIndex)
  787. {
  788. return m_InnerArrayList.LastIndexOf(value, startIndex);
  789. }
  790. public override int LastIndexOf(object value, int startIndex, int count)
  791. {
  792. return m_InnerArrayList.LastIndexOf(value, startIndex, count);
  793. }
  794. public override void Insert(int index, object value)
  795. {
  796. m_InnerArrayList.Insert(index, value);
  797. }
  798. public override void InsertRange(int index, ICollection c)
  799. {
  800. m_InnerArrayList.InsertRange(index, c);
  801. }
  802. public override void Remove(object value)
  803. {
  804. m_InnerArrayList.Remove(value);
  805. }
  806. public override void RemoveAt(int index)
  807. {
  808. m_InnerArrayList.RemoveAt(index);
  809. }
  810. public override void RemoveRange(int index, int count)
  811. {
  812. m_InnerArrayList.RemoveRange(index, count);
  813. }
  814. public override void Reverse()
  815. {
  816. m_InnerArrayList.Reverse();
  817. }
  818. public override void Reverse(int index, int count)
  819. {
  820. m_InnerArrayList.Reverse(index, count);
  821. }
  822. public override void SetRange(int index, ICollection c)
  823. {
  824. m_InnerArrayList.SetRange(index, c);
  825. }
  826. public override void CopyTo(System.Array array)
  827. {
  828. m_InnerArrayList.CopyTo(array);
  829. }
  830. public override void CopyTo(System.Array array, int index)
  831. {
  832. m_InnerArrayList.CopyTo(array, index);
  833. }
  834. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  835. {
  836. m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
  837. }
  838. public override IEnumerator GetEnumerator()
  839. {
  840. return m_InnerArrayList.GetEnumerator();
  841. }
  842. public override IEnumerator GetEnumerator(int index, int count)
  843. {
  844. return m_InnerArrayList.GetEnumerator(index, count);
  845. }
  846. public override void AddRange(ICollection c)
  847. {
  848. m_InnerArrayList.AddRange(c);
  849. }
  850. public override int BinarySearch(object value)
  851. {
  852. return m_InnerArrayList.BinarySearch(value);
  853. }
  854. public override int BinarySearch(object value, IComparer comparer)
  855. {
  856. return m_InnerArrayList.BinarySearch(value, comparer);
  857. }
  858. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  859. {
  860. return m_InnerArrayList.BinarySearch(index, count, value, comparer);
  861. }
  862. public override object Clone()
  863. {
  864. return m_InnerArrayList.Clone();
  865. }
  866. public override ArrayList GetRange(int index, int count)
  867. {
  868. return m_InnerArrayList.GetRange(index, count);
  869. }
  870. public override void TrimToSize()
  871. {
  872. m_InnerArrayList.TrimToSize();
  873. }
  874. public override void Sort()
  875. {
  876. m_InnerArrayList.Sort();
  877. }
  878. public override void Sort(IComparer comparer)
  879. {
  880. m_InnerArrayList.Sort(comparer);
  881. }
  882. public override void Sort(int index, int count, IComparer comparer)
  883. {
  884. m_InnerArrayList.Sort(index, count, comparer);
  885. }
  886. public override object[] ToArray()
  887. {
  888. return m_InnerArrayList.ToArray();
  889. }
  890. public override Array ToArray(Type elementType)
  891. {
  892. return m_InnerArrayList.ToArray(elementType);
  893. }
  894. #endregion
  895. }
  896. #endregion
  897. #region SynchronizedArrayListWrapper
  898. /// <summary>
  899. /// ArrayListWrapper that synchronizes calls to all methods/properties.
  900. /// </summary>
  901. /// <remarks>
  902. /// Works by just synchronizing all method calls. In the future careful optimisation
  903. /// could give better performance...
  904. /// </remarks>
  905. [Serializable]
  906. private sealed class SynchronizedArrayListWrapper
  907. : ArrayListWrapper
  908. {
  909. private object m_SyncRoot;
  910. #region Constructors
  911. /// <summary>
  912. /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
  913. /// </summary>
  914. /// <param name="innerArrayList"></param>
  915. internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
  916. : base(innerArrayList)
  917. {
  918. m_SyncRoot = innerArrayList.SyncRoot;
  919. }
  920. #endregion
  921. #region Indexers
  922. public override object this[int index]
  923. {
  924. get
  925. {
  926. lock (m_SyncRoot)
  927. {
  928. return m_InnerArrayList[index];
  929. }
  930. }
  931. set
  932. {
  933. lock (m_SyncRoot)
  934. {
  935. m_InnerArrayList[index] = value;
  936. }
  937. }
  938. }
  939. #endregion
  940. #region Properties
  941. // Some of these properties may be calculated so it's best to synchronize
  942. // them even though it might cause a performance hit.
  943. // Better safe than sorry ;D.
  944. public override int Count
  945. {
  946. get
  947. {
  948. lock (m_SyncRoot)
  949. {
  950. return m_InnerArrayList.Count;
  951. }
  952. }
  953. }
  954. public override int Capacity
  955. {
  956. get
  957. {
  958. lock (m_SyncRoot)
  959. {
  960. return m_InnerArrayList.Capacity;
  961. }
  962. }
  963. set
  964. {
  965. lock (m_SyncRoot)
  966. {
  967. m_InnerArrayList.Capacity = value;
  968. }
  969. }
  970. }
  971. public override bool IsFixedSize
  972. {
  973. get
  974. {
  975. lock (m_SyncRoot)
  976. {
  977. return m_InnerArrayList.IsFixedSize;
  978. }
  979. }
  980. }
  981. public override bool IsReadOnly
  982. {
  983. get
  984. {
  985. lock (m_SyncRoot)
  986. {
  987. return m_InnerArrayList.IsReadOnly;
  988. }
  989. }
  990. }
  991. public override bool IsSynchronized
  992. {
  993. get
  994. {
  995. return true;
  996. }
  997. }
  998. public override object SyncRoot
  999. {
  1000. get
  1001. {
  1002. return m_SyncRoot;
  1003. }
  1004. }
  1005. #endregion
  1006. #region Methods
  1007. public override int Add(object value)
  1008. {
  1009. lock (m_SyncRoot)
  1010. {
  1011. return m_InnerArrayList.Add(value);
  1012. }
  1013. }
  1014. public override void Clear()
  1015. {
  1016. lock (m_SyncRoot)
  1017. {
  1018. m_InnerArrayList.Clear();
  1019. }
  1020. }
  1021. public override bool Contains(object value)
  1022. {
  1023. lock (m_SyncRoot)
  1024. {
  1025. return m_InnerArrayList.Contains(value);
  1026. }
  1027. }
  1028. public override int IndexOf(object value)
  1029. {
  1030. lock (m_SyncRoot)
  1031. {
  1032. return m_InnerArrayList.IndexOf(value);
  1033. }
  1034. }
  1035. public override int IndexOf(object value, int startIndex)
  1036. {
  1037. lock (m_SyncRoot)
  1038. {
  1039. return m_InnerArrayList.IndexOf(value, startIndex);
  1040. }
  1041. }
  1042. public override int IndexOf(object value, int startIndex, int count)
  1043. {
  1044. lock (m_SyncRoot)
  1045. {
  1046. return m_InnerArrayList.IndexOf(value, startIndex, count);
  1047. }
  1048. }
  1049. public override int LastIndexOf(object value)
  1050. {
  1051. lock (m_SyncRoot)
  1052. {
  1053. return m_InnerArrayList.LastIndexOf(value);
  1054. }
  1055. }
  1056. public override int LastIndexOf(object value, int startIndex)
  1057. {
  1058. lock (m_SyncRoot)
  1059. {
  1060. return m_InnerArrayList.LastIndexOf(value, startIndex);
  1061. }
  1062. }
  1063. public override int LastIndexOf(object value, int startIndex, int count)
  1064. {
  1065. lock (m_SyncRoot)
  1066. {
  1067. return m_InnerArrayList.LastIndexOf(value, startIndex, count);
  1068. }
  1069. }
  1070. public override void Insert(int index, object value)
  1071. {
  1072. lock (m_SyncRoot)
  1073. {
  1074. m_InnerArrayList.Insert(index, value);
  1075. }
  1076. }
  1077. public override void InsertRange(int index, ICollection c)
  1078. {
  1079. lock (m_SyncRoot)
  1080. {
  1081. m_InnerArrayList.InsertRange(index, c);
  1082. }
  1083. }
  1084. public override void Remove(object value)
  1085. {
  1086. lock (m_SyncRoot)
  1087. {
  1088. m_InnerArrayList.Remove(value);
  1089. }
  1090. }
  1091. public override void RemoveAt(int index)
  1092. {
  1093. lock (m_SyncRoot)
  1094. {
  1095. m_InnerArrayList.RemoveAt(index);
  1096. }
  1097. }
  1098. public override void RemoveRange(int index, int count)
  1099. {
  1100. lock (m_SyncRoot)
  1101. {
  1102. m_InnerArrayList.RemoveRange(index, count);
  1103. }
  1104. }
  1105. public override void Reverse()
  1106. {
  1107. lock (m_SyncRoot)
  1108. {
  1109. m_InnerArrayList.Reverse();
  1110. }
  1111. }
  1112. public override void Reverse(int index, int count)
  1113. {
  1114. lock (m_SyncRoot)
  1115. {
  1116. m_InnerArrayList.Reverse(index, count);
  1117. }
  1118. }
  1119. public override void CopyTo(System.Array array)
  1120. {
  1121. lock (m_SyncRoot)
  1122. {
  1123. m_InnerArrayList.CopyTo(array);
  1124. }
  1125. }
  1126. public override void CopyTo(System.Array array, int index)
  1127. {
  1128. lock (m_SyncRoot)
  1129. {
  1130. m_InnerArrayList.CopyTo(array, index);
  1131. }
  1132. }
  1133. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  1134. {
  1135. lock (m_SyncRoot)
  1136. {
  1137. m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
  1138. }
  1139. }
  1140. public override IEnumerator GetEnumerator()
  1141. {
  1142. lock (m_SyncRoot)
  1143. {
  1144. return m_InnerArrayList.GetEnumerator();
  1145. }
  1146. }
  1147. public override IEnumerator GetEnumerator(int index, int count)
  1148. {
  1149. lock (m_SyncRoot)
  1150. {
  1151. return m_InnerArrayList.GetEnumerator(index, count);
  1152. }
  1153. }
  1154. public override void AddRange(ICollection c)
  1155. {
  1156. lock (m_SyncRoot)
  1157. {
  1158. m_InnerArrayList.AddRange(c);
  1159. }
  1160. }
  1161. public override int BinarySearch(object value)
  1162. {
  1163. lock (m_SyncRoot)
  1164. {
  1165. return m_InnerArrayList.BinarySearch(value);
  1166. }
  1167. }
  1168. public override int BinarySearch(object value, IComparer comparer)
  1169. {
  1170. lock (m_SyncRoot)
  1171. {
  1172. return m_InnerArrayList.BinarySearch(value, comparer);
  1173. }
  1174. }
  1175. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  1176. {
  1177. lock (m_SyncRoot)
  1178. {
  1179. return m_InnerArrayList.BinarySearch(index, count, value, comparer);
  1180. }
  1181. }
  1182. public override object Clone()
  1183. {
  1184. lock (m_SyncRoot)
  1185. {
  1186. return m_InnerArrayList.Clone();
  1187. }
  1188. }
  1189. public override ArrayList GetRange(int index, int count)
  1190. {
  1191. lock (m_SyncRoot)
  1192. {
  1193. return m_InnerArrayList.GetRange(index, count);
  1194. }
  1195. }
  1196. public override void TrimToSize()
  1197. {
  1198. lock (m_SyncRoot)
  1199. {
  1200. m_InnerArrayList.TrimToSize();
  1201. }
  1202. }
  1203. public override void Sort()
  1204. {
  1205. lock (m_SyncRoot)
  1206. {
  1207. m_InnerArrayList.Sort();
  1208. }
  1209. }
  1210. public override void Sort(IComparer comparer)
  1211. {
  1212. lock (m_SyncRoot)
  1213. {
  1214. m_InnerArrayList.Sort(comparer);
  1215. }
  1216. }
  1217. public override void Sort(int index, int count, IComparer comparer)
  1218. {
  1219. lock (m_SyncRoot)
  1220. {
  1221. m_InnerArrayList.Sort(index, count, comparer);
  1222. }
  1223. }
  1224. public override object[] ToArray()
  1225. {
  1226. lock (m_SyncRoot)
  1227. {
  1228. return m_InnerArrayList.ToArray();
  1229. }
  1230. }
  1231. public override Array ToArray(Type elementType)
  1232. {
  1233. lock (m_SyncRoot)
  1234. {
  1235. return m_InnerArrayList.ToArray(elementType);
  1236. }
  1237. }
  1238. #endregion
  1239. }
  1240. #endregion
  1241. #region FixedSizeArrayListWrapper
  1242. [Serializable]
  1243. private class FixedSizeArrayListWrapper
  1244. : ArrayListWrapper
  1245. {
  1246. #region Constructors
  1247. public FixedSizeArrayListWrapper(ArrayList innerList)
  1248. : base(innerList)
  1249. {
  1250. }
  1251. #endregion
  1252. #region Properties
  1253. /// <summary>
  1254. /// Gets the error message to display when an readonly/fixedsize related exception is
  1255. /// thrown.
  1256. /// </summary>
  1257. protected virtual string ErrorMessage
  1258. {
  1259. get
  1260. {
  1261. return "Can't add or remove from a fixed-size list.";
  1262. }
  1263. }
  1264. public override int Capacity
  1265. {
  1266. get
  1267. {
  1268. return base.Capacity;
  1269. }
  1270. set
  1271. {
  1272. throw new NotSupportedException(this.ErrorMessage);
  1273. }
  1274. }
  1275. public override bool IsFixedSize
  1276. {
  1277. get
  1278. {
  1279. return true;
  1280. }
  1281. }
  1282. #endregion
  1283. #region Methods
  1284. public override int Add(object value)
  1285. {
  1286. throw new NotSupportedException(this.ErrorMessage);
  1287. }
  1288. public override void AddRange(ICollection c)
  1289. {
  1290. throw new NotSupportedException(this.ErrorMessage);
  1291. }
  1292. public override void Clear()
  1293. {
  1294. throw new NotSupportedException(this.ErrorMessage);
  1295. }
  1296. public override void Insert(int index, object value)
  1297. {
  1298. throw new NotSupportedException(this.ErrorMessage);
  1299. }
  1300. public override void InsertRange(int index, ICollection c)
  1301. {
  1302. throw new NotSupportedException(this.ErrorMessage);
  1303. }
  1304. public override void Remove(object value)
  1305. {
  1306. throw new NotSupportedException(this.ErrorMessage);
  1307. }
  1308. public override void RemoveAt(int index)
  1309. {
  1310. throw new NotSupportedException(this.ErrorMessage);
  1311. }
  1312. public override void RemoveRange(int index, int count)
  1313. {
  1314. throw new NotSupportedException(this.ErrorMessage);
  1315. }
  1316. public override void TrimToSize()
  1317. {
  1318. throw new NotSupportedException(this.ErrorMessage);
  1319. }
  1320. #endregion
  1321. }
  1322. #endregion
  1323. #region ReadOnlyArrayListWrapper
  1324. [Serializable]
  1325. private sealed class ReadOnlyArrayListWrapper
  1326. : FixedSizeArrayListWrapper
  1327. {
  1328. protected override string ErrorMessage
  1329. {
  1330. get
  1331. {
  1332. return "Can't modify a readonly list.";
  1333. }
  1334. }
  1335. public override bool IsReadOnly
  1336. {
  1337. get
  1338. {
  1339. return true;
  1340. }
  1341. }
  1342. public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
  1343. : base(innerArrayList)
  1344. {
  1345. }
  1346. public override object this[int index]
  1347. {
  1348. get
  1349. {
  1350. return m_InnerArrayList[index];
  1351. }
  1352. set
  1353. {
  1354. throw new NotSupportedException(this.ErrorMessage);
  1355. }
  1356. }
  1357. public override void Reverse()
  1358. {
  1359. throw new NotSupportedException(this.ErrorMessage);
  1360. }
  1361. public override void Reverse(int index, int count)
  1362. {
  1363. throw new NotSupportedException(this.ErrorMessage);
  1364. }
  1365. public override void SetRange(int index, ICollection c)
  1366. {
  1367. throw new NotSupportedException(this.ErrorMessage);
  1368. }
  1369. public override void Sort()
  1370. {
  1371. throw new NotSupportedException(this.ErrorMessage);
  1372. }
  1373. public override void Sort(IComparer comparer)
  1374. {
  1375. throw new NotSupportedException(this.ErrorMessage);
  1376. }
  1377. public override void Sort(int index, int count, IComparer comparer)
  1378. {
  1379. throw new NotSupportedException(this.ErrorMessage);
  1380. }
  1381. }
  1382. #endregion
  1383. #region RangedArrayList
  1384. [Serializable]
  1385. private sealed class RangedArrayList
  1386. : ArrayListWrapper
  1387. {
  1388. private int m_InnerIndex;
  1389. private int m_InnerCount;
  1390. private int m_InnerStateChanges;
  1391. public RangedArrayList(ArrayList innerList, int index, int count)
  1392. : base(innerList)
  1393. {
  1394. m_InnerIndex = index;
  1395. m_InnerCount = count;
  1396. m_InnerStateChanges = innerList._version;
  1397. }
  1398. #region Indexers
  1399. public override bool IsSynchronized
  1400. {
  1401. get
  1402. {
  1403. return false;
  1404. }
  1405. }
  1406. public override object this[int index]
  1407. {
  1408. get
  1409. {
  1410. if (index < 0 || index > m_InnerCount)
  1411. {
  1412. throw new ArgumentOutOfRangeException("index");
  1413. }
  1414. return m_InnerArrayList[m_InnerIndex + index];
  1415. }
  1416. set
  1417. {
  1418. if (index < 0 || index > m_InnerCount)
  1419. {
  1420. throw new ArgumentOutOfRangeException("index");
  1421. }
  1422. m_InnerArrayList[m_InnerIndex + index] = value;
  1423. }
  1424. }
  1425. #endregion
  1426. #region Properties
  1427. public override int Count
  1428. {
  1429. get
  1430. {
  1431. VerifyStateChanges();
  1432. return m_InnerCount;
  1433. }
  1434. }
  1435. public override int Capacity
  1436. {
  1437. get
  1438. {
  1439. return m_InnerArrayList.Capacity;
  1440. }
  1441. set
  1442. {
  1443. if (value < m_InnerCount)
  1444. {
  1445. throw new ArgumentOutOfRangeException();
  1446. }
  1447. }
  1448. }
  1449. #endregion
  1450. #region Methods
  1451. private void VerifyStateChanges()
  1452. {
  1453. if (m_InnerStateChanges != m_InnerArrayList._version)
  1454. {
  1455. throw new InvalidOperationException
  1456. ("ArrayList view is invalid because the underlying ArrayList was modified.");
  1457. }
  1458. }
  1459. public override int Add(object value)
  1460. {
  1461. VerifyStateChanges();
  1462. m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
  1463. m_InnerStateChanges = m_InnerArrayList._version;
  1464. return ++m_InnerCount;
  1465. }
  1466. public override void Clear()
  1467. {
  1468. VerifyStateChanges();
  1469. m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
  1470. m_InnerCount = 0;
  1471. m_InnerStateChanges = m_InnerArrayList._version;
  1472. }
  1473. public override bool Contains(object value)
  1474. {
  1475. return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
  1476. }
  1477. public override int IndexOf(object value)
  1478. {
  1479. return IndexOf(value, 0);
  1480. }
  1481. public override int IndexOf(object value, int startIndex)
  1482. {
  1483. return IndexOf(value, startIndex, m_InnerCount - startIndex);
  1484. }
  1485. public override int IndexOf(object value, int startIndex, int count)
  1486. {
  1487. if (startIndex < 0 || startIndex > m_InnerCount)
  1488. {
  1489. throw new ArgumentOutOfRangeException("startIndex", startIndex,
  1490. "Does not specify valid index.");
  1491. }
  1492. if (count < 0)
  1493. {
  1494. throw new ArgumentOutOfRangeException("count", count,
  1495. "Can't be less than 0.");
  1496. }
  1497. // re-ordered to avoid possible integer overflow
  1498. if (startIndex > m_InnerCount - count)
  1499. {
  1500. // LAMESPEC: Every other method throws ArgumentException
  1501. throw new ArgumentOutOfRangeException("count",
  1502. "Start index and count do not specify a valid range.");
  1503. }
  1504. int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
  1505. if (retval == -1)
  1506. {
  1507. return -1;
  1508. }
  1509. else
  1510. {
  1511. return retval - m_InnerIndex;
  1512. }
  1513. }
  1514. public override int LastIndexOf(object value)
  1515. {
  1516. return LastIndexOf(value, m_InnerCount - 1);
  1517. }
  1518. public override int LastIndexOf(object value, int startIndex)
  1519. {
  1520. return LastIndexOf(value, startIndex, startIndex + 1);
  1521. }
  1522. public override int LastIndexOf(object value, int startIndex, int count)
  1523. {
  1524. if (startIndex < 0)
  1525. {
  1526. throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
  1527. }
  1528. if (count < 0)
  1529. {
  1530. throw new ArgumentOutOfRangeException("count", count, "count is negative.");
  1531. }
  1532. int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
  1533. if (retval == -1)
  1534. {
  1535. return -1;
  1536. }
  1537. else
  1538. {
  1539. return retval - m_InnerIndex;
  1540. }
  1541. }
  1542. public override void Insert(int index, object value)
  1543. {
  1544. VerifyStateChanges();
  1545. if (index < 0 || index > m_InnerCount)
  1546. {
  1547. throw new ArgumentOutOfRangeException("index", index,
  1548. "Index must be >= 0 and <= Count.");
  1549. }
  1550. m_InnerArrayList.Insert(m_InnerIndex + index, value);
  1551. m_InnerCount++;
  1552. m_InnerStateChanges = m_InnerArrayList._version;
  1553. }
  1554. public override void InsertRange(int index, ICollection c)
  1555. {
  1556. VerifyStateChanges();
  1557. if (index < 0 || index > m_InnerCount)
  1558. {
  1559. throw new ArgumentOutOfRangeException("index", index,
  1560. "Index must be >= 0 and <= Count.");
  1561. }
  1562. m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
  1563. m_InnerCount += c.Count;
  1564. m_InnerStateChanges = m_InnerArrayList._version;
  1565. }
  1566. public override void Remove(object value)
  1567. {
  1568. VerifyStateChanges();
  1569. int x = IndexOf(value);
  1570. if (x > -1)
  1571. {
  1572. RemoveAt(x);
  1573. }
  1574. m_InnerStateChanges = m_InnerArrayList._version;
  1575. }
  1576. public override void RemoveAt(int index)
  1577. {
  1578. VerifyStateChanges();
  1579. if (index < 0 || index > m_InnerCount)
  1580. {
  1581. throw new ArgumentOutOfRangeException("index", index,
  1582. "Index must be >= 0 and <= Count.");
  1583. }
  1584. m_InnerArrayList.RemoveAt(m_InnerIndex + index);
  1585. m_InnerCount--;
  1586. m_InnerStateChanges = m_InnerArrayList._version;
  1587. }
  1588. public override void RemoveRange(int index, int count)
  1589. {
  1590. VerifyStateChanges();
  1591. CheckRange(index, count, m_InnerCount);
  1592. m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
  1593. m_InnerCount -= count;
  1594. m_InnerStateChanges = m_InnerArrayList._version;
  1595. }
  1596. public override void Reverse()
  1597. {
  1598. Reverse(0, m_InnerCount);
  1599. }
  1600. public override void Reverse(int index, int count)
  1601. {
  1602. VerifyStateChanges();
  1603. CheckRange(index, count, m_InnerCount);
  1604. m_InnerArrayList.Reverse(m_InnerIndex + index, count);
  1605. m_InnerStateChanges = m_InnerArrayList._version;
  1606. }
  1607. public override void SetRange(int index, ICollection c)
  1608. {
  1609. VerifyStateChanges();
  1610. if (index < 0 || index > m_InnerCount)
  1611. {
  1612. throw new ArgumentOutOfRangeException("index", index,
  1613. "Index must be >= 0 and <= Count.");
  1614. }
  1615. m_InnerArrayList.SetRange(m_InnerIndex + index, c);
  1616. m_InnerStateChanges = m_InnerArrayList._version;
  1617. }
  1618. public override void CopyTo(System.Array array)
  1619. {
  1620. CopyTo(array, 0);
  1621. }
  1622. public override void CopyTo(System.Array array, int index)
  1623. {
  1624. CopyTo(0, array, index, m_InnerCount);
  1625. }
  1626. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  1627. {
  1628. CheckRange(index, count, m_InnerCount);
  1629. m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
  1630. }
  1631. public override IEnumerator GetEnumerator()
  1632. {
  1633. return GetEnumerator(0, m_InnerCount);
  1634. }
  1635. public override IEnumerator GetEnumerator(int index, int count)
  1636. {
  1637. CheckRange(index, count, m_InnerCount);
  1638. return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
  1639. }
  1640. public override void AddRange(ICollection c)
  1641. {
  1642. VerifyStateChanges();
  1643. m_InnerArrayList.InsertRange(m_InnerCount, c);
  1644. m_InnerCount += c.Count;
  1645. m_InnerStateChanges = m_InnerArrayList._version;
  1646. }
  1647. public override int BinarySearch(object value)
  1648. {
  1649. return BinarySearch(0, m_InnerCount, value, Comparer.Default);
  1650. }
  1651. public override int BinarySearch(object value, IComparer comparer)
  1652. {
  1653. return BinarySearch(0, m_InnerCount, value, comparer);
  1654. }
  1655. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  1656. {
  1657. CheckRange(index, count, m_InnerCount);
  1658. return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
  1659. }
  1660. public override object Clone()
  1661. {
  1662. return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
  1663. }
  1664. public override ArrayList GetRange(int index, int count)
  1665. {
  1666. CheckRange(index, count, m_InnerCount);
  1667. return new RangedArrayList(this, index, count);
  1668. }
  1669. public override void TrimToSize()
  1670. {
  1671. throw new NotSupportedException();
  1672. }
  1673. public override void Sort()
  1674. {
  1675. Sort(Comparer.Default);
  1676. }
  1677. public override void Sort(IComparer comparer)
  1678. {
  1679. Sort(0, m_InnerCount, comparer);
  1680. }
  1681. public override void Sort(int index, int count, IComparer comparer)
  1682. {
  1683. VerifyStateChanges();
  1684. CheckRange(index, count, m_InnerCount);
  1685. m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
  1686. m_InnerStateChanges = m_InnerArrayList._version;
  1687. }
  1688. public override object[] ToArray()
  1689. {
  1690. object[] array;
  1691. array = new object[m_InnerCount];
  1692. m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
  1693. return array;
  1694. }
  1695. public override Array ToArray(Type elementType)
  1696. {
  1697. Array array;
  1698. array = Array.CreateInstance(elementType, m_InnerCount);
  1699. m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
  1700. return array;
  1701. }
  1702. #endregion
  1703. }
  1704. #endregion
  1705. //
  1706. // List wrappers
  1707. //
  1708. #region SynchronizedListWrapper
  1709. [Serializable]
  1710. private sealed class SynchronizedListWrapper
  1711. : ListWrapper
  1712. {
  1713. private object m_SyncRoot;
  1714. public SynchronizedListWrapper(IList innerList)
  1715. : base(innerList)
  1716. {
  1717. m_SyncRoot = innerList.SyncRoot;
  1718. }
  1719. public override int Count
  1720. {
  1721. get
  1722. {
  1723. lock (m_SyncRoot)
  1724. {
  1725. return m_InnerList.Count;
  1726. }
  1727. }
  1728. }
  1729. public override bool IsSynchronized
  1730. {
  1731. get
  1732. {
  1733. return true;
  1734. }
  1735. }
  1736. public override object SyncRoot
  1737. {
  1738. get
  1739. {
  1740. lock (m_SyncRoot)
  1741. {
  1742. return m_InnerList.SyncRoot;
  1743. }
  1744. }
  1745. }
  1746. public override bool IsFixedSize
  1747. {
  1748. get
  1749. {
  1750. lock (m_SyncRoot)
  1751. {
  1752. return m_InnerList.IsFixedSize;
  1753. }
  1754. }
  1755. }
  1756. public override bool IsReadOnly
  1757. {
  1758. get
  1759. {
  1760. lock (m_SyncRoot)
  1761. {
  1762. return m_InnerList.IsReadOnly;
  1763. }
  1764. }
  1765. }
  1766. public override object this[int index]
  1767. {
  1768. get
  1769. {
  1770. lock (m_SyncRoot)
  1771. {
  1772. return m_InnerList[index];
  1773. }
  1774. }
  1775. set
  1776. {
  1777. lock (m_SyncRoot)
  1778. {
  1779. m_InnerList[index] = value;
  1780. }
  1781. }
  1782. }
  1783. public override int Add(object value)
  1784. {
  1785. lock (m_SyncRoot)
  1786. {
  1787. return m_InnerList.Add(value);
  1788. }
  1789. }
  1790. public override void Clear()
  1791. {
  1792. lock (m_SyncRoot)
  1793. {
  1794. m_InnerList.Clear();
  1795. }
  1796. }
  1797. public override bool Contains(object value)
  1798. {
  1799. lock (m_SyncRoot)
  1800. {
  1801. return m_InnerList.Contains(value);
  1802. }
  1803. }
  1804. public override int IndexOf(object value)
  1805. {
  1806. lock (m_SyncRoot)
  1807. {
  1808. return m_InnerList.IndexOf(value);
  1809. }
  1810. }
  1811. public override void Insert(int index, object value)
  1812. {
  1813. lock (m_SyncRoot)
  1814. {
  1815. m_InnerList.Insert(index, value);
  1816. }
  1817. }
  1818. public override void Remove(object value)
  1819. {
  1820. lock (m_SyncRoot)
  1821. {
  1822. m_InnerList.Remove(value);
  1823. }
  1824. }
  1825. public override void RemoveAt(int index)
  1826. {
  1827. lock (m_SyncRoot)
  1828. {
  1829. m_InnerList.RemoveAt(index);
  1830. }
  1831. }
  1832. public override void CopyTo(Array array, int index)
  1833. {
  1834. lock (m_SyncRoot)
  1835. {
  1836. m_InnerList.CopyTo(array, index);
  1837. }
  1838. }
  1839. public override IEnumerator GetEnumerator()
  1840. {
  1841. lock (m_SyncRoot)
  1842. {
  1843. return m_InnerList.GetEnumerator();
  1844. }
  1845. }
  1846. }
  1847. #endregion
  1848. #region FixedSizeListWrapper
  1849. [Serializable]
  1850. private class FixedSizeListWrapper
  1851. : ListWrapper
  1852. {
  1853. protected virtual string ErrorMessage
  1854. {
  1855. get
  1856. {
  1857. return "List is fixed-size.";
  1858. }
  1859. }
  1860. public override bool IsFixedSize
  1861. {
  1862. get
  1863. {
  1864. return true;
  1865. }
  1866. }
  1867. public FixedSizeListWrapper(IList innerList)
  1868. : base(innerList)
  1869. {
  1870. }
  1871. public override int Add(object value)
  1872. {
  1873. throw new NotSupportedException(this.ErrorMessage);
  1874. }
  1875. public override void Clear()
  1876. {
  1877. throw new NotSupportedException(this.ErrorMessage);
  1878. }
  1879. public override void Insert(int index, object value)
  1880. {
  1881. throw new NotSupportedException(this.ErrorMessage);
  1882. }
  1883. public override void Remove(object value)
  1884. {
  1885. throw new NotSupportedException(this.ErrorMessage);
  1886. }
  1887. public override void RemoveAt(int index)
  1888. {
  1889. throw new NotSupportedException(this.ErrorMessage);
  1890. }
  1891. }
  1892. #endregion
  1893. #region ReadOnlyListWrapper
  1894. [Serializable]
  1895. private sealed class ReadOnlyListWrapper
  1896. : FixedSizeListWrapper
  1897. {
  1898. protected override string ErrorMessage
  1899. {
  1900. get
  1901. {
  1902. return "List is read-only.";
  1903. }
  1904. }
  1905. public override bool IsReadOnly
  1906. {
  1907. get
  1908. {
  1909. return true;
  1910. }
  1911. }
  1912. public ReadOnlyListWrapper(IList innerList)
  1913. : base(innerList)
  1914. {
  1915. }
  1916. public override object this[int index]
  1917. {
  1918. get
  1919. {
  1920. return m_InnerList[index];
  1921. }
  1922. set
  1923. {
  1924. throw new NotSupportedException(this.ErrorMessage);
  1925. }
  1926. }
  1927. }
  1928. #endregion
  1929. #region ListWrapper
  1930. /// <summary>
  1931. /// Decorates/Wraps any <c>IList</c> implementing object.
  1932. /// </summary>
  1933. [Serializable]
  1934. private class ListWrapper
  1935. : IList
  1936. {
  1937. #region Fields
  1938. protected IList m_InnerList;
  1939. #endregion
  1940. #region Constructors
  1941. public ListWrapper(IList innerList)
  1942. {
  1943. m_InnerList = innerList;
  1944. }
  1945. #endregion
  1946. #region Indexers
  1947. public virtual object this[int index]
  1948. {
  1949. get
  1950. {
  1951. return m_InnerList[index];
  1952. }
  1953. set
  1954. {
  1955. m_InnerList[index] = value;
  1956. }
  1957. }
  1958. #endregion
  1959. #region Properties
  1960. public virtual int Count
  1961. {
  1962. get
  1963. {
  1964. return m_InnerList.Count;
  1965. }
  1966. }
  1967. public virtual bool IsSynchronized
  1968. {
  1969. get
  1970. {
  1971. return m_InnerList.IsSynchronized;
  1972. }
  1973. }
  1974. public virtual object SyncRoot
  1975. {
  1976. get
  1977. {
  1978. return m_InnerList.SyncRoot;
  1979. }
  1980. }
  1981. public virtual bool IsFixedSize
  1982. {
  1983. get
  1984. {
  1985. return m_InnerList.IsFixedSize;
  1986. }
  1987. }
  1988. public virtual bool IsReadOnly
  1989. {
  1990. get
  1991. {
  1992. return m_InnerList.IsReadOnly;
  1993. }
  1994. }
  1995. #endregion
  1996. #region Methods
  1997. public virtual int Add(object value)
  1998. {
  1999. return m_InnerList.Add(value);
  2000. }
  2001. public virtual void Clear()
  2002. {
  2003. m_InnerList.Clear();
  2004. }
  2005. public virtual bool Contains(object value)
  2006. {
  2007. return m_InnerList.Contains(value);
  2008. }
  2009. public virtual int IndexOf(object value)
  2010. {
  2011. return m_InnerList.IndexOf(value);
  2012. }
  2013. public virtual void Insert(int index, object value)
  2014. {
  2015. m_InnerList.Insert(index, value);
  2016. }
  2017. public virtual void Remove(object value)
  2018. {
  2019. m_InnerList.Remove(value);
  2020. }
  2021. public virtual void RemoveAt(int index)
  2022. {
  2023. m_InnerList.RemoveAt(index);
  2024. }
  2025. public virtual void CopyTo(Array array, int index)
  2026. {
  2027. m_InnerList.CopyTo(array, index);
  2028. }
  2029. public virtual IEnumerator GetEnumerator()
  2030. {
  2031. return m_InnerList.GetEnumerator();
  2032. }
  2033. #endregion
  2034. }
  2035. #endregion
  2036. //
  2037. // Start of ArrayList
  2038. //
  2039. #region Fields
  2040. private const int DefaultInitialCapacity = 0x10;
  2041. /// <summary>
  2042. /// Number of items in the list.
  2043. /// </summary>
  2044. private int _size;
  2045. /// <summary>
  2046. /// Array to store the items.
  2047. /// </summary>
  2048. private object[] _items;
  2049. /// <summary>
  2050. /// Total number of state changes.
  2051. /// </summary>
  2052. private int _version;
  2053. #endregion
  2054. #region Constructors
  2055. /// <summary>
  2056. /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
  2057. /// has the default initial capacity (16).
  2058. /// </summary>
  2059. public ArrayList()
  2060. {
  2061. _items = new object[DefaultInitialCapacity];
  2062. }
  2063. /// <summary>
  2064. /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
  2065. /// elements copied from the specified collection and that has the same initial capacity
  2066. /// as the number of elements copied.
  2067. /// </summary>
  2068. /// <param name="c">
  2069. /// The <see cref="ICollection"/> whose elements are copied into the new list.
  2070. /// </param>
  2071. /// <exception cref="ArgumentNullException">
  2072. /// The argument <c>c</c> is a null reference.
  2073. /// </exception>
  2074. public ArrayList(ICollection c)
  2075. {
  2076. Array array;
  2077. if (c == null)
  2078. {
  2079. throw new ArgumentNullException("c");
  2080. }
  2081. array = c as Array;
  2082. if (array != null && array.Rank != 1)
  2083. {
  2084. throw new RankException();
  2085. }
  2086. _items = new object[c.Count];
  2087. AddRange(c);
  2088. }
  2089. /// <summary>
  2090. /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
  2091. /// has the specified initial capacity.
  2092. /// </summary>
  2093. /// <param name="initialCapacity">
  2094. /// The number of elements that hte new list is initially capable of storing.
  2095. /// </param>
  2096. /// <exception cref="ArgumentOutOfRangeException">
  2097. /// The <c>capacity</c> is less than zero.
  2098. /// </exception>
  2099. public ArrayList(int initialCapacity)
  2100. {
  2101. if (initialCapacity < 0)
  2102. {
  2103. throw new ArgumentOutOfRangeException("initialCapacity",
  2104. initialCapacity, "The initial capacity can't be smaller than zero.");
  2105. }
  2106. if (initialCapacity == 0)
  2107. {
  2108. initialCapacity = DefaultInitialCapacity;
  2109. }
  2110. _items = new object[initialCapacity];
  2111. }
  2112. /// <summary>
  2113. /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
  2114. /// </summary>
  2115. private ArrayList(int initialCapacity, bool forceZeroSize)
  2116. {
  2117. if (forceZeroSize)
  2118. {
  2119. _items = null;
  2120. }
  2121. else
  2122. {
  2123. throw new InvalidOperationException("Use ArrayList(int)");
  2124. }
  2125. }
  2126. /// <summary>
  2127. /// Initializes a new array list that contains a copy of the given array and with the
  2128. /// given count.
  2129. /// </summary>
  2130. /// <param name="array"></param>
  2131. private ArrayList(object[] array, int index, int count)
  2132. {
  2133. _items = new object[count];
  2134. if (count == 0)
  2135. {
  2136. _items = new object[DefaultInitialCapacity];
  2137. }
  2138. else
  2139. {
  2140. _items = new object[count];
  2141. }
  2142. Array.Copy(array, index, _items, 0, count);
  2143. _size = count;
  2144. }
  2145. #endregion
  2146. #region Indexers
  2147. /// <summary>
  2148. /// Gets/Sets an element in the list by index.
  2149. /// </summary>
  2150. /// <exception cref="ArgumentOutOfRangeException">
  2151. /// The index is less than 0 or more then or equal to the list count.
  2152. /// </exception>
  2153. public virtual object this[int index]
  2154. {
  2155. get
  2156. {
  2157. if (index < 0 || index >= _size)
  2158. {
  2159. throw new ArgumentOutOfRangeException("index", index,
  2160. "Index is less than 0 or more than or equal to the list count.");
  2161. }
  2162. return _items[index];
  2163. }
  2164. set
  2165. {
  2166. if (index < 0 || index >= _size)
  2167. {
  2168. throw new ArgumentOutOfRangeException("index", index,
  2169. "Index is less than 0 or more than or equal to the list count.");
  2170. }
  2171. _items[index] = value;
  2172. _version++;
  2173. }
  2174. }
  2175. #endregion
  2176. #region Properties
  2177. /// <summary>
  2178. /// Gets the number of elements in the list.
  2179. /// </summary>
  2180. public virtual int Count
  2181. {
  2182. get
  2183. {
  2184. return _size;
  2185. }
  2186. }
  2187. /// <summary>
  2188. /// Gets the number of elements the list can carry without needing to expand.
  2189. /// </summary>
  2190. /// <remarks>
  2191. /// ArrayLists automatically double their capacity when the capacity limit is broken.
  2192. /// </remarks>
  2193. /// <exception cref="ArgumentOutOfRangeException">
  2194. /// The capacity is less than the count.
  2195. /// </exception>
  2196. public virtual int Capacity
  2197. {
  2198. get
  2199. {
  2200. return _items.Length;
  2201. }
  2202. set
  2203. {
  2204. if (value < _size)
  2205. {
  2206. throw new ArgumentOutOfRangeException("Capacity", value,
  2207. "Must be more than count.");
  2208. }
  2209. object[] newArray;
  2210. newArray = new object[value];
  2211. Array.Copy(_items, 0, newArray, 0, _size);
  2212. _items = newArray;
  2213. }
  2214. }
  2215. /// <summary>
  2216. /// <see cref="IList.IsFixedSize"/>
  2217. /// </summary>
  2218. /// <remarks/>
  2219. public virtual bool IsFixedSize
  2220. {
  2221. get
  2222. {
  2223. return false;
  2224. }
  2225. }
  2226. /// <summary>
  2227. /// <see cref="IList.IsReadOnly"/>
  2228. /// </summary>
  2229. public virtual bool IsReadOnly
  2230. {
  2231. get
  2232. {
  2233. return false;
  2234. }
  2235. }
  2236. /// <summary>
  2237. /// <see cref="ICollection.IsSynchronized"/>
  2238. /// </summary>
  2239. public virtual bool IsSynchronized
  2240. {
  2241. get
  2242. {
  2243. return false;
  2244. }
  2245. }
  2246. /// <summary>
  2247. /// <see cref="ICollection.SyncRoot"/>
  2248. /// </summary>
  2249. public virtual object SyncRoot
  2250. {
  2251. get
  2252. {
  2253. return this;
  2254. }
  2255. }
  2256. #endregion
  2257. #region Methods
  2258. /// <remarks>
  2259. /// Ensures that the list has the capacity to contain the given <c>count</c> by
  2260. /// automatically expanding the capacity when required.
  2261. /// </remarks>
  2262. private void EnsureCapacity(int count)
  2263. {
  2264. if (count <= _items.Length)
  2265. {
  2266. return;
  2267. }
  2268. int newLength;
  2269. object[] newData;
  2270. newLength = _items.Length << 1;
  2271. if (newLength == 0)
  2272. newLength = DefaultInitialCapacity;
  2273. while (newLength < count)
  2274. {
  2275. newLength <<= 1;
  2276. }
  2277. newData = new object[newLength];
  2278. Array.Copy(_items, 0, newData, 0, _items.Length);
  2279. _items = newData;
  2280. }
  2281. /// <summary>
  2282. /// Shifts a section of the list.
  2283. /// </summary>
  2284. /// <param name="index">
  2285. /// The start of the section to shift (the element at index is included in the shift).
  2286. /// </param>
  2287. /// <param name="count">
  2288. /// The number of positions to shift by (can be negative).
  2289. /// </param>
  2290. private void Shift(int index, int count)
  2291. {
  2292. if (count > 0)
  2293. {
  2294. if (_size + count > _items.Length)
  2295. {
  2296. int newLength;
  2297. object[] newData;
  2298. newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
  2299. while (newLength < _size + count)
  2300. {
  2301. newLength <<= 1;
  2302. }
  2303. newData = new object[newLength];
  2304. Array.Copy(_items, 0, newData, 0, index);
  2305. Array.Copy(_items, index, newData, index + count, _size - index);
  2306. _items = newData;
  2307. }
  2308. else
  2309. {
  2310. Array.Copy(_items, index, _items, index + count, _size - index);
  2311. }
  2312. }
  2313. else if (count < 0)
  2314. {
  2315. // Remember count is negative so this is actually index + (-count)
  2316. int x = index - count ;
  2317. Array.Copy(_items, x, _items, index, _size - x);
  2318. }
  2319. }
  2320. public virtual int Add(object value)
  2321. {
  2322. // Do a check here in case EnsureCapacity isn't inlined.
  2323. if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
  2324. {
  2325. EnsureCapacity(_size + 1);
  2326. }
  2327. _items[_size] = value;
  2328. _version++;
  2329. return _size++;
  2330. }
  2331. public virtual void Clear()
  2332. {
  2333. // Keep the array but null all members so they can be garbage collected.
  2334. Array.Clear(_items, 0, _size);
  2335. _size = 0;
  2336. _version++;
  2337. }
  2338. public virtual bool Contains(object value)
  2339. {
  2340. return IndexOf(value, 0, _size) > -1;
  2341. }
  2342. internal virtual bool Contains(object value, int startIndex, int count)
  2343. {
  2344. return IndexOf(value, startIndex, count) > -1;
  2345. }
  2346. public virtual int IndexOf(object value)
  2347. {
  2348. return IndexOf(value, 0);
  2349. }
  2350. public virtual int IndexOf(object value, int startIndex)
  2351. {
  2352. return IndexOf(value, startIndex, _size - startIndex);
  2353. }
  2354. public virtual int IndexOf(object value, int startIndex, int count)
  2355. {
  2356. if (startIndex < 0 || startIndex > _size)
  2357. {
  2358. throw new ArgumentOutOfRangeException("startIndex", startIndex,
  2359. "Does not specify valid index.");
  2360. }
  2361. if (count < 0)
  2362. {
  2363. throw new ArgumentOutOfRangeException("count", count,
  2364. "Can't be less than 0.");
  2365. }
  2366. // re-ordered to avoid integer overflow
  2367. if (startIndex > _size - count)
  2368. {
  2369. // LAMESPEC: Every other method throws ArgumentException
  2370. throw new ArgumentOutOfRangeException("count",
  2371. "Start index and count do not specify a valid range.");
  2372. }
  2373. return Array.IndexOf(_items, value, startIndex, count);
  2374. }
  2375. public virtual int LastIndexOf(object value)
  2376. {
  2377. return LastIndexOf(value, _size - 1);
  2378. }
  2379. public virtual int LastIndexOf(object value, int startIndex)
  2380. {
  2381. return LastIndexOf(value, startIndex, startIndex + 1);
  2382. }
  2383. public virtual int LastIndexOf (object value, int startIndex, int count)
  2384. {
  2385. // Array will catch the exceptions
  2386. return Array.LastIndexOf (_items, value, startIndex, count);
  2387. }
  2388. public virtual void Insert(int index, object value)
  2389. {
  2390. if (index < 0 || index > _size)
  2391. {
  2392. throw new ArgumentOutOfRangeException("index", index,
  2393. "Index must be >= 0 and <= Count.");
  2394. }
  2395. Shift(index, 1);
  2396. _items[index] = value;
  2397. _size++;
  2398. _version++;
  2399. }
  2400. public virtual void InsertRange(int index, ICollection c)
  2401. {
  2402. int i;
  2403. if (c == null)
  2404. {
  2405. throw new ArgumentNullException("c");
  2406. }
  2407. if (index < 0 || index > _size)
  2408. {
  2409. throw new ArgumentOutOfRangeException("index", index,
  2410. "Index must be >= 0 and <= Count.");
  2411. }
  2412. i = c.Count;
  2413. // Do a check here in case EnsureCapacity isn't inlined.
  2414. if (_items.Length < _size + i)
  2415. {
  2416. EnsureCapacity(_size + i);
  2417. }
  2418. if (index < _size)
  2419. {
  2420. Array.Copy(_items, index, _items, index + i, _size - index);
  2421. }
  2422. // Handle inserting a range from a list to itself specially.
  2423. if (this == c.SyncRoot)
  2424. {
  2425. // Copy range before the insert point.
  2426. Array.Copy(_items, 0, _items, index, index);
  2427. // Copy range after the insert point.
  2428. Array.Copy(_items, index + i, _items, index << 1, _size - index);
  2429. }
  2430. else
  2431. {
  2432. c.CopyTo(_items, index);
  2433. }
  2434. _size += c.Count;
  2435. _version++;
  2436. }
  2437. public virtual void Remove(object value)
  2438. {
  2439. int x;
  2440. x = IndexOf(value);
  2441. if (x > -1)
  2442. {
  2443. RemoveAt(x);
  2444. }
  2445. _version++;
  2446. }
  2447. public virtual void RemoveAt(int index)
  2448. {
  2449. if (index < 0 || index >= _size)
  2450. {
  2451. throw new ArgumentOutOfRangeException("index", index,
  2452. "Less than 0 or more than list count.");
  2453. }
  2454. Shift(index, -1);
  2455. _size--;
  2456. _version++;
  2457. }
  2458. public virtual void RemoveRange(int index, int count)
  2459. {
  2460. ArrayList.CheckRange(index, count, _size);
  2461. Shift(index, -count);
  2462. _size -= count;
  2463. _version++;
  2464. }
  2465. public virtual void Reverse()
  2466. {
  2467. Array.Reverse(_items, 0, _size);
  2468. _version++;
  2469. }
  2470. public virtual void Reverse(int index, int count)
  2471. {
  2472. ArrayList.CheckRange(index, count, _size);
  2473. Array.Reverse(_items, index, count);
  2474. _version++;
  2475. }
  2476. public virtual void CopyTo(System.Array array)
  2477. {
  2478. Array.Copy(_items, array, _size);
  2479. }
  2480. public virtual void CopyTo(System.Array array, int index)
  2481. {
  2482. CopyTo(0, array, index, _size);
  2483. }
  2484. public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
  2485. {
  2486. if (array == null)
  2487. {
  2488. throw new ArgumentNullException("array");
  2489. }
  2490. if (array.Rank != 1)
  2491. {
  2492. // LAMESPEC:
  2493. // This should be a RankException because Array.Copy throws RankException.
  2494. throw new ArgumentException("Must have only 1 dimensions.", "array");
  2495. }
  2496. Array.Copy(_items, index, array, arrayIndex, count);
  2497. }
  2498. public virtual IEnumerator GetEnumerator()
  2499. {
  2500. return new SimpleEnumerator(this);
  2501. }
  2502. public virtual IEnumerator GetEnumerator(int index, int count)
  2503. {
  2504. ArrayList.CheckRange(index, count, _size);
  2505. return new ArrayListEnumerator(this, index, count);
  2506. }
  2507. public virtual void AddRange(ICollection c)
  2508. {
  2509. InsertRange(_size, c);
  2510. }
  2511. public virtual int BinarySearch(object value)
  2512. {
  2513. try
  2514. {
  2515. return Array.BinarySearch(_items, 0, _size, value);
  2516. }
  2517. catch (InvalidOperationException e)
  2518. {
  2519. throw new ArgumentException(e.Message);
  2520. }
  2521. }
  2522. public virtual int BinarySearch(object value, IComparer comparer)
  2523. {
  2524. try
  2525. {
  2526. return Array.BinarySearch(_items, 0, _size, value, comparer);
  2527. }
  2528. catch (InvalidOperationException e)
  2529. {
  2530. throw new ArgumentException(e.Message);
  2531. }
  2532. }
  2533. public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
  2534. {
  2535. try
  2536. {
  2537. return Array.BinarySearch(_items, index, count, value, comparer);
  2538. }
  2539. catch (InvalidOperationException e)
  2540. {
  2541. throw new ArgumentException(e.Message);
  2542. }
  2543. }
  2544. public virtual ArrayList GetRange(int index, int count)
  2545. {
  2546. ArrayList.CheckRange(index, count, _size);
  2547. if (this.IsSynchronized)
  2548. {
  2549. return ArrayList.Synchronized(new RangedArrayList(this, index, count));
  2550. }
  2551. else
  2552. {
  2553. return new RangedArrayList(this, index, count);
  2554. }
  2555. }
  2556. public virtual void SetRange(int index, ICollection c)
  2557. {
  2558. int x = index;
  2559. if (c == null)
  2560. {
  2561. throw new ArgumentNullException("c");
  2562. }
  2563. if (index < 0 || index + c.Count > _size)
  2564. {
  2565. throw new ArgumentOutOfRangeException("index");
  2566. }
  2567. c.CopyTo(_items, index);
  2568. _version++;
  2569. }
  2570. public virtual void TrimToSize()
  2571. {
  2572. if (_items.Length > _size)
  2573. {
  2574. object[] newArray;
  2575. if (_size == 0)
  2576. {
  2577. newArray = new object[DefaultInitialCapacity];
  2578. }
  2579. else
  2580. {
  2581. newArray = new object[_size];
  2582. }
  2583. Array.Copy(_items, 0, newArray, 0, _size);
  2584. _items = newArray;
  2585. }
  2586. }
  2587. public virtual void Sort()
  2588. {
  2589. Array.Sort(_items, 0, _size);
  2590. _version++;
  2591. }
  2592. public virtual void Sort(IComparer comparer)
  2593. {
  2594. Array.Sort(_items, 0, _size, comparer);
  2595. }
  2596. public virtual void Sort(int index, int count, IComparer comparer)
  2597. {
  2598. ArrayList.CheckRange(index, count, _size);
  2599. Array.Sort(_items, index, count, comparer);
  2600. }
  2601. public virtual object[] ToArray()
  2602. {
  2603. object[] retval;
  2604. retval = new object[_size];
  2605. CopyTo(retval);
  2606. return retval;
  2607. }
  2608. public virtual Array ToArray(Type elementType)
  2609. {
  2610. Array retval;
  2611. retval = Array.CreateInstance(elementType, _size);
  2612. CopyTo(retval);
  2613. return retval;
  2614. }
  2615. public virtual object Clone()
  2616. {
  2617. return new ArrayList(this._items, 0, this._size);
  2618. }
  2619. #endregion
  2620. #region Static Methods
  2621. /// <summary>
  2622. /// Does a check of the arguments many of the methods in ArrayList use.
  2623. /// </summary>
  2624. /// <remarks>
  2625. /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
  2626. /// not all methods actually make use of CheckRange.
  2627. /// </remarks>
  2628. internal static void CheckRange(int index, int count, int listCount)
  2629. {
  2630. if (index < 0)
  2631. {
  2632. throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
  2633. }
  2634. if (count < 0)
  2635. {
  2636. throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
  2637. }
  2638. // re-ordered to avoid possible integer overflow
  2639. if (index > listCount - count)
  2640. {
  2641. throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
  2642. }
  2643. }
  2644. public static ArrayList Adapter(IList list)
  2645. {
  2646. // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
  2647. if (list == null)
  2648. {
  2649. throw new ArgumentNullException("list");
  2650. }
  2651. if (list.IsSynchronized)
  2652. {
  2653. return ArrayList.Synchronized(new ArrayListAdapter(list));
  2654. }
  2655. else
  2656. {
  2657. return new ArrayListAdapter(list);
  2658. }
  2659. }
  2660. public static ArrayList Synchronized(ArrayList arrayList)
  2661. {
  2662. if (arrayList == null)
  2663. {
  2664. throw new ArgumentNullException("arrayList");
  2665. }
  2666. if (arrayList.IsSynchronized)
  2667. {
  2668. return arrayList;
  2669. }
  2670. return new SynchronizedArrayListWrapper(arrayList);
  2671. }
  2672. public static IList Synchronized(IList list)
  2673. {
  2674. if (list == null)
  2675. {
  2676. throw new ArgumentNullException("list");
  2677. }
  2678. if (list.IsSynchronized)
  2679. {
  2680. return list;
  2681. }
  2682. return new SynchronizedListWrapper(list);
  2683. }
  2684. public static ArrayList ReadOnly(ArrayList arrayList)
  2685. {
  2686. if (arrayList == null)
  2687. {
  2688. throw new ArgumentNullException("arrayList");
  2689. }
  2690. if (arrayList.IsReadOnly)
  2691. {
  2692. return arrayList;
  2693. }
  2694. return new ReadOnlyArrayListWrapper(arrayList);
  2695. }
  2696. public static IList ReadOnly(IList list)
  2697. {
  2698. if (list == null)
  2699. {
  2700. throw new ArgumentNullException("list");
  2701. }
  2702. if (list.IsReadOnly)
  2703. {
  2704. return list;
  2705. }
  2706. return new ReadOnlyListWrapper(list);
  2707. }
  2708. public static ArrayList FixedSize(ArrayList arrayList)
  2709. {
  2710. if (arrayList == null)
  2711. {
  2712. throw new ArgumentNullException("arrayList");
  2713. }
  2714. if (arrayList.IsFixedSize)
  2715. {
  2716. return arrayList;
  2717. }
  2718. return new FixedSizeArrayListWrapper(arrayList);
  2719. }
  2720. public static IList FixedSize(IList list)
  2721. {
  2722. if (list == null)
  2723. {
  2724. throw new ArgumentNullException("list");
  2725. }
  2726. if (list.IsFixedSize)
  2727. {
  2728. return list;
  2729. }
  2730. return new FixedSizeListWrapper(list);
  2731. }
  2732. public static ArrayList Repeat(object value, int count)
  2733. {
  2734. ArrayList arrayList = new ArrayList(count);
  2735. for (int i = 0; i < count; i++)
  2736. {
  2737. arrayList.Add(value);
  2738. }
  2739. return arrayList;
  2740. }
  2741. #endregion
  2742. }
  2743. }