From babdfe547262f39ee451c6812257a66e2e10bac5 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Thu, 22 Nov 2018 22:29:08 +1300 Subject: [PATCH] feat(KBucket): closest by contact ID --- src/KBucket.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/KBucket.cs b/src/KBucket.cs index c35b52b..e15cc92 100644 --- a/src/KBucket.cs +++ b/src/KBucket.cs @@ -93,8 +93,25 @@ public int Distance(byte[] a, byte[] b) public IEnumerable Closest(IContact contact) { Validate(contact); + return Closest(contact.Id); + } + + /// + /// Gets the closest contacts to the provided contact. + /// + /// + /// The unique of a contact. + /// + /// + /// An ordered sequence of contact, sorted by closeness. + /// + /// + /// "Closest" is the XOR metric of the contact. + /// + public IEnumerable Closest(byte[] id) + { return this - .Select(c => new { distance = Distance(c.Id, contact.Id), contact = c }) + .Select(c => new { distance = Distance(c.Id, id), contact = c }) .OrderBy(a => a.distance) .Select(a => a.contact); }