public bool Contains(T item) { if (!_isHashSetFallback) { var existingItem = _slots[CustomCollectionsHelpers.PositiveHashCode(item) % _slotsLength]; return(existingItem != null && item.Equals(existingItem)); } return(_hashSet.Contains(item)); }
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)); }
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)); }
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)); }
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); }