golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.93k stars 17.66k forks source link

proposal: Go 2: #63406

Closed himanshu-patel-dev closed 1 year ago

himanshu-patel-dev commented 1 year ago

Author background

Related proposals

Proposal

Costs

type IntHeap []int

func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return (h)[i] < (h)[j] } func (h IntHeap) Swap(i, j int) { (h)[i], (h)[j] = (h)[j], (h)[i] }

func (h IntHeap) Push(x any) { h = append(*h, x.(int)) }

func (h IntHeap) Pop() any { n := h.Len() lastEle := (h)[n-1] h = (h)[0 : n-1] return lastEle }

// ----------------- quick use case -----------------

func main() { h := &IntHeap{2, 1, 5} heap.Init(h) heap.Push(h, 3) fmt.Println(heap.Pop(h)) }

seankhliao commented 1 year ago

At this point in time, we're not considering changes to the container packages until we have a way forward with iterators.