Max Eisenhardt
Max Eisenhardt

Reputation: 462

Locating objects in BindingList

I have an Asynchronous BindingList that contains objects that are manipulated on a worker thread and bound to a BindingSource on the Main UI thread with a BindingSource bound to a DataGridView.

Is there anyway possible to locate objects in my BindingList without iterating through the list?

I've looked under the hood of LINQ and it's basically a sugar coated foreach loop. Also from my understand if I implement IBindingList.Find() it's nothing more than a for-loop...

I've "tried" to synchronize/map my BindingList to a Dictionary that mirrors my BindingList and using the Dictionary to located objects and pass the results(index) to my BindingList but this is not working because there is just too much adding and removing of the objects and I can't keep things organized.

This is a high-performance app that's dealing with real-time high frequency data from the stock market. That's why I can't iterate through the BindingList, it's just too inefficient.

Can someone please give me some advice and/or solutions.

Upvotes: 4

Views: 4415

Answers (1)

Steven P
Steven P

Reputation: 1996

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);
    }

Upvotes: 5

Related Questions