public override Map <TMapValue> TryAdd(int key, TMapValue value, out bool success) { if (key == _key1) { success = false; return(this); } else if (key == _key2) { success = false; return(this); } else if (key == _key3) { success = false; return(this); } else { success = true; var multi = new MultiElementMap(4); multi.UnsafeStore(0, _key1, _value1); multi.UnsafeStore(1, _key2, _value2); multi.UnsafeStore(2, _key3, _value3); multi.UnsafeStore(3, key, value); return(multi); } }
public override Map <TMapValue> TryAdd(int key, TMapValue value, out bool success) { for (int i = 0; i < _keyValues.Length; i++) { if (key == _keyValues[i].Key) { // The key is in the map. success = false; return(this); } } // The key does not already exist in this map. // We need to create a new map that has the additional key/value pair. // If with the addition we can still fit in a multi map, create one. if (_keyValues.Length < MaxMultiElements) { var multi = new MultiElementMap(_keyValues.Length + 1); Array.Copy(_keyValues, 0, multi._keyValues, 0, _keyValues.Length); multi._keyValues[_keyValues.Length] = new KeyValuePair <int, TMapValue>(key, value); success = true; return(multi); } // Otherwise, upgrade to a many map. var many = new ManyElementMap(MaxMultiElements + 1); foreach (KeyValuePair <int, TMapValue> pair in _keyValues) { many[pair.Key] = pair.Value; } many[key] = value; success = true; return(many); }