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