golang中的container/heap包使用
包 heap 為所有實現(xiàn)了 heap.Interface 的類型提供堆操作。 一個堆即是一棵樹, 這棵樹的每個節(jié)點的值都比它的子節(jié)點的值要小, 而整棵樹最小的值位于樹根(root), 也即是索引 0 的位置上。
包 heap 采樣接口的形式來滿足不同的類型的比較。
type Interface interface { sort.Interface Push(x any) // add x as element Len() Pop() any // remove and return element Len() - 1. } // sort.Interface type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) }
因此,你要比較的對象要先實現(xiàn)heap.Interface
簡單排序
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) } // 把最后一個彈出,因為最小的值已經(jīng)被換到了最后 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 HeapSort(data []int) []int { hp := &myHeap{} for _, v := range data { heap.Push(hp, v) } heap.Init(hp) res := make([]int, hp.Len()) for i := 0; hp.Len() > 0; i++ { res[i] = heap.Pop(hp).(int) } return res } // data = 6, 0, 1, 7, 9, 4, 3, 8, 2, 5 // hp 中存儲的是 0, 2, 1, 6, 5, 4, 3, 8, 7, 9
優(yōu)先隊列
使用一個 priority 字段來表示優(yōu)先級大小,使用 index 字段來表示索引。
type Item struct { value string priority int index int } type PriorityQueue []*Item func (pq PriorityQueue) Len() int { return len(pq) } func (pq PriorityQueue) Less(i, j int) bool { return pq[i].priority > pq[j].priority } func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] pq[i].index = i pq[j].index = j } func (pq *PriorityQueue) Push(x any) { n := len(*pq) item := x.(*Item) item.index = n *pq = append(*pq, item) } func (pq *PriorityQueue) Pop() any { old := *pq n := len(old) item := old[n-1] old[n-1] = nil // avoid memory leak item.index = -1 // for safety *pq = old[0 : n-1] return item } func (pq *PriorityQueue) update(item *Item, value string, priority int) { item.value = value item.priority = priority heap.Fix(pq, item.index) } func PriorityQueueTest() { items := map[string]int{ "banana": 3, "apple": 2, "pear": 4, } pq := make(PriorityQueue, len(items)) i := 0 for value, priority := range items { pq[i] = &Item{ value: value, priority: priority, index: i, } i++ } heap.Init(&pq) item := &Item{ value: "orange", priority: 1, } heap.Push(&pq, item) pq.update(item, item.value, 5) for pq.Len() > 0 { item := heap.Pop(&pq).(*Item) fmt.Printf("%.2d:%s ", item.priority, item.value) } }
按時間排序
type TimeSortedQueueItem struct { Time int64 Value interface{} } type TimeSortedQueue []*TimeSortedQueueItem func (q TimeSortedQueue) Len() int { return len(q) } func (q TimeSortedQueue) Less(i, j int) bool { return q[i].Time < q[j].Time } func (q TimeSortedQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] } func (q *TimeSortedQueue) Push(v interface{}) { *q = append(*q, v.(*TimeSortedQueueItem)) } func (q *TimeSortedQueue) Pop() interface{} { n := len(*q) item := (*q)[n-1] *q = (*q)[0 : n-1] return item } func NewTimeSortedQueue(items ...*TimeSortedQueueItem) *TimeSortedQueue { q := make(TimeSortedQueue, len(items)) for i, item := range items { q[i] = item } heap.Init(&q) return &q } func (q *TimeSortedQueue) PushItem(time int64, value interface{}) { heap.Push(q, &TimeSortedQueueItem{ Time: time, Value: value, }) } func (q *TimeSortedQueue) PopItem() interface{} { if q.Len() == 0 { return nil } return heap.Pop(q).(*TimeSortedQueueItem).Value }
到此這篇關于golang中的container/heap包使用的文章就介紹到這了,更多相關golang container/heap包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式
這篇文章主要介紹了Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Go語言調(diào)用SiliconFlow實現(xiàn)文本轉(zhuǎn)換為MP3格式
這篇文章主要為大家詳細介紹了Go語言如何調(diào)用?SiliconFlow?語音生成?API?的腳本,用于將文本轉(zhuǎn)換為?MP3?格式的語音文件,感興趣的小伙伴可以了解下2025-02-02