Beispiel #1
0
        /// <summary>
        /// Intersects the specified lists of integers. This method is guaranteed to return a new list instance
        /// and not to reuse any of the lists passed to it.
        /// </summary>
        /// <param name="list1">The first list to intersect.</param>
        /// <param name="list2">The second list to intersect.</param>
        /// <returns>The intersection result.</returns>
        public static IntArrayList IntersectSortedNew(IntArrayList list1, IntArrayList list2)
        {
            int count1 = list1.Count;
            int count2 = list2.Count;
            int index1 = 0;
            int index2 = 0;

            int[] result    = new int [AdjustCapacity(Math.Min(count1, count2))];
            int   destIndex = 0;

            if (count1 > 0 && count2 > 0)
            {
                int compareResult = list1[0] - list2[0];
                if (compareResult < 0)
                {
                    compareResult = list1.BinarySearch(list2[0]);
                    if (compareResult >= 0)
                    {
                        index1 = compareResult;
                    }
                    else
                    {
                        index1 = ~compareResult;
                    }
                }
                else if (compareResult > 0)
                {
                    compareResult = list2.BinarySearch(list1[0]);
                    if (compareResult >= 0)
                    {
                        index2 = compareResult;
                    }
                    else
                    {
                        index2 = ~compareResult;
                    }
                }
                compareResult = list1[count1 - 1] - list2[count2 - 1];
                if (compareResult < 0)
                {
                    compareResult = Array.BinarySearch(list2._items, index2, count2 - index2, list1[count1 - 1]);
                    if (compareResult >= 0)
                    {
                        count2 = compareResult + 1;
                    }
                    else
                    {
                        count2 = ~compareResult;
                    }
                }
                else if (compareResult > 0)
                {
                    compareResult = Array.BinarySearch(list1._items, index1, count1 - index1, list2[count2 - 1]);
                    if (compareResult >= 0)
                    {
                        count1 = compareResult + 1;
                    }
                    else
                    {
                        count1 = ~compareResult;
                    }
                }
                while (index1 < count1 && index2 < count2)
                {
                    compareResult = list1._items[index1] - list2._items[index2];
                    if (compareResult == 0)
                    {
                        int value = list1._items [index1];
                        result [destIndex++] = value;
                        index1++;
                        // skip duplicates
                        while (index1 < count1 && list1._items [index1] == value)
                        {
                            index1++;
                        }
                        do
                        {
                            index2++;
                        }while(index2 < count2 && list2._items [index2] == value);
                    }
                    else if (compareResult < 0)
                    {
                        index1++;
                    }
                    else
                    {
                        index2++;
                    }
                }
            }
            return(new IntArrayList(result, destIndex));
        }
Beispiel #2
0
        public void IntersectSorted(IntArrayList list)
        {
            int count1 = _size;
            int count2 = list._size;
            int compareResult;
            int index1    = 0;
            int index2    = 0;
            int destIndex = 0;

            if (count1 > 0 && count2 > 0)
            {
                compareResult = _items[0] - list[0];
                if (compareResult < 0)
                {
                    compareResult = BinarySearch(list[0]);
                    if (compareResult >= 0)
                    {
                        index1 = compareResult;
                    }
                    else
                    {
                        index1 = ~compareResult;
                    }
                }
                else if (compareResult > 0)
                {
                    compareResult = list.BinarySearch(_items[0]);
                    if (compareResult >= 0)
                    {
                        index2 = compareResult;
                    }
                    else
                    {
                        index2 = ~compareResult;
                    }
                }
                compareResult = _items[count1 - 1] - list[count2 - 1];
                if (compareResult < 0)
                {
                    compareResult = Array.BinarySearch(list._items, index2, count2 - index2, _items[count1 - 1]);
                    if (compareResult >= 0)
                    {
                        count2 = compareResult + 1;
                    }
                    else
                    {
                        count2 = ~compareResult;
                    }
                }
                else if (compareResult > 0)
                {
                    compareResult = Array.BinarySearch(_items, index1, count1 - index1, list[count2 - 1]);
                    if (compareResult >= 0)
                    {
                        count1 = compareResult + 1;
                    }
                    else
                    {
                        count1 = ~compareResult;
                    }
                }
                while (index1 < count1 && index2 < count2)
                {
                    compareResult = _items[index1] - list._items[index2];
                    if (compareResult == 0)
                    {
                        int value = _items[index1++];
                        _items[destIndex++] = value;
                        // skip duplicates
                        while (index1 < count1 && _items[index1] == value)
                        {
                            index1++;
                        }
                        do
                        {
                            index2++;
                        }while(index2 < count2 && list._items[index2] == value);
                    }
                    else if (compareResult < 0)
                    {
                        index1++;
                    }
                    else
                    {
                        index2++;
                    }
                }
            }
            _size = destIndex;
            _version++;
        }