using System;
using System.Collections;
using System.Collections.Generic;
namespace BizHawk
{
///
/// a Dictionary-of-lists with key K and values List<V>
///
[Serializable]
public class Bag : BagBase>, List> { }
///
/// a Dictionary-of-lists with key K and values List<V>
///
[Serializable]
public class SortedBag : BagBase>, List> { }
///
/// A dictionary that creates new values on the fly as necessary so that any key you need will be defined.
///
/// dictionary keys
/// dictionary values
public class WorkingDictionary : Dictionary where V : new() {
public new V this[K key] {
get {
V temp;
if(!TryGetValue(key, out temp))
temp = this[key] = new V();
return temp;
}
set { base[key] = value; }
}
}
///
/// base class for Bag and SortedBag
///
/// dictionary keys
/// list values
/// dictionary type
/// list type
[Serializable]
public class BagBase : IEnumerable
where D : IDictionary, new()
where L : IList, IEnumerable, new() {
D dictionary = new D();
public void Add(K key, V val) {
this[key].Add(val);
}
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
public IEnumerator GetEnumerator() {
foreach(L lv in dictionary.Values)
foreach(V v in lv)
yield return v;
}
public IEnumerable KeyValuePairEnumerator { get { return dictionary; } }
///
/// the list of keys contained herein
///
public IList Keys { get { return new List(dictionary.Keys); } }
public L this[K key] {
get {
L slot;
if(!dictionary.TryGetValue(key, out slot))
dictionary[key] = slot = new L();
return slot;
}
set {
dictionary[key] = value;
}
}
}
}