public DefaultArrayMapValue(IEnumerable <KeyValuePair <IValuable <G>, ValuePointer <G> > > entries, IValueProvider <G> valueProvider) { int index = 0; _valuesList = new List <EntryValuePointer <G> >(); foreach (KeyValuePair <IValuable <G>, ValuePointer <G> > entry in entries) { if (!IsMap && AsArrayIndex(entry.Key) != index) { IsMap = true; } EntryValuePointer <G> valuePointer = new EntryValuePointer <G>(this, entry.Key, index++, valueProvider, true) { Value = entry.Value.Value }; _valuesList.Add(valuePointer); } if (IsMap) { _valuesDictionary = new Dictionary <IValuable <G>, EntryValuePointer <G> >(); foreach (EntryValuePointer <G> pointer in _valuesList) { _valuesDictionary[pointer.Key] = pointer; } } }
public void DefaultCollectionValue_Get_OnNonMapItemNotExistsNoCreate() { DefaultLongValue <G> key = new DefaultLongValue <G>(20); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > values = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultIntValue <G>(0), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(1), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(2), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(3), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(4), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(5), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } } }; DefaultArrayMapValue <G> sut = new DefaultArrayMapValue <G>(values, valueProvider); EntryValuePointer <G> result = sut.Get(key, false, valueProvider) as EntryValuePointer <G>; Assert.False(sut.IsMap); Assert.Null(result); }
public void DefaultCollectionValue_Get_OnNonMapItemExists(bool create) { DefaultIntValue <G> key = new DefaultIntValue <G>(3); IValuable <G> value = Mock.Of <IValuable <G> >(); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > values = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultIntValue <G>(0), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(1), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(2), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { key, new ValuePointer <G> { Value = value } }, { new DefaultIntValue <G>(4), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(5), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } } }; DefaultArrayMapValue <G> sut = new DefaultArrayMapValue <G>(values, valueProvider); EntryValuePointer <G> result = sut.Get(key, create, valueProvider) as EntryValuePointer <G>; Assert.False(sut.IsMap); Assert.Equal(key, result.Key); Assert.Equal(value, result.Value); Assert.Equal(3, result.Index); Assert.True(result.IsSet); }
public void DefaultCollectionValue_Get_OnNonMapItemNotExistsCreateEntry(int keyVal, bool isMap) { DefaultIntValue <G> key = new DefaultIntValue <G>(keyVal); DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > values = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultIntValue <G>(0), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(1), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(2), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(3), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(4), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(5), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } } }; DefaultArrayMapValue <G> sut = new DefaultArrayMapValue <G>(values, valueProvider); EntryValuePointer <G> result = sut.Get(key, true, valueProvider) as EntryValuePointer <G>; Assert.Equal(isMap, sut.IsMap); Assert.Equal(key, result.Key); Assert.Null(result.Value); Assert.Equal(6, result.Index); Assert.False(result.IsSet); }
public void DefaultCollectionValue_FinalizeEntry_EntryAdd() { DefaultValueProvider <G> valueProvider = new DefaultValueProvider <G>(); Dictionary <IValuable <G>, ValuePointer <G> > values = new Dictionary <IValuable <G>, ValuePointer <G> >() { { new DefaultDoubleValue <G>(0), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(1), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultDoubleValue <G>(2), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultLongValue <G>(4), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultIntValue <G>(5), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } }, { new DefaultLongValue <G>(3), new ValuePointer <G> { Value = Mock.Of <IValuable <G> >() } } }; DefaultArrayMapValue <G> sut = new DefaultArrayMapValue <G>(values, valueProvider); EntryValuePointer <G> entryPointer = new EntryValuePointer <G>(sut, new DefaultLongValue <G>(34), 6, valueProvider); sut.FinalizeEntry(new DefaultLongValue <G>(34), entryPointer); Assert.Equal(7, sut.Size); KeyValuePair <IValuable <G>, ValuePointer <G> > item = sut.GetEntries()[6]; Assert.Equal(entryPointer.Key, item.Key); Assert.Equal(entryPointer, item.Value); }
public override void FinalizeEntry(IValuable <G> key, EntryValuePointer <G> entryValuePointer) { lock (this) { _valuesDictionary[key] = entryValuePointer; } }
public override void FinalizeEntry(IValuable <G> key, EntryValuePointer <G> entryValuePointer) { lock (this) { if (entryValuePointer.Index >= _valuesList.Count) { _valuesList.Add(entryValuePointer); } else { _valuesList[entryValuePointer.Index] = entryValuePointer; } } }
public override ValuePointer <G> Get(IValuable <G> key, bool createNonExistent, IValueProvider <G> valueProvider) { lock (this) { EntryValuePointer <G> pointer = null; if (IsMap && !_valuesDictionary.TryGetValue(key, out pointer)) { if (createNonExistent) { pointer = new EntryValuePointer <G>(this, key, _valuesDictionary.Count, valueProvider); } } return(pointer); } }
public override ValuePointer <G> Get(IValuable <G> key, bool createNonExistent, IValueProvider <G> valueProvider) { lock (this) { EntryValuePointer <G> pointer = null; int keyInt = (int)((DefaultDoubleValue <G>)key).Data; if (keyInt >= 0 && keyInt <= _valuesList.Count) { if (keyInt >= 0 && keyInt < _valuesList.Count) { pointer = _valuesList[keyInt]; } else if (createNonExistent) { pointer = new EntryValuePointer <G>(this, key, keyInt, valueProvider); } } return(pointer); } }
public override ValuePointer <G> Get(IValuable <G> key, bool createNonExistent, IValueProvider <G> valueProvider) { lock (this) { EntryValuePointer <G> pointer = null; if (!IsMap) { int?keyInt = AsArrayIndex(key); if (keyInt != null && keyInt.Value >= 0 && keyInt.Value <= _valuesList.Count) { if (keyInt.Value >= 0 && keyInt.Value < _valuesList.Count) { pointer = _valuesList[keyInt.Value]; } else if (createNonExistent) { pointer = new EntryValuePointer <G>(this, key, keyInt.Value, valueProvider); } } else if (createNonExistent) { IsMap = true; _valuesDictionary = new Dictionary <IValuable <G>, EntryValuePointer <G> >(); for (int i = 0; i < _valuesList.Count; i++) { _valuesDictionary[valueProvider.GetInt(i)] = _valuesList[i]; } } } if (IsMap && !_valuesDictionary.TryGetValue(key, out pointer)) { if (createNonExistent) { pointer = new EntryValuePointer <G>(this, key, _valuesList.Count, valueProvider); } } return(pointer); } }
public abstract void FinalizeEntry(IValuable <G> key, EntryValuePointer <G> entryValuePointer);