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