2014-06-24 21:20:49 +08:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nodeMaxHeap struct {
|
|
|
|
IDs []string
|
|
|
|
Target string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me nodeMaxHeap) Len() int { return len(me.IDs) }
|
|
|
|
|
|
|
|
func (me nodeMaxHeap) Less(i, j int) bool {
|
2014-11-17 15:47:24 +08:00
|
|
|
return idDistance(me.IDs[i], me.Target).Cmp(idDistance(me.IDs[j], me.Target)) > 0
|
2014-06-24 21:20:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *nodeMaxHeap) Pop() (ret interface{}) {
|
|
|
|
ret, me.IDs = me.IDs[len(me.IDs)-1], me.IDs[:len(me.IDs)-1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func (me *nodeMaxHeap) Push(val interface{}) {
|
|
|
|
me.IDs = append(me.IDs, val.(string))
|
|
|
|
}
|
|
|
|
func (me nodeMaxHeap) Swap(i, j int) {
|
2014-07-10 00:59:37 +08:00
|
|
|
me.IDs[i], me.IDs[j] = me.IDs[j], me.IDs[i]
|
2014-06-24 21:20:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type closestNodesSelector struct {
|
|
|
|
closest nodeMaxHeap
|
|
|
|
k int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *closestNodesSelector) Push(id string) {
|
|
|
|
heap.Push(&me.closest, id)
|
|
|
|
if me.closest.Len() > me.k {
|
|
|
|
heap.Pop(&me.closest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *closestNodesSelector) IDs() []string {
|
|
|
|
return me.closest.IDs
|
|
|
|
}
|
|
|
|
|
|
|
|
func newKClosestNodesSelector(k int, targetID string) (ret closestNodesSelector) {
|
|
|
|
ret.k = k
|
|
|
|
ret.closest.Target = targetID
|
|
|
|
return
|
|
|
|
}
|