Skip to content

Commit

Permalink
feat(KBucket): closest by contact ID
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Nov 22, 2018
1 parent 805e8b3 commit babdfe5
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/KBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,25 @@ public int Distance(byte[] a, byte[] b)
public IEnumerable<IContact> Closest(IContact contact)
{
Validate(contact);
return Closest(contact.Id);
}

/// <summary>
/// Gets the closest contacts to the provided contact.
/// </summary>
/// <param name="id">
/// The unique <see cref="IContact.Id"/> of a contact.
/// </param>
/// <returns>
/// An ordered sequence of contact, sorted by closeness.
/// </returns>
/// <remarks>
/// "Closest" is the XOR metric of the contact.
/// </remarks>
public IEnumerable<IContact> 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);
}
Expand Down

0 comments on commit babdfe5

Please sign in to comment.