You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
607 B
41 lines
607 B
package timer |
|
|
|
import ( |
|
"container/heap" |
|
"fmt" |
|
"testing" |
|
) |
|
|
|
type myHeap []int |
|
|
|
func (h *myHeap) Less(i, j int) bool { |
|
return (*h)[i] < (*h)[j] |
|
} |
|
|
|
func (h *myHeap) Swap(i, j int) { |
|
(*h)[i], (*h)[j] = (*h)[j], (*h)[i] |
|
} |
|
|
|
func (h *myHeap) Len() int { |
|
return len(*h) |
|
} |
|
|
|
func (h *myHeap) Pop() (v any) { |
|
*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1] |
|
return |
|
} |
|
|
|
func (h *myHeap) Push(v any) { |
|
*h = append(*h, v.(int)) |
|
} |
|
|
|
func TestMyHelp(t *testing.T) { |
|
h := new(myHeap) |
|
for _, v := range []int{5, 3, 8, 1, 2, 7} { |
|
heap.Push(h, v) |
|
} |
|
fmt.Println(h) |
|
for range len(*h) { |
|
fmt.Println(heap.Pop(h), h) |
|
} |
|
}
|
|
|