3 import "github.com/google/btree"
5 // Peers are stored with their priority at insertion. Their priority may
6 // change if our apparent IP changes, we don't currently handle that.
7 type prioritizedPeersItem struct {
12 func (me prioritizedPeersItem) Less(than btree.Item) bool {
13 return me.prio < than.(prioritizedPeersItem).prio
16 type prioritizedPeers struct {
18 getPrio func(Peer) peerPriority
21 func (me *prioritizedPeers) Each(f func(Peer)) {
22 me.om.Ascend(func(i btree.Item) bool {
23 f(i.(prioritizedPeersItem).p)
28 func (me *prioritizedPeers) Len() int {
32 // Returns true if a peer is replaced.
33 func (me *prioritizedPeers) Add(p Peer) bool {
34 return me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) != nil
37 func (me *prioritizedPeers) DeleteMin() (ret prioritizedPeersItem, ok bool) {
38 i := me.om.DeleteMin()
42 ret = i.(prioritizedPeersItem)
47 func (me *prioritizedPeers) PopMax() Peer {
48 return me.om.DeleteMax().(prioritizedPeersItem).p