/// <summary>
        /// Filters the results.
        /// </summary>
        /// <param name="addresses">The addresses.</param>
        /// <returns></returns>
        protected override AddressInformation[] FilterResults(AddressInformation[] addresses)
        {
            var results = addresses.Where(s => !s.Components.Any(c => c.Types.Contains(AddressPartsNames.NaturalFeature, StringComparer.InvariantCultureIgnoreCase)));

            if (this.IgnoreCloseMatches)
            {
                if (results.Any(r => r.Type == null || !Priorities.ContainsKey(r.Type)))
                {
                    return(addresses);
                }
                // Filter stuffs here.
                foreach (var address in results.ToList())
                {
                    // If there are more than one of the same type, then don't filter.
                    if (results.Count(r => Priorities[r.Type] == Priorities[address.Type]) > 1)
                    {
                        return(results.ToArray());
                    }
                }

                results = results.OrderByDescending(r => Priorities[r.Type]).Take(1);
            }

            return(results.ToArray());
        }
Exemplo n.º 2
0
 private int GetPriority(string operation)
 {
     if (Priorities.ContainsKey(operation))
     {
         return(Priorities[operation]);
     }
     if (Funcs.ContainsKey(operation))
     {
         return(int.MaxValue);
     }
     throw new Exception("Операция не найдена");
 }
Exemplo n.º 3
0
        public void Remove(DataType dt)
        {
            bool         found = false;
            PriorityType pt    = default(PriorityType);

            foreach (var List in Priorities)
            {
                if (List.Value.Contains(dt))
                {
                    if (List.Value.Remove(dt))
                    {
                        found = true;
                        if (List.Value.Count == 0)
                        {
                            pt = List.Key;
                        }
                        break;
                    }
                    else
                    {
                        throw new Exception("PQ Value Remove Failed");
                    }
                }
                else
                {
                    continue;
                }
            }
            if (!found)
            {
                throw new InvalidOperationException(
                          "Priority Queue Does Not Contain " + dt);
            }
            else
            {
                if (pt != null)
                {
                    if (!Priorities.ContainsKey(pt))
                    {
                        System.Console.WriteLine("PQ Priority Remove Will Fail");
                    }

                    if (!Priorities.Keys.ToArray( ).Contains(pt))
                    {
                        System.Console.WriteLine("PQ Priority Remove Will Fail");
                    }
                    if (!Priorities.Remove(pt))
                    {
                        throw new Exception("PQ Priority Remove Failed");
                    }
                }
            }
        }
Exemplo n.º 4
0
 private void Work(string token)
 {
     if (FuncsSignatures.ContainsKey(token))
     {
         OperationStack.Push(token);
     }
     else if (token == "(" || token == "[")
     {
         OperationStack.Push(token);
     }
     else if (token == ",")
     {
     }
     else if (token == ")" || token == "]")
     {
         while (OperationStack.Peek() != "[" && OperationStack.Peek() != "(")
         {
             Eval();
         }
         Eval();
     }
     else if (Priorities.ContainsKey(token))
     {
         while (OperationStack.Count > 0 && GetPriority(OperationStack.Peek()) >= GetPriority(token))
         {
             Eval();
         }
         OperationStack.Push(token);
     }
     else if (owner.Variables.ContainsKey(token))
     {
         OperandStack.Push(owner.Variables[token].Value);
     }
     else
     {
         OperandStack.Push(Parser.ParseTypes(token));
     }
 }
Exemplo n.º 5
0
        public List <string> ConvertToRPN(string str)
        {
            List <string> splittedString = GetSplittingPattern().Split(str.Replace(" ", "")).Where((e) => e != "").ToList();

            var postfixNotation = new List <string>();
            var operationsStack = new Stack <string>();

            for (int i = 0; i < splittedString.Count(); ++i)
            {
                if (Priorities.ContainsKey((Operator)splittedString[i][0]))
                {
                    if (splittedString[i][0] == (char)Operator.CloseBracket)
                    {
                        var isOpenBracketExist = false;

                        while (operationsStack.Count != 0)
                        {
                            if (operationsStack.Peek()[0] == (char)Operator.OpenBracket)
                            {
                                operationsStack.Pop();
                                isOpenBracketExist = true;
                                break;
                            }

                            postfixNotation.Add(operationsStack.Pop());
                        }

                        if (!isOpenBracketExist)
                        {
                            throw new Exception("Open bracket not found!");
                        }

                        continue;
                    }

                    if (i != 0 && i + 1 < splittedString.Count && splittedString[i][0] == (char)Operator.Minus &&
                        Priorities.ContainsKey((Operator)splittedString[i - 1][0]))
                    {
                        postfixNotation.Add(string.Concat(splittedString[i], splittedString[i + 1]));
                        i += 1;
                        continue;
                    }

                    while (operationsStack.Count != 0)
                    {
                        if (Priorities[(Operator)splittedString[i][0]] <= Priorities[(Operator)operationsStack.Peek()[0]] &&
                            splittedString[i][0] != (char)Operator.OpenBracket)
                        {
                            postfixNotation.Add(operationsStack.Pop());
                        }
                        else
                        {
                            break;
                        }
                    }

                    operationsStack.Push(splittedString[i]);
                }
                else
                {
                    postfixNotation.Add(splittedString[i]);
                }
            }

            while (operationsStack.Count != 0)
            {
                postfixNotation.Add(operationsStack.Pop());
            }

            return(postfixNotation);
        }
Exemplo n.º 6
0
        public object Calculate(string str, MatrixVM[] matrices)
        {
            List <string>  rpn   = ConvertToRPN(str);
            Stack <object> value = new Stack <object>();
            double         i;
            double         j;

            foreach (string elem in rpn)
            {
                if (elem.Length == 1 && Priorities.ContainsKey((Operator)elem[0]))
                {
                    object secondOperand = value.Pop();
                    object firstOperand  = value.Pop();
                    switch ((Operator)elem[0])
                    {
                    case Operator.Plus:
                        if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j))
                        {
                            value.Push((i + j).ToString());
                        }
                        else if (firstOperand is MatrixVM && Double.TryParse((string)secondOperand, out i))
                        {
                            value.Push((MatrixVM)firstOperand + i);
                        }
                        else if (firstOperand is MatrixVM && secondOperand is MatrixVM && IsMatricesConsistent((MatrixVM)firstOperand, (MatrixVM)secondOperand))
                        {
                            value.Push((MatrixVM)firstOperand + (MatrixVM)secondOperand);
                        }
                        break;

                    case Operator.Minus:
                        if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j))
                        {
                            value.Push((i - j).ToString());
                        }
                        else if (firstOperand is MatrixVM && Double.TryParse((string)secondOperand, out i))
                        {
                            value.Push((MatrixVM)firstOperand - i);
                        }
                        else if (firstOperand is MatrixVM && secondOperand is MatrixVM && IsMatricesConsistent((MatrixVM)firstOperand, (MatrixVM)secondOperand))
                        {
                            value.Push((MatrixVM)firstOperand - (MatrixVM)secondOperand);
                        }
                        break;

                    case Operator.Divide:
                        if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j))
                        {
                            value.Push((i / j).ToString());
                        }
                        break;

                    case Operator.Multiply:
                        if (firstOperand is string && secondOperand is string && Double.TryParse((string)firstOperand, out i) && Double.TryParse((string)secondOperand, out j))
                        {
                            value.Push((i * j).ToString());
                        }
                        else if (firstOperand is MatrixVM && secondOperand is string && Double.TryParse((string)secondOperand, out i))
                        {
                            value.Push((MatrixVM)firstOperand * i);
                        }
                        else if (firstOperand is MatrixVM && secondOperand is MatrixVM && ((MatrixVM)firstOperand).Columns == ((MatrixVM)secondOperand).Rows || ((MatrixVM)secondOperand).Rows == ((MatrixVM)firstOperand).Columns)
                        {
                            value.Push((MatrixVM)firstOperand * (MatrixVM)secondOperand);
                        }
                        break;
                    }
                }
                else
                {
                    MatrixVM matrix = matrices.FirstOrDefault((m) => m.Name == elem);
                    if (matrix != null)
                    {
                        value.Push(matrix);
                    }
                    else
                    {
                        value.Push(elem);
                    }
                }
            }

            if (value.Count == 0)
            {
                value.Push("Нет результата");
                return(value.Pop());
            }
            else
            {
                return(value.Pop());
            }
        }