public bool TryAdd(int key, TMapValue value, out IMap <TMapValue> map) { if (key == _key1 || key == _key2 || key == _key3) { map = this; return(false); } else { var multi = new MultiElementMap <TMapValue>(4); multi.UnsafeStore(0, _key1, _value1); multi.UnsafeStore(1, _key2, _value2); multi.UnsafeStore(2, _key3, _value3); multi.UnsafeStore(3, key, value); map = multi; return(true); } }
public bool TryAdd(int key, TMapValue value, out IMap <TMapValue> map) { for (int i = 0; i < _keyValues.Length; i++) { if (key == _keyValues[i].Key) { // The key is in the map. map = this; return(false); } } // 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 <TMapValue>(_keyValues.Length + 1); Array.Copy(_keyValues, 0, multi._keyValues, 0, _keyValues.Length); multi._keyValues[_keyValues.Length] = new KeyValuePair <int, TMapValue>(key, value); map = multi; return(true); } // Otherwise, upgrade to a many map. var many = new ManyElementMap <TMapValue>(MaxMultiElements + 1); foreach (KeyValuePair <int, TMapValue> pair in _keyValues) { many[pair.Key] = pair.Value; } many[key] = value; map = many; return(true); }