Is there a better data structure than Dictionary if the values are objects and a property of those objects are the keys?

前端 未结 4 1797
暖寄归人
暖寄归人 2021-01-18 02:27

I have a Dictionary where the int is a property of obj. Is there a better data structure for this? I feel like u

4条回答
  •  一向
    一向 (楼主)
    2021-01-18 02:50

    You can implement your own KeyedCollection trivially if the extra overhead that comes with the factory settings isn't worth it. The original KeyedCollection in System.Collections.ObjectModel is internally a Dictionary and a List which means you can have operations defined on both IList<> and IDictionary<>. For e.g., you can insert, access by index, traverse collection in the inserted order (all which IList<> facilitates) and at the same time you can have quick lookups based on key (with the help of dictionary). This means that when you're adding or removing an item they have to be performed on both underlying collections, apart from the small memory overhead to hold the extra List<> (but the objects are not duplicated as such). Though the addition speeds are not affected much (List<> addition is O(1)), removal speed is affected a little.

    If you don't care about insertion order and accessing by index:

    public class KeyedCollection : ICollection
    {
        MemberInfo _keyInfo;
        Func _keySelector;
        Dictionary _dict;
    
        public TItem this[TKey key]
        {
            get { return _dict[key]; }
        }
    
        public int Count
        {
            get { return _dict.Count; }
        }
    
        public bool IsReadOnly
        {
            get { return false; }
        }
    
        public ICollection Keys
        {
            get { return _dict.Keys; }
        }
    
        private ICollection Items
        {
            get { return _dict.Values; }
        }
    
        public KeyedCollection(Expression> keySelector, IEqualityComparer comparer = null)
        {
            var keyExpression = keySelector.Body as MemberExpression;
            if (keyExpression != null)
                _keyInfo = keyExpression.Member;
    
            _keySelector = keySelector.Compile();
            _dict = new Dictionary(comparer);
        }
    
    
    
        private TKey GetKeyForItem(TItem item)
        {
            return _keySelector(item);
        }
    
        public bool ContainsKey(TKey key)
        {
            return _dict.ContainsKey(key);
        }
    
        public bool Contains(TItem item)
        {
            return ContainsKey(GetKeyForItem(item));
        }
    
        public bool TryGetItem(TKey key, out TItem item)
        {
            return _dict.TryGetValue(key, out item);
        }
    
        public void Add(TItem item)
        {
            _dict.Add(GetKeyForItem(item), item);
        }
    
        public void AddOrUpdate(TItem item)
        {
            _dict[GetKeyForItem(item)] = item;
        }
    
        public bool UpdateKey(TKey oldKey, TKey newKey)
        {
            TItem oldItem;
            if (_keyInfo == null || !TryGetItem(oldKey, out oldItem) || !SetItem(oldItem, newKey))   // important
                return false;
    
            RemoveKey(oldKey);
            Add(oldItem);
            return true;
        }
    
        private bool SetItem(TItem item, TKey key)
        {
            var propertyInfo = _keyInfo as PropertyInfo;
            if (propertyInfo != null)
            {
                if (!propertyInfo.CanWrite)
                    return false;
    
                propertyInfo.SetValue(item, key, null);
                return true;
            }
    
            var fieldInfo = _keyInfo as FieldInfo;
            if (fieldInfo != null)
            {
                if (fieldInfo.IsInitOnly)
                    return false;
    
                fieldInfo.SetValue(item, key);
                return true;
            }
    
            return false;
        }
    
        public bool RemoveKey(TKey key)
        {
            return _dict.Remove(key);
        }
    
        public bool Remove(TItem item)
        {
            return RemoveKey(GetKeyForItem(item));
        }
    
        public void Clear()
        {
            _dict.Clear();
        }
    
        public void CopyTo(TItem[] array, int arrayIndex)
        {
            Items.CopyTo(array, arrayIndex);
        }
    
        public IEnumerator GetEnumerator()
        {
            return Items.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    I have implemented ICollection to make it more standard compliant - and also you get the nice collection initializer syntax! :)

    A sample usage:

    var p1 = new Person { Name = "a" };
    var p2 = new Person { Name = "b" };
    
    var people = new KeyedCollection(p => p.Name) { p1, p2 };
    // p1 == people["a"];
    // p2 == people["b"];
    

提交回复
热议问题