예제 #1
0
        /// <summary>
        /// Inserts a value into a sorted list in-place, maintaining the sort order. Returns the index of the inserted item. To "insert" an item without modifying the source list, call <see cref="O:SortedEnumerableExtensions.MergeSorted"/>.
        /// </summary>
        /// <typeparam name="T">The type of object contained in the list.</typeparam>
        /// <param name="list">The sorted list into which to insert.</param>
        /// <param name="item">The item to insert into the list.</param>
        /// <returns>The index at which the new item was inserted.</returns>
        public static int Insert <T>(this ISortedList <T> list, T item)
        {
            int index = list.BinarySearch(item);

            if (index < 0)
            {
                index = ~index;
            }

            list.Insert(index, item);
            return(index);
        }
예제 #2
0
        /// <summary>
        /// Searches a sorted list for a given value, returning its index if found. If not found, the return value is the bitwise complement of the next element larger than the value.
        /// </summary>
        /// <typeparam name="T">The type of items in the list.</typeparam>
        /// <param name="list">The sorted list.</param>
        /// <param name="item">The item to search for in the list.</param>
        /// <returns>The index of <paramref name="item"/> if it was in the list; otherwise, the bitwise complement of the next larger element in the list.</returns>
        public static int BinarySearch <T>(this ISortedList <T> list, T item)
        {
            IComparer <T> comparer = list.Comparer;

            return(list.BinarySearch(x => comparer.Compare(item, x)));
        }