/// <summary> /// Finds the closest contact in the network. Does several round trips to the network /// </summary> /// <param name="target">The target.</param> /// <returns>an IEnumerable<contact> in order of distance from the target. Can be cast into a GetClosestNodes.ClosestResults</contact></returns> public IEnumerable <Contact> GetClosestContacts(Identifier512 target, Func <Contact, bool> terminate = null) { MinMaxHeap <Contact> heap = new MinMaxHeap <Contact>(new ContactComparer(target), contacts.ClosestNodes(target).Take(RoutingTable.Configuration.LookupConcurrency)); HashSet <Identifier512> contacted = new HashSet <Identifier512>(); contacted.Add(RoutingTable.LocalIdentifier); int iterations = 0; HashSet <Contact> uniqueDiscoveries; do { iterations++; uniqueDiscoveries = new HashSet <Contact>( //hashet means we only get each result once heap //from the set of results we know about .Where(c => !contacted.Contains(c.Identifier)) //which we have not already contacted .SelectMany(c => { try { return(RemoteGetClosest(RoutingTable.LocalContact, c, target, RoutingTable.Configuration.LookupConcurrency, RoutingTable.Configuration.LookupTimeout)); } catch (TimeoutException) { return(null); } }) //select the closest ones they know about .Where(n => n != null) .Where(r => !heap.Contains(r))); //remove the results we already know about //Make the system aware of these potentially new nodes foreach (var c in uniqueDiscoveries) { contacts.Update(c); } //make sure we never contact these nodes again contacted.UnionWith(heap.Select(a => a.Identifier)); //add the new results heap.AddMany(uniqueDiscoveries); while (heap.Count > RoutingTable.Configuration.LookupConcurrency) { heap.RemoveMax(); } if (terminate != null) { if (uniqueDiscoveries.Where(a => terminate(a)).FirstOrDefault() != null) { break; } } }while (uniqueDiscoveries.Count != 0 && heap.Minimum.Identifier != target); return(new ClosestResults(heap, iterations)); //while (heap.Count > 0) // yield return heap.RemoveMin(); }
static void HeapTest() { MinMaxHeap<int> heapRemoveMin = new MinMaxHeap<int>(); heapRemoveMin.DebugCheckHeapProperty = true; MinMaxHeap<int> heapRemoveMax = new MinMaxHeap<int>(); heapRemoveMax.DebugCheckHeapProperty = true; List<int> doubleCheckMin = new List<int>(); List<int> doubleCheckMax = new List<int>(); Random r = new Random(); // Generate the list of numbers to populate the heaps and to check against if (true) { for (int i = 0; i < 700; i++) { int randInt = r.Next(-10000, 10000); doubleCheckMin.Add(randInt); } } else { // Manually test degenerate cases doubleCheckMin.Add(-55); doubleCheckMin.Add(31); doubleCheckMin.Add(-93); //doubleCheckMin.Add(64); } for (int i = 0; i < doubleCheckMin.Count; i++) { int randInt = doubleCheckMin[i]; // heap.Add("" + i, i); heapRemoveMin.Add(randInt, randInt); heapRemoveMax.Add(randInt, randInt); doubleCheckMax.Add(randInt); } doubleCheckMin.Sort(); // Default. Ascending doubleCheckMax.Sort(delegate (int x, int y) { if (x == y) return 0; if (x > y) return -1; if (x < y) return 1; return 0; }); Console.WriteLine(" -- NOW REMOVE MIN --"); int checkCount = 0; while (heapRemoveMin.Count > 0) { int min = heapRemoveMin.FindMin(); if (doubleCheckMin[checkCount] != min) { throw new Exception("WRONG!"); } heapRemoveMin.RemoveMin(); checkCount++; Console.WriteLine(min); } Console.WriteLine(" -- NOW REMOVE MAX --"); checkCount = 0; while (heapRemoveMax.Count > 0) { //Console.WriteLine("iteration " + checkCount); //Console.WriteLine(heapRemoveMax.PrintTree()); int max = heapRemoveMax.RemoveMax(); if (doubleCheckMax[checkCount] != max) { throw new Exception("WRONG!"); } checkCount++; Console.WriteLine(max); } }