/********************************************************** * Private Methods *********************************************************/ private bool AddToWeakReferenceDict(TKey key, TValue value) { int hashCodeKey = key.GetHashCode(); if (IsAutoClean) { Clean(hashCodeKey); } if (!_weakRefDict.ContainsKey(hashCodeKey)) { WeakKeyPair <TKey, TValue> weakPair = new WeakKeyPair <TKey, TValue>(key, value); _weakRefDict.Add(hashCodeKey, weakPair); return(true); } else { if (_weakRefDict[hashCodeKey].Key.Equals(key)) { return(true); } } return(false); }
public TValue this[TKey key] { get { TValue value = GetFromWeakReferenceDict(key); if (object.Equals(value, default(TValue))) { value = GetFromHardReferenceDict(key); } return(value); } set { if (!_hardRefDict.ContainsKey(key)) { int hashCodeKey = key.GetHashCode(); if (_weakRefDict.ContainsKey(hashCodeKey)) { WeakKeyPair <TKey, TValue> weakKeyPair = _weakRefDict[hashCodeKey]; weakKeyPair.Value = value; } else { Debug.LogWarning("WeakDictionary - There is no entry for key [" + key + "]."); } } else { _hardRefDict[key] = value; } } }
private bool Clean(int hashCodeKey) { bool success = false; if (_weakRefDict.ContainsKey(hashCodeKey)) { WeakKeyPair <TKey, TValue> weakKeyPair = _weakRefDict[hashCodeKey]; if (!weakKeyPair.IsAlive) { weakKeyPair.Dispose(); _weakRefDict.Remove(hashCodeKey); success = true; } } return(success); }