/// <summary> /// Trim the excess items from the Dictionary /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="dic"></param> /// <returns></returns> public static IDictionary <TKey, TValue> TrimExcess <TKey, TValue>(this IDictionary <TKey, TValue> dic) { var kv = new KeyValuePair <TKey, TValue> [dic.Count]; dic.CopyTo(kv, 0); List <KeyValuePair <TKey, TValue> > l = kv.ToList(); l.TrimExcess(); var newDic = new Dictionary <TKey, TValue>(l.Count); AutoParallel.AutoParallelForEach(l, (p) => { newDic.Add(p.Key, p.Value); }); return(newDic); }
/// <summary> /// Trim the excess items from the Dictionary /// </summary> /// <typeparam name="TKey"></typeparam> /// <typeparam name="TValue"></typeparam> /// <param name="dic"></param> /// <returns></returns> public static IDictionary <TKey, TValue> TrimExcess <TKey, TValue>(this IDictionary <TKey, TValue> dic) { var kv = new KeyValuePair <TKey, TValue> [dic.Count]; dic.CopyTo(kv, 0); List <KeyValuePair <TKey, TValue> > l = kv.ToList(); l.TrimExcess(); var newDic = new Dictionary <TKey, TValue>(l.Count); foreach (var p in l) { newDic.Add(p.Key, p.Value); } return(newDic); }