Exemple #1
0
        public static KeyedList <TKey, TItem> Intersect(KeyedList <TKey, TItem> a, KeyedList <TKey, TItem> b)
        {
            KeyedList <TKey, TItem> resultList = new KeyedList <TKey, TItem>();
            int indexA = 0, indexB = 0, result;

            while (indexA < a.Count && indexB < b.Count)
            {
                result = a[indexA].Key.CompareTo(b[indexB].Key);
                if (result == 0)
                {
                    resultList.list.Add(a[indexA]);
                    indexA++;
                    indexB++;
                }
                else if (result > 0)
                {
                    indexB++;
                }
                else if (result < 0)
                {
                    indexA++;
                }
            }
            return(resultList);
        }
Exemple #2
0
        public static KeyedList <TKey, TItem> Except(KeyedList <TKey, TItem> a, KeyedList <TKey, TItem> b)
        {
            KeyedList <TKey, TItem> resultList = new KeyedList <TKey, TItem>(a);
            int indexA = 0, indexB = 0;
            int result;

            while (indexA < resultList.Count && indexB < b.Count)
            {
                result = resultList[indexA].Key.CompareTo(b[indexB].Key);
                if (result == 0)
                {
                    resultList.list.RemoveAt(indexA);
                    indexB++;
                }
                else if (result > 0)
                {
                    indexB++;
                }
                else if (result < 0)
                {
                    indexA++;
                }
            }
            return(resultList);
        }
Exemple #3
0
        public void IntersectWith(KeyedList <TKey, TItem> items)
        {
            List <TItem> resultlist = new List <TItem>();
            int          indexA = 0, indexB = 0, result;

            while (indexA < list.Count && indexB < list.Count)
            {
                result = list[indexA].Key.CompareTo(items[indexB].Key);
                if (result == 0)
                {
                    resultlist.Add(list[indexA]);
                    indexA++;
                    indexB++;
                }
                else if (result > 0)
                {
                    indexB++;
                }
                else if (result < 0)
                {
                    indexA++;
                }
            }
            list = resultlist;
        }
Exemple #4
0
        /// <summary>
        /// Union with other list
        /// </summary>
        /// <param name="items">unioned list</param>
        public void UnionWith(KeyedList <TKey, TItem> items)
        {
            List <TItem> resultList = new List <TItem>();
            int          indexA = 0, indexB = 0;

            while (indexA < list.Count && indexB < items.Count)
            {
                int result = list[indexA].Key.CompareTo(items[indexB].Key);
                if (result == 0)
                {
                    resultList.Add(list[indexA++]);
                    indexB++;
                }
                if (result < 0)
                {
                    resultList.Add(list[indexA++]);
                }
                else if (result > 0)
                {
                    resultList.Add(items[indexB++]);
                }
            }
            while (indexA < list.Count)
            {
                resultList.Add(list[indexA++]);
            }
            while (indexB < items.Count)
            {
                resultList.Add(items[indexB++]);
            }
            list = resultList;
        }
Exemple #5
0
        /// <summary>
        /// Merge with other list
        /// </summary>
        /// <param name="items">merged list</param>
        /// <exception cref="InvalidOperationException">Thrown when items with same key.</exception>
        public void MergeWith(KeyedList <TKey, TItem> items)
        {
            List <TItem> resultList = new List <TItem>();
            int          indexA = 0, indexB = 0;

            while (indexA < list.Count && indexB < items.Count)
            {
                int result = list[indexA].Key.CompareTo(items[indexB].Key);
                if (result == 0)
                {
                    throw new InvalidOperationException("Same key item has been existed.");
                }
                else if (result < 0)
                {
                    resultList.Add(list[indexA++]);
                }
                else if (result > 0)
                {
                    resultList.Add(items[indexB++]);
                }
            }
            while (indexA < list.Count)
            {
                resultList.Add(list[indexA++]);
            }
            while (indexB < items.Count)
            {
                resultList.Add(items[indexB++]);
            }
            list = resultList;
        }
Exemple #6
0
        public void ExceptWith(KeyedList <TKey, TItem> items)
        {
            int indexA = 0, indexB = 0;
            int result;

            while (indexA < Count && indexB < items.Count)
            {
                result = list[indexA].Key.CompareTo(items[indexB].Key);
                if (result == 0)
                {
                    list.RemoveAt(indexA);
                    indexB++;
                }
                else if (result > 0)
                {
                    indexB++;
                }
                else if (result < 0)
                {
                    indexA++;
                }
            }
        }