github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/container/heap/example_intheap_test.go (about)

     1  // Copyright 2012 The Go Authors. 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  // This example demonstrates an integer heap built using the heap interface.
     6  package heap_test
     7  
     8  import (
     9  	"github.com/shogo82148/std/container/heap"
    10  	"github.com/shogo82148/std/fmt"
    11  )
    12  
    13  // This example inserts several ints into an IntHeap, checks the minimum,
    14  // and removes them in order of priority.
    15  func Example_intHeap() {
    16  	h := &IntHeap{2, 1, 5}
    17  	heap.Init(h)
    18  	heap.Push(h, 3)
    19  	fmt.Printf("minimum: %d\n", (*h)[0])
    20  	for h.Len() > 0 {
    21  		fmt.Printf("%d ", heap.Pop(h))
    22  	}
    23  	// Output:
    24  	// minimum: 1
    25  	// 1 2 3 5
    26  }