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