예제 #1
0
        /// <summary>
        /// Compares the instance with the argument.
        /// </summary>
        /// <param mainFile="obj">the argument to compare</param>
        /// <returns>1 if the instance is greater than the object obj</returns>
        public int CompareTo(object obj)
        {
            if (obj is ElementPointer)
            {
                ElementPointer ElementObject = (ElementPointer)obj;

                return(LeftDown.CompareTo(ElementObject.LeftDown));
            }
            else
            {
                throw new ArgumentException("object is not ElementPointer");
            }
        }
예제 #2
0
        /// <summary>
        /// It is assumed that the list is sorted with the help of the Sort method (in the increasing order). The procedure locates at the argument list the last item which is smaller than the itemToFind object.
        /// </summary>
        /// <param mainFile="itemToFind">the item which has to be located</param>
        /// <param mainFile="list">the nonempty list</param>
        /// <returns>The 1-based Number of the last item at the list which is smaller or  equal the itemToFind. If none exists than returns 0.</returns>
        public static int FindAtList(MyPointDouble itemToFind, List <MyPointDouble> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException();
            }
            if (list.Count == 0)
            {
                throw new ArgumentException("The passed argument is empty");
            }

            //if itemToFind is smaller than list[0] then return(-1)
            if (itemToFind.CompareTo(list[0]) == -1)
            {
                return(-1);
            }


            if (itemToFind.CompareTo(list[0]) == 0)
            {
                return(-1);
            }



            //if itemToFind is larger than list.Last then return the Number of elements at list
            if ((itemToFind.CompareTo(list.Last()) == 1) || (itemToFind.CompareTo(list.Last()) == 0))
            {
                return(list.Count);
            }



            int Start = 1;
            int End   = list.Count;

            while
            (true)
            {
                if ((End - Start) <= 1)
                {
                    return(Start);
                }

                int Middle = ((Start + End) / 2);


                //if the element at the Middle position is larger than itemToFind then put the value Middle into the variable End
                if (list[Middle - 1].CompareTo(itemToFind) == 1)
                {
                    End = Middle;
                }

                //if the element at the Middle position is larger than itemToFind then put the value Middle into the variable End
                if (list[Middle - 1].CompareTo(itemToFind) == (-1))
                {
                    Start = Middle;
                }

                //if the element at the Middle position has the same value as the itemToFind then look at which position stands the element which is larger than itemToFind

                if (list[Middle - 1].CompareTo(itemToFind) == (0))
                {
                    for (int Iterator = (Middle - 1); Iterator >= 1; Iterator--)
                    {
                        if (list[Iterator - 1].CompareTo(itemToFind) != 0)
                        {
                            return(Iterator);
                        }
                    }
                    return(-1);
                }
            }

            //    return (-1);
        }