/// <summary> /// Add a value to the dictionary. /// </summary> public void Add(K k, V v) { object item; if (!this.IsEmpty && _dictionary.TryGetValue(k, out item)) { var arrayBuilder = item as ArrayBuilder <V>; if (arrayBuilder == null) { // Promote from singleton V to ArrayBuilder<V>. Debug.Assert(item is V, "Item must be either a V or an ArrayBuilder<V>"); arrayBuilder = new ArrayBuilder <V>(2); arrayBuilder.Add((V)item); arrayBuilder.Add(v); _dictionary[k] = arrayBuilder; } else { arrayBuilder.Add(v); } } else { this.EnsureDictionary(); _dictionary[k] = v; } }
public void Add(K k, V v) { if (_dictionary is object && _dictionary.TryGetValue(k, out var valueSet)) { Debug.Assert(valueSet.Count >= 1); // Have to re-store the ValueSet in case we upgraded the existing ValueSet from // holding a single item to holding multiple items. _dictionary[k] = valueSet.WithAddedItem(v); } else { this.EnsureDictionary(); _dictionary ![k] = new ValueSet(v);