public static void Main(string[] args) { var rnd = new Random(); while (true) { var generator = new RandomGenerator(rnd.Next(0, 1000000), rnd.Next(0, 1000000)); generator.Spikes = rnd.Next(0, 100); generator.Generate(); Console.Clear(); Console.WriteLine("Press any key to try another example, or Q to quit"); Console.WriteLine($"m:{generator.Array1.Count} n:{generator.Array2.Count} spikes:{generator.Spikes}"); var counter = new Counter(); IIntersecter intersecter = new StandardIntersecter(counter.Reset()); displayIntersect(intersecter, generator, "Standard search"); var count = intersecter.Result.Count; var comparisons = intersecter.Comparisons; displayIntersect(new BinarySearchIntersecter(counter.Reset()), generator, "Binary search", count, comparisons); displayIntersect(new MiddleOutIntersecter(counter.Reset()), generator, "Middle out", count, comparisons); displayIntersect(new AcceleratingIntersecter(counter.Reset()), generator, "Accelerating", count, comparisons); displayIntersect(new DivideEtImperaIntersecter(counter.Reset()), generator, "Divide at impera", count, comparisons); displayIntersect(new PairSearchIntersecter(counter.Reset()), generator, "Pair search", count, comparisons); displayIntersect(new SmartChoiceIntersecter(counter.Reset()), generator, "Smart choice", count, comparisons); if (Console.ReadKey().Key == ConsoleKey.Q) { break; } } }
protected override IList <int> DoIntersect(IList <int> arr1, IList <int> arr2) { IIntersecter intersecter; if (arr1.Count * Math.Ceiling(Math.Log(arr2.Count)) < arr1.Count + arr2.Count) { intersecter = new BinarySearchIntersecter(_counter); } else { intersecter = new StandardIntersecter(_counter); } intersecter.Intersect(arr1, arr2); return(intersecter.Result); }
public MiddleOutIntersecter(ICounter counter) : base(counter) { _intersecter1 = new ReverseIntersecter(counter); _intersecter2 = new StandardIntersecter(counter); }