static void MultiDictionaryExample() { Console.WriteLine("Wintellect.PowerCollections.MultiDictionary<K, V> example:"); Console.WriteLine("Map keys to multiple values. Keys order is unpredictable. Values keep their insertion order."); MultiDictionary <string, int> score = new MultiDictionary <string, int>(true); score.Add("Peter", 5); score.Add("Peter", 6); score.Add("Peter", 6); score.AddMany("Maria", new int[] { 5, 4, 5, 4, 6 }); score.AddMany("George", new int[] { 6, 6, 5, 5 }); Console.WriteLine("Elements: {0}", score); Console.WriteLine("Removed element `Maria -> 4`: {0}", score.Remove("Maria", 4)); Console.WriteLine("Elements: {0}", score); }
/// <summary> /// 请尽可能的使用AddMany(...) 比 Add(...) 添加单个快得多 /// </summary> /// <param name="serial"></param> /// <param name="realm"></param> public void AddMany(KeyT key, IEnumerable <ValueT> values) { MultiDictionary <KeyT, ValueT> newMultiDict = null; MultiDictionary <KeyT, ValueT> oldMultiDict = null; do { oldMultiDict = m_MultiDictionary; newMultiDict = new MultiDictionary <KeyT, ValueT>(m_AllowDuplicateValues); foreach (var item in oldMultiDict) { newMultiDict.AddMany(item.Key, item.Value); } newMultiDict.AddMany(key, values); } while (Interlocked.CompareExchange <MultiDictionary <KeyT, ValueT> >(ref m_MultiDictionary, newMultiDict, oldMultiDict) != oldMultiDict); }
/// <summary> ///AddMany 的测试 ///</summary> public void AddManyTestHelper <KeyT, ValueT>() { bool allowDuplicateValues = false; // TODO: 初始化为适当的值 MultiDictionary <KeyT, ValueT> target = new MultiDictionary <KeyT, ValueT>(allowDuplicateValues); // TODO: 初始化为适当的值 KeyT key = default(KeyT); // TODO: 初始化为适当的值 IEnumerable <ValueT> values = null; // TODO: 初始化为适当的值 target.AddMany(key, values); Assert.Inconclusive("无法验证不返回值的方法。"); }
/// <summary> /// 请尽可能的使用AddMany(...) 比 Add(...) 添加单个快得多 /// </summary> /// <param name="serial"></param> /// <param name="realm"></param> public void AddMany(KeyT key, IEnumerable <ValueT> values) { m_LockMultiDictionary.EnterWriteLock(); { m_MultiDictionary.AddMany(key, values); m_bIsValueChange = true; } m_LockMultiDictionary.ExitWriteLock(); }
/// <summary> /// /// </summary> /// <param name="match"></param> /// <returns></returns> public int RemoveAll(Predicate <KeyT, ValueT> match) { MultiDictionary <KeyT, ValueT> tempMultiDictionary = m_MultiDictionary; if (tempMultiDictionary.Count <= 0) { return(0); } List <KeyValuePair <KeyT, ValueT> > removeList = new List <KeyValuePair <KeyT, ValueT> >(tempMultiDictionary.Count); foreach (KeyValuePair <KeyT, ICollection <ValueT> > keyValuePair in tempMultiDictionary) { foreach (ValueT valueItem in keyValuePair.Value) { if (match(keyValuePair.Key, valueItem) == true) { removeList.Add(new KeyValuePair <KeyT, ValueT>(keyValuePair.Key, valueItem)); } } } if (removeList.Count <= 0) { return(0); } int iRemoveCount = 0; MultiDictionary <KeyT, ValueT> newMultiDict = null; MultiDictionary <KeyT, ValueT> oldMultiDict = null; do { iRemoveCount = 0; oldMultiDict = m_MultiDictionary; newMultiDict = new MultiDictionary <KeyT, ValueT>(m_AllowDuplicateValues); foreach (var item in oldMultiDict) { newMultiDict.AddMany(item.Key, item.Value); } foreach (KeyValuePair <KeyT, ValueT> keyValuePair in removeList) { if (newMultiDict.Remove(keyValuePair.Key, keyValuePair.Value) == true) { iRemoveCount++; } } } while (Interlocked.CompareExchange <MultiDictionary <KeyT, ValueT> >(ref m_MultiDictionary, newMultiDict, oldMultiDict) != oldMultiDict); return(iRemoveCount); }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="values"></param> public void ReplaceMany(KeyT key, IEnumerable <ValueT> values) { MultiDictionary <KeyT, ValueT> newMultiDict = null; MultiDictionary <KeyT, ValueT> oldMultiDict = null; newMultiDict = new MultiDictionary <KeyT, ValueT>(m_AllowDuplicateValues); newMultiDict.AddMany(key, values); do { oldMultiDict = m_MultiDictionary; } while (Interlocked.CompareExchange <MultiDictionary <KeyT, ValueT> >(ref m_MultiDictionary, newMultiDict, oldMultiDict) != oldMultiDict); }