Example #1
0
        bool TryAddInternal(byte[] key, TValue value)
        {
            var h     = unchecked ((int)FarmHash.Hash64(key, 0, key.Length));
            var entry = new Entry {
                Key = key, Value = value
            };

            var array = buckets[h & (indexFor)];

            if (array == null)
            {
                buckets[h & (indexFor)] = new[] { entry };
            }
            else
            {
                // check duplicate
                for (int i = 0; i < array.Length; i++)
                {
                    var e = array[i].Key;
                    if (new ReadOnlySpan <byte>(key).SequenceEqual(e))
                    {
                        return(false);
                    }
                }

                var newArray = new Entry[array.Length + 1];
                Array.Copy(array, newArray, array.Length);
                array = newArray;
                array[array.Length - 1] = entry;
                buckets[h & (indexFor)] = array;
            }

            return(true);
        }
Example #2
0
        public bool TryGetValue(ReadOnlySpan <byte> key, out TValue value)
        {
            var table = buckets;
            var hash  = unchecked ((int)FarmHash.Hash64(key));

            var entry = table[hash & indexFor];

            if (entry == null)
            {
                value = default(TValue);
                return(false);
            }

            {
                var v = entry[0];
                if (key.SequenceEqual(v.Key))
                {
                    value = v.Value;
                    return(true);
                }
            }

            return(TryGetValueSlow(entry, key, out value));
        }