github.com/searKing/golang/go@v1.2.74/exp/slices/heap_test.go (about) 1 // Copyright 2023 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slices_test 6 7 import ( 8 "container/heap" 9 "fmt" 10 11 "github.com/searKing/golang/go/exp/slices" 12 ) 13 14 // This example inserts several ints into an MinHeap, checks the minimum, 15 // and removes them in order of priority. 16 func Example_minHeap() { 17 h := &slices.MinHeap[int]{2, 1, 5} 18 heap.Init(h) 19 heap.Push(h, 3) 20 fmt.Printf("minimum: %d\n", (*h)[0]) 21 for h.Len() > 0 { 22 fmt.Printf("%d ", heap.Pop(h)) 23 } 24 25 // Output: 26 // minimum: 1 27 // 1 2 3 5 28 } 29 30 // This example inserts several ints into an MaxHeap, checks the maximum, 31 // and removes them in order of priority. 32 func Example_maxHeap() { 33 h := &slices.MaxHeap[int]{2, 1, 5} 34 heap.Init(h) 35 heap.Push(h, 3) 36 fmt.Printf("maximum: %d\n", (*h)[0]) 37 for h.Len() > 0 { 38 fmt.Printf("%d ", heap.Pop(h)) 39 } 40 41 // Output: 42 // maximum: 5 43 // 5 3 2 1 44 }