public void CompareLettersTestB() { var a = new SwedishCarPlate { Letters = "FDS" }; var b = new SwedishCarPlate { Letters = "FSD" }; var actual = SwedishCarPlate.CompareLetters(a, b); var expected = -1; Assert.AreEqual(expected, actual); }
static void ProcessCarplates() { var plates = SwedishCarPlate.GenerateCarPlates((int)Math.Pow(2, 13)); //var plates = SwedishCarPlate.GenerateCarPlates(5); //foreach (var item in plates) //{ // Console.WriteLine(item.CarPlate); //} //SortAlgorithm.BubbleSort<SwedishCarPlate>(plates, SwedishCarPlate.IsLettersOfALessThanB); //var letterSorted = (SwedishCarPlate[]) plates.Clone(); //foreach (var item in letterSorted) //{ // Console.WriteLine(item.CarPlate); //} SortAlgorithm.BubbleSort <SwedishCarPlate>(plates, SwedishCarPlate.IsNumbersOfALessThanB); //var numberSorted = (SwedishCarPlate[])plates.Clone(); //foreach (var item in numberSorted) //{ // Console.WriteLine(item.CarPlate); //} //Console.Write("Search Letters:"); //var input = Console.ReadLine(); //var position = SearchAlgorithms.BinarySearch<SwedishCarPlate>(letterSorted, new SwedishCarPlate { Letters = input }, SwedishCarPlate.CompareLetters); //Console.WriteLine("Position: " + position); Console.Write("Search Numbers:"); var input = Console.ReadLine(); var position = SearchAlgorithms.BinarySearch <SwedishCarPlate>(plates, new SwedishCarPlate { Numbers = input }, SwedishCarPlate.CompareNumbers); if (position > -1) { var similarPlates = SearchAlgorithms.RegionGrow <SwedishCarPlate>(plates, position, (a, b) => SwedishCarPlate.CompareNumbers(a, b) == 0); Console.WriteLine("Position: " + position + " -> " + plates[position].CarPlate); Console.WriteLine(String.Join(", ", similarPlates.Select(a => a.CarPlate))); } }
static void CarPlateBenchmark(int repeats) { for (int i = 10; i <= 15; i++) { var plates = SwedishCarPlate.GenerateCarPlates((int)Math.Pow(2, i)); var dict = plates.GroupBy(p => p.Letters, p => p).ToDictionary(g => g.Key, g => g.ToList()); //foreach (var item in dict) //{ // Console.WriteLine(item.Key + ": " + string.Join(",", item.Value.Select(p => p.CarPlate))); //} Stopwatch w = Stopwatch.StartNew(); for (int j = 0; j < repeats; j++) { dict.ContainsKey(SwedishCarPlate.GetRandomLetters()); } w.Stop(); Console.WriteLine($"Time to search in Dictionary {repeats} times: {w.ElapsedMilliseconds} ms"); SortAlgorithm.BubbleSort <SwedishCarPlate>(plates, SwedishCarPlate.IsLettersOfALessThanB); w.Restart(); for (int j = 0; j < repeats; j++) { var plate = new SwedishCarPlate { Letters = SwedishCarPlate.GetRandomLetters() }; //Console.WriteLine("search for: " + plate.CarPlate); var pos = SearchAlgorithms.BinarySearch <SwedishCarPlate>(plates, plate, SwedishCarPlate.CompareLetters); //if (pos >= 0) //{ // var result = SearchAlgorithms.RegionGrow<SwedishCarPlate>(plates, pos, (a, b) => SwedishCarPlate.CompareLetters(a, b) == 0); //} } w.Stop(); Console.WriteLine($"Time to binarySearch {repeats} times: {w.ElapsedMilliseconds} ms"); } }