欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang中的container/heap包使用

 更新時間:2025年02月20日 09:59:05   作者:raoxiaoya  
Golang中的container/heap包提供堆操作,適用于實現(xiàn)了heap.Interface的類型,本文主要介紹了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中Append()使用實例詳解

    Golang中Append()使用實例詳解

    今天在刷leetcode的時候,第113題讓我遇到了一個Go語言中append函數(shù)的一個坑,所以復習下,這篇文章主要給大家介紹了關于Golang中Append()使用的相關資料,需要的朋友可以參考下
    2023-01-01
  • Go語言學習技巧之如何合理使用Pool

    Go語言學習技巧之如何合理使用Pool

    這篇文章主要給大家介紹了關于Go語言學習技巧之如何合理使用Pool的相關資料,Pool用于存儲那些被分配了但是沒有被使用,而未來可能會使用的值,以減小垃圾回收的壓力。文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。
    2017-12-12
  • Golang?WaitGroup?底層原理及源碼解析

    Golang?WaitGroup?底層原理及源碼解析

    WaitGroup?是?Golang?中最常見的并發(fā)控制技術之一,它的作用我們可以簡單類比為其他語言中多線程并發(fā)控制中的?join(),這篇文章主要介紹了Golang?WaitGroup?底層原理及源碼詳解,需要的朋友可以參考下
    2023-04-04
  • Golang庫插件注冊加載機制的問題

    Golang庫插件注冊加載機制的問題

    這篇文章主要介紹了Golang庫插件注冊加載機制,這里說的插件并不是指的golang原生的可以在buildmode中加載指定so文件的那種加載機制,需要的朋友可以參考下
    2022-03-03
  • GoLand編寫 TCP 端口掃描器的詳細過程

    GoLand編寫 TCP 端口掃描器的詳細過程

    TCP,也就是傳輸控制協(xié)議(Transmission Control Protocol),這篇文章主要介紹了Go語言(Golang)編寫 TCP 端口掃描器,需要的朋友可以參考下
    2023-05-05
  • 解決go 生成的exe不在bin文件夾里的問題

    解決go 生成的exe不在bin文件夾里的問題

    這篇文章主要介紹了解決go 生成的exe不在bin文件夾里的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式

    Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式

    這篇文章主要介紹了Golang IPv4 字符串與整數(shù)互轉(zhuǎn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Go語言調(diào)用SiliconFlow實現(xiàn)文本轉(zhuǎn)換為MP3格式

    Go語言調(diào)用SiliconFlow實現(xiàn)文本轉(zhuǎn)換為MP3格式

    這篇文章主要為大家詳細介紹了Go語言如何調(diào)用?SiliconFlow?語音生成?API?的腳本,用于將文本轉(zhuǎn)換為?MP3?格式的語音文件,感興趣的小伙伴可以了解下
    2025-02-02
  • Go 計時器使用示例全面講解

    Go 計時器使用示例全面講解

    這篇文章主要為大家介紹了Go 計時器使用示例全面講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • go語言base64用法實例

    go語言base64用法實例

    這篇文章主要介紹了go語言base64用法,實例分析了Go語言base64編碼的實用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-02-02

最新評論