📄 multidictionarybase.cs
字号:
private MultiDictionaryBase<TKey, TValue> myDictionary;
private TKey key;
/// <summary>
/// Constructor. Initializes this collection.
/// </summary>
/// <param name="myDictionary">Dictionary we're using.</param>
/// <param name="key">The key we're looking at.</param>
public ValuesForKeyCollection(MultiDictionaryBase<TKey, TValue> myDictionary, TKey key)
{
this.myDictionary = myDictionary;
this.key = key;
}
/// <summary>
/// Remove the key and all values associated with it.
/// </summary>
public override void Clear()
{
myDictionary.Remove(key);
}
/// <summary>
/// Add a new values to this key.
/// </summary>
/// <param name="item">New values to add.</param>
public override void Add(TValue item)
{
myDictionary.Add(key, item);
}
/// <summary>
/// Remove a value currently associated with key.
/// </summary>
/// <param name="item">Value to remove.</param>
/// <returns>True if item was assocaited with key, false otherwise.</returns>
public override bool Remove(TValue item)
{
return myDictionary.Remove(key, item);
}
/// <summary>
/// Get the number of values associated with the key.
/// </summary>
public override int Count
{
get {
return myDictionary.CountValues(key);
}
}
/// <summary>
/// A simple function that returns an IEnumerator<TValue> that
/// doesn't yield any values. A helper.
/// </summary>
/// <returns>An IEnumerator<TValue> that yields no values.</returns>
private IEnumerator<TValue> NoValues()
{
yield break;
}
/// <summary>
/// Enumerate all the values associated with key.
/// </summary>
/// <returns>An IEnumerator<TValue> that enumerates all the values associated with key.</returns>
public override IEnumerator<TValue> GetEnumerator()
{
IEnumerator<TValue> values;
if (myDictionary.TryEnumerateValuesForKey(key, out values))
return values;
else
return NoValues();
}
/// <summary>
/// Determines if the given values is associated with key.
/// </summary>
/// <param name="item">Value to check for.</param>
/// <returns>True if value is associated with key, false otherwise.</returns>
public override bool Contains(TValue item)
{
return myDictionary.Contains(key, item);
}
}
/// <summary>
/// A private class that implements ICollection<TKey> and ICollection for the
/// Keys collection. The collection is read-only.
/// </summary>
[Serializable]
private sealed class KeysCollection : ReadOnlyCollectionBase<TKey>
{
private MultiDictionaryBase<TKey, TValue> myDictionary;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="myDictionary">The dictionary this is associated with.</param>
public KeysCollection(MultiDictionaryBase<TKey, TValue> myDictionary)
{
this.myDictionary = myDictionary;
}
public override int Count
{
get { return myDictionary.Count; }
}
public override IEnumerator<TKey> GetEnumerator()
{
return myDictionary.EnumerateKeys();
}
public override bool Contains(TKey key)
{
return myDictionary.ContainsKey(key);
}
}
/// <summary>
/// A private class that implements ICollection<TValue> and ICollection for the
/// Values collection. The collection is read-only.
/// </summary>
[Serializable]
private sealed class ValuesCollection : ReadOnlyCollectionBase<TValue>
{
private MultiDictionaryBase<TKey, TValue> myDictionary;
public ValuesCollection(MultiDictionaryBase<TKey, TValue> myDictionary)
{
this.myDictionary = myDictionary;
}
public override int Count
{
get { return myDictionary.CountAllValues(); }
}
public override IEnumerator<TValue> GetEnumerator()
{
using (IEnumerator<TKey> enumKeys = myDictionary.EnumerateKeys()) {
while (enumKeys.MoveNext()) {
TKey key = enumKeys.Current;
IEnumerator<TValue> enumValues;
if (myDictionary.TryEnumerateValuesForKey(key, out enumValues)) {
using (enumValues) {
while (enumValues.MoveNext())
yield return enumValues.Current;
}
}
}
}
}
public override bool Contains(TValue value)
{
foreach (TValue v in this) {
if (myDictionary.EqualValues(v, value))
return true;
}
return false;
}
}
/// <summary>
/// A private class that implements ICollection<ICollection<TValue>> and ICollection for the
/// Values collection on IDictionary. The collection is read-only.
/// </summary>
[Serializable]
private sealed class EnumerableValuesCollection : ReadOnlyCollectionBase<ICollection<TValue>>
{
private MultiDictionaryBase<TKey, TValue> myDictionary;
public EnumerableValuesCollection(MultiDictionaryBase<TKey, TValue> myDictionary)
{
this.myDictionary = myDictionary;
}
public override int Count
{
get { return myDictionary.Count; }
}
public override IEnumerator<ICollection<TValue>> GetEnumerator()
{
using (IEnumerator<TKey> enumKeys = myDictionary.EnumerateKeys()) {
while (enumKeys.MoveNext()) {
TKey key = enumKeys.Current;
yield return new ValuesForKeyCollection(myDictionary, key);
}
}
}
public override bool Contains(ICollection<TValue> values)
{
if (values == null)
return false;
TValue[] valueArray = Algorithms.ToArray(values);
foreach (ICollection<TValue> v in this) {
if (v.Count != valueArray.Length)
continue;
// First check in order for efficiency.
if (Algorithms.EqualCollections(v, values, myDictionary.EqualValues))
return true;
// Now check not in order. We can't use Algorithms.EqualSets, because we don't
// have an IEqualityComparer, just the ability to compare for equality. Unfortunately this is N squared,
// but there isn't a good choice here. We don't really expect this method to be used much.
bool[] found = new bool[valueArray.Length];
foreach (TValue x in v) {
for (int i = 0; i < valueArray.Length; ++i) {
if (!found[i] && myDictionary.EqualValues(x, valueArray[i]))
found[i] = true;
}
}
if (Array.IndexOf(found, false) < 0)
return true; // every item was found. The sets must be equal.
}
return false;
}
}
/// <summary>
/// A private class that implements ICollection<KeyValuePair<TKey,TValue>> and ICollection for the
/// KeyValuePairs collection. The collection is read-only.
/// </summary>
[Serializable]
private sealed class KeyValuePairsCollection : ReadOnlyCollectionBase<KeyValuePair<TKey,TValue>>
{
private MultiDictionaryBase<TKey, TValue> myDictionary;
public KeyValuePairsCollection(MultiDictionaryBase<TKey, TValue> myDictionary)
{
this.myDictionary = myDictionary;
}
public override int Count
{
get { return myDictionary.CountAllValues(); }
}
public override IEnumerator<KeyValuePair<TKey,TValue>> GetEnumerator()
{
using (IEnumerator<TKey> enumKeys = myDictionary.EnumerateKeys()) {
while (enumKeys.MoveNext()) {
TKey key = enumKeys.Current;
IEnumerator<TValue> enumValues;
if (myDictionary.TryEnumerateValuesForKey(key, out enumValues)) {
using (enumValues) {
while (enumValues.MoveNext())
yield return new KeyValuePair<TKey,TValue>(key, enumValues.Current);
}
}
}
}
}
public override bool Contains(KeyValuePair<TKey,TValue> pair)
{
return myDictionary[pair.Key].Contains(pair.Value);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -