Example #1
0
        /// <param name="n">0 ~ source.Length - 1</param>
        public static ItemWithIndex <T> NthSmallest <T>(this ReadOnlySpan <T> source, int n) where T : IComparable <T>
        {
            var pool = ArrayPool <int> .Shared.Rent(source.Length);

            try
            {
                var indices = pool.AsSpan(0, source.Length);
                QuickSelect.Iota(indices);
                QuickSelect.Execute(source, indices, n);

                return(new ItemWithIndex <T>(source[indices[n]], indices[n]));
            }
            finally
            {
                ArrayPool <int> .Shared.Return(pool);
            }
        }
Example #2
0
        /// <param name="n">0 ~ source.Count - 1</param>
        public static ItemWithIndex <T> NthSmallest <T>(this IReadOnlyList <T> source, int n, Comparer <T> comparer)
        {
            var pool = ArrayPool <int> .Shared.Rent(source.Count);

            try
            {
                var indices = pool.AsSpan(0, source.Count);
                QuickSelect.Iota(indices);
                QuickSelect.Execute(source, indices, n, comparer);

                return(new ItemWithIndex <T>(source[indices[n]], indices[n]));
            }
            finally
            {
                ArrayPool <int> .Shared.Return(pool);
            }
        }