예제 #1
0
        public void MergeSort(CompareDelegate compareMethod, BackgroundWorker bgWorker) // Uses a CompareDelegate method to accomplish merge step.
        {
            int            i, midPos;
            GArrayList <T> leftList  = new GArrayList <T>();
            GArrayList <T> rightList = new GArrayList <T>();

            if (!bgWorker.CancellationPending)
            {
                if (this.count < 100)
                {
                    this.SelectionSort(compareMethod, bgWorker);
                }
                else
                {
                    midPos = (1 + this.count) / 2;
                    for (i = 1; i <= midPos; i++)
                    {
                        leftList.Add(this[i]);
                    }
                    for (i = midPos + 1; i <= this.count; i++)
                    {
                        rightList.Add(this[i]);
                    }
                    leftList.MergeSort(compareMethod, bgWorker);
                    rightList.MergeSort(compareMethod, bgWorker);
                    this.Merge(leftList, rightList, compareMethod);
                }
            }
        }
예제 #2
0
        public void MergeSort() // Uses the IComparable<T> CompareTo() method to accomplish merge step.
        {
            int            i, midPos;
            GArrayList <T> leftList  = new GArrayList <T>();
            GArrayList <T> rightList = new GArrayList <T>();

            if (this.count < 100)
            {
                this.SelectionSort();
            }
            else
            {
                midPos = (1 + this.count) / 2;
                for (i = 1; i <= midPos; i++)
                {
                    leftList.Add(this[i]);
                }
                for (i = midPos + 1; i <= this.count; i++)
                {
                    rightList.Add(this[i]);
                }
                leftList.MergeSort();
                rightList.MergeSort();
                this.Merge(leftList, rightList);
            }
        }
예제 #3
0
        public void Merge(GArrayList <T> leftList, GArrayList <T> rightList, // Uses a CompareDelegate method.
                          CompareDelegate compareMethod)
        {
            int i, j, k, count = leftList.Count + rightList.Count;

            for (i = 1, j = 1, k = 1; i <= this.count; i++)
            {
                if ((j <= leftList.count) && (k <= rightList.Count))
                {
                    if (compareMethod(leftList[j], rightList[k]) <= 0)
                    {
                        this[i] = leftList[j];
                        j++;
                    }
                    else
                    {
                        this[i] = rightList[k];
                        k++;
                    }
                }
                else if (j <= leftList.count)
                {
                    this[i] = leftList[j];
                    j++;
                }
                else
                {
                    this[i] = rightList[k];
                    k++;
                }
            }
        }
예제 #4
0
        public void Merge(GArrayList <T> leftList, GArrayList <T> rightList) // Uses the IComparable<T> CompareTo() method.
        {
            int i, j, k;

            for (i = 1, j = 1, k = 1; i <= this.count; i++)
            {
                if ((j <= leftList.count) && (k <= rightList.count))
                {
                    if (leftList[j].CompareTo(rightList[k]) <= 0)
                    {
                        this[i] = leftList[j];
                        j++;
                    }
                    else
                    {
                        this[i] = rightList[k];
                        k++;
                    }
                }
                else if (j <= leftList.count)
                {
                    this[i] = leftList[j];
                    j++;
                }
                else
                {
                    this[i] = rightList[k];
                    k++;
                }
            }
        }
예제 #5
0
 public void Copy(GArrayList <T> sourceList)
 {
     this.Clear();
     if (sourceList.count > 0)
     {
         if (sourceList[1] is ICloneable) // Perform deep copy
         {
             foreach (T data in sourceList)
             {
                 this.Add((T)((ICloneable)data).Clone());
             }
         }
         else if (default(T) == null)
         {
             ProcessError("Use of the GArrayList Copy method on a list of a reference type requires\n" +
                          "that the type implement the ICloneable interface.");
         }
         else // Perform shallow copy
         {
             foreach (T data in sourceList)
             {
                 this.Add(data);
             }
         }
     }
 }
예제 #6
0
        public void QuickSort(CompareDelegate compareMethod) // Uses a CompareDelegate method.
        {
            int            i;
            T              pivot;
            GArrayList <T> leftList  = new GArrayList <T>();
            GArrayList <T> rightList = new GArrayList <T>();

            if (this.count < 100)
            {
                this.SelectionSort(compareMethod);
            }
            else
            {
                pivot = this[1];
                for (i = 2; i <= this.count; i++)
                {
                    if (this[i].CompareTo(pivot) < 0)
                    {
                        leftList.Add(this[i]);
                    }
                    else
                    {
                        rightList.Add(this[i]);
                    }
                }
                leftList.Add(pivot);
                leftList.QuickSort(compareMethod);
                rightList.QuickSort(compareMethod);
                this.Merge(leftList, rightList);
            }
        }
예제 #7
0
        public static GArrayList <T> operator+(GArrayList <T> list1, GArrayList <T> list2)
        {
            GArrayList <T> returnList = list1.Clone();

            foreach (T data in list2)
            {
                returnList.Add(data);
            }
            return(returnList);
        }
예제 #8
0
        private void IncreaseCapacity()
        {
            GArrayList <T> tempList = new GArrayList <T>(2 * this.capacity);

            foreach (T data in this)
            {
                tempList.Add(data);
            }
            this.capacity = tempList.capacity;
            this.count    = tempList.count;
            this.items    = tempList.items;
        }
예제 #9
0
        public static GArrayList <T> operator-(GArrayList <T> list1, GArrayList <T> list2)
        {
            GArrayList <T> returnList = new GArrayList <T>();

            foreach (T data in list1)
            {
                if (list2.LinearSearch(data) < 0)
                {
                    returnList.Add(data);
                }
            }
            return(returnList);
        }
예제 #10
0
        public static GArrayList <T> operator|(GArrayList <T> list1, GArrayList <T> list2)
        {
            int            index;
            GArrayList <T> returnList = list1.Clone();

            foreach (T data in list2)
            {
                index = list1.LinearSearch(data);
                if (index < 0)
                {
                    returnList.InsertAt(-index, data);
                }
            }
            return(returnList);
        }
예제 #11
0
        public void QuickSort() // Uses the IComparable<T> CompareTo() method.
        {
            int            i;
            T              pivot;
            GArrayList <T> leftList  = new GArrayList <T>();
            GArrayList <T> rightList = new GArrayList <T>();

            if (this.count < 100)
            {
                this.SelectionSort();
            }
            else
            {
                pivot = this[1];
                for (i = 2; i <= this.count; i++)
                {
                    if (this[i].CompareTo(pivot) <= 0)
                    {
                        leftList.Add(this[i]);
                    }
                    else
                    {
                        rightList.Add(this[i]);
                    }
                }
                leftList.Add(pivot);
                leftList.QuickSort();
                rightList.QuickSort();
                for (i = 1; i <= leftList.count; i++)
                {
                    this[i] = leftList[i];
                }
                for (i = 1; i <= rightList.count; i++)
                {
                    this[leftList.count + i] = rightList[i];
                }
                //this.Clear();
                //for (i=1; i<=leftList.count; i++)
                //  this.Add(leftList[i]);
                //for (i=1; i<=rightList.count; i++)
                //  this.Add(rightList[i]);
            }
        }
예제 #12
0
        public void QuickSort(CompareDelegate compareMethod, BackgroundWorker bgWorker) // Uses a CompareDelegate method.
        {
            int            i;
            T              pivot;
            GArrayList <T> leftList  = new GArrayList <T>();
            GArrayList <T> rightList = new GArrayList <T>();

            if (!bgWorker.CancellationPending)
            {
                if (this.count < 100)
                {
                    this.SelectionSort(compareMethod, bgWorker);
                }
                else
                {
                    pivot = this[1];
                    for (i = 2; i <= this.count; i++)
                    {
                        if (compareMethod(this[i], pivot) <= 0)
                        {
                            leftList.Add(this[i]);
                        }
                        else
                        {
                            rightList.Add(this[i]);
                        }
                    }
                    leftList.Add(pivot);
                    leftList.QuickSort(compareMethod, bgWorker);
                    rightList.QuickSort(compareMethod, bgWorker);
                    for (i = 1; i <= leftList.count; i++)
                    {
                        this[i] = leftList[i];
                    }
                    for (i = 1; i <= rightList.count; i++)
                    {
                        this[leftList.count + i] = rightList[i];
                    }
                }
            }
        }
예제 #13
0
 public GArrayList(GArrayList <T> sourceList) // Copy constructor
 {
     this.Copy(sourceList);
 }