ArrayList.cs 63 KB

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