Exemplo n.º 1
0
        public void UnitRd_TryGetValueForUnfoundKeyString()
        {
#if TEST_BCL
            var sd = new SortedDictionary <string, int> (StringComparer.Ordinal);
#else
            var sd = new RankedDictionary <string, int> (StringComparer.Ordinal)
            {
                Capacity = 4
            };
#endif

            for (char c = 'A'; c <= 'Z'; ++c)
            {
                sd.Add(c.ToString(), (int)c);
            }

            bool result1 = sd.TryGetValue("M", out int val1);
            bool result2 = sd.TryGetValue("U", out int val2);
            bool result3 = sd.TryGetValue("$", out int val3);

            Assert.AreEqual(val1, 'M');
            Assert.AreEqual(val2, 'U');

            Assert.IsTrue(result1);
            Assert.IsTrue(result2);
            Assert.IsFalse(result3);
        }
Exemplo n.º 2
0
        static void Main()
        {
            #region Ctor0
            // Instantiate with four ISO 3166-1 country codes:
            var cc = new RankedDictionary <string, string>
            {
                { "TO", "Tonga" },
                { "DD", "German Democratic Republic" },
                { "CH", "Switzerland" },
                { "RU", "Burundi" }
            };
            #endregion

            #region Add
            // The Add method throws an exception if the new key is
            // already in the dictionary.
            try
            {
                cc.Add("DD", "East Germany");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with Key = 'DD' already exists.");
            }
            #endregion

            // The Item property is another name for the indexer,
            // so you can omit its name when accessing elements.
            Console.WriteLine($"For key = 'CH', value = {cc["CH"]}.");

            #region Indexer
            // The indexer can be used to change the value associated with a key.
            cc["RU"] = "Russian Federation";

            // The indexer can be used to get a value for a key.
            Console.WriteLine($"For key = 'RU', value = {cc["RU"]}.");

            // If a key does not exist, setting the indexer for that key
            // adds a new key/value pair.
            cc["SS"] = "South Sudan";
            #endregion

            // The indexer throws an exception if the supplied key is
            // not in the dictionary.
            try
            {
                Console.WriteLine($"For key = 'ZZ', value = {cc["ZZ"]}.");
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Key = 'ZZ' is not found.");
            }

            #region TryGetValue
            // When a program often has to try keys that are usually not in the
            // dictionary, TryGetValue can be a more efficient way to get values.
            if (cc.TryGetValue("ZZ", out string value))
            {
                Console.WriteLine($"For key = 'ZZ', value = {value}.");
            }
            else
            {
                Console.WriteLine("Key = 'ZZ' is not found.");
            }
            #endregion

            #region ContainsKey
            // ContainsKey can be used to test keys before inserting them.
            if (!cc.ContainsKey("GG"))
            {
                cc.Add("GG", "Guernsey");
                Console.WriteLine($"Value added for key = 'GG': {cc["GG"]}");
            }
            #endregion

            // When you use foreach to enumerate dictionary elements,
            // the elements are retrieved as KeyValuePair instances.
            Console.WriteLine();
            foreach (KeyValuePair <string, string> pair in cc)
            {
                Console.WriteLine($"Key = {pair.Key}, Value = {pair.Value}");
            }

            #region Values
            // To get the values alone, use the Values property.
            RankedDictionary <string, string> .ValueCollection vals = cc.Values;

            // The elements of the ValueCollection are strongly typed
            // with the type that was specified for dictionary values.
            Console.WriteLine();
            foreach (string val in vals)
            {
                Console.WriteLine($"Value = {val}");
            }
            #endregion

            #region Keys
            // To get the keys alone, use the Keys property.
            RankedDictionary <string, string> .KeyCollection keys = cc.Keys;

            // The elements of the KeyCollection are strongly typed
            // with the type that was specified for dictionary keys.
            Console.WriteLine();
            foreach (string key in keys)
            {
                Console.WriteLine($"Key = {key}");
            }
            #endregion

            // Use the Remove method to remove a key/value pair.
            Console.WriteLine("\nRemoving 'DD'.");
            cc.Remove("DD");

            Console.WriteLine("\nChecking if 'DD' exists:");
            if (!cc.ContainsKey("DD"))
            {
                Console.WriteLine("  Key 'DD' not found.");
            }
        }
Exemplo n.º 3
0
        public void ReadData()
        {
            var allNodes = new Dictionary <long, Node>();
            RankedDictionary <double, RankedDictionary <double, SeattleNode> > orderedNodes = new RankedDictionary <double, RankedDictionary <double, SeattleNode> >();

            var reader = XmlReader.Create(new FileStream(_sourceXml, FileMode.Open));
            HashSet <string> nodeNames = new HashSet <string>()
            {
                { "node" },
                { "way" },
            };

            foreach (var data in ElementsNamed(reader, nodeNames))
            {
                TagInfo tags = new TagInfo();
                switch (data.Name.LocalName)
                {
                case "node":
                    var id        = Convert.ToInt64(data.Attribute("id").Value);
                    var lat       = Convert.ToDouble(data.Attribute("lat").Value);
                    var longitude = Convert.ToDouble(data.Attribute("lon").Value);
                    foreach (var node in data.Descendants())
                    {
                        switch (node.Name.LocalName)
                        {
                        case "tag":
                            ReadTag(ref tags, node);
                            break;
                        }
                    }
                    Node newNode;
                    if (tags.houseNumber != null && tags.street != null)
                    {
                        RankedDictionary <double, SeattleNode> longNodes;
                        if (!orderedNodes.TryGetValue(lat, out longNodes))
                        {
                            longNodes = orderedNodes[lat] = new RankedDictionary <double, SeattleNode>();
                        }
                        allNodes[id] = newNode = longNodes[longitude] = new SeattleNode(lat, longitude, id, tags.name, tags.houseNumber, tags.street, tags.stories);
                    }
                    else
                    {
                        newNode = allNodes[id] = new Node(lat, longitude);
                    }

                    if (tags.crossing == CrossingType.Zebra)
                    {
                        var zebra = Ways[id] = new Way(
                            tags.name,
                            new[] { newNode },
                            tags.lanes,
                            tags.roadType,
                            tags.sidewalk,
                            tags.layer,
                            tags.crossing,
                            tags.surface,
                            tags.oneWay
                            );
                    }
                    else if (tags.roadType == RoadType.BusStop)
                    {
                        if (tags.name != null && (tags.shelter ?? false))
                        {
                            var streetNames = tags.name.Split('&');
                            if (streetNames.Length != 2)
                            {
                                Console.WriteLine("BS: {0}", tags.name);
                            }
                            var         normalized = NormalizeName(streetNames[0]);
                            List <Node> busNodes;
                            if (!BusStops.TryGetValue(normalized, out busNodes))
                            {
                                BusStops[normalized] = busNodes = new List <Node>();
                            }
                            busNodes.Add(newNode);
                        }
                    }
                    else if (tags.signType != SignType.None)
                    {
                        Signs[newNode] = tags.signType;
                    }
                    break;

                case "way":
                    List <Node> nodes = new List <Node>();
                    foreach (var node in data.Descendants())
                    {
                        switch (node.Name.LocalName)
                        {
                        case "nd":
                            nodes.Add(allNodes[Convert.ToInt64(node.Attribute("ref").Value)]);
                            break;

                        case "tag":
                            ReadTag(ref tags, node);
                            break;
                        }
                    }

                    RoofInfo roof = null;
                    if (tags.roofType != RoofType.None ||
                        tags.roofColor != null)
                    {
                        roof = new RoofInfo(tags.roofType, tags.roofColor, tags.roofDirection, tags.roofHeight, tags.roofMaterial, tags.roofOrientationAcross);
                    }
                    if (tags.building != BuildingType.None)
                    {
                        var buildingObj = Buildings[Convert.ToInt64(data.Attribute("id").Value)] = new Building(
                            tags.name,
                            tags.houseNumber,
                            tags.street,
                            tags.amenity,
                            tags.material,
                            roof,
                            nodes.ToArray()
                            );
                        buildingObj.Stories = tags.stories;
                        if (tags.houseNumber != null && tags.street != null)
                        {
                            BuildingsByAddress[tags.houseNumber + " " + tags.street] = buildingObj;
                        }

                        double maxLat = double.MinValue, minLat = double.MaxValue, maxLong = double.MinValue, minLong = double.MaxValue;
                        foreach (var point in nodes)
                        {
                            maxLat  = Math.Max(point.Lat, maxLat);
                            minLat  = Math.Min(point.Lat, minLat);
                            maxLong = Math.Max(point.Long, maxLong);
                            minLong = Math.Min(point.Long, minLong);
                        }

                        int itemsCount = 0;
                        foreach (var group in orderedNodes.ElementsBetween(minLat, maxLat))
                        {
                            foreach (var longAndNode in group.Value.ElementsBetween(minLong, maxLong))
                            {
                                var node = longAndNode.Value;
                                if (node.Lat >= minLat && node.Lat <= maxLat &&
                                    node.Long >= minLong && node.Long <= maxLong)
                                {
                                    var buildingFromPoint = Buildings[node.NodeNumber] = new Building(
                                        node.Name ?? tags.name,
                                        node.HouseNumber,
                                        node.Street,
                                        tags.amenity,
                                        tags.material,
                                        node.Lat,
                                        node.Long,
                                        roof,
                                        nodes.ToArray());
                                    buildingFromPoint.Stories = tags.stories;
                                    itemsCount++;
                                    Console.WriteLine("ByAddressNode: " + node.HouseNumber + " " + node.Street + " (" + itemsCount + ")");
                                    BuildingsByAddress[node.HouseNumber + " " + node.Street] = buildingFromPoint;
                                }
                            }
                        }
                    }
                    else if (tags.roadType != RoadType.None)
                    {
                        var road = Ways[Convert.ToInt64(data.Attribute("id").Value)] = new Way(
                            tags.name,
                            nodes.ToArray(),
                            tags.lanes,
                            tags.roadType,
                            tags.sidewalk,
                            tags.layer,
                            tags.crossing,
                            tags.surface,
                            tags.oneWay
                            );
                        foreach (var point in nodes)
                        {
                            List <Way> ways;
                            if (!RoadsByNode.TryGetValue(point, out ways))
                            {
                                RoadsByNode[point] = ways = new List <Way>(1);
                            }
                            ways.Add(road);
                        }
                        if (tags.name != null)
                        {
                            List <Way> roads;
                            if (!RoadsByName.TryGetValue(tags.name, out roads))
                            {
                                roads = RoadsByName[tags.name] = new List <Way>();
                            }
                            roads.Add(road);
                        }
                    }
                    else if (tags.barrier != BarrierKind.None)
                    {
                        Barriers.Add(new Barrier(nodes.ToArray(), tags.barrier, tags.wall));
                    }
                    break;
                }
            }
        }