Example #1
0
        public static bool IsSorted <T>(this T[] values, ComparerDirection direction) where T : IComparable <T>
        {
            if (direction == ComparerDirection.Descending)
            {
                for (int i = 1; i < values.Length; i++)
                {
                    if (values[i - 1].CompareTo(values[i]) >= 0)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                for (int i = 1; i < values.Length; i++)
                {
                    if (values[i - 1].CompareTo(values[i]) <= 0)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        ///     Sorts the elements of an entire one-dimensional array using the given comparison.
        /// </summary>
        public static T[] Sorted <T>(this T[] values, out int[] order, bool stable = false,
                                     ComparerDirection direction = ComparerDirection.Ascending)
            where T : IComparable <T>
        {
            var clone = (T[])values.Clone();

            Sort(clone, out order, stable, direction);
            return(clone);
        }
        /// <summary>
        ///     Constructs a new General Comparer.
        /// </summary>
        /// <param name="direction">The direction to compare.</param>
        /// <param name="useAbsoluteValues">True to compare absolute values, false otherwise. Default is false.</param>
        public GeneralComparer(ComparerDirection direction, bool useAbsoluteValues)
        {
            if (useAbsoluteValues)
            {
                map = Math.Abs;
            }
            else
            {
                map = a => a;
            }

            this.direction = (int)direction;
        }
Example #4
0
 public GeneralComparer(ComparerDirection direction, bool useAbsoluteValues)
 {
     this.direction = (direction == ComparerDirection.Ascending) ? 1 : -1;
     this.absolute  = useAbsoluteValues;
 }
Example #5
0
        /// <summary>
        ///   Sorts the elements of an entire one-dimensional array using the given comparison.
        /// </summary>
        ///
        public static void Sort <T>(this T[] values, out int[] order, bool stable = false, ComparerDirection direction = ComparerDirection.Ascending)
            where T : IComparable <T>
        {
            if (!stable)
            {
                order = Vector.Range(values.Length);
                if (direction == ComparerDirection.Ascending)
                {
                    Array.Sort(values, order);
                }
                else
                {
                    Array.Sort(values, order, new GeneralComparer <T>(direction));
                }

                return;
            }

            var keys = new KeyValuePair <int, T> [values.Length];

            for (var i = 0; i < values.Length; i++)
            {
                keys[i] = new KeyValuePair <int, T>(i, values[i]);
            }

            if (direction == ComparerDirection.Ascending)
            {
                Array.Sort(keys, values, new StableComparer <T>((a, b) => a.CompareTo(b)));
            }
            else
            {
                Array.Sort(keys, values, new StableComparer <T>((a, b) => - a.CompareTo(b)));
            }

            order = new int[values.Length];
            for (int i = 0; i < keys.Length; i++)
            {
                order[i] = keys[i].Key;
            }
        }
Example #6
0
 /// <summary>
 ///   Constructs a new General Comparer.
 /// </summary>
 ///
 /// <param name="direction">The direction to compare.</param>
 ///
 public GeneralComparer(ComparerDirection direction)
     : this(direction, false)
 {
 }
 /// <summary>
 ///     Constructs a new General Comparer.
 /// </summary>
 /// <param name="direction">The direction to compare.</param>
 /// <param name="map">
 ///     The mapping function which will be applied to
 ///     each vector element prior to any comparisons.
 /// </param>
 public GeneralComparer(ComparerDirection direction, Func <double, double> map)
 {
     this.map       = map;
     this.direction = (int)direction;
 }
Example #8
0
 /// <summary>
 ///   Constructs a new General Comparer.
 /// </summary>
 /// <param name="direction">The direction to compare.</param>
 /// <param name="useAbsoluteValues">True to compare absolute values, false otherwise. Default is false.</param>
 public GeneralComparer(ComparerDirection direction, bool useAbsoluteValues)
 {
     _direction = direction == ComparerDirection.Ascending ? 1 : -1;
     _absolute  = useAbsoluteValues;
 }