github.com/FlowerWrong/netstack@v0.0.0-20191009141956-e5848263af28/tcpip/transport/tcp/segment_heap.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tcp
    16  
    17  type segmentHeap []*segment
    18  
    19  // Len returns the length of h.
    20  func (h segmentHeap) Len() int {
    21  	return len(h)
    22  }
    23  
    24  // Less determines whether the i-th element of h is less than the j-th element.
    25  func (h segmentHeap) Less(i, j int) bool {
    26  	return h[i].sequenceNumber.LessThan(h[j].sequenceNumber)
    27  }
    28  
    29  // Swap swaps the i-th and j-th elements of h.
    30  func (h segmentHeap) Swap(i, j int) {
    31  	h[i], h[j] = h[j], h[i]
    32  }
    33  
    34  // Push adds x as the last element of h.
    35  func (h *segmentHeap) Push(x interface{}) {
    36  	*h = append(*h, x.(*segment))
    37  }
    38  
    39  // Pop removes the last element of h and returns it.
    40  func (h *segmentHeap) Pop() interface{} {
    41  	old := *h
    42  	n := len(old)
    43  	x := old[n-1]
    44  	*h = old[:n-1]
    45  	return x
    46  }