Ejemplo n.º 1
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(); }
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
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;
                }
            }
        }
Ejemplo n.º 4
0
 private bool IsNoCollision(IDictionary <TKey, TValue> items, int prime)
 {
     return(items.Select(_ => CustomCollectionsHelpers.PositiveHashCode(_.Key) % prime).Distinct().Count() == items.Count);
 }
Ejemplo n.º 5
0
 private bool IsNoCollision(ISet <T> items, int prime)
 {
     return(items.Select(_ => CustomCollectionsHelpers.PositiveHashCode(_) % prime).Distinct().Count() == items.Count);
 }