Exemplo n.º 1
0
 public void SetValueIndirect(int a, int b)
 {
     sort_operation = SortOperation.SetValueIndirect;
     operand_a = a;
     operand_b = b;
     Data[operand_a] = Data[operand_b];
 }
Exemplo n.º 2
0
        protected VisitResult VisitSort(SortOperation operation)
        {
            var source = Visit(operation.Source);

            if (source.ResultSchema.Kind != SchemaNodeKind.Collection)
            {
                throw new InvalidOperationException("Can only use sort operations on collections");
            }
            var result = source.Expression;

            foreach (var step in operation.Steps)
            {
                var parameter = Expression.Parameter(source.ElementType, operation.ParameterName);
                using (CreateScope(new[] { new FunctionScopeVariable(operation.ParameterName, ((SchemaNodeCollection)source.ResultSchema).ElementSchema, parameter) }))
                {
                    var sort      = Visit(step.SortBy);
                    var lambda    = Expression.Lambda(sort.Expression, new[] { parameter });
                    var arguments = new[] { result, Expression.Quote(lambda) };
                    var method    = result == source.Expression ?
                                    (step.Ascending ? "OrderBy" : "OrderByDescending") :
                                    (step.Ascending ? "ThenBy" : "ThenByDescending");
                    result = Expression.Call(
                        null,
                        typeof(Queryable).GetMethod(method, arguments.Select(expr => expr.Type).ToArray()),
                        arguments);
                }
            }
            return(new VisitResult
            {
                ResultSchema = source.ResultSchema,
                Expression = result,
                ElementType = source.ElementType,
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Select sort operation and execute
        /// </summary>
        /// <typeparam name="T">Type of object being sorted</typeparam>
        /// <typeparam name="TKey">Type of result from lambda</typeparam>
        /// <param name="sortType">Type of sort to perform</param>
        /// <param name="list">List to sort</param>
        /// <param name="lambdaExpr">Lambda expression for sort condition</param>
        /// <returns>Sorted List</returns>
        private IOrderedEnumerable <T> PerformSort <T, TKey>(string sortType, IEnumerable <T> list, LambdaExpression lambdaExpr)
        {
            SortOperation <T, TKey> sortOp = null;

            switch (sortType)
            {
            case "OrderBy":
                sortOp = new SortOperation <T, TKey>(list.OrderBy);
                break;

            case "OrderByDescending":
                sortOp = new SortOperation <T, TKey>(list.OrderByDescending);
                break;

            case "ThenBy":
                sortOp = new SortOperation <T, TKey>((list as IOrderedEnumerable <T>).ThenBy);
                break;

            case "ThenByDescending":
                sortOp = new SortOperation <T, TKey>((list as IOrderedEnumerable <T>).ThenByDescending);
                break;

            default:
                break;
            }

            return(sortOp((Func <T, TKey>)lambdaExpr.Compile()));
        }
Exemplo n.º 4
0
 public void SetValue(int a, int b)
 {
     sort_operation = SortOperation.SetValue;
     operand_a = a;
     operand_b = b;
     Data[operand_a] = operand_b;
 }
Exemplo n.º 5
0
            /// <summary>
            /// Determines whether the specified operation terminates successfully as expected.
            /// </summary>
            /// <param name="operation">The operation.</param>
            public static void Succeed(
                SortOperation <DoubleMatrixState> operation)
            {
                var result = operation.Expected;

                // Dense
                Succeed(
                    operators: operation.DataWritableOps,
                    data: operation.Data.AsDense,
                    sortDirection: operation.SortDirection,
                    expected: result);

                // Sparse
                Succeed(
                    operators: operation.DataWritableOps,
                    data: operation.Data.AsSparse,
                    sortDirection: operation.SortDirection,
                    expected: result);

                // Dense.AsReadOnly()
                Succeed(
                    operators: operation.DataReadOnlyOps,
                    data: operation.Data.AsDense.AsReadOnly(),
                    sortDirection: operation.SortDirection,
                    expected: result);

                // Sparse.AsReadOnly()
                Succeed(
                    operators: operation.DataReadOnlyOps,
                    data: operation.Data.AsSparse.AsReadOnly(),
                    sortDirection: operation.SortDirection,
                    expected: result);
            }
Exemplo n.º 6
0
            /// <summary>
            /// Determines whether the specified operation fails as expected.
            /// </summary>
            /// <param name="operation">The operation to test.</param>
            public static void Fail <TException>(
                SortOperation <TException> operation)
                where TException : Exception
            {
                var exception = operation.Expected;

                // Dense
                Fail(
                    operators: operation.DataWritableOps,
                    data: operation.Data.AsDense,
                    sortDirection: operation.SortDirection,
                    exception: exception);

                // Sparse
                Fail(
                    operators: operation.DataWritableOps,
                    data: operation.Data.AsSparse,
                    sortDirection: operation.SortDirection,
                    exception: exception);

                // Dense.AsReadOnly()
                Fail(
                    operators: operation.DataReadOnlyOps,
                    data: operation.Data.AsDense.AsReadOnly(),
                    sortDirection: operation.SortDirection,
                    exception: exception);

                // Sparse.AsReadOnly()
                Fail(
                    operators: operation.DataReadOnlyOps,
                    data: operation.Data.AsSparse.AsReadOnly(),
                    sortDirection: operation.SortDirection,
                    exception: exception);
            }
Exemplo n.º 7
0
        public void Swap(int a, int b)
        {
            sort_operation = SortOperation.Swap;
            operand_a = a;
            operand_b = b;

            if (operand_a >= 0 && operand_b >= 0)
            {
                Data.Swap(operand_a, operand_b);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 策略模式
        /// </summary>
        public static void StragetyPattern()
        {
            int[] array         = new int[] { 49, 5, 10, 99, 43, 3, 25, 28, 43, 47, 56, 1 };
            var   sortOperation = new SortOperation(new BubbleSort());

            Console.WriteLine("冒泡排序结果:");
            sortOperation.Sort(array);

            array         = new int[] { 49, 5, 10, 99, 43, 3, 25, 28, 43, 47, 56, 1 };
            sortOperation = new SortOperation(new QuickSort());
            Console.WriteLine("快速排序结果:");
            sortOperation.Sort(array);
        }
Exemplo n.º 9
0
 public static SchemaException SortConvention_OperationIsNotNamed(
     ISortConvention sortConvention,
     SortOperation sortOperation) =>
 new SchemaException(
     SchemaErrorBuilder.New()
     .SetMessage(
         DataResources.SortProvider_UnableToCreateFieldHandler,
         sortOperation.Id,
         sortConvention.GetType().FullName ?? sortConvention.GetType().Name,
         sortConvention.Scope ?? "Default")
     .SetExtension(nameof(sortConvention), sortConvention)
     .SetExtension(nameof(sortOperation), sortOperation)
     .Build());
Exemplo n.º 10
0
            /// <summary>
            /// Tests an operation
            /// whose data operand is set through a value represented by a <b>null</b> instance.
            /// </summary>
            /// <param name="operation">The operation to test.</param>
            public static void DataIsNull(
                SortOperation <ArgumentNullException> operation)
            {
                var exception = operation.Expected;

                Fail(
                    operators: operation.DataWritableOps,
                    data: null,
                    sortDirection: SortDirection.Ascending,
                    exception: exception);
                Fail(
                    operators: operation.DataReadOnlyOps,
                    data: null,
                    sortDirection: SortDirection.Descending,
                    exception: exception);
            }
Exemplo n.º 11
0
        /// <summary>
        ///  Sort array
        ///  <example>For example:
        /// <code>
        ///    (a,b) => ((int)a.)CompareTo(b) == -1
        /// </code>
        /// sorts array from larger to smaller.
        /// </example>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array">operated array type IComparable</param>
        /// <param name="sortOperation">1 argument - first position array for sort, 2 argument - second position array for sort</param>
        public static void Sort <T>(T[] array, SortOperation sortOperation)
        {
            T exchange = default;

            for (int i = 0; i < array.Length; i++)
            {
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (sortOperation(array[i], array[j]))
                    {
                        exchange = array[i];
                        array[i] = array[j];
                        array[j] = exchange;
                    }
                }
            }
        }
Exemplo n.º 12
0
 public void Reset()
 {
     sort_operation = SortOperation.None;
 }
Exemplo n.º 13
0
 public void Compare(int a, int b)
 {
     sort_operation = SortOperation.Compare;
     operand_a = a;
     operand_b = b;
 }
Exemplo n.º 14
0
        public void Sort(SortOperation sort)
        {
            List <ZTask> ZTaskList = Ztasks.ToList <ZTask>();

            Task.Run(() =>
            {
                switch (sort)
                {
                case SortOperation.DueDateAscending:
                    for (int i = 0; i < ZTaskList.Count; i++)
                    {
                        for (int j = i + 1; j < ZTaskList.Count; j++)
                        {
                            if (ZTaskList[i].TaskDetails.DueDate > ZTaskList[j].TaskDetails.DueDate || ZTaskList[i].TaskDetails.DueDate == null)
                            {
                                var tempTask = ZTaskList[i];
                                ZTaskList[i] = ZTaskList[j];
                                ZTaskList[j] = tempTask;
                            }
                        }
                    }
                    break;

                case SortOperation.DueDateDescending:
                    for (int i = 0; i < ZTaskList.Count; i++)
                    {
                        for (int j = i + 1; j < ZTaskList.Count; j++)
                        {
                            if (ZTaskList[i].TaskDetails.DueDate < ZTaskList[j].TaskDetails.DueDate || ZTaskList[i].TaskDetails.DueDate == null)
                            {
                                var tempTask = ZTaskList[i];
                                ZTaskList[i] = ZTaskList[j];
                                ZTaskList[j] = tempTask;
                            }
                        }
                    }
                    break;

                case SortOperation.ModifiedDateAscending:
                    for (int i = 0; i < ZTaskList.Count; i++)
                    {
                        for (int j = i + 1; j < ZTaskList.Count; j++)
                        {
                            if (ZTaskList[i].TaskDetails.ModifiedDate > ZTaskList[j].TaskDetails.ModifiedDate || ZTaskList[i].TaskDetails.ModifiedDate == null)
                            {
                                var tempTask = ZTaskList[i];
                                ZTaskList[i] = ZTaskList[j];
                                ZTaskList[j] = tempTask;
                            }
                        }
                    }
                    break;

                case SortOperation.ModifiedDateDescending:
                    for (int i = 0; i < ZTaskList.Count; i++)
                    {
                        for (int j = i + 1; j < ZTaskList.Count; j++)
                        {
                            if (ZTaskList[i].TaskDetails.ModifiedDate < ZTaskList[j].TaskDetails.ModifiedDate || ZTaskList[i].TaskDetails.ModifiedDate == null)
                            {
                                var tempTask = ZTaskList[i];
                                ZTaskList[i] = ZTaskList[j];
                                ZTaskList[j] = tempTask;
                            }
                        }
                    }
                    break;
                }
            });

            OnOperationCompletion(ZTaskList);
        }
Exemplo n.º 15
0
 public void Initialize()
 {
     this.cut = new SortOperation();
 }