java泛型:C#泛型秘诀(5.2)
私有思路方法#region 私有思路方法
private void EnsureCapacity( min)
{
num1 = (this.keys.Length 0) ? 4 : (this.keys.Length * 2);
(num1 < min)
{
num1 = min;
}
this.InternalSetCapacity(num1, false);
}
//返回指定索引值
private TValue GetByIndex( index)
{
((index < 0) || (index >= this._size))
{
throw ArgumentOutOfRangeException("index", "Index out of range");
}
this.values[index];
}
//返回指定索引键
private TKey GetKey( index)
{
((index < 0) || (index >= this._size))
{
throw ArgumentOutOfRangeException("index", "Index out of range");
}
this.keys[index];
}
private KeyList<TKey, TValue> GetKeyListHelper
{
(this.keyList null)
{
this.keyList = KeyList<TKey, TValue>(this);
}
this.keyList;
}
private ValueList<TKey, TValue> GetValueListHelper
{
(this.valueList null)
{
this.valueList = ValueList<TKey, TValue>(this);
}
this.valueList;
}
//在指定位置插入元素
private void Insert( index, TKey key, TValue value)
{
(this._size this.keys.Length)
{
this.EnsureCapacity(this._size + 1);
}
(index < this._size)
{
Array.Copy(this.keys, index, this.keys, ()(index + 1),
()(this._size - index));
Array.Copy(this.values, index, this.values, ()(index + 1),
()(this._size - index));
}
this.keys[index] = key;
this.values[index] = value;
this._size;
this.version;
}
private void InternalSetCapacity( value, bool updateVersion)
{
(value != this.keys.Length)
{
(value < this._size)
{
throw ArgumentOutOfRangeException(
"value", "Too small capacity");
}
(value > 0)
{
TKey localArray1 = TKey[value];
TValue localArray2 = TValue[value];
(this._size > 0)
{
Array.Copy(this.keys, 0, localArray1, 0, this._size);
Array.Copy(this.values, 0, localArray2, 0, this._size);
}
this.keys = localArray1;
this.values = localArray2;
}
{
this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
}
(updateVersion)
{
this.version;
}
}
}
private bool IsCompatibleKey(object key)
{
(key.Equals(null))
{
throw ArgumentNullException("key");
}
(key is TKey);
}
//显式接口成员实现
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair)
{
this.Add(keyValuePair.Key, keyValuePair.Value);
}
//显式接口成员实现
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
KeyValuePair<TKey, TValue> keyValuePair)
{
num1 = this.IndexOfKey(keyValuePair.Key);
((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
keyValuePair.Value))
{
true;
}
false;
}
//显式接口成员实现
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
KeyValuePair<TKey,TValue> .gif' />, .gif' />Index)
{
(.gif' /> null)
{
throw ArgumentNullException(".gif' />");
}
((.gif' />Index < 0) || (.gif' />Index > .gif' />.Length))
{
throw ArgumentOutOfRangeException(
".gif' />Index", "Need a non-negative number");
}
((.gif' />.Length - .gif' />Index) < this.Count)
{
throw ArgumentException("ArrayPlusOffTooSmall");
}
for ( num1 = 0; num1 < this.Count; num1)
{
KeyValuePair<TKey, TValue> pair1;
pair1 = KeyValuePair<TKey, TValue>(
this.keys[num1], this.values[num1]);
.gif' />[.gif' />Index + num1] = pair1;
}
}
//显式接口成员实现
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
KeyValuePair<TKey, TValue> keyValuePair)
{
num1 = this.IndexOfKey(keyValuePair.Key);
((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
this.values[num1], keyValuePair.Value))
{
this.RemoveAt(num1);
true;
}
false;
}
IEnumerator<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator
{
ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
//显式接口成员实现
void ICollection.CopyTo(Array .gif' />, .gif' />Index)
{
(.gif' /> null)
{
throw ArgumentNullException(".gif' />");
}
(.gif' />.Rank != 1)
{
throw ArgumentException(
"MultiDimensional .gif' /> copies are not supported");
}
(.gif' />.GetLowerBound(0) != 0)
{
throw ArgumentException("A non-zero lower bound was provided");
}
((.gif' />Index < 0) || (.gif' />Index > .gif' />.Length))
{
throw ArgumentOutOfRangeException(
".gif' />Index", "Need non negative number");
}
((.gif' />.Length - .gif' />Index) < this.Count)
{
throw ArgumentException("Array plus the off is too small");
}
KeyValuePair<TKey, TValue> pairArray1 =
.gif' /> as KeyValuePair<TKey, TValue>;
(pairArray1 != null)
{
for ( num1 = 0; num1 < this.Count; num1)
{
pairArray1[num1 + .gif' />Index] =
KeyValuePair<TKey, TValue>(this.keys[num1],
this.values[num1]);
}
}
{
object objArray1 = .gif' /> as object;
(objArray1 null)
{
throw ArgumentException("Invalid .gif' /> type");
}
try
{
for ( num2 = 0; num2 < this.Count; num2)
{
objArray1[num2 + .gif' />Index] =
KeyValuePair<TKey, TValue>(this.keys[num2],
this.values[num2]);
}
}
catch (ArrayTypeMismatchException)
{
throw ArgumentException("Invalid .gif' /> type");
}
}
}
//显式接口成员实现
void IDictionary.Add(object key, object value)
{
ReversibleSortedList<TKey, TValue>.VeryKey(key);
ReversibleSortedList<TKey, TValue>.VeryValueType(value);
this.Add((TKey)key, (TValue)value);
}
//显式接口成员实现
bool IDictionary.Contains(object key)
{
(ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
{
this.ContainsKey((TKey)key);
}
false;
}
//显式接口成员实现
IDictionaryEnumerator IDictionary.GetEnumerator
{
ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
//显式接口成员实现
void IDictionary.Remove(object key)
{
(ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
{
this.Remove((TKey)key);
}
}
//显式接口成员实现
IEnumerator IEnumerable.GetEnumerator
{
ReversibleSortedList<TKey, TValue>.Enumerator<TKey, TValue>(
this);
}
private void VeryKey(object key)
{
(key.Equals(null))
{
throw ArgumentNullException("key");
}
(!(key is TKey))
{
throw ArgumentException(
"Argument passed is of wrong type", "key");
}
}
private void VeryValueType(object value)
{
(!(value is TValue) && ((value != null) || typeof(TValue).IsValueType))
{
throw ArgumentException(
"Argument passed is of wrong type", "value");
}
}
#endregion // Private methods
Public Properties#region Public Properties
public Capacity
{
get
{
this.keys.Length;
}
{
this.InternalSetCapacity(value, true);
}
}
public SortDirectionComparer<TKey> Comparer
{
get
{
this._sortDirectionComparer;
}
}
public Count
{
get
{
this._size;
}
}
public TValue this[TKey key]
{
get
{
TValue local1;
num1 = this.IndexOfKey(key);
(num1 >= 0)
{
this.values[num1];
}
{
//throw KeyNotFoundException;
local1 = default(TValue);
local1;
}
}
{
(key null)
{
throw ArgumentNullException("key");
}
num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
(num1 >= 0)
{
this.values[num1] = value;
this.version;
}
{
this.Insert(~num1, key, value);
}
}
}
public IList<TKey> Keys
{
get
{
this.GetKeyListHelper;
}
}
public IList<TValue> Values
{
get
{
this.GetValueListHelper;
}
}
#endregion // Public Properties
Private Properties#region Private Properties
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get
{
false;
}
}
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get
{
this.GetKeyListHelper;
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get
{
this.GetValueListHelper;
}
}
bool ICollection.IsSynchronized
{
get
{
false;
}
}
object ICollection.SyncRoot
{
get
{
this;
}
}
bool IDictionary.IsFixedSize
{
get
{
false;
}
}
bool IDictionary.IsReadOnly
{
get
{
false;
}
}
object IDictionary.this[object key]
{
get
{
(ReversibleSortedList<TKey, TValue>.IsCompatibleKey(key))
{
num1 = this.IndexOfKey((TKey)key);
(num1 >= 0)
{
this.values[num1];
}
}
null;
}
{
ReversibleSortedList<TKey, TValue>.VeryKey(key);
ReversibleSortedList<TKey, TValue>.VeryValueType(value);
this[(TKey)key] = (TValue)value;
}
}
ICollection IDictionary.Keys
{
get
{
this.GetKeyListHelper;
}
}
ICollection IDictionary.Values
{
get
{
this.GetValueListHelper;
}
}
#endregion // Private properties
Fields#region Fields
private const _defaultCapacity = 4;
private _size;
//private IComparer<TKey> comparer;
private TKey emptyKeys;
private TValue emptyValues;
private KeyList<TKey, TValue> keyList;
private TKey keys;
private ValueList<TKey, TValue> valueList;
private TValue values;
private version;
// Declare comparison object.
private SortDirectionComparer<TKey> _sortDirectionComparer = null;
// Default to ascending.
private ListSortDirection _currentSortDirection = ListSortDirection.Descending;
#endregion
延伸阅读
最新评论