SortedList.cs 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. //
  2. // System.Collections.Generic.SortedList.cs
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. // Duncan Mak ([email protected])
  7. // Herve Poussineau ([email protected]
  8. // Zoltan Varga ([email protected])
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0
  33. using System;
  34. using System.Collections;
  35. using System.Globalization;
  36. using System.Runtime.InteropServices;
  37. namespace System.Collections.Generic {
  38. /// <summary>
  39. /// Represents a collection of associated keys and values
  40. /// that are sorted by the keys and are accessible by key
  41. /// and by index.
  42. /// </summary>
  43. [Serializable]
  44. [ComVisible(false)]
  45. public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>,
  46. IDictionary,
  47. ICollection,
  48. ICollection<KeyValuePair<TKey, TValue>>,
  49. IEnumerable<KeyValuePair<TKey, TValue>>,
  50. IEnumerable {
  51. private readonly static int INITIAL_SIZE = 16;
  52. private enum EnumeratorMode : int { KEY_MODE = 0, VALUE_MODE, ENTRY_MODE }
  53. private int inUse;
  54. private int modificationCount;
  55. private KeyValuePair<TKey, TValue>[] table;
  56. private IComparer<TKey> comparer;
  57. private int defaultCapacity;
  58. //
  59. // Constructors
  60. //
  61. public SortedList ()
  62. : this (INITIAL_SIZE, null)
  63. {
  64. }
  65. public SortedList (int capacity)
  66. : this (capacity, null)
  67. {
  68. }
  69. public SortedList (int capacity, IComparer<TKey> comparer)
  70. {
  71. if (capacity < 0)
  72. throw new ArgumentOutOfRangeException ("initialCapacity");
  73. if (capacity == 0)
  74. defaultCapacity = 0;
  75. else
  76. defaultCapacity = INITIAL_SIZE;
  77. Init (comparer, capacity, true);
  78. }
  79. public SortedList (IComparer<TKey> comparer) : this (INITIAL_SIZE, comparer)
  80. {
  81. }
  82. public SortedList (IDictionary<TKey, TValue> dictionary) : this (dictionary, null)
  83. {
  84. }
  85. public SortedList (IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
  86. {
  87. if (dictionary == null)
  88. throw new ArgumentNullException ("dictionary");
  89. Init (comparer, dictionary.Count, true);
  90. foreach (KeyValuePair<TKey, TValue> kvp in dictionary)
  91. Add (kvp.Key, kvp.Value);
  92. }
  93. //
  94. // Properties
  95. //
  96. // ICollection
  97. public virtual int Count {
  98. get {
  99. return inUse;
  100. }
  101. }
  102. bool ICollection.IsSynchronized {
  103. get {
  104. return false;
  105. }
  106. }
  107. Object ICollection.SyncRoot {
  108. get {
  109. return this;
  110. }
  111. }
  112. // IDictionary
  113. bool IDictionary.IsFixedSize {
  114. get {
  115. return false;
  116. }
  117. }
  118. bool IDictionary.IsReadOnly {
  119. get {
  120. return false;
  121. }
  122. }
  123. public virtual TValue this [TKey key] {
  124. get {
  125. if (key == null)
  126. throw new ArgumentNullException("key");
  127. int i = Find (key);
  128. if (i >= 0)
  129. return table [i].Value;
  130. else
  131. throw new KeyNotFoundException ();
  132. }
  133. set {
  134. if (key == null)
  135. throw new ArgumentNullException("key");
  136. PutImpl (key, value, true);
  137. }
  138. }
  139. object IDictionary.this [object key] {
  140. get {
  141. if (!(key is TKey))
  142. return null;
  143. else
  144. return this [(TKey)key];
  145. }
  146. set {
  147. this [ToKey (key)] = ToValue (value);
  148. }
  149. }
  150. public int Capacity {
  151. get {
  152. return table.Length;
  153. }
  154. set {
  155. int current = this.table.Length;
  156. if (inUse > value) {
  157. throw new ArgumentOutOfRangeException("capacity too small");
  158. }
  159. else if (value == 0) {
  160. // return to default size
  161. KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [defaultCapacity];
  162. Array.Copy (table, newTable, inUse);
  163. this.table = newTable;
  164. }
  165. #if NET_1_0
  166. else if (current > defaultCapacity && value < current) {
  167. KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [defaultCapacity];
  168. Array.Copy (table, newTable, inUse);
  169. this.table = newTable;
  170. }
  171. #endif
  172. else if (value > inUse) {
  173. KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [value];
  174. Array.Copy (table, newTable, inUse);
  175. this.table = newTable;
  176. }
  177. else if (value > current) {
  178. KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [value];
  179. Array.Copy (table, newTable, current);
  180. this.table = newTable;
  181. }
  182. }
  183. }
  184. public IList<TKey> Keys {
  185. get {
  186. return new ListKeys (this);
  187. }
  188. }
  189. public IList<TValue> Values {
  190. get {
  191. return new ListValues (this);
  192. }
  193. }
  194. ICollection IDictionary.Keys {
  195. get {
  196. return new ListKeys (this);
  197. }
  198. }
  199. ICollection IDictionary.Values {
  200. get {
  201. return new ListValues (this);
  202. }
  203. }
  204. ICollection<TKey> IDictionary<TKey, TValue>.Keys {
  205. get {
  206. return Keys;
  207. }
  208. }
  209. ICollection<TValue> IDictionary<TKey, TValue>.Values {
  210. get {
  211. return Values;
  212. }
  213. }
  214. public IComparer<TKey> Comparer {
  215. get {
  216. return comparer;
  217. }
  218. }
  219. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
  220. get {
  221. return false;
  222. }
  223. }
  224. //
  225. // Public instance methods.
  226. //
  227. // IDictionary<TKey, TValue>
  228. void IDictionary<TKey,TValue>.Add (TKey key, TValue value)
  229. {
  230. if (key == null)
  231. throw new ArgumentNullException ("key");
  232. PutImpl (key, value, false);
  233. }
  234. public virtual void Add (TKey key, TValue value)
  235. {
  236. if (key == null)
  237. throw new ArgumentNullException ("key");
  238. PutImpl (key, value, false);
  239. }
  240. public bool ContainsKey (TKey key)
  241. {
  242. if (key == null)
  243. throw new ArgumentNullException ("key");
  244. return (Find (key) >= 0);
  245. }
  246. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
  247. {
  248. for (int i = 0; i < inUse; i ++) {
  249. KeyValuePair<TKey, TValue> current = this.table [i];
  250. yield return new KeyValuePair<TKey, TValue> (current.Key, current.Value);
  251. }
  252. }
  253. bool IDictionary<TKey,TValue>.Remove (TKey key)
  254. {
  255. if (key == null)
  256. throw new ArgumentNullException ("key");
  257. int i = IndexOfKey (key);
  258. if (i >= 0) {
  259. RemoveAt (i);
  260. return true;
  261. }
  262. else
  263. return false;
  264. }
  265. public virtual bool Remove (TKey key)
  266. {
  267. if (key == null)
  268. throw new ArgumentNullException ("key");
  269. int i = IndexOfKey (key);
  270. if (i >= 0) {
  271. RemoveAt (i);
  272. return true;
  273. }
  274. else
  275. return false;
  276. }
  277. // ICollection<KeyValuePair<TKey, TValue>>
  278. void ICollection<KeyValuePair<TKey, TValue>>.Clear ()
  279. {
  280. defaultCapacity = INITIAL_SIZE;
  281. this.table = new KeyValuePair<TKey, TValue> [defaultCapacity];
  282. inUse = 0;
  283. modificationCount++;
  284. }
  285. public virtual void Clear ()
  286. {
  287. defaultCapacity = INITIAL_SIZE;
  288. this.table = new KeyValuePair<TKey, TValue> [defaultCapacity];
  289. inUse = 0;
  290. modificationCount++;
  291. }
  292. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  293. {
  294. if (null == array)
  295. throw new ArgumentNullException();
  296. if (arrayIndex < 0)
  297. throw new ArgumentOutOfRangeException();
  298. if (arrayIndex >= array.Length)
  299. throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
  300. if (Count > (array.Length - arrayIndex))
  301. throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");
  302. int i = arrayIndex;
  303. foreach (KeyValuePair<TKey, TValue> pair in this)
  304. array [i++] = pair;
  305. }
  306. void ICollection<KeyValuePair<TKey, TValue>>.Add (KeyValuePair<TKey, TValue> keyValuePair) {
  307. Add (keyValuePair.Key, keyValuePair.Value);
  308. }
  309. bool ICollection<KeyValuePair<TKey, TValue>>.Contains (KeyValuePair<TKey, TValue> keyValuePair) {
  310. int i = Find (keyValuePair.Key);
  311. if (i >= 0)
  312. return Comparer<KeyValuePair<TKey, TValue>>.Default.Compare (table [i], keyValuePair) == 0;
  313. else
  314. return false;
  315. }
  316. bool ICollection<KeyValuePair<TKey, TValue>>.Remove (KeyValuePair<TKey, TValue> keyValuePair) {
  317. int i = Find (keyValuePair.Key);
  318. if (i >= 0 && (Comparer<KeyValuePair<TKey, TValue>>.Default.Compare (table [i], keyValuePair) == 0)) {
  319. RemoveAt (i);
  320. return true;
  321. }
  322. else
  323. return false;
  324. }
  325. // IEnumerable<KeyValuePair<TKey, TValue>>
  326. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
  327. {
  328. for (int i = 0; i < inUse; i ++) {
  329. KeyValuePair<TKey, TValue> current = this.table [i];
  330. yield return new KeyValuePair<TKey, TValue> (current.Key, current.Value);
  331. }
  332. }
  333. // IEnumerable
  334. IEnumerator IEnumerable.GetEnumerator ()
  335. {
  336. return GetEnumerator ();
  337. }
  338. // IDictionary
  339. void IDictionary.Add (object key, object value)
  340. {
  341. PutImpl (ToKey (key), ToValue (value), false);
  342. }
  343. bool IDictionary.Contains (object key)
  344. {
  345. if (null == key)
  346. throw new ArgumentNullException();
  347. if (!(key is TKey))
  348. return false;
  349. return (Find ((TKey)key) >= 0);
  350. }
  351. IDictionaryEnumerator IDictionary.GetEnumerator ()
  352. {
  353. return new Enumerator (this, EnumeratorMode.ENTRY_MODE);
  354. }
  355. void IDictionary.Remove (object key)
  356. {
  357. if (null == key)
  358. throw new ArgumentNullException ("key");
  359. if (!(key is TKey))
  360. return;
  361. int i = IndexOfKey ((TKey)key);
  362. if (i >= 0) RemoveAt (i);
  363. }
  364. // ICollection
  365. void ICollection.CopyTo (Array array, int arrayIndex)
  366. {
  367. if (null == array)
  368. throw new ArgumentNullException();
  369. if (arrayIndex < 0)
  370. throw new ArgumentOutOfRangeException();
  371. if (array.Rank > 1)
  372. throw new ArgumentException("array is multi-dimensional");
  373. if (arrayIndex >= array.Length)
  374. throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
  375. if (Count > (array.Length - arrayIndex))
  376. throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");
  377. IEnumerator<KeyValuePair<TKey,TValue>> it = GetEnumerator ();
  378. int i = arrayIndex;
  379. while (it.MoveNext ()) {
  380. array.SetValue (it.Current, i++);
  381. }
  382. }
  383. //
  384. // SortedList<TKey, TValue>
  385. //
  386. public void RemoveAt (int index)
  387. {
  388. KeyValuePair<TKey, TValue> [] table = this.table;
  389. int cnt = Count;
  390. if (index >= 0 && index < cnt) {
  391. if (index != cnt - 1) {
  392. Array.Copy (table, index+1, table, index, cnt-1-index);
  393. } else {
  394. table [index] = default (KeyValuePair <TKey, TValue>);
  395. }
  396. --inUse;
  397. ++modificationCount;
  398. } else {
  399. throw new ArgumentOutOfRangeException("index out of range");
  400. }
  401. }
  402. public int IndexOfKey (TKey key)
  403. {
  404. if (key == null)
  405. throw new ArgumentNullException ("key");
  406. int indx = 0;
  407. try {
  408. indx = Find (key);
  409. } catch (Exception) {
  410. throw new InvalidOperationException();
  411. }
  412. return (indx | (indx >> 31));
  413. }
  414. public int IndexOfValue (TValue value)
  415. {
  416. if (inUse == 0)
  417. return -1;
  418. for (int i = 0; i < inUse; i ++) {
  419. KeyValuePair<TKey, TValue> current = this.table [i];
  420. if (Equals (value, current.Value))
  421. return i;
  422. }
  423. return -1;
  424. }
  425. public bool ContainsValue (TValue value)
  426. {
  427. return IndexOfValue (value) >= 0;
  428. }
  429. public void TrimExcess ()
  430. {
  431. if (inUse < table.Length * 0.9)
  432. Capacity = inUse;
  433. }
  434. public bool TryGetValue (TKey key, out TValue value)
  435. {
  436. if (key == null)
  437. throw new ArgumentNullException("key");
  438. int i = Find (key);
  439. if (i >= 0) {
  440. value = table [i].Value;
  441. return true;
  442. }
  443. else {
  444. value = default (TValue);
  445. return false;
  446. }
  447. }
  448. //
  449. // Private methods
  450. //
  451. private void EnsureCapacity (int n, int free)
  452. {
  453. KeyValuePair<TKey, TValue> [] table = this.table;
  454. KeyValuePair<TKey, TValue> [] newTable = null;
  455. int cap = Capacity;
  456. bool gap = (free >=0 && free < Count);
  457. if (n > cap) {
  458. newTable = new KeyValuePair<TKey, TValue> [n << 1];
  459. }
  460. if (newTable != null) {
  461. if (gap) {
  462. int copyLen = free;
  463. if (copyLen > 0) {
  464. Array.Copy (table, 0, newTable, 0, copyLen);
  465. }
  466. copyLen = Count - free;
  467. if (copyLen > 0) {
  468. Array.Copy (table, free, newTable, free+1, copyLen);
  469. }
  470. } else {
  471. // Just a resizing, copy the entire table.
  472. Array.Copy (table, newTable, Count);
  473. }
  474. this.table = newTable;
  475. } else if (gap) {
  476. Array.Copy (table, free, table, free+1, Count - free);
  477. }
  478. }
  479. private void PutImpl (TKey key, TValue value, bool overwrite)
  480. {
  481. if (key == null)
  482. throw new ArgumentNullException ("null key");
  483. KeyValuePair<TKey, TValue> [] table = this.table;
  484. int freeIndx = -1;
  485. try {
  486. freeIndx = Find (key);
  487. } catch (Exception) {
  488. throw new InvalidOperationException();
  489. }
  490. if (freeIndx >= 0) {
  491. if (!overwrite)
  492. throw new ArgumentException("element already exists");
  493. table [freeIndx] = new KeyValuePair <TKey, TValue> (key, value);
  494. ++modificationCount;
  495. return;
  496. }
  497. freeIndx = ~freeIndx;
  498. if (freeIndx > Capacity + 1)
  499. throw new Exception ("SortedList::internal error ("+key+", "+value+") at ["+freeIndx+"]");
  500. EnsureCapacity (Count+1, freeIndx);
  501. table = this.table;
  502. table [freeIndx] = new KeyValuePair <TKey, TValue> (key, value);
  503. ++inUse;
  504. ++modificationCount;
  505. }
  506. private void Init (IComparer<TKey> comparer, int capacity, bool forceSize)
  507. {
  508. if (comparer == null)
  509. comparer = Comparer<TKey>.Default;
  510. this.comparer = comparer;
  511. if (!forceSize && (capacity < defaultCapacity))
  512. capacity = defaultCapacity;
  513. this.table = new KeyValuePair<TKey, TValue> [capacity];
  514. this.inUse = 0;
  515. this.modificationCount = 0;
  516. }
  517. private void CopyToArray (Array arr, int i,
  518. EnumeratorMode mode)
  519. {
  520. if (arr == null)
  521. throw new ArgumentNullException ("arr");
  522. if (i < 0 || i + this.Count > arr.Length)
  523. throw new ArgumentOutOfRangeException ("i");
  524. IEnumerator it = new Enumerator (this, mode);
  525. while (it.MoveNext ()) {
  526. arr.SetValue (it.Current, i++);
  527. }
  528. }
  529. private int Find (TKey key)
  530. {
  531. KeyValuePair<TKey, TValue> [] table = this.table;
  532. int len = Count;
  533. if (len == 0) return ~0;
  534. int left = 0;
  535. int right = len-1;
  536. while (left <= right) {
  537. int guess = (left + right) >> 1;
  538. int cmp = comparer.Compare (key, table[guess].Key);
  539. if (cmp == 0) return guess;
  540. if (cmp > 0) left = guess+1;
  541. else right = guess-1;
  542. }
  543. return ~left;
  544. }
  545. private TKey ToKey (object key) {
  546. if (key == null)
  547. throw new ArgumentNullException ("key");
  548. if (!(key is TKey))
  549. throw new ArgumentException ("The value \"" + key + "\" isn't of type \"" + typeof (TKey) + "\" and can't be used in this generic collection.", "key");
  550. return (TKey)key;
  551. }
  552. private TValue ToValue (object value) {
  553. if (!(value is TValue))
  554. throw new ArgumentException ("The value \"" + value + "\" isn't of type \"" + typeof (TValue) + "\" and can't be used in this generic collection.", "value");
  555. return (TValue)value;
  556. }
  557. internal TKey KeyAt (int index) {
  558. if (index >= 0 && index < Count)
  559. return table [index].Key;
  560. else
  561. throw new ArgumentOutOfRangeException("Index out of range");
  562. }
  563. internal TValue ValueAt (int index) {
  564. if (index >= 0 && index < Count)
  565. return table [index].Value;
  566. else
  567. throw new ArgumentOutOfRangeException("Index out of range");
  568. }
  569. //
  570. // Inner classes
  571. //
  572. private sealed class Enumerator : ICloneable, IDictionaryEnumerator, IEnumerator {
  573. private SortedList<TKey, TValue>host;
  574. private int stamp;
  575. private int pos;
  576. private int size;
  577. private EnumeratorMode mode;
  578. private object currentKey;
  579. private object currentValue;
  580. bool invalid = false;
  581. private readonly static string xstr = "SortedList.Enumerator: snapshot out of sync.";
  582. public Enumerator (SortedList<TKey, TValue>host, EnumeratorMode mode)
  583. {
  584. this.host = host;
  585. stamp = host.modificationCount;
  586. size = host.Count;
  587. this.mode = mode;
  588. Reset ();
  589. }
  590. public Enumerator (SortedList<TKey, TValue>host)
  591. : this (host, EnumeratorMode.ENTRY_MODE)
  592. {
  593. }
  594. public void Reset ()
  595. {
  596. if (host.modificationCount != stamp || invalid)
  597. throw new InvalidOperationException (xstr);
  598. pos = -1;
  599. currentKey = null;
  600. currentValue = null;
  601. }
  602. public bool MoveNext ()
  603. {
  604. if (host.modificationCount != stamp || invalid)
  605. throw new InvalidOperationException (xstr);
  606. KeyValuePair<TKey, TValue> [] table = host.table;
  607. if (++pos < size) {
  608. KeyValuePair<TKey, TValue> entry = table [pos];
  609. currentKey = entry.Key;
  610. currentValue = entry.Value;
  611. return true;
  612. }
  613. currentKey = null;
  614. currentValue = null;
  615. return false;
  616. }
  617. public DictionaryEntry Entry
  618. {
  619. get {
  620. if (invalid || pos >= size || pos == -1)
  621. throw new InvalidOperationException (xstr);
  622. return new DictionaryEntry (currentKey,
  623. currentValue);
  624. }
  625. }
  626. public Object Key {
  627. get {
  628. if (invalid || pos >= size || pos == -1)
  629. throw new InvalidOperationException (xstr);
  630. return currentKey;
  631. }
  632. }
  633. public Object Value {
  634. get {
  635. if (invalid || pos >= size || pos == -1)
  636. throw new InvalidOperationException (xstr);
  637. return currentValue;
  638. }
  639. }
  640. public Object Current {
  641. get {
  642. if (invalid || pos >= size || pos == -1)
  643. throw new InvalidOperationException (xstr);
  644. switch (mode) {
  645. case EnumeratorMode.KEY_MODE:
  646. return currentKey;
  647. case EnumeratorMode.VALUE_MODE:
  648. return currentValue;
  649. case EnumeratorMode.ENTRY_MODE:
  650. return this.Entry;
  651. default:
  652. throw new NotSupportedException (mode + " is not a supported mode.");
  653. }
  654. }
  655. }
  656. // ICloneable
  657. public object Clone ()
  658. {
  659. Enumerator e = new Enumerator (host, mode);
  660. e.stamp = stamp;
  661. e.pos = pos;
  662. e.size = size;
  663. e.currentKey = currentKey;
  664. e.currentValue = currentValue;
  665. e.invalid = invalid;
  666. return e;
  667. }
  668. }
  669. private class ListKeys : IList<TKey>, ICollection, IEnumerable {
  670. private SortedList<TKey, TValue> host;
  671. public ListKeys (SortedList<TKey, TValue> host)
  672. {
  673. if (host == null)
  674. throw new ArgumentNullException ();
  675. this.host = host;
  676. }
  677. // ICollection<TKey>
  678. public virtual void Add (TKey item) {
  679. throw new NotSupportedException();
  680. }
  681. public virtual bool Remove (TKey key) {
  682. throw new NotSupportedException ();
  683. }
  684. public virtual void Clear () {
  685. throw new NotSupportedException();
  686. }
  687. public virtual void CopyTo (TKey[] array, int arrayIndex) {
  688. if (array == null)
  689. throw new ArgumentNullException ("array");
  690. if (arrayIndex < 0)
  691. throw new ArgumentOutOfRangeException();
  692. if (arrayIndex >= array.Length)
  693. throw new ArgumentOutOfRangeException ("arrayIndex is greater than or equal to array.Length");
  694. if (Count > (array.Length - arrayIndex))
  695. throw new ArgumentOutOfRangeException("Not enough space in array from arrayIndex to end of array");
  696. int j = arrayIndex;
  697. for (int i = 0; i < Count; ++i)
  698. array [j ++] = host.KeyAt (i);
  699. }
  700. public virtual bool Contains (TKey item) {
  701. return host.IndexOfKey (item) > -1;
  702. }
  703. //
  704. // IList<TKey>
  705. //
  706. public virtual int IndexOf (TKey item) {
  707. return host.IndexOfKey (item);
  708. }
  709. public virtual void Insert (int index, TKey item) {
  710. throw new NotSupportedException ();
  711. }
  712. public virtual void RemoveAt (int index) {
  713. throw new NotSupportedException ();
  714. }
  715. public virtual TKey this [int index] {
  716. get {
  717. return host.KeyAt (index);
  718. }
  719. set {
  720. throw new NotSupportedException("attempt to modify a key");
  721. }
  722. }
  723. //
  724. // IEnumerable<TKey>
  725. //
  726. public virtual IEnumerator<TKey> GetEnumerator ()
  727. {
  728. for (int i = 0; i < host.Count; ++i)
  729. yield return host.KeyAt (i);
  730. }
  731. //
  732. // ICollection
  733. //
  734. public virtual int Count {
  735. get {
  736. return host.Count;
  737. }
  738. }
  739. public virtual bool IsSynchronized {
  740. get {
  741. return ((ICollection)host).IsSynchronized;
  742. }
  743. }
  744. public virtual bool IsReadOnly {
  745. get {
  746. return true;
  747. }
  748. }
  749. public virtual Object SyncRoot {
  750. get {
  751. return ((ICollection)host).SyncRoot;
  752. }
  753. }
  754. public virtual void CopyTo (Array array, int arrayIndex)
  755. {
  756. host.CopyToArray (array, arrayIndex, EnumeratorMode.KEY_MODE);
  757. }
  758. //
  759. // IEnumerable
  760. //
  761. IEnumerator IEnumerable.GetEnumerator ()
  762. {
  763. for (int i = 0; i < host.Count; ++i)
  764. yield return host.KeyAt (i);
  765. }
  766. }
  767. private class ListValues : IList<TValue>, ICollection, IEnumerable {
  768. private SortedList<TKey, TValue>host;
  769. public ListValues (SortedList<TKey, TValue>host)
  770. {
  771. if (host == null)
  772. throw new ArgumentNullException ();
  773. this.host = host;
  774. }
  775. // ICollection<TValue>
  776. public virtual void Add (TValue item) {
  777. throw new NotSupportedException();
  778. }
  779. public virtual bool Remove (TValue value) {
  780. throw new NotSupportedException ();
  781. }
  782. public virtual void Clear () {
  783. throw new NotSupportedException();
  784. }
  785. public virtual void CopyTo (TValue[] array, int arrayIndex) {
  786. if (array == null)
  787. throw new ArgumentNullException ("array");
  788. if (arrayIndex < 0)
  789. throw new ArgumentOutOfRangeException();
  790. if (arrayIndex >= array.Length)
  791. throw new ArgumentOutOfRangeException ("arrayIndex is greater than or equal to array.Length");
  792. if (Count > (array.Length - arrayIndex))
  793. throw new ArgumentOutOfRangeException("Not enough space in array from arrayIndex to end of array");
  794. int j = arrayIndex;
  795. for (int i = 0; i < Count; ++i)
  796. array [j ++] = host.ValueAt (i);
  797. }
  798. public virtual bool Contains (TValue item) {
  799. return host.IndexOfValue (item) > -1;
  800. }
  801. //
  802. // IList<TValue>
  803. //
  804. public virtual int IndexOf (TValue item) {
  805. return host.IndexOfValue (item);
  806. }
  807. public virtual void Insert (int index, TValue item) {
  808. throw new NotSupportedException ();
  809. }
  810. public virtual void RemoveAt (int index) {
  811. throw new NotSupportedException ();
  812. }
  813. public virtual TValue this [int index] {
  814. get {
  815. return host.ValueAt (index);
  816. }
  817. set {
  818. throw new NotSupportedException("attempt to modify a key");
  819. }
  820. }
  821. //
  822. // IEnumerable<TValue>
  823. //
  824. public virtual IEnumerator<TValue> GetEnumerator ()
  825. {
  826. for (int i = 0; i < host.Count; ++i)
  827. yield return host.ValueAt (i);
  828. }
  829. //
  830. // ICollection
  831. //
  832. public virtual int Count {
  833. get {
  834. return host.Count;
  835. }
  836. }
  837. public virtual bool IsSynchronized {
  838. get {
  839. return ((ICollection)host).IsSynchronized;
  840. }
  841. }
  842. public virtual bool IsReadOnly {
  843. get {
  844. return true;
  845. }
  846. }
  847. public virtual Object SyncRoot {
  848. get {
  849. return ((ICollection)host).SyncRoot;
  850. }
  851. }
  852. public virtual void CopyTo (Array array, int arrayIndex)
  853. {
  854. host.CopyToArray (array, arrayIndex, EnumeratorMode.VALUE_MODE);
  855. }
  856. //
  857. // IEnumerable
  858. //
  859. IEnumerator IEnumerable.GetEnumerator ()
  860. {
  861. for (int i = 0; i < host.Count; ++i)
  862. yield return host.ValueAt (i);
  863. }
  864. }
  865. } // SortedList
  866. } // System.Collections.Generic
  867. #endif