public object this[int key] { get { // If no entry, DependencyProperty.UnsetValue is returned if (null != _mapStore) { return _mapStore.Search(key); } return DependencyProperty.UnsetValue; } set { if (value != DependencyProperty.UnsetValue) { // If not unset value, ensure write success if (null != _mapStore) { // This is done because forward branches // default prediction is not to be taken // making this a CPU win because set is // a common operation. } else { _mapStore = new SingleObjectMap(); } FrugalMapStoreState myState = _mapStore.InsertEntry(key, value); if (FrugalMapStoreState.Success == myState) { return; } else { // Need to move to a more complex storage FrugalMapBase newStore; if (FrugalMapStoreState.ThreeObjectMap == myState) { newStore = new ThreeObjectMap(); } else if (FrugalMapStoreState.SixObjectMap == myState) { newStore = new SixObjectMap(); } else if (FrugalMapStoreState.Array == myState) { newStore = new ArrayObjectMap(); } else if (FrugalMapStoreState.SortedArray == myState) { newStore = new SortedObjectMap(); } else if (FrugalMapStoreState.Hashtable == myState) { newStore = new HashObjectMap(); } else { throw new InvalidOperationException(SR.Get(SRID.FrugalMap_CannotPromoteBeyondHashtable)); } // Extract the values from the old store and insert them into the new store _mapStore.Promote(newStore); // Insert the new value _mapStore = newStore; _mapStore.InsertEntry(key, value); } } else { // DependencyProperty.UnsetValue means remove the value if (null != _mapStore) { _mapStore.RemoveEntry(key); if (_mapStore.Count == 0) { // Map Store is now empty ... throw it away _mapStore = null; } } } } }
private void SetCachedDefaultValue(DependencyObject owner, DependencyProperty property, object value) { FrugalMapBase map = _defaultValueFactoryCache.GetValue(owner); if (map == null) { map = new SingleObjectMap(); _defaultValueFactoryCache.SetValue(owner, map); } else if (!(map is HashObjectMap)) { FrugalMapBase newMap = new HashObjectMap(); map.Promote(newMap); map = newMap; _defaultValueFactoryCache.SetValue(owner, map); } map.InsertEntry(property.GlobalIndex, value); }