sorting.go 1.41 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 16 17 18 19 20 21 22 23 24 25 26 27
)

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

// peerSorterArr implements sort.Interface to sort peers by xor distance
type peerSorterArr []*peerDistance

func (p peerSorterArr) Len() int      { return len(p) }
func (p peerSorterArr) Swap(a, b int) { p[a], p[b] = p[b], p[a] }
func (p peerSorterArr) Less(a, b int) bool {
	return p[a].distance.less(p[b].distance)
}

//

func copyPeersFromList(target ID, peerArr peerSorterArr, peerList *list.List) peerSorterArr {
28
	if cap(peerArr) < len(peerArr)+peerList.Len() {
29
		newArr := make(peerSorterArr, len(peerArr), len(peerArr)+peerList.Len())
30 31 32
		copy(newArr, peerArr)
		peerArr = newArr
	}
33 34 35 36 37 38 39 40 41 42 43 44 45
	for e := peerList.Front(); e != nil; e = e.Next() {
		p := e.Value.(peer.ID)
		pID := ConvertPeerID(p)
		pd := peerDistance{
			p:        p,
			distance: xor(target, pID),
		}
		peerArr = append(peerArr, &pd)
	}
	return peerArr
}

func SortClosestPeers(peers []peer.ID, target ID) []peer.ID {
46
	psarr := make(peerSorterArr, 0, len(peers))
47 48 49 50 51 52 53 54 55
	for _, p := range peers {
		pID := ConvertPeerID(p)
		pd := &peerDistance{
			p:        p,
			distance: xor(target, pID),
		}
		psarr = append(psarr, pd)
	}
	sort.Sort(psarr)
56
	out := make([]peer.ID, 0, len(psarr))
57 58 59 60 61
	for _, p := range psarr {
		out = append(out, p.p)
	}
	return out
}