Nested Types#region Nested Types
Enumerator K, V#region Enumerator <K, V>
[Serializable, StructLayout(LayoutKind.Sequential)]
private struct Enumerator<K, V> : IEnumerator<KeyValuePair<K, V>>, IDisposable,
IDictionaryEnumerator, IEnumerator
{
private ReversibleSortedList<K, V> _ReversibleSortedList;
private K key;
private V value;
private int index;
private int version;
internal Enumerator(ReversibleSortedList<K, V> ReversibleSortedList)
{
this._ReversibleSortedList = ReversibleSortedList;
this.index = 0;
this.version = this._ReversibleSortedList.version;
this.key = default(K);
this.value = default(V);
}
public void Dispose()
{
this.index = 0;
this.key = default(K);
this.value = default(V);
}
object IDictionaryEnumerator.Key
{
get
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation cannot occur.");
}
return this.key;
}
}
public bool MoveNext()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
{
this.key = this._ReversibleSortedList.keys[this.index];
this.value = this._ReversibleSortedList.values[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.key = default(K);
this.value = default(V);
return false;
}
DictionaryEntry IDictionaryEnumerator.Entry
{
get
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation cannot happen.");
}
return new DictionaryEntry(this.key, this.value);
}
}
public KeyValuePair<K, V> Current
{
get
{
return new KeyValuePair<K, V>(this.key, this.value);
}
}
object IEnumerator.Current
{
get
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation cannot occur");
}
return new DictionaryEntry(this.key, this.value);
}
}
object IDictionaryEnumerator.Value
{
get
{
if ((this.index == 0) ||
(this.index == (this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation cannot occur");
}
return this.value;
}
}
void IEnumerator.Reset()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration version check failed");
}
this.index = 0;
this.key = default(K);
this.value = default(V);
}
}
#endregion // Enumerator <K, V>
KeyListK,V#region KeyList<K,V>
[Serializable]
private sealed class KeyList<K, V> : IList<K>, ICollection<K>,
IEnumerable<K>, ICollection, IEnumerable
{
// Methods
internal KeyList(ReversibleSortedList<K, V> dictionary)
{
this._dict = dictionary;
}
public void Add(K key)
{
throw new NotSupportedException("Add is unsupported");
}
public void Clear()
{
throw new NotSupportedException("Clear is unsupported");
}
public bool Contains(K key)
{
return this._dict.ContainsKey(key);
}
public void CopyTo(K[] array, int arrayIndex)
{
Array.Copy(this._dict.keys, 0, array, arrayIndex, this._dict.Count);
}
public IEnumerator<K> GetEnumerator()
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
this._dict);
}
public int IndexOf(K key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<K>(this._dict.keys, 0,
this._dict.Count, key,
this._dict._sortDirectionComparer);
if (num1 >= 0)
{
return num1;
}
return -1;
}
public void Insert(int index, K value)
{
throw new NotSupportedException("Insert is unsupported");
}
public bool Remove(K key)
{
//throw new NotSupportedException("Remove is unsupported");
return false;
}
public void RemoveAt(int index)
{
throw new NotSupportedException("RemoveAt is unsupported");
}
void ICollection.CopyTo(Array array, int arrayIndex)
{
if ((array != null) && (array.Rank != 1))
{
throw new ArgumentException(
"MultiDimensional arrays are not unsupported");
}
try
{
Array.Copy(this._dict.keys, 0, array, arrayIndex,
this._dict.Count);
}
catch (ArrayTypeMismatchException atme)
{
throw new ArgumentException("InvalidArrayType", atme);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListKeyEnumerator(
this._dict);
}
// PropertIEs
public int Count
{
get
{
return this._dict._size;
}
}
public bool IsReadOnly
{
get
{
return true;
}
}
public K this[int index]
{
get
{
return this._dict.GetKey(index);
}
set
{
throw new NotSupportedException("Set is an unsupported Operation");
}
}
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
object ICollection.SyncRoot
{
get
{
return this._dict;
}
}
// FIElds
private ReversibleSortedList<K, V> _dict;
}
#endregion // KeyList<K,V>
ReversibleSortedListKeyEnumerator definition#region ReversibleSortedListKeyEnumerator definition
[Serializable]
private sealed class ReversibleSortedListKeyEnumerator : IEnumerator<TKey>,
IDisposable,
IEnumerator
{
// Methods
internal ReversibleSortedListKeyEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)
{
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
public void Dispose()
{
this.index = 0;
this.currentKey = default(TKey);
}
public bool MoveNext()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
{
this.currentKey = this._ReversibleSortedList.keys[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentKey = default(TKey);
return false;
}
void IEnumerator.Reset()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
this.index = 0;
this.currentKey = default(TKey);
}
// PropertIEs
public TKey Current
{
get
{
return this.currentKey;
}
}
object IEnumerator.Current
{
get
{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation could not occur");
}
return this.currentKey;
}
}
// FIElds
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TKey currentKey;
private int index;
private int version;
}
#endregion //ReversibleSortedListKeyEnumerator definition
ReversibleSortedListValueEnumerator definition#region ReversibleSortedListValueEnumerator definition
[Serializable]
private sealed class ReversibleSortedListValueEnumerator : IEnumerator<TValue>,
IDisposable,
IEnumerator
{
// Methods
internal ReversibleSortedListValueEnumerator(
ReversibleSortedList<TKey, TValue> ReversibleSortedList)
{
this._ReversibleSortedList = ReversibleSortedList;
this.version = ReversibleSortedList.version;
}
public void Dispose()
{
this.index = 0;
this.currentValue = default(TValue);
}
public bool MoveNext()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
if (this.index < this._ReversibleSortedList.Count)
{
this.currentValue = this._ReversibleSortedList.values[this.index];
this.index++;
return true;
}
this.index = this._ReversibleSortedList.Count + 1;
this.currentValue = default(TValue);
return false;
}
void IEnumerator.Reset()
{
if (this.version != this._ReversibleSortedList.version)
{
throw new InvalidOperationException(
"Enumeration failed version check");
}
this.index = 0;
this.currentValue = default(TValue);
}
// PropertIEs
public TValue Current
{
get
{
return this.currentValue;
}
}
object IEnumerator.Current
{
get
{
if ((this.index == 0) || (this.index ==
(this._ReversibleSortedList.Count + 1)))
{
throw new InvalidOperationException(
"Enumeration Operation could not occur");
}
return this.currentValue;
}
}
// FIElds
private ReversibleSortedList<TKey, TValue> _ReversibleSortedList;
private TValue currentValue;
private int index;
private int version;
}
#endregion //ReversibleSortedListValueEnumerator
ValueList K, V definition#region ValueList <K, V> definition
[Serializable]
private sealed class ValueList<K, V> : IList<V>, ICollection<V>,
IEnumerable<V>, ICollection, IEnumerable
{
// Methods
internal ValueList(ReversibleSortedList<K, V> dictionary)
{
this._dict = dictionary;
}
public void Add(V key)
{
throw new NotSupportedException("Add is not supported");
}
public void Clear()
{
throw new NotSupportedException("Clear is not supported");
}
public bool Contains(V value)
{
return this._dict.ContainsValue(value);
}
public void CopyTo(V[] array, int arrayIndex)
{
Array.Copy(this._dict.values, 0, array, arrayIndex, this._dict.Count);
}
public IEnumerator<V> GetEnumerator()
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
this._dict);
}
public int IndexOf(V value)
{
return Array.IndexOf<V>(this._dict.values, value, 0, this._dict.Count);
}
public void Insert(int index, V value)
{
throw new NotSupportedException("Insert is not supported");
}
public bool Remove(V value)
{
//throw new NotSupportedException("Remove is not supported");
return false;
}
public void RemoveAt(int index)
{
throw new NotSupportedException("RemoveAt is not supported");
}
void ICollection.CopyTo(Array array, int arrayIndex)
{
if ((array != null) && (array.Rank != 1))
{
throw new ArgumentException(
"MultiDimensional arrays not supported");
}
try
{
Array.Copy(this._dict.values, 0, array, arrayIndex,
this._dict.Count);
}
catch (ArrayTypeMismatchException atme)
{
throw new ArgumentException("Invalid array type", atme);
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return new
ReversibleSortedList<K, V>.ReversibleSortedListValueEnumerator(
this._dict);
}
// PropertIEs
public int Count
{
get
{
return this._dict._size;
}
}
public bool IsReadOnly
{
get
{
return true;
}
}
public V this[int index]
{
get
{
return this._dict.GetByIndex(index);
}
set
{
throw new NotSupportedException("Set by indexer is not supported");
}
}
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
object ICollection.SyncRoot
{
get
{
return this._dict;
}
}
// FIElds
private ReversibleSortedList<K, V> _dict;
}
#endregion // ValueList <TKey, TValue> definition
#endregion // Nested types
}
在SortedList混合使用了數組和列表語法,這使得以任一方式訪問數據變得非常容易。ReversibleSortedList<T>中數據也可以使用鍵/值對或索引來訪問。和SortedList一樣,它不允許重復鍵。另外不管值是引用類型還是值類型都可以為null,但鍵不行。ReversibleSortedList<T>的默認容量是16,這和SortedList是一樣的。裡面的項可以使用foreach循環進行迭代,它返回KeyValuePair,但這是只讀的,迭代語法禁止在讀取列表時進行元素的更新或刪除,否則就是無效的迭代器。