sorting.go 1.78 KB
Newer Older
1 2 3 4 5
package kbucket

import (
	"container/list"
	"sort"
Jeromy's avatar
Jeromy committed
6 7

	peer "github.com/libp2p/go-libp2p-peer"
8 9 10 11 12 13 14 15
)

// A helper struct to sort peers by their distance to the local node
type peerDistance struct {
	p        peer.ID
	distance ID
}

16 17 18 19 20
// peerDistanceSorter implements sort.Interface to sort peers by xor distance
type peerDistanceSorter struct {
	peers  []peerDistance
	target ID
}
21

22 23 24 25
func (pds peerDistanceSorter) Len() int      { return len(pds.peers) }
func (pds peerDistanceSorter) Swap(a, b int) { pds.peers[a], pds.peers[b] = pds.peers[b], pds.peers[a] }
func (pds peerDistanceSorter) Less(a, b int) bool {
	return pds.peers[a].distance.less(pds.peers[b].distance)
26 27
}

28 29 30 31 32 33 34 35
// Append the peer.ID to the sorter's slice. It may no longer be sorted.
func (pds peerDistanceSorter) appendPeer(p peer.ID) peerDistanceSorter {
	pds.peers = append(pds.peers, peerDistance{
		p:        p,
		distance: xor(pds.target, ConvertPeerID(p)),
	})
	return pds
}
36

37 38 39 40 41
// Append the peer.ID values in the list to the sorter's slice. It may no longer be sorted.
func (pds peerDistanceSorter) appendPeersFromList(l *list.List) peerDistanceSorter {
	startLen := pds.Len()
	for e := l.Front(); e != nil; e = e.Next() {
		pds = pds.appendPeer(e.Value.(peer.ID))
42
	}
43 44
	if pds.Len() != startLen+l.Len() {
		panic("len did not increase")
45
	}
46
	return pds
47 48
}

49 50 51 52 53
func (pds peerDistanceSorter) sort() {
	sort.Sort(pds)
}

// Sort the given peers by their ascending distance from the target. A new slice is returned.
54
func SortClosestPeers(peers []peer.ID, target ID) []peer.ID {
55 56 57 58
	sorter := peerDistanceSorter{
		peers:  make([]peerDistance, 0, len(peers)),
		target: target,
	}
59
	for _, p := range peers {
60
		sorter = sorter.appendPeer(p)
61
	}
62 63 64
	sorter.sort()
	out := make([]peer.ID, 0, sorter.Len())
	for _, p := range sorter.peers {
65 66 67 68
		out = append(out, p.p)
	}
	return out
}