Example #1
0
        public bool TryGetValue(ArraySegment <byte> key, out T value)
        {
            var table = buckets;
            var hash  = ByteArrayGetHashCode(key.Array, key.Offset, key.Count);
            var entry = table[hash & indexFor];

            if (entry == null)
            {
                goto NOT_FOUND;
            }

            {
                var v = entry[0];
                if (ByteArrayComparer.Equals(key.Array, key.Offset, key.Count, v.Key))
                {
                    value = v.Value;
                    return(true);
                }
            }

            for (int i = 1; i < entry.Length; i++)
            {
                var v = entry[i];
                if (ByteArrayComparer.Equals(key.Array, key.Offset, key.Count, v.Key))
                {
                    value = v.Value;
                    return(true);
                }
            }

NOT_FOUND:
            value = default(T);
            return(false);
        }
Example #2
0
        bool TryAddInternal(byte[] key, T value)
        {
            var h     = ByteArrayGetHashCode(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 (ByteArrayComparer.Equals(key, 0, key.Length, 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);
        }