github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/vector/heap.go (about)

     1  package vector
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/prometheus/prometheus/promql"
     7  )
     8  
     9  type HeapByMaxValue promql.Vector
    10  
    11  func (s HeapByMaxValue) Len() int {
    12  	return len(s)
    13  }
    14  
    15  func (s HeapByMaxValue) Less(i, j int) bool {
    16  	if math.IsNaN(s[i].V) {
    17  		return true
    18  	}
    19  	return s[i].V < s[j].V
    20  }
    21  
    22  func (s HeapByMaxValue) Swap(i, j int) {
    23  	s[i], s[j] = s[j], s[i]
    24  }
    25  
    26  func (s *HeapByMaxValue) Push(x interface{}) {
    27  	*s = append(*s, *(x.(*promql.Sample)))
    28  }
    29  
    30  func (s *HeapByMaxValue) Pop() interface{} {
    31  	old := *s
    32  	n := len(old)
    33  	el := old[n-1]
    34  	*s = old[0 : n-1]
    35  	return el
    36  }
    37  
    38  type HeapByMinValue promql.Vector
    39  
    40  func (s HeapByMinValue) Len() int {
    41  	return len(s)
    42  }
    43  
    44  func (s HeapByMinValue) Less(i, j int) bool {
    45  	if math.IsNaN(s[i].V) {
    46  		return true
    47  	}
    48  	return s[i].V > s[j].V
    49  }
    50  
    51  func (s HeapByMinValue) Swap(i, j int) {
    52  	s[i], s[j] = s[j], s[i]
    53  }
    54  
    55  func (s *HeapByMinValue) Push(x interface{}) {
    56  	*s = append(*s, *(x.(*promql.Sample)))
    57  }
    58  
    59  func (s *HeapByMinValue) Pop() interface{} {
    60  	old := *s
    61  	n := len(old)
    62  	el := old[n-1]
    63  	*s = old[0 : n-1]
    64  	return el
    65  }