Exemplo n.º 1
0
        /// <summary>
        /// Добавить объект
        /// </summary>
        /// <param name="obj">Значение</param>
        public void Enqueue(object obj)
        {
            var node = new SortNode(obj as IComparable);

            root = (root == null) ? node : root.Put(node);
            Count++;
        }
        /// <summary>
        /// Process the sorting parameters and create the array of sort nodes that will be used by query processors
        /// </summary>
        /// <param name="processedQueryParameters"></param>
        /// <param name="castGeneralQueryParameters"></param>
        private void ProcessSortingParams(QueryParameters processedQueryParameters, JQueryDataTableParams castGeneralQueryParameters)
        {
            // Reference all the bound property names as sent from the client
            var boundDataProperties = castGeneralQueryParameters.amDataProp;

            var sortPriority = 0;

            // Reference the sorting directions array
            var sortingDirections = castGeneralQueryParameters.asSortDir;

            foreach (var columBeingSorted in castGeneralQueryParameters.aiSortCol)
            {
                var sortNode = new SortNode
                {
                    SortTarget    = boundDataProperties[columBeingSorted],
                    SortPriority  = sortPriority++,
                    SortDirection =
                        GetSortDirection(sortingDirections, columBeingSorted) == "asc"
                                               ? SortDirection.Asc
                                               : SortDirection.Desc
                };

                // Set the sorting target
                processedQueryParameters.SortNodes.Add(sortNode);
            }
        }
Exemplo n.º 3
0
        public void SortNodeTest()
        {
            var node = new SortNode
            {
                Sorts =
                {
                    new ExpressionWithSortOrder
                    {
                        Expression = new ColumnReferenceExpression{
                            MultiPartIdentifier = new MultiPartIdentifier{
                                Identifiers ={ new Identifier                           {
                          Value = "value1"
                      } }
                            }
                        },
                        SortOrder = SortOrder.Ascending
                    },
                    new ExpressionWithSortOrder
                    {
                        Expression = new ColumnReferenceExpression{
                            MultiPartIdentifier = new MultiPartIdentifier{
                                Identifiers ={ new Identifier                           {
                          Value = "value2"
                      } }
                            }
                        },
                        SortOrder = SortOrder.Descending
                    }
                },
                Source = new ConstantScanNode
                {
                    Values =
                    {
                        new Entity {
                            ["value1"] = new SqlString("hello", CultureInfo.CurrentCulture.LCID, SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreNonSpace), ["value2"] = new SqlInt32(1), ["expectedorder"] = new SqlInt32(2)
                        },
                        new Entity {
                            ["value1"] = new SqlString("world", CultureInfo.CurrentCulture.LCID, SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreNonSpace), ["value2"] = new SqlInt32(2), ["expectedorder"] = new SqlInt32(3)
                        },
                        new Entity {
                            ["value1"] = new SqlString("Hello", CultureInfo.CurrentCulture.LCID, SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreNonSpace), ["value2"] = new SqlInt32(3), ["expectedorder"] = new SqlInt32(1)
                        }
                    },
                    Schema =
                    {
                        ["value1"]        = typeof(SqlString),
                        ["value2"]        = typeof(SqlInt32),
                        ["expectedorder"] = typeof(SqlInt32)
                    }
                }
            };

            var results = node.Execute(_dataSources, new StubOptions(), null, null)
                          .Select(e => e.GetAttributeValue <SqlInt32>("expectedorder").Value)
                          .ToArray();

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, results);
        }
Exemplo n.º 4
0
        public void Successfully_SortDescending()
        {
            var node = new SortNode(SortDirection.Descending)
            {
                Expression = _4d20
            };

            EvaluateNode(node, Data(SortConf), 4, "4d20.sortDesc() => 18 + 9 + 6 + 3 => 36");
        }
        /// <summary>
        /// Process sorting parameters on the queryable collection based on the query parameters sort nodes, created from the
        /// appropriate parameter processors.
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="queryParameters"></param>
        /// <returns></returns>
        private static IQueryable <T> ProcessSorting(IQueryable <T> queryable, QueryParameters queryParameters)
        {
            // If there is no sorting nodes set for any reason from the client
            // we will sort on the default initial property
            if (queryParameters.SortNodes.Count == 0)
            {
                // Get the first property name used in initial ordering
                var type = typeof(T);
                var firstPropertyName = type.GetProperties()[0].Name;

                // sort the queryable by the first property of the type
                queryable = queryable.OrderBy(x => firstPropertyName);
            }
            else
            {
                // Order the sort nodes based on priority. Bassically gives back the same order
                // because of no extra data beeing sent from client for priority.
                var orderedSortNodes = queryParameters.SortNodes.OrderBy(x => x.SortPriority);

                foreach (var orderedSortNode in orderedSortNodes)
                {
                    SortNode node = orderedSortNode;

                    var type         = typeof(T);
                    var sortProperty = type.GetProperties().FirstOrDefault(x => x.Name == node.SortTarget);

                    if (sortProperty == null)
                    {
                        // Try and see if there is a property from a processed sort target for rebound Model Properties using the _ splitter
                        var procSortTarget = node.SortTarget.Split('_')[0];

                        sortProperty = type.GetProperties().FirstOrDefault(x => x.Name == procSortTarget);

                        // if we still dont have a sort property we can continue
                        if (sortProperty == null)
                        {
                            continue;
                        }
                    }

                    // Apply the sorting on the target depending on the sort direction
                    if (orderedSortNode.SortDirection == SortDirection.Asc)
                    {
                        queryable = queryable.OrderBy(sortProperty.Name);
                    }
                    else
                    {
                        queryable = queryable.OrderBy(sortProperty.Name + " DESC");
                    }
                }
            }

            return(queryable);
        }
Exemplo n.º 6
0
        public static SortDefinition <T> OrderBy <T>(SortNode sort)
        {
            var propertyName = string.Join(".", sort.Path);

            if (sort.Order == SortOrder.Ascending)
            {
                return(Builders <T> .Sort.Ascending(propertyName));
            }
            else
            {
                return(Builders <T> .Sort.Descending(propertyName));
            }
        }
Exemplo n.º 7
0
        public static IEnumerable <T> FloodFill(T start, int maxCost = -1, int maxDepth = -1)
        {
            HashSet <T>     domain = new HashSet <T>();
            List <SortNode> open   = new List <SortNode>();

            open.Add(new SortNode()
            {
                Node = start, Level = 1
            });
            domain.Add(open[0].Node);

            //Profiler.BeginSample("GraphMethods.FloodFill");
            while (open.Count > 0)
            {
                open.Sort();
                SortNode current = open[0];
                open.RemoveAt(0);
                //domain.Add(current.Node);

                if (maxDepth == -1 || current.Level < maxDepth)
                {
                    foreach (T neighbor in current.Node.GetNeighbors())
                    {
                        if (domain.Contains(neighbor))
                        {
                            continue;
                        }

                        int tentativeGScore = current.GScore + current.Node.GetEdgeCost(neighbor);

                        if (maxCost == -1 || tentativeGScore < maxCost)
                        {
                            domain.Add(neighbor);
                            open.Add(new SortNode()
                            {
                                Node   = neighbor,
                                GScore = tentativeGScore,
                                FScore = tentativeGScore,
                                Level  = current.Level + 1
                            });
                        }
                    }
                }
            }
            //Profiler.EndSample();

            return(domain);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Извлечь значение
 /// </summary>
 /// <param name="ptr">Новая ссылка на корень ветви</param>
 /// <returns>"Минимальное значение"</returns>
 public Object Get(out SortNode ptr)
 {
     if (left == null)
     {
         ptr = null;
     }
     else if (right == null)
     {
         ptr = left;
     }
     else
     {
         ptr = (left.Value.CompareTo(right.Value) < 0) ? left.Put(right) : right.Put(left);
     }
     return(Value);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Добавить узел
 /// </summary>
 /// <param name="node">Добавляемый узел</param>
 /// <returns>Ссылка ветви</returns>
 public SortNode Put(SortNode node)
 {
     if (node.Value.CompareTo(Value) < 0)
     {
         return(node.Put(this));
     }
     else if (lcount <= rcount)
     {
         left = (left == null) ? node : left.Put(node); lcount = left.Count;
     }
     else
     {
         right = (right == null) ? node : right.Put(node); rcount = right.Count;
     }
     return(this);
 }
Exemplo n.º 10
0
        private static SortNode Split(IEnumerable <T> elements, T mediumElement)
        {
            var sortNode = new SortNode();

            foreach (var element in elements)
            {
                if (element.CompareTo(mediumElement) < 0)
                {
                    sortNode.SmallerValues.Add(element);
                }
                else
                {
                    sortNode.GreaterValues.Add(element);
                }
            }
            return(sortNode);
        }
Exemplo n.º 11
0
        public void Successfully_SortAscendingWithNestedMath()
        {
            var data   = Data(SortConf);
            var inner1 = new MathNode(MathOp.Add, Three, Two);
            var inner2 = new MathNode(MathOp.Add, Five, Four);
            var sub    = new MathNode(MathOp.Subtract, inner1, inner2);
            var inner3 = new MathNode(MathOp.Multiply, Six, One);
            var add    = new MathNode(MathOp.Add, sub, inner3);
            var group  = new GroupNode(null, new List <DiceAST> {
                add
            });
            var func = new FunctionNode(FunctionScope.Group, "expand", new DiceAST[0], data);

            func.Context.Expression = group;
            var node = new SortNode(SortDirection.Ascending)
            {
                Expression = func
            };

            EvaluateNode(node, data, 0, "{3 + 2 - (5 + 4) + 6 * 1}.expand().sortAsc() => (2 + 3 - (4 + 5) + 1 * 6) => 2");
        }
Exemplo n.º 12
0
        public static IEnumerable <T> FindPath(T start, T end, Heuristic heuristic)
        {
            HashSet <T>       visited  = new HashSet <T>();
            List <SortNode>   open     = new List <SortNode>();
            Dictionary <T, T> cameFrom = new Dictionary <T, T>();

            Stack <T> path = new Stack <T>();

            open.Add(new SortNode()
            {
                Node = start
            });

            while (open.Count > 0)
            {
                open.Sort();
                SortNode current = open[0];

                if (current.Node.IsMatch(end))
                {
                    T pathNode = current.Node;
                    path.Push(pathNode);
                    while (cameFrom.ContainsKey(pathNode))
                    {
                        pathNode = cameFrom[pathNode];
                        path.Push(pathNode);
                    }
                    break;
                }

                open.RemoveAt(0);

                foreach (T neighbor in current.Node.GetNeighbors())
                {
                    if (visited.Contains(neighbor))
                    {
                        continue;
                    }

                    // Calculate GScore by current node's accumulated cost plus the neighbor's
                    // entrance edge cost
                    int tentativeGScore = current.GScore + current.Node.GetEdgeCost(neighbor);

                    // If the recorded distance is less than the neighbor distance, replace it
                    SortNode oldNeighbor = open.Find(node => node.Node.Equals(neighbor));

                    if (oldNeighbor == null)
                    {
                        oldNeighbor = new SortNode()
                        {
                            Node   = neighbor,
                            GScore = Int32.MaxValue
                        };

                        open.Add(oldNeighbor);
                    }

                    if (tentativeGScore < oldNeighbor.GScore)
                    {
                        oldNeighbor.GScore         = tentativeGScore;
                        oldNeighbor.FScore         = tentativeGScore + heuristic(oldNeighbor.Node, end);
                        cameFrom[oldNeighbor.Node] = current.Node;
                    }
                }

                visited.Add(current.Node);
            }

            return(path);
        }