SortedList.cs 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. public virtual void Add (TKey key, TValue value)
  229. {
  230. if (key == null)
  231. throw new ArgumentNullException ("key");
  232. PutImpl (key, value, false);
  233. }
  234. public bool ContainsKey (TKey key)
  235. {
  236. if (key == null)
  237. throw new ArgumentNullException ("key");
  238. return (Find (key) >= 0);
  239. }
  240. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator ()
  241. {
  242. for (int i = 0; i < inUse; i ++) {
  243. KeyValuePair<TKey, TValue> current = this.table [i];
  244. yield return new KeyValuePair<TKey, TValue> (current.Key, current.Value);
  245. }
  246. }
  247. public virtual bool Remove (TKey key)
  248. {
  249. if (key == null)
  250. throw new ArgumentNullException ("key");
  251. int i = IndexOfKey (key);
  252. if (i >= 0) {
  253. RemoveAt (i);
  254. return true;
  255. }
  256. else
  257. return false;
  258. }
  259. // ICollection<KeyValuePair<TKey, TValue>>
  260. public virtual void Clear ()
  261. {
  262. defaultCapacity = INITIAL_SIZE;
  263. this.table = new KeyValuePair<TKey, TValue> [defaultCapacity];
  264. inUse = 0;
  265. modificationCount++;
  266. }
  267. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo (KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  268. {
  269. if (null == array)
  270. throw new ArgumentNullException();
  271. if (arrayIndex < 0)
  272. throw new ArgumentOutOfRangeException();
  273. if (arrayIndex >= array.Length)
  274. throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
  275. if (Count > (array.Length - arrayIndex))
  276. throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");
  277. int i = arrayIndex;
  278. foreach (KeyValuePair<TKey, TValue> pair in this)
  279. array [i++] = pair;
  280. }
  281. void ICollection<KeyValuePair<TKey, TValue>>.Add (KeyValuePair<TKey, TValue> keyValuePair) {
  282. Add (keyValuePair.Key, keyValuePair.Value);
  283. }
  284. bool ICollection<KeyValuePair<TKey, TValue>>.Contains (KeyValuePair<TKey, TValue> keyValuePair) {
  285. int i = Find (keyValuePair.Key);
  286. if (i >= 0)
  287. return Comparer<KeyValuePair<TKey, TValue>>.Default.Compare (table [i], keyValuePair) == 0;
  288. else
  289. return false;
  290. }
  291. bool ICollection<KeyValuePair<TKey, TValue>>.Remove (KeyValuePair<TKey, TValue> keyValuePair) {
  292. int i = Find (keyValuePair.Key);
  293. if (i >= 0 && (Comparer<KeyValuePair<TKey, TValue>>.Default.Compare (table [i], keyValuePair) == 0)) {
  294. RemoveAt (i);
  295. return true;
  296. }
  297. else
  298. return false;
  299. }
  300. // IEnumerable<KeyValuePair<TKey, TValue>>
  301. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
  302. {
  303. for (int i = 0; i < inUse; i ++) {
  304. KeyValuePair<TKey, TValue> current = this.table [i];
  305. yield return new KeyValuePair<TKey, TValue> (current.Key, current.Value);
  306. }
  307. }
  308. // IEnumerable
  309. IEnumerator IEnumerable.GetEnumerator ()
  310. {
  311. return GetEnumerator ();
  312. }
  313. // IDictionary
  314. void IDictionary.Add (object key, object value)
  315. {
  316. PutImpl (ToKey (key), ToValue (value), false);
  317. }
  318. bool IDictionary.Contains (object key)
  319. {
  320. if (null == key)
  321. throw new ArgumentNullException();
  322. if (!(key is TKey))
  323. return false;
  324. return (Find ((TKey)key) >= 0);
  325. }
  326. IDictionaryEnumerator IDictionary.GetEnumerator ()
  327. {
  328. return new Enumerator (this, EnumeratorMode.ENTRY_MODE);
  329. }
  330. void IDictionary.Remove (object key)
  331. {
  332. if (null == key)
  333. throw new ArgumentNullException ("key");
  334. if (!(key is TKey))
  335. return;
  336. int i = IndexOfKey ((TKey)key);
  337. if (i >= 0) RemoveAt (i);
  338. }
  339. // ICollection
  340. void ICollection.CopyTo (Array array, int arrayIndex)
  341. {
  342. if (null == array)
  343. throw new ArgumentNullException();
  344. if (arrayIndex < 0)
  345. throw new ArgumentOutOfRangeException();
  346. if (array.Rank > 1)
  347. throw new ArgumentException("array is multi-dimensional");
  348. if (arrayIndex >= array.Length)
  349. throw new ArgumentNullException("arrayIndex is greater than or equal to array.Length");
  350. if (Count > (array.Length - arrayIndex))
  351. throw new ArgumentNullException("Not enough space in array from arrayIndex to end of array");
  352. IEnumerator<KeyValuePair<TKey,TValue>> it = GetEnumerator ();
  353. int i = arrayIndex;
  354. while (it.MoveNext ()) {
  355. array.SetValue (it.Current, i++);
  356. }
  357. }
  358. //
  359. // SortedList<TKey, TValue>
  360. //
  361. public void RemoveAt (int index)
  362. {
  363. KeyValuePair<TKey, TValue> [] table = this.table;
  364. int cnt = Count;
  365. if (index >= 0 && index < cnt) {
  366. if (index != cnt - 1) {
  367. Array.Copy (table, index+1, table, index, cnt-1-index);
  368. } else {
  369. table [index] = default (KeyValuePair <TKey, TValue>);
  370. }
  371. --inUse;
  372. ++modificationCount;
  373. } else {
  374. throw new ArgumentOutOfRangeException("index out of range");
  375. }
  376. }
  377. public int IndexOfKey (TKey key)
  378. {
  379. if (key == null)
  380. throw new ArgumentNullException ("key");
  381. int indx = 0;
  382. try {
  383. indx = Find (key);
  384. } catch (Exception) {
  385. throw new InvalidOperationException();
  386. }
  387. return (indx | (indx >> 31));
  388. }
  389. public int IndexOfValue (TValue value)
  390. {
  391. if (inUse == 0)
  392. return -1;
  393. for (int i = 0; i < inUse; i ++) {
  394. KeyValuePair<TKey, TValue> current = this.table [i];
  395. if (Equals (value, current.Value))
  396. return i;
  397. }
  398. return -1;
  399. }
  400. public bool ContainsValue (TValue value)
  401. {
  402. return IndexOfValue (value) >= 0;
  403. }
  404. public void TrimExcess ()
  405. {
  406. if (inUse < table.Length * 0.9)
  407. Capacity = inUse;
  408. }
  409. public bool TryGetValue (TKey key, out TValue value)
  410. {
  411. if (key == null)
  412. throw new ArgumentNullException("key");
  413. int i = Find (key);
  414. if (i >= 0) {
  415. value = table [i].Value;
  416. return true;
  417. }
  418. else {
  419. value = default (TValue);
  420. return false;
  421. }
  422. }
  423. //
  424. // Private methods
  425. //
  426. private void EnsureCapacity (int n, int free)
  427. {
  428. KeyValuePair<TKey, TValue> [] table = this.table;
  429. KeyValuePair<TKey, TValue> [] newTable = null;
  430. int cap = Capacity;
  431. bool gap = (free >=0 && free < Count);
  432. if (n > cap) {
  433. newTable = new KeyValuePair<TKey, TValue> [n << 1];
  434. }
  435. if (newTable != null) {
  436. if (gap) {
  437. int copyLen = free;
  438. if (copyLen > 0) {
  439. Array.Copy (table, 0, newTable, 0, copyLen);
  440. }
  441. copyLen = Count - free;
  442. if (copyLen > 0) {
  443. Array.Copy (table, free, newTable, free+1, copyLen);
  444. }
  445. } else {
  446. // Just a resizing, copy the entire table.
  447. Array.Copy (table, newTable, Count);
  448. }
  449. this.table = newTable;
  450. } else if (gap) {
  451. Array.Copy (table, free, table, free+1, Count - free);
  452. }
  453. }
  454. private void PutImpl (TKey key, TValue value, bool overwrite)
  455. {
  456. if (key == null)
  457. throw new ArgumentNullException ("null key");
  458. KeyValuePair<TKey, TValue> [] table = this.table;
  459. int freeIndx = -1;
  460. try {
  461. freeIndx = Find (key);
  462. } catch (Exception) {
  463. throw new InvalidOperationException();
  464. }
  465. if (freeIndx >= 0) {
  466. if (!overwrite)
  467. throw new ArgumentException("element already exists");
  468. table [freeIndx] = new KeyValuePair <TKey, TValue> (key, value);
  469. ++modificationCount;
  470. return;
  471. }
  472. freeIndx = ~freeIndx;
  473. if (freeIndx > Capacity + 1)
  474. throw new Exception ("SortedList::internal error ("+key+", "+value+") at ["+freeIndx+"]");
  475. EnsureCapacity (Count+1, freeIndx);
  476. table = this.table;
  477. table [freeIndx] = new KeyValuePair <TKey, TValue> (key, value);
  478. ++inUse;
  479. ++modificationCount;
  480. }
  481. private void Init (IComparer<TKey> comparer, int capacity, bool forceSize)
  482. {
  483. if (comparer == null)
  484. comparer = Comparer<TKey>.Default;
  485. this.comparer = comparer;
  486. if (!forceSize && (capacity < defaultCapacity))
  487. capacity = defaultCapacity;
  488. this.table = new KeyValuePair<TKey, TValue> [capacity];
  489. this.inUse = 0;
  490. this.modificationCount = 0;
  491. }
  492. private void CopyToArray (Array arr, int i,
  493. EnumeratorMode mode)
  494. {
  495. if (arr == null)
  496. throw new ArgumentNullException ("arr");
  497. if (i < 0 || i + this.Count > arr.Length)
  498. throw new ArgumentOutOfRangeException ("i");
  499. IEnumerator it = new Enumerator (this, mode);
  500. while (it.MoveNext ()) {
  501. arr.SetValue (it.Current, i++);
  502. }
  503. }
  504. private int Find (TKey key)
  505. {
  506. KeyValuePair<TKey, TValue> [] table = this.table;
  507. int len = Count;
  508. if (len == 0) return ~0;
  509. int left = 0;
  510. int right = len-1;
  511. while (left <= right) {
  512. int guess = (left + right) >> 1;
  513. int cmp = comparer.Compare (key, table[guess].Key);
  514. if (cmp == 0) return guess;
  515. if (cmp > 0) left = guess+1;
  516. else right = guess-1;
  517. }
  518. return ~left;
  519. }
  520. private TKey ToKey (object key) {
  521. if (key == null)
  522. throw new ArgumentNullException ("key");
  523. if (!(key is TKey))
  524. throw new ArgumentException ("The value \"" + key + "\" isn't of type \"" + typeof (TKey) + "\" and can't be used in this generic collection.", "key");
  525. return (TKey)key;
  526. }
  527. private TValue ToValue (object value) {
  528. if (!(value is TValue))
  529. throw new ArgumentException ("The value \"" + value + "\" isn't of type \"" + typeof (TValue) + "\" and can't be used in this generic collection.", "value");
  530. return (TValue)value;
  531. }
  532. internal TKey KeyAt (int index) {
  533. if (index >= 0 && index < Count)
  534. return table [index].Key;
  535. else
  536. throw new ArgumentOutOfRangeException("Index out of range");
  537. }
  538. internal TValue ValueAt (int index) {
  539. if (index >= 0 && index < Count)
  540. return table [index].Value;
  541. else
  542. throw new ArgumentOutOfRangeException("Index out of range");
  543. }
  544. //
  545. // Inner classes
  546. //
  547. private sealed class Enumerator : ICloneable, IDictionaryEnumerator, IEnumerator {
  548. private SortedList<TKey, TValue>host;
  549. private int stamp;
  550. private int pos;
  551. private int size;
  552. private EnumeratorMode mode;
  553. private object currentKey;
  554. private object currentValue;
  555. bool invalid = false;
  556. private readonly static string xstr = "SortedList.Enumerator: snapshot out of sync.";
  557. public Enumerator (SortedList<TKey, TValue>host, EnumeratorMode mode)
  558. {
  559. this.host = host;
  560. stamp = host.modificationCount;
  561. size = host.Count;
  562. this.mode = mode;
  563. Reset ();
  564. }
  565. public Enumerator (SortedList<TKey, TValue>host)
  566. : this (host, EnumeratorMode.ENTRY_MODE)
  567. {
  568. }
  569. public void Reset ()
  570. {
  571. if (host.modificationCount != stamp || invalid)
  572. throw new InvalidOperationException (xstr);
  573. pos = -1;
  574. currentKey = null;
  575. currentValue = null;
  576. }
  577. public bool MoveNext ()
  578. {
  579. if (host.modificationCount != stamp || invalid)
  580. throw new InvalidOperationException (xstr);
  581. KeyValuePair<TKey, TValue> [] table = host.table;
  582. if (++pos < size) {
  583. KeyValuePair<TKey, TValue> entry = table [pos];
  584. currentKey = entry.Key;
  585. currentValue = entry.Value;
  586. return true;
  587. }
  588. currentKey = null;
  589. currentValue = null;
  590. return false;
  591. }
  592. public DictionaryEntry Entry
  593. {
  594. get {
  595. if (invalid || pos >= size || pos == -1)
  596. throw new InvalidOperationException (xstr);
  597. return new DictionaryEntry (currentKey,
  598. currentValue);
  599. }
  600. }
  601. public Object Key {
  602. get {
  603. if (invalid || pos >= size || pos == -1)
  604. throw new InvalidOperationException (xstr);
  605. return currentKey;
  606. }
  607. }
  608. public Object Value {
  609. get {
  610. if (invalid || pos >= size || pos == -1)
  611. throw new InvalidOperationException (xstr);
  612. return currentValue;
  613. }
  614. }
  615. public Object Current {
  616. get {
  617. if (invalid || pos >= size || pos == -1)
  618. throw new InvalidOperationException (xstr);
  619. switch (mode) {
  620. case EnumeratorMode.KEY_MODE:
  621. return currentKey;
  622. case EnumeratorMode.VALUE_MODE:
  623. return currentValue;
  624. case EnumeratorMode.ENTRY_MODE:
  625. return this.Entry;
  626. default:
  627. throw new NotSupportedException (mode + " is not a supported mode.");
  628. }
  629. }
  630. }
  631. // ICloneable
  632. public object Clone ()
  633. {
  634. Enumerator e = new Enumerator (host, mode);
  635. e.stamp = stamp;
  636. e.pos = pos;
  637. e.size = size;
  638. e.currentKey = currentKey;
  639. e.currentValue = currentValue;
  640. e.invalid = invalid;
  641. return e;
  642. }
  643. }
  644. private class ListKeys : IList<TKey>, ICollection, IEnumerable {
  645. private SortedList<TKey, TValue> host;
  646. public ListKeys (SortedList<TKey, TValue> host)
  647. {
  648. if (host == null)
  649. throw new ArgumentNullException ();
  650. this.host = host;
  651. }
  652. // ICollection<TKey>
  653. public virtual void Add (TKey item) {
  654. throw new NotSupportedException();
  655. }
  656. public virtual bool Remove (TKey key) {
  657. throw new NotSupportedException ();
  658. }
  659. public virtual void Clear () {
  660. throw new NotSupportedException();
  661. }
  662. public virtual void CopyTo (TKey[] array, int arrayIndex) {
  663. if (array == null)
  664. throw new ArgumentNullException ("array");
  665. if (arrayIndex < 0)
  666. throw new ArgumentOutOfRangeException();
  667. if (arrayIndex >= array.Length)
  668. throw new ArgumentOutOfRangeException ("arrayIndex is greater than or equal to array.Length");
  669. if (Count > (array.Length - arrayIndex))
  670. throw new ArgumentOutOfRangeException("Not enough space in array from arrayIndex to end of array");
  671. int j = arrayIndex;
  672. for (int i = 0; i < Count; ++i)
  673. array [j ++] = host.KeyAt (i);
  674. }
  675. public virtual bool Contains (TKey item) {
  676. return host.IndexOfKey (item) > -1;
  677. }
  678. //
  679. // IList<TKey>
  680. //
  681. public virtual int IndexOf (TKey item) {
  682. return host.IndexOfKey (item);
  683. }
  684. public virtual void Insert (int index, TKey item) {
  685. throw new NotSupportedException ();
  686. }
  687. public virtual void RemoveAt (int index) {
  688. throw new NotSupportedException ();
  689. }
  690. public virtual TKey this [int index] {
  691. get {
  692. return host.KeyAt (index);
  693. }
  694. set {
  695. throw new NotSupportedException("attempt to modify a key");
  696. }
  697. }
  698. //
  699. // IEnumerable<TKey>
  700. //
  701. public virtual IEnumerator<TKey> GetEnumerator ()
  702. {
  703. for (int i = 0; i < host.Count; ++i)
  704. yield return host.KeyAt (i);
  705. }
  706. //
  707. // ICollection
  708. //
  709. public virtual int Count {
  710. get {
  711. return host.Count;
  712. }
  713. }
  714. public virtual bool IsSynchronized {
  715. get {
  716. return ((ICollection)host).IsSynchronized;
  717. }
  718. }
  719. public virtual bool IsReadOnly {
  720. get {
  721. return true;
  722. }
  723. }
  724. public virtual Object SyncRoot {
  725. get {
  726. return ((ICollection)host).SyncRoot;
  727. }
  728. }
  729. public virtual void CopyTo (Array array, int arrayIndex)
  730. {
  731. host.CopyToArray (array, arrayIndex, EnumeratorMode.KEY_MODE);
  732. }
  733. //
  734. // IEnumerable
  735. //
  736. IEnumerator IEnumerable.GetEnumerator ()
  737. {
  738. for (int i = 0; i < host.Count; ++i)
  739. yield return host.KeyAt (i);
  740. }
  741. }
  742. private class ListValues : IList<TValue>, ICollection, IEnumerable {
  743. private SortedList<TKey, TValue>host;
  744. public ListValues (SortedList<TKey, TValue>host)
  745. {
  746. if (host == null)
  747. throw new ArgumentNullException ();
  748. this.host = host;
  749. }
  750. // ICollection<TValue>
  751. public virtual void Add (TValue item) {
  752. throw new NotSupportedException();
  753. }
  754. public virtual bool Remove (TValue value) {
  755. throw new NotSupportedException ();
  756. }
  757. public virtual void Clear () {
  758. throw new NotSupportedException();
  759. }
  760. public virtual void CopyTo (TValue[] array, int arrayIndex) {
  761. if (array == null)
  762. throw new ArgumentNullException ("array");
  763. if (arrayIndex < 0)
  764. throw new ArgumentOutOfRangeException();
  765. if (arrayIndex >= array.Length)
  766. throw new ArgumentOutOfRangeException ("arrayIndex is greater than or equal to array.Length");
  767. if (Count > (array.Length - arrayIndex))
  768. throw new ArgumentOutOfRangeException("Not enough space in array from arrayIndex to end of array");
  769. int j = arrayIndex;
  770. for (int i = 0; i < Count; ++i)
  771. array [j ++] = host.ValueAt (i);
  772. }
  773. public virtual bool Contains (TValue item) {
  774. return host.IndexOfValue (item) > -1;
  775. }
  776. //
  777. // IList<TValue>
  778. //
  779. public virtual int IndexOf (TValue item) {
  780. return host.IndexOfValue (item);
  781. }
  782. public virtual void Insert (int index, TValue item) {
  783. throw new NotSupportedException ();
  784. }
  785. public virtual void RemoveAt (int index) {
  786. throw new NotSupportedException ();
  787. }
  788. public virtual TValue this [int index] {
  789. get {
  790. return host.ValueAt (index);
  791. }
  792. set {
  793. throw new NotSupportedException("attempt to modify a key");
  794. }
  795. }
  796. //
  797. // IEnumerable<TValue>
  798. //
  799. public virtual IEnumerator<TValue> GetEnumerator ()
  800. {
  801. for (int i = 0; i < host.Count; ++i)
  802. yield return host.ValueAt (i);
  803. }
  804. //
  805. // ICollection
  806. //
  807. public virtual int Count {
  808. get {
  809. return host.Count;
  810. }
  811. }
  812. public virtual bool IsSynchronized {
  813. get {
  814. return ((ICollection)host).IsSynchronized;
  815. }
  816. }
  817. public virtual bool IsReadOnly {
  818. get {
  819. return true;
  820. }
  821. }
  822. public virtual Object SyncRoot {
  823. get {
  824. return ((ICollection)host).SyncRoot;
  825. }
  826. }
  827. public virtual void CopyTo (Array array, int arrayIndex)
  828. {
  829. host.CopyToArray (array, arrayIndex, EnumeratorMode.VALUE_MODE);
  830. }
  831. //
  832. // IEnumerable
  833. //
  834. IEnumerator IEnumerable.GetEnumerator ()
  835. {
  836. for (int i = 0; i < host.Count; ++i)
  837. yield return host.ValueAt (i);
  838. }
  839. }
  840. } // SortedList
  841. } // System.Collections.Generic
  842. #endif