github.com/wangyougui/gf/v2@v2.6.5/os/gtimer/gtimer_queue_heap.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gtimer 8 9 // Len is used to implement the interface of sort.Interface. 10 func (h *priorityQueueHeap) Len() int { 11 return len(h.array) 12 } 13 14 // Less is used to implement the interface of sort.Interface. 15 // The least one is placed to the top of the heap. 16 func (h *priorityQueueHeap) Less(i, j int) bool { 17 return h.array[i].priority < h.array[j].priority 18 } 19 20 // Swap is used to implement the interface of sort.Interface. 21 func (h *priorityQueueHeap) Swap(i, j int) { 22 if len(h.array) == 0 { 23 return 24 } 25 h.array[i], h.array[j] = h.array[j], h.array[i] 26 } 27 28 // Push pushes an item to the heap. 29 func (h *priorityQueueHeap) Push(x interface{}) { 30 h.array = append(h.array, x.(priorityQueueItem)) 31 } 32 33 // Pop retrieves, removes and returns the most high priority item from the heap. 34 func (h *priorityQueueHeap) Pop() interface{} { 35 length := len(h.array) 36 if length == 0 { 37 return nil 38 } 39 item := h.array[length-1] 40 h.array = h.array[0 : length-1] 41 return item 42 }