Example #1
0
 public static bool ArrayContains( Array array, object value, System.Collections.IComparer comparer )
 {
     foreach ( object item in array )
     {
         if ( comparer.Compare( item, value ) == 0 )
             return true;
     }
     return false;
 }
Example #2
0
		/// <summary> Find an item in the tree.
		/// </summary>
		/// <param name="x">the item to search for.</param>
		/// <param name="comparer">The ICompararer used to compare when finding the given node.</param>
		/// <returns> the matching item or null if not found.</returns>
		public virtual IComparable Find(IComparable x, System.Collections.IComparer comparer)
		{
			// TODO: why is this line here?
			nullNode.element = x;
			current = header.right;

			for (; ; )
			{
				if (comparer.Compare(x,current.element) < 0)
					current = current.left;
				else if (comparer.Compare(x,current.element) > 0)
					current = current.right;
				else if (current != nullNode)
					return current.element;
				else
					return null;
			}
		}
Example #3
0
		/// <summary>
		/// The actual implementation
		/// </summary>
		/// <param name="primary"></param>
		/// <param name="left"></param>
		/// <param name="right"></param>
		/// <param name="compare"></param>
		protected void InternalSort(
		ref System.Collections.ArrayList primary,
		int left,
		int right,
		System.Collections.IComparer compare)
		{
			if (secondaryList == null || secondaryList.Count != primary.Count)
				secondaryList = (System.Collections.ArrayList)primary.Clone();

			if (right > left)
			{
				int middle = (left + right) / 2;
				InternalSort(ref primary, left, middle, compare);
				InternalSort(ref primary, middle + 1, right, compare);

				int i, j, k;
				for (i = middle + 1; i > left; i--)
					secondaryList[i - 1] = primary[i - 1];
				for (j = middle; j < right; j++)
					secondaryList[right + middle - j] = primary[j + 1];
				for (k = left; k <= right; k++)
					primary[k] = (compare.Compare(secondaryList[i], secondaryList[j]) < 0) ?
					secondaryList[i++] : secondaryList[j--];
			}
		}
Example #4
0
		/// <summary>
		/// The actual implementation
		/// </summary>
		/// <param name="primary"></param>
		/// <param name="left"></param>
		/// <param name="right"></param>
		/// <param name="compare"></param>
		protected void InternalSort(
		ref Array primary,
		int left,
		int right,
		System.Collections.IComparer compare)
		{
			if (secondary == null || secondary.Length != primary.Length)
				secondary = (Array)primary.Clone();

			if (right > left)
			{
				int middle = (left + right) / 2;
				InternalSort(ref primary, left, middle, compare);
				InternalSort(ref primary, middle + 1, right, compare);

				int i, j, k;
				for (i = middle + 1; i > left; i--)
					secondary.SetValue(primary.GetValue(i - 1), i - 1);
				for (j = middle; j < right; j++)
					secondary.SetValue(primary.GetValue(j + 1), right + middle - j);
				for (k = left; k <= right; k++)
					primary.SetValue(
					(compare.Compare(secondary.GetValue(i), secondary.GetValue(j)) < 0) ?
					secondary.GetValue(i++) :
					secondary.GetValue(j--), k);
			}
		}
Example #5
0
        /// <summary>
        /// Gets the index of the object in the specified arraylist.
        /// </summary>
        /// <param name="arraylist">The array list to obtain and index from.</param>
        /// <param name="obj">The object to obtain an index for.</param>
        /// <param name="comparer">The comparer algorithm to use in the matching.</param>
        /// <returns>The index of the object in the arraylist or -1 if not found.</returns>
        public static int IndexOf(System.Collections.ArrayList arraylist, object obj, System.Collections.IComparer comparer)
        {
            if (arraylist == null) throw new ArgumentNullException("arraylist", "Unable to get an index of a particular object for a null array list.");
            if (comparer == null) throw new ArgumentNullException("comparer", "Unable to get an index of a particular object with a null comparer reference");

            // object is not in the list because there are no items in the list.
            if (arraylist.Count == 0) return -1;

            // compare the single item to see if it matches.
            if (arraylist.Count == 1)
            {
                if (comparer.Compare(arraylist[0], obj) == 0)
                    return 0;
                else
                    return -1;
            }

            // get a random generator to calcuate the starting iterative index.
            Random randgen = new Random();
            // get the random number between the indexes of the list for the search terminator
            int endsearch = randgen.Next(0, arraylist.Count - 1);
            // store the array max index value
            int arraymax = arraylist.Count - 1;
            // the start index is one position from the termination index
            int index = (endsearch == arraymax ? 0 /* set to zero since at end */: endsearch + 1);
            // the terminator is the index...remember it
            int searchterminator = index;

            // while we haven't hit the termination index.
            // circulate through the arraylist.
            do
            {
                if (comparer.Compare(arraylist[index], obj) == 0)
                    return index; // we have a match! return it
                else
                    // increment index or set it to beginning
                    index = (index == arraymax ? 0 /* set to zero since at end */: index + 1);
            } while (index != searchterminator);

            // if we haven't found anything yet....
            return -1;
        }
Example #6
0
        /// <summary>
        /// A Bubblesort for good sorting of mostly sorted lists</summary>
        /// <param name='c'>
        /// The <see cref="IComparer"/> implementation to use when comparing elements.</param>
        public void BubbleSort(System.Collections.IComparer c)
        {
            object temp; // used when swapping values
            bool sorted = false;

            // bubblesort starting from the end of the list (since that's where the game adds the MapPoints)
            while (!sorted) {
                sorted = true;
                for (int i = Count - 1; i > 0; --i) {
                    // if i is less than i - 1, swap
                    if (0 < c.Compare (this [i - 1], this [i])) {
                        sorted = false;
                        temp = this[i];
                        this[i] = this[i-1];
                        this[i-1] = temp;
                    }
                }
            }
        }