private void AddToListInOrder(List <ItemWithDistances> list, ItemWithDistances newItem) { Int32 index = list.FindIndex(item => item.MinDistance > newItem.MinDistance); if (index >= 0) { list.Insert(index, newItem); } else { list.Add(newItem); } }
private void FetchNext() { if (this.finished || this.yieldedCount >= this.searchQuery.limit) { this.finished = true; return; } while (this.pendingList.Count > 0 || this.nearestList.Count > 0) { if (this.PrepareNextNearest()) { return; } ItemWithDistances pending = this.pendingList[0]; this.pendingList.RemoveAt(0); Node node = pending.Item; foreach (Node child in node.Children.Values) { if (Math.Abs(pending.Distance - child.DistanceFromParent) - child.Radius <= this.searchQuery.range) { Double childDistance = this.searchQuery.tree.distanceMetric.Invoke(this.searchQuery.data, child.Data); Double childMinDistance = Math.Max(childDistance - child.Radius, 0.0D); if (childMinDistance <= this.searchQuery.range) { if (child.IsEntry) { this.AddToListInOrder(this.nearestList, new ItemWithDistances(child, childDistance, childMinDistance)); } else { this.AddToListInOrder(this.pendingList, new ItemWithDistances(child, childDistance, childMinDistance)); } } } } if (this.pendingList.Count == 0) { this.nextPendingMinDistance = Double.PositiveInfinity; } else { this.nextPendingMinDistance = this.pendingList[0].MinDistance; } } this.finished = true; }
private Boolean PrepareNextNearest() { if (this.nearestList.Count > 0) { ItemWithDistances nextNearest = this.nearestList[0]; if (nextNearest.Distance <= this.nextPendingMinDistance) { this.nearestList.RemoveAt(0); this.nextResultItem = new ResultItem <T>(nextNearest.Item.Data, nextNearest.Distance); this.yieldedCount++; return(true); } } return(false); }