ArrayList.cs 63 KB

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