public void UnitRdx_ElementsBetweenA() { var rd = new RankedDictionary <string, int>(); var pc = (ICollection <KeyValuePair <string, int> >)rd; rd.Add("Alpha", 1); rd.Add("Beta", 2); rd.Add("Omega", 24); pc.Add(new KeyValuePair <string, int> (null, 0)); int actual = 0; foreach (var kv in rd.ElementsBetween(null, "C")) { ++actual; } Assert.AreEqual(3, actual); }
public void UnitRdx_ElementsBetweenPassedEnd() { var rd = new RankedDictionary <int, int>(); for (int i = 0; i < 1000; ++i) { rd.Add(i, -i); } int iterations = 0; int sumVals = 0; foreach (KeyValuePair <int, int> e in rd.ElementsBetween(500, 1500)) { ++iterations; sumVals += e.Value; } Assert.AreEqual(500, iterations); Assert.AreEqual(-374750, sumVals, "Sum of values not correct"); }
public void UnitRdx_ElementsBetweenB() { var rd = new RankedDictionary <int, int> { Capacity = 4 }; for (int i = 90; i >= 0; i -= 10) { rd.Add(i, -100 - i); } int iterations = 0; int sumVals = 0; foreach (var kv in rd.ElementsBetween(35, 55)) { ++iterations; sumVals += kv.Value; } Assert.AreEqual(2, iterations); Assert.AreEqual(-290, sumVals); }
static void Main() { var towns = new RankedDictionary <string, int>(); // Load sample data. towns.Add("Albany", 43600); towns.Add("Bandon", 2960); towns.Add("Corvallis", 54462); towns.Add("Damascus", 10539); towns.Add("Elkton", 195); towns.Add("Florence", 8466); towns.Add("Glide", 1795); towns.Add("Jacksonville", 2235); towns.Add("Lebanon", 13140); towns.Add("Lookingglass", 855); towns.Add("Medford", 75180); towns.Add("Powers", 689); towns.Add("Riddle", 1020); towns.Add("Roseburg", 20480); towns.Add("Scio", 710); towns.Add("Talent", 6066); towns.Add("Umatilla", 6906); towns.Add("Winston", 5379); towns.Add("Yamhill", 820); // Here's a typical LINQ-To-Objects operation. double avg = towns.Average(x => x.Value); Console.WriteLine($"Average population of all towns = {avg:f0}"); // Lambda expression IEnumerable <KeyValuePair <string, int> > r1 = towns.Where(t => t.Key.CompareTo("E") < 0); Console.WriteLine("\nTowns A-D:"); foreach (KeyValuePair <string, int> e in r1) { Console.WriteLine(e.Key); } // LINQ range: O(n) IEnumerable <KeyValuePair <string, int> > r2 = towns.SkipWhile(t => t.Key.CompareTo("E") < 0).TakeWhile(t => t.Key.CompareTo("J") < 0); Console.WriteLine("\nTowns E-G:"); foreach (KeyValuePair <string, int> e in r2) { Console.WriteLine(e.Key); } // // Use the ElementsBetween iterator to query range. // Unlike LINQ SkipWhile and TakeWhile, this will perform an optimized (partial scan) lookup. // // BtreeDictionary range operator: O(log n) IEnumerable <KeyValuePair <string, int> > r3 = towns.ElementsBetween("K", "M"); Console.WriteLine("\nTowns K-L:"); foreach (KeyValuePair <string, int> town in r3) { Console.WriteLine(town.Key); } // Range operator without upper limit: O(log n) IEnumerable <KeyValuePair <string, int> > r4 = towns.ElementsFrom("M"); Console.WriteLine("\nTowns M-R:"); foreach (KeyValuePair <string, int> town in r4) { // This avoids the issue in the last example where a town named "M" would be included. if (town.Key.CompareTo("S") >= 0) { break; } else { Console.WriteLine(town.Key); } } // Range operator without upper limit: O(log n) IEnumerable <KeyValuePair <string, int> > r5 = towns.ElementsFrom("T"); Console.WriteLine("\nTowns T-Z:"); foreach (KeyValuePair <string, int> town in r5) { Console.WriteLine(town.Key); } }
static void Main() { int reps = 5000000; Console.WriteLine("LINQ's Last extension method will enumerate over the *entire* collection giving"); Console.WriteLine("a time complexity of O(n) regardless of the data structure. This is due to the"); Console.WriteLine("\"one size fits all\" approach of LINQ. SortedDictionary supplies no optimized"); Console.WriteLine("implementation of Last. Also, SortedDictionary does not supply any optimized"); Console.WriteLine("way of performing queries based on a key range. Again, the time complexity of"); Console.WriteLine("such an operation is O(n).\n"); var sd = new SortedDictionary <int, int>(); Console.Write("Loading SortedDictionary with " + reps + " elements:\n\nLoad time = "); Stopwatch watch1 = new Stopwatch(); watch1.Reset(); watch1.Start(); for (int i = 0; i < reps; ++i) { sd.Add(i, -i); } var time11 = watch1.ElapsedMilliseconds; var last1 = sd.Last(); var time12 = watch1.ElapsedMilliseconds; Console.WriteLine(time11 + "ms"); Console.WriteLine("Last time = " + (time12 - time11) + "ms"); //// Console.WriteLine("\nRankedDictionary has its own implementation of Last() which does not suffer the"); Console.WriteLine("performance hit that SortedDictionary.Last() does. RankedDictionary also"); Console.WriteLine("supports optimized range queries with its ElementsFrom(TKey) and"); Console.WriteLine("ElementsBetween(TKey,TKey) enumerators.\n"); var bt = new RankedDictionary <int, int>(); Console.Write("Loading RankedDictionary with " + reps + " elements:\n\nLoad time = "); Stopwatch watch2 = new Stopwatch(); watch2.Reset(); watch2.Start(); for (int i = 0; i < reps; ++i) { bt.Add(i, -i); } var time21 = watch2.ElapsedMilliseconds; var lastKV = bt.Last(); var time22 = watch2.ElapsedMilliseconds; // Range query: Sum the middle 100 values. var rangeVals = bt.ElementsBetween(reps / 2 - 50, reps / 2 + 50).Sum(x => x.Value); var time23 = watch2.ElapsedMilliseconds; Console.WriteLine(time21 + "ms"); Console.WriteLine("Last time = " + (time22 - time21) + "ms"); Console.WriteLine("Range time = " + (time23 - time22) + "ms"); #if DEBUG bt.SanityCheck(); Console.WriteLine(); Console.Write("---- height = " + bt.GetHeight()); Console.Write(", branch fill = " + bt.BranchSlotsUsed * 100 / bt.BranchSlotCount + "%"); Console.WriteLine(", leaf fill = " + bt.LeafSlotsUsed * 100 / bt.LeafSlotCount + "% ----"); #endif }
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; } } }