github.com/status-im/status-go@v1.1.0/peers/topic_peer_queue.go (about)

     1  package peers
     2  
     3  import (
     4  	"container/heap"
     5  )
     6  
     7  type peerInfoItem struct {
     8  	*peerInfo
     9  	index int
    10  }
    11  
    12  type peerPriorityQueue []*peerInfoItem
    13  
    14  var _ heap.Interface = (*peerPriorityQueue)(nil)
    15  
    16  func (q peerPriorityQueue) Len() int { return len(q) }
    17  
    18  func (q peerPriorityQueue) Less(i, j int) bool {
    19  	return q[i].discoveredTime.After(q[j].discoveredTime)
    20  }
    21  
    22  func (q peerPriorityQueue) Swap(i, j int) {
    23  	q[i], q[j] = q[j], q[i]
    24  	q[i].index = i
    25  	q[j].index = j
    26  }
    27  
    28  func (q *peerPriorityQueue) Push(x interface{}) {
    29  	item := x.(*peerInfoItem)
    30  	item.index = len(*q)
    31  	*q = append(*q, item)
    32  }
    33  
    34  func (q *peerPriorityQueue) Pop() interface{} {
    35  	old := *q
    36  	n := len(old)
    37  	item := old[n-1]
    38  	item.index = -1
    39  	*q = old[0 : n-1]
    40  	return item
    41  }