Example #1
0
        public bool Contains(T item)
        {
            if (!_isHashSetFallback)
            {
                var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(item) % _slotsLength];
                return(existingItem != null && item.Equals(existingItem));
            }

            return(_hashSet.Contains(item));
        }
Example #2
0
        public bool ContainsKey(TKey key)
        {
            if (!_isDictionaryFallback)
            {
                var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(key) % _slotsLength];
                return(existingItem.Key != null && key.Equals(existingItem.Key));
            }

            return(_dictionary.ContainsKey(key));
        }
Example #3
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            if (!_isDictionaryFallback)
            {
                var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(key) % _slotsLength];
                value = existingItem.Value;
                return(existingItem.Key != null && key.Equals(existingItem.Key));
            }

            return(_dictionary.TryGetValue(key, out value));
        }
Example #4
0
        public bool Contains(KeyValuePair <TKey, TValue> item)
        {
            if (!_isDictionaryFallback)
            {
                var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(item.Key) % _slotsLength];
                return(existingItem.Key != null &&
                       item.Key.Equals(existingItem.Key) &&
                       item.Value.Equals(existingItem.Value));
            }

            return(_dictionary.Contains(item));
        }
Example #5
0
        public TValue this[TKey key]
        {
            get
            {
                if (!_isDictionaryFallback)
                {
                    var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(key) % _slotsLength];
                    if (existingItem.Key != null && key.Equals(existingItem.Key))
                    {
                        return(existingItem.Value);
                    }
                }

                return(_dictionary[key]);
            }
            set { throw new NotSupportedException(); }
        }
Example #6
0
        public ReadonlyHashSet(ISet <T> items)
        {
            _hashSet     = new HashSet <T>(items);
            _slotsLength = CustomCollectionsHelpers.Primes.FirstOrDefault(_ => IsNoCollision(items, _));
            if (_slotsLength == 0)
            {
                _isHashSetFallback = true;
            }
            else
            {
                _slots = new T[_slotsLength];

                foreach (var item in items)
                {
                    _slots[CustomCollectionsHelpers.PositiveHashCode(item) % _slots.Length] = item;
                }
            }
        }
Example #7
0
        public ReadonlyDictionary(IDictionary <TKey, TValue> items)
        {
            _dictionary  = items;
            _slotsLength = CustomCollectionsHelpers.Primes.FirstOrDefault(_ => IsNoCollision(items, _));
            if (_slotsLength == 0)
            {
                _isDictionaryFallback = true;
            }
            else
            {
                _slots = new Entry[_slotsLength];

                foreach (var item in items)
                {
                    _slots[CustomCollectionsHelpers.PositiveHashCode(item.Key) % _slotsLength].Key   = item.Key;
                    _slots[CustomCollectionsHelpers.PositiveHashCode(item.Key) % _slotsLength].Value = item.Value;
                }
            }
        }
Example #8
0
 private bool IsNoCollision(IDictionary <TKey, TValue> items, int prime)
 {
     return(items.Select(_ => CustomCollectionsHelpers.PositiveHashCode(_.Key) % prime).Distinct().Count() == items.Count);
 }
Example #9
0
 private bool IsNoCollision(ISet <T> items, int prime)
 {
     return(items.Select(_ => CustomCollectionsHelpers.PositiveHashCode(_) % prime).Distinct().Count() == items.Count);
 }