public void SetValue()
 {
     IDictionary<string, int> Test = new Dictionary<string, int>();
     Test.Add("Q", 4);
     Test.Add("Z", 2);
     Test.Add("C", 3);
     Test.Add("A", 1);
     Assert.Equal(4, Test.GetValue("Q"));
     Test.SetValue("Q", 40);
     Assert.Equal(40, Test.GetValue("Q"));
 }
Example #2
0
    public static IDictionary<string, string> ParseAuthorizationHeader(string header) {
      while (header.StartsWith("OAuth")) header = header.Substring(5).Trim();

      var result = new Dictionary<string, string>();
      while (header.Length > 0) {
        var eq = header.IndexOf('=');
        if (eq < 0) eq = header.Length;
        var name = header.Substring(0, eq).Trim().Trim(',').Trim();

        var value = header = header.Substring((eq + 1).AtMost(header.Length)).Trim();

        if (value.StartsWith("\"")) {
          ProcessHeaderValue(1, ref header, ref value, '"');
        } else if (value.StartsWith("'")) {
          ProcessHeaderValue(1, ref header, ref value, '\'');
        } else {
          ProcessHeaderValue(0, ref header, ref value, ' ', ',');
        }

        result.SetValue(name, Uri.UnescapeDataString(value));
      }

      return result;
    }
Example #3
0
        /// <summary>
        /// Searches for shortest path in the graph. Uses Dijkstra 1-1 form (A*).
        /// </summary>
        /// <param name="from">Source vertex</param>
        /// <param name="to">Target vertex</param>
        /// <param name="keepInTheSameQuarter">Indicator whether the searching has to use only vertices from the same quarter as the from position is in</param>
        /// <returns>Set of vertices forming result path</returns>
        public static IEnumerable<PathGraphVertex> FindShortestPath(PathGraphVertex from, PathGraphVertex to, bool keepInTheSameQuarter)
        {
            keepInTheSameQuarter = keepInTheSameQuarter && from.Position.Quarter == to.Position.Quarter;
            TownQuarter fromQuarter = from.Position.Quarter;

            LinkedList<PathGraphVertex> resultPath = new LinkedList<PathGraphVertex>();
            HashSet<PathGraphVertex> closed = new HashSet<PathGraphVertex>();
            Dictionary<PathGraphVertex, PathGraphVertex> cameFrom = new Dictionary<PathGraphVertex, PathGraphVertex>();
            Dictionary<PathGraphVertex, float> gScore = new Dictionary<PathGraphVertex, float>();
            gScore.Add(from, 0);
            Dictionary<PathGraphVertex, float> fScore = new Dictionary<PathGraphVertex, float>();
            fScore.Add(from, gScore[from] + HeuristicDistance(from, to));
            HashSet<PathGraphVertex> open = new HashSet<PathGraphVertex>();
            open.Add( from);

            while (open.Count != 0)
            {
                float lowestScore = float.MaxValue;
                PathGraphVertex lowestVertex = null;
                foreach (PathGraphVertex openedOne in open)
                {
                    float score = fScore[openedOne];
                    if (score < lowestScore)
                    {
                        lowestVertex = openedOne;
                    }
                }
                PathGraphVertex current = lowestVertex;//open.OrderBy(x => fScore[x]).First();
                if (current == to)
                {
                    PathGraphVertex t = to;
                    while(t != from)
                    {
                        resultPath.AddFirst(t);
                        t = cameFrom[t];
                    }
                    resultPath.AddFirst(from);
                    return resultPath;
                }
                open.Remove(current);

                closed.Add(current);
                foreach (PathGraphVertex n in current.Neighbors)
                {
                    if (closed.Contains(n) || (keepInTheSameQuarter && n.Position.Quarter != fromQuarter))
                    {
                        continue;
                    }
                    else
                    {
                        float tempGSore = gScore[current] + current.GetDistanceToNeighbor(n);
                        if (!open.Contains(n) || tempGSore <= gScore[n])
                        {
                            cameFrom.SetValue(n, current);
                            gScore.SetValue(n, tempGSore);
                            fScore.SetValue(n, gScore[n] + HeuristicDistance(current, n));
                            if (!open.Contains(n))
                            {
                                open.Add(n);
                            }
                        }
                    }
                }
            }

            throw new PathNotFoundException("Source and target vertices aren't in the same component.");
        }
		public void Extensions_SetValue_Dictionary_Works() {
			var dict = new Dictionary<int, int>();
			dict.SetValue(23, 456);
			Assert.AreEqual(456, dict[23]);
		}