using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace System.Collections.ObjectModel
{
/// Provides the abstract base class for a collection whose keys are embedded in the values.
/// The type of keys in the collection.
/// The type of items in the collection.
// Token: 0x02000111 RID: 273
[ComVisible(false)]
[Serializable]
public abstract class KeyedCollection : Collection
{
/// Initializes a new instance of the class that uses the default equality comparer.
// Token: 0x06000D8B RID: 3467 RVA: 0x0003B5FC File Offset: 0x000397FC
protected KeyedCollection()
: this(null, 0)
{
}
/// Initializes a new instance of the class that uses the specified equality comparer.
/// The implementation of the generic interface to use when comparing keys, or null to use the default equality comparer for the type of the key, obtained from .
// Token: 0x06000D8C RID: 3468 RVA: 0x0003B608 File Offset: 0x00039808
protected KeyedCollection(IEqualityComparer comparer)
: this(comparer, 0)
{
}
/// Initializes a new instance of the class that uses the specified equality comparer and creates a lookup dictionary when the specified threshold is exceeded.
/// The implementation of the generic interface to use when comparing keys, or null to use the default equality comparer for the type of the key, obtained from .
/// The number of elements the collection can hold without creating a lookup dictionary (0 creates the lookup dictionary when the first item is added), or –1 to specify that a lookup dictionary is never created.
///
/// is less than –1.
// Token: 0x06000D8D RID: 3469 RVA: 0x0003B614 File Offset: 0x00039814
protected KeyedCollection(IEqualityComparer comparer, int dictionaryCreationThreshold)
{
if (comparer != null)
{
this.comparer = comparer;
}
else
{
this.comparer = EqualityComparer.Default;
}
this.dictionaryCreationThreshold = dictionaryCreationThreshold;
if (dictionaryCreationThreshold == 0)
{
this.dictionary = new Dictionary(this.comparer);
}
}
/// Determines whether the collection contains an element with the specified key.
/// true if the contains an element with the specified key; otherwise, false.
/// The key to locate in the .
///
/// is null.
// Token: 0x06000D8E RID: 3470 RVA: 0x0003B664 File Offset: 0x00039864
public bool Contains(TKey key)
{
if (this.dictionary != null)
{
return this.dictionary.ContainsKey(key);
}
return this.IndexOfKey(key) >= 0;
}
// Token: 0x06000D8F RID: 3471 RVA: 0x0003B68C File Offset: 0x0003988C
private int IndexOfKey(TKey key)
{
for (int i = this.Count - 1; i >= 0; i--)
{
TKey keyForItem = this.GetKeyForItem(this[i]);
if (this.comparer.Equals(key, keyForItem))
{
return i;
}
}
return -1;
}
/// Removes the element with the specified key from the .
/// true if the element is successfully removed; otherwise, false. This method also returns false if is not found in the .
/// The key of the element to remove.
///
/// is null.
// Token: 0x06000D90 RID: 3472 RVA: 0x0003B6D8 File Offset: 0x000398D8
public bool Remove(TKey key)
{
if (this.dictionary != null)
{
TItem titem;
return this.dictionary.TryGetValue(key, out titem) && base.Remove(titem);
}
int num = this.IndexOfKey(key);
if (num == -1)
{
return false;
}
this.RemoveAt(num);
return true;
}
/// Gets the generic equality comparer that is used to determine equality of keys in the collection.
/// The implementation of the generic interface that is used to determine equality of keys in the collection.
// Token: 0x17000208 RID: 520
// (get) Token: 0x06000D91 RID: 3473 RVA: 0x0003B728 File Offset: 0x00039928
public IEqualityComparer Comparer
{
get
{
return this.comparer;
}
}
/// Gets the element with the specified key.
/// The element with the specified key. If an element with the specified key is not found, an exception is thrown.
/// The key of the element to get.
///
/// is null.
/// An element with the specified key does not exist in the collection.
// Token: 0x17000209 RID: 521
public TItem this[TKey key]
{
get
{
if (this.dictionary != null)
{
return this.dictionary[key];
}
int num = this.IndexOfKey(key);
if (num >= 0)
{
return base[num];
}
throw new KeyNotFoundException();
}
}
/// Changes the key associated with the specified element in the lookup dictionary.
/// The element to change the key of.
/// The new key for .
///
/// is null.-or- is null.
///
/// is not found.-or- already exists in the .
// Token: 0x06000D93 RID: 3475 RVA: 0x0003B774 File Offset: 0x00039974
protected void ChangeItemKey(TItem item, TKey newKey)
{
if (!this.Contains(item))
{
throw new ArgumentException();
}
TKey keyForItem = this.GetKeyForItem(item);
if (this.comparer.Equals(keyForItem, newKey))
{
return;
}
if (this.Contains(newKey))
{
throw new ArgumentException();
}
if (this.dictionary != null)
{
if (!this.dictionary.Remove(keyForItem))
{
throw new ArgumentException();
}
this.dictionary.Add(newKey, item);
}
}
/// Removes all elements from the .
// Token: 0x06000D94 RID: 3476 RVA: 0x0003B7F0 File Offset: 0x000399F0
protected override void ClearItems()
{
if (this.dictionary != null)
{
this.dictionary.Clear();
}
base.ClearItems();
}
/// When implemented in a derived class, extracts the key from the specified element.
/// The key for the specified element.
/// The element from which to extract the key.
// Token: 0x06000D95 RID: 3477
protected abstract TKey GetKeyForItem(TItem item);
/// Inserts an element into the at the specified index.
/// The zero-based index at which should be inserted.
/// The object to insert.
///
/// is less than 0.-or- is greater than .
// Token: 0x06000D96 RID: 3478 RVA: 0x0003B810 File Offset: 0x00039A10
protected override void InsertItem(int index, TItem item)
{
TKey keyForItem = this.GetKeyForItem(item);
if (keyForItem == null)
{
throw new ArgumentNullException("GetKeyForItem(item)");
}
if (this.dictionary != null && this.dictionary.ContainsKey(keyForItem))
{
throw new ArgumentException("An element with the same key already exists in the dictionary.");
}
if (this.dictionary == null)
{
for (int i = 0; i < this.Count; i++)
{
if (this.comparer.Equals(keyForItem, this.GetKeyForItem(this[i])))
{
throw new ArgumentException("An element with the same key already exists in the dictionary.");
}
}
}
base.InsertItem(index, item);
if (this.dictionary != null)
{
this.dictionary.Add(keyForItem, item);
}
else if (this.dictionaryCreationThreshold != -1 && this.Count > this.dictionaryCreationThreshold)
{
this.dictionary = new Dictionary(this.comparer);
for (int j = 0; j < this.Count; j++)
{
TItem titem = this[j];
this.dictionary.Add(this.GetKeyForItem(titem), titem);
}
}
}
/// Removes the element at the specified index of the .
/// The index of the element to remove.
// Token: 0x06000D97 RID: 3479 RVA: 0x0003B934 File Offset: 0x00039B34
protected override void RemoveItem(int index)
{
if (this.dictionary != null)
{
TKey keyForItem = this.GetKeyForItem(this[index]);
this.dictionary.Remove(keyForItem);
}
base.RemoveItem(index);
}
/// Replaces the item at the specified index with the specified item.
/// The zero-based index of the item to be replaced.
/// The new item.
// Token: 0x06000D98 RID: 3480 RVA: 0x0003B970 File Offset: 0x00039B70
protected override void SetItem(int index, TItem item)
{
if (this.dictionary != null)
{
this.dictionary.Remove(this.GetKeyForItem(this[index]));
this.dictionary.Add(this.GetKeyForItem(item), item);
}
base.SetItem(index, item);
}
/// Gets the lookup dictionary of the .
/// The lookup dictionary of the , if it exists; otherwise, null.
// Token: 0x1700020A RID: 522
// (get) Token: 0x06000D99 RID: 3481 RVA: 0x0003B9BC File Offset: 0x00039BBC
protected IDictionary Dictionary
{
get
{
return this.dictionary;
}
}
// Token: 0x04000389 RID: 905
private Dictionary dictionary;
// Token: 0x0400038A RID: 906
private IEqualityComparer comparer;
// Token: 0x0400038B RID: 907
private int dictionaryCreationThreshold;
}
}