So some sort of fast lookup binding list... Here is one I prepared earlier.
This is the 'synchronize/map' approach you referred to. I've used this before for fast ticking data, where main bottleneck was looking up items in the list. I believe I've covered all the methods required to keep in sync or 'organised'. You might to add a test for AddRange - I don't have a decompiler to hand, I'm not sure if it just calls InsertItem.
Obviously you have a direct trade-off here of greater memory use and insert time, maintaining two lists, but for fast ticking data, that's normally a very acceptable trade-off for improved lookup times.
Use the class like you would a BindingList, but when you need look up an item quickly, use the FastFind method.
public class FastLookupBindingList<TKey, TVal> : BindingList<TVal>
{
private readonly IDictionary<TKey, TVal> _dict = new Dictionary<TKey, TVal>();
private readonly Func<TVal, TKey> _keyFunc;
public FastLookupBindingList(Func<TVal, TKey> keyFunc)
{
_keyFunc = keyFunc;
}
public FastLookupBindingList(Func<TVal, TKey> keyFunc, IList<TVal> sourceList) : base(sourceList)
{
_keyFunc = keyFunc;
foreach (var item in sourceList)
{
var key = _keyFunc(item);
_dict.Add(key, item);
}
}
public TVal FastFind(TKey key)
{
TVal val;
_dict.TryGetValue(key, out val);
return val;
}
protected override void InsertItem(int index, TVal val)
{
_dict.Add(_keyFunc(val), val);
base.InsertItem(index, val);
}
protected override void SetItem(int index, TVal val)
{
var key = _keyFunc(val);
_dict[key] = val;
base.SetItem(index, val);
}
protected override void RemoveItem(int index)
{
var item = this[index];
var key = _keyFunc(item);
_dict.Remove(key);
base.RemoveItem(index);
}
protected override void ClearItems()
{
_dict.Clear();
base.ClearItems();
}
}
Usage:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
private void simpleButton1_Click(object sender, EventArgs e)
{
var keyedBindingList = new FastLookupBindingList<int, Person>(p => p.Id)
{
new Person {Id = 1, Name = "Joe"},
new Person {Id = 2, Name = "Josephine"}
};
var person = keyedBindingList.FastFind(2);
var unkonwn = keyedBindingList.FastFind(4);
}