github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/libkbfs/block_retrieval_heap.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkbfs
     6  
     7  import "container/heap"
     8  
     9  type blockRetrievalHeap []*blockRetrieval
    10  
    11  var _ heap.Interface = (*blockRetrievalHeap)(nil)
    12  
    13  // Heap methods: do not use directly
    14  func (brh blockRetrievalHeap) Less(i, j int) bool {
    15  	reqI := brh[i]
    16  	reqJ := brh[j]
    17  	if reqI.priority > reqJ.priority {
    18  		return true
    19  	}
    20  	if reqI.priority < reqJ.priority {
    21  		return false
    22  	}
    23  	return reqI.insertionOrder < reqJ.insertionOrder
    24  }
    25  
    26  func (brh blockRetrievalHeap) Len() int { return len(brh) }
    27  
    28  func (brh blockRetrievalHeap) Swap(i, j int) {
    29  	brh[i], brh[j] = brh[j], brh[i]
    30  	brh[i].index = i
    31  	brh[j].index = j
    32  }
    33  
    34  func (brh *blockRetrievalHeap) Push(item interface{}) {
    35  	n := len(*brh)
    36  	retrieval := item.(*blockRetrieval)
    37  	retrieval.index = n
    38  	*brh = append(*brh, retrieval)
    39  }
    40  
    41  func (brh *blockRetrievalHeap) Pop() interface{} {
    42  	old := *brh
    43  	n := len(old)
    44  	x := old[n-1]
    45  	x.index = -1
    46  	*brh = old[0 : n-1]
    47  	return x
    48  }