Exemple #1
0
        public bool TryAppend(T item, out SortedListSlim <T> newSortedList)
        {
            newSortedList = null;
            var pair  = new KeyValuePair <int, T>(m_codeGenerator.GetHashCode(item), item);
            var index = m_sortedList.BinarySearch(pair, _pairComparer);

            if (index >= 0)
            {
                // 有可能是哈希冲突
                if (IsExists(pair, ref index))
                {
                    return(false);
                }

                index = ~index;
            }

            var largerIndex = ~index;
            var newList     = new List <KeyValuePair <int, T> >(m_sortedList.Count + 1);

            if (largerIndex > 0)
            {
                newList.AddRange(m_sortedList.Take(largerIndex));
            }

            newList.Add(pair);

            if (largerIndex < m_sortedList.Count)
            {
                newList.AddRange(m_sortedList.Skip(largerIndex));
            }

            newSortedList = new SortedListSlim <T>(newList, m_valueComparer, m_codeGenerator);
            return(true);
        }
Exemple #2
0
 public SortedListSlim(T firstItem,
                       IEqualityComparer <T> comparer,
                       IHashCodeGenerator <T> hashCodeGenerator)
     : this(new List <KeyValuePair <int, T> >
 {
     new KeyValuePair <int, T>(hashCodeGenerator.GetHashCode(firstItem), firstItem)
 }, comparer, hashCodeGenerator)
 {
 }