github.com/ttpreport/gvisor-ligolo@v0.0.0-20240123134145-a858404967ba/pkg/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 import "container/heap" 18 19 type segmentHeap []*segment 20 21 var _ heap.Interface = (*segmentHeap)(nil) 22 23 // Len returns the length of h. 24 func (h *segmentHeap) Len() int { 25 return len(*h) 26 } 27 28 // Less determines whether the i-th element of h is less than the j-th element. 29 func (h *segmentHeap) Less(i, j int) bool { 30 return (*h)[i].sequenceNumber.LessThan((*h)[j].sequenceNumber) 31 } 32 33 // Swap swaps the i-th and j-th elements of h. 34 func (h *segmentHeap) Swap(i, j int) { 35 (*h)[i], (*h)[j] = (*h)[j], (*h)[i] 36 } 37 38 // Push adds x as the last element of h. 39 func (h *segmentHeap) Push(x any) { 40 *h = append(*h, x.(*segment)) 41 } 42 43 // Pop removes the last element of h and returns it. 44 func (h *segmentHeap) Pop() any { 45 old := *h 46 n := len(old) 47 x := old[n-1] 48 old[n-1] = nil 49 *h = old[:n-1] 50 return x 51 }