Пример #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="iCapacity"></param>
 public LockFreeMultiDictionary(bool allowDuplicateValues)
 {
     m_AllowDuplicateValues = allowDuplicateValues;
     m_MultiDictionary      = new MultiDictionary <KeyT, ValueT>(m_AllowDuplicateValues);
 }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        private ValueT[] InternalToArrayValuesByKey(KeyT key)
        {
            MultiDictionary <KeyT, ValueT> newMultiDict = null;
            MultiDictionary <KeyT, ValueT> oldMultiDict = null;

            do
            {
                if (m_CachedMultiDictionary == m_MultiDictionary)
                {
                    ValueT[] tempValues = s_ZeroValueArray;

                    Dictionary <KeyT, ValueT[]> tempValueDictionary = m_ValueDictionary;
                    if (tempValueDictionary != null)
                    {
                        tempValueDictionary.TryGetValue(key, out tempValues);
                    }

                    return(tempValues);
                }

                oldMultiDict = m_CachedMultiDictionary;
                newMultiDict = m_MultiDictionary;
            } while (Interlocked.CompareExchange <MultiDictionary <KeyT, ValueT> >(ref m_CachedMultiDictionary, newMultiDict, oldMultiDict) != oldMultiDict);


            KeyT[] keyArray = new KeyT[newMultiDict.Count];
            Dictionary <KeyT, ValueT[]> valueDictionary = new Dictionary <KeyT, ValueT[]>(newMultiDict.Count);

            KeyValuePair <KeyT, ValueT[]>[] keyValuePairArray = new KeyValuePair <KeyT, ValueT[]> [newMultiDict.Count];

            int iIndex = 0;

            foreach (KeyValuePair <KeyT, ICollection <ValueT> > keyValuePair in newMultiDict)
            {
                keyArray[iIndex] = keyValuePair.Key;

                ValueT[] tempValueArray = new ValueT[keyValuePair.Value.Count];

                int iIndex2 = 0;
                foreach (ValueT value in keyValuePair.Value)
                {
                    tempValueArray[iIndex2] = value;

                    ++iIndex2;
                }

                valueDictionary.Add(keyValuePair.Key, tempValueArray);
                keyValuePairArray[iIndex] = new KeyValuePair <KeyT, ValueT[]>(keyValuePair.Key, tempValueArray);

                ++iIndex;
            }

            // 不能总是获取最新的数据
            //bool locktoken = false;
            //m_LockCachedDictionary.Enter(ref locktoken);
            Monitor.Enter(m_LockCachedDictionary);
            {
                if (m_CachedMultiDictionary == newMultiDict)
                {
                    m_KeyArray          = keyArray;
                    m_ValueDictionary   = valueDictionary;
                    m_KeyValuePairArray = keyValuePairArray;
                }
            }
            // m_LockCachedDictionary.Exit();
            Monitor.Exit(m_LockCachedDictionary);
            ValueT[] returnValues = s_ZeroValueArray;

            valueDictionary.TryGetValue(key, out returnValues);

            return(returnValues);
        }
Пример #3
0
 /// <summary>
 ///
 /// </summary>
 public SafeMultiDictionary(bool allowDuplicateValues)
 {
     m_MultiDictionary = new MultiDictionary <KeyT, ValueT>(allowDuplicateValues);
 }