C# hashcode for array of ints

后端 未结 7 591
不思量自难忘°
不思量自难忘° 2020-11-28 13:24

I have a class that internally is just an array of integers. Once constructed the array never changes. I\'d like to pre-compute a good hashcode so that this class can be ver

相关标签:
7条回答
  • 2020-11-28 14:19

    You could take a different approach and use a recursive dictionary for each value in your int array. This way you can leave .net to do primitive type hashing.

    internal class DictionaryEntry<TKey, TValue>
    {
        public Dictionary<TKey, DictionaryEntry<TKey, TValue>> Children { get; private set; }
        public TValue Value { get; private set; }
        public bool HasValue { get; private set; }
    
        public void SetValue(TValue value)
        {
            Value = value;
            HasValue = true;
        }
    
        public DictionaryEntry()
        {
            Children = new Dictionary<TKey, DictionaryEntry<TKey, TValue>>();
        }
    }
    
    internal class KeyStackDictionary<TKey, TValue>
    {
        // Helper dictionary to work with a stack of keys
        // Usage:
        // var dict = new KeyStackDictionary<int, string>();
        // int[] keyStack = new int[] {23, 43, 54};
        // dict.SetValue(keyStack, "foo");
        // string value;
        // if (dict.GetValue(keyStack, out value))
        // {   
        // }
    
        private DictionaryEntry<TKey, TValue> _dict;
    
        public KeyStackDictionary()
        {
            _dict = new DictionaryEntry<TKey, TValue>();
        }
    
        public void SetValue(TKey[] keyStack, TValue value)
        {
            DictionaryEntry<TKey, TValue> dict = _dict;
    
            for (int i = 0; i < keyStack.Length; i++)
            {
                TKey key = keyStack[i];
                if (dict.Children.ContainsKey(key))
                {
                    dict = dict.Children[key];
                }
                else
                {
                    var child = new DictionaryEntry<TKey, TValue>();
                    dict.Children.Add(key, child);
                    dict = child;
                }
    
                if (i == keyStack.Length - 1)
                {
                    dict.SetValue(value);
                }
            }
        }
    
        // returns false if the value is not found using the key stack
        public bool GetValue(TKey[] keyStack, out TValue value)
        {
            DictionaryEntry<TKey, TValue> dict = _dict;
    
            for (int i = 0; i < keyStack.Length; i++)
            {
                TKey key = keyStack[i];
    
                if (dict.Children.ContainsKey(key))
                {
                    dict = dict.Children[key];
                }
                else
                {
                    break;
                }
    
                if (i == keyStack.Length - 1 && dict.HasValue)
                {
                    value = dict.Value;
                    return true;
                }
            }
    
            value = default(TValue);
            return false;
        }
    }
    
    0 讨论(0)
提交回复
热议问题