/// <summary> /// Array optimized /// source.ToDictionary(keySelector, valueSelector) /// </summary> public static CompactDictionary <TKey, TValue, TComparer> ToComapctDictionary <TSource, TKey, TValue, TComparer>(this TSource[] source, Func <TSource, TKey> keySelector, Func <TSource, TValue> valueSelector) where TComparer : struct, IEqualityComparer <TKey> { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (keySelector == null) { throw new ArgumentNullException(nameof(keySelector)); } if (valueSelector == null) { throw new ArgumentNullException(nameof(valueSelector)); } var d = new CompactDictionary <TKey, TValue, TComparer>(source.Length); foreach (var x in source) { d.AddOrUpdate(keySelector(x), valueSelector(x)); } return(d); }
public static void RemoveAll() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; var result1 = dict.RemoveAll(new[] { "a", "c", "e" }, out var removedKeys1, out var removedValues1); Assert.AreEqual(2, result1); Assert.AreEqual(false, dict.ContainsValue(1)); Assert.AreEqual(true, dict.ContainsValue(2)); Assert.AreEqual(false, dict.ContainsValue(3)); Assert.AreEqual(true, dict.ContainsValue(4)); Assert.AreEqual(false, dict.ContainsKey("a")); Assert.AreEqual(true, dict.ContainsKey("b")); Assert.AreEqual(false, dict.ContainsKey("c")); Assert.AreEqual(true, dict.ContainsKey("d")); Assert.AreEqual(new[] { "a", "c" }, removedKeys1); Assert.AreEqual(new[] { 1, 3 }, removedValues1); var result2 = dict.RemoveAll(new[] { "a", "c", "e" }, out var removedKeys2, out var removedValues2); Assert.AreEqual(0, result2); Assert.AreEqual(Array.Empty <string>(), removedKeys2); Assert.AreEqual(Array.Empty <int>(), removedValues2); var result3 = dict.RemoveAll(new[] { "b" }, out var removedKeys3, out var removedValues3); Assert.AreEqual(1, result3); Assert.AreEqual(false, dict.ContainsValue(2)); Assert.AreEqual(false, dict.ContainsKey("b")); Assert.AreEqual(new[] { "b" }, removedKeys3); Assert.AreEqual(new[] { 2 }, removedValues3); }
public void TryGet() { var d = new CompactDictionary <string, int>(items); foreach (var n in notExsist) { Assert.Null(Get(d, n)); } foreach (var n in items) { Assert.Equal(n.Value, Get(d, n.Key)); } }
public static void FindValue() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.AreEqual(true, dict.ContainsValue(3)); Assert.AreEqual(false, dict.ContainsValue(5)); }
public static void TryAdd() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.AreEqual(false, dict.TryAdd("a", 0)); Assert.AreEqual(true, dict.TryAdd("e", 5)); Assert.AreEqual(false, dict.TryAdd("e", 5)); }
public static void Trim() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.AreEqual(16, dict.Capacity); dict.TrimToSize(); Assert.AreEqual(4, dict.Capacity); }
public static void AddingBeyondCapacityGrows() { var dict = new CompactDictionary <string, int>(null, 4) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.AreEqual(4, dict.Capacity); dict.Add("e", 5); Assert.GreaterOrEqual(dict.Capacity, 5); }
public static void UnableToReduceCapacityBelowCount() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.Throws <ArgumentOutOfRangeException>(() => dict.Capacity = 2); Assert.AreEqual(16, dict.Capacity); dict.Capacity = 5; Assert.AreEqual(5, dict.Capacity); }
public void MarginalCapacity() { var d = new CompactDictionary <string, int>(items.Length / 2); foreach (var n in items) { d.AddOrUpdate(n.Key, n.Value); } foreach (var n in notExsist) { Assert.Null(Get(d, n)); } foreach (var n in items) { Assert.Equal(n.Value, Get(d, n.Key)); } }
public bool RegisterEntity(TEntityId entityId) { if (_componentsByEntity.ContainsKey(entityId)) { return(false); } var componentIndex = new CompactDictionary <TComponentKind, ComponentId>(_componentKindComparer, 16); try { _componentsByEntity.Add(entityId, componentIndex); return(true); } catch (ArgumentException) { return(false); } }
public static void SetReturnTrueOnNewItems() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; Assert.AreEqual(1, dict["a"]); dict["a"] = 42; Assert.AreEqual(42, dict["a"]); dict["e"] = 42; Assert.AreEqual(false, dict.Set("e", 5)); Assert.AreEqual(5, dict["e"]); Assert.AreEqual(true, dict.Set("f", 6)); Assert.AreEqual(6, dict["f"]); }
public static void SetAllReturnsTheNewKeys() { var dict = new CompactDictionary <string, int>(null, 16) { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 } }; var result1 = dict.SetAll(new[] { "a", "b", "e" }, new[] { 42, 84, 5 }); Assert.AreEqual(new[] { "e" }, result1); Assert.AreEqual(42, dict["a"]); Assert.AreEqual(84, dict["b"]); Assert.AreEqual(5, dict["e"]); var sourceA = new Dictionary <string, int> { { "e", 5 }, { "f", 6 }, { "g", 7 } }; var result2 = dict.SetAll(sourceA); Assert.AreEqual(new[] { "f", "g" }, result2); Assert.AreEqual(5, dict["e"]); Assert.AreEqual(6, dict["f"]); Assert.AreEqual(7, dict["g"]); var sourceB = new Dictionary <string, int> { { "g", 7 }, { "h", 8 }, { "i", 9 } }; var result3 = dict.SetAll(sourceB.Keys, key => sourceB[key], pair => pair.Value * 2); Assert.AreEqual(new[] { "h", "i" }, result3); Assert.AreEqual(14, dict["g"]); Assert.AreEqual(8, dict["h"]); Assert.AreEqual(9, dict["i"]); }
public QueryStorage(IComparer <Query <TComponentKindSet> > queryComparer) { _queryByQueryId = new Dictionary <QueryId, Query <TComponentKindSet> >(); _queryIdByQuery = new CompactDictionary <Query <TComponentKindSet>, QueryId>(queryComparer, 16); }
static int?Get(CompactDictionary <string, int> x, string s) => x.TryGetValue(s, out var v) ? v : default(int?);
/// <summary> /// Array optimized /// source.GroupBy(keySelector, valueSelector) /// </summary> public static unsafe KeyValuePair <TKey, TValue[]>[] GroupByToArray <TSource, TKey, TValue, TComparer>(this TSource[] source, Func <TSource, TKey> keySelector, Func <TSource, TValue> valueSelector) where TComparer : struct, IEqualityComparer <TKey> { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (keySelector == null) { throw new ArgumentNullException(nameof(keySelector)); } if (valueSelector == null) { throw new ArgumentNullException(nameof(valueSelector)); } var keyCount = 0; var keyToIndex = new CompactDictionary <TKey, int, TComparer>(source.Length); var valueCounts = stackalloc int[source.Length]; var valueToKey = stackalloc int[source.Length]; var valueIndexes = stackalloc int[source.Length]; for (int i = 0; i < source.Length; i++) { valueCounts[i] = 0; } for (int i = 0; i < source.Length; i++) { var value = source[i]; var key = keySelector(value); var keyIndex = keyToIndex.GetOrAdd(key, keyCount); if (keyIndex == keyCount) { keyCount++; } valueToKey[i] = keyIndex; valueIndexes[i] = valueCounts[keyIndex]++; } var groups = new KeyValuePair <TKey, TValue[]> [keyCount]; for (int i = 0; i < source.Length; i++) { var value = source[i]; var key = keySelector(value); var ki = keyToIndex[key]; if (!(groups[ki].Value is TValue[] values)) { groups[ki] = new KeyValuePair <TKey, TValue[]>(key, values = new TValue[valueCounts[ki]]); } values[valueIndexes[i]] = valueSelector(value); } return(groups); }