smallheap.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // 最小堆存放数据项
  2. // 并实现了一个时序队列,支持添加、删除、修改等操作
  3. // 实现了heap.Interface接口,提供O(logn)的插入和删除操作
  4. // 同时使用map提供O(1)的查找操作
  5. // 修改时间:2024-12-26
  6. package smallheap
  7. import (
  8. "container/heap"
  9. "errors"
  10. )
  11. type SortItem interface {
  12. SetIndex(index int)
  13. GetIndex() int
  14. GetSortValue() int64
  15. SetSortValue(int64)
  16. GetUniqueKey() int64
  17. }
  18. type HeapSlice[T SortItem] []T
  19. type SmallHeap[T SortItem] struct {
  20. Items HeapSlice[T]
  21. QueryTable map[int64]T
  22. // sequence *snowflake.Node //1MS生成400万个,不分节点
  23. }
  24. var (
  25. ErrItemExists = errors.New("item already exists")
  26. ErrItemNotFound = errors.New("item not found")
  27. )
  28. func (h HeapSlice[T]) Len() int {
  29. return len(h)
  30. }
  31. func (h HeapSlice[T]) Less(i, j int) bool {
  32. return h[i].GetSortValue() < h[j].GetSortValue()
  33. }
  34. func (h HeapSlice[T]) Swap(i, j int) {
  35. h[i], h[j] = h[j], h[i]
  36. h[i].SetIndex(i)
  37. h[j].SetIndex(j)
  38. }
  39. func (h *HeapSlice[T]) Push(x interface{}) {
  40. v, ok := x.(T)
  41. if ok {
  42. v.SetIndex(len(*h))
  43. *h = append(*h, v)
  44. }
  45. }
  46. func (h *HeapSlice[T]) Pop() interface{} {
  47. n := len(*h)
  48. x := (*h)[n-1]
  49. *h = (*h)[:n-1]
  50. x.SetIndex(-1) // 设置索引为 -1
  51. return x
  52. }
  53. func NewSmallHeap[T SortItem]() *SmallHeap[T] {
  54. sh := new(SmallHeap[T])
  55. sh.Items = make([]T, 0, 16)
  56. sh.QueryTable = make(map[int64]T)
  57. // seq, err := snowflake.NewMsNode(0, 0)
  58. // if err != nil {
  59. // panic("NewSmallHeap init sequence error")
  60. // return nil
  61. // }
  62. // sh.sequence = seq
  63. heap.Init(&sh.Items)
  64. return sh
  65. }
  66. func (h *SmallHeap[T]) Clear() {
  67. h.Items = make([]T, 0)
  68. h.QueryTable = make(map[int64]T)
  69. }
  70. // 使用接口
  71. // 添加
  72. func (h *SmallHeap[T]) Add(it T) error {
  73. // 检查元素是否已存在
  74. if _, exists := h.QueryTable[it.GetUniqueKey()]; exists {
  75. return ErrItemExists
  76. }
  77. heap.Push(&h.Items, it)
  78. k := it.GetUniqueKey()
  79. h.QueryTable[k] = it
  80. return nil
  81. }
  82. // 删除
  83. func (h *SmallHeap[T]) Remove(index int) (T, error) {
  84. if index < 0 || index >= h.Items.Len() {
  85. var t T
  86. return t, ErrItemNotFound
  87. }
  88. it := heap.Remove(&h.Items, index)
  89. v, ok := it.(T)
  90. if ok {
  91. v.SetIndex(-1)
  92. delete(h.QueryTable, v.GetUniqueKey())
  93. return v, nil
  94. }
  95. var t T
  96. return t, ErrItemNotFound
  97. }
  98. func (h *SmallHeap[T]) RemoveByUniqueKey(uniqueKey int64) (T, error) {
  99. v, ok := h.QueryTable[uniqueKey]
  100. if !ok {
  101. var t T
  102. return t, ErrItemNotFound
  103. }
  104. return h.Remove(v.GetIndex())
  105. }
  106. // 重新排序
  107. func (h *SmallHeap[T]) UpdateSortVal(key int64, sortVal int64) bool {
  108. old, ok := h.GetByUniqueKey(key)
  109. if ok {
  110. old.SetSortValue(sortVal)
  111. heap.Fix(&h.Items, old.GetIndex())
  112. return true
  113. }
  114. return false
  115. }
  116. // 查看切片中的元素
  117. func (h *SmallHeap[T]) Get(pos int) (T, error) {
  118. var zero T
  119. if pos < 0 || pos >= h.Items.Len() {
  120. return zero, ErrItemNotFound
  121. }
  122. return h.Items[pos], nil
  123. }
  124. func (h *SmallHeap[T]) GetByUniqueKey(uniqueKey int64) (T, bool) {
  125. v, ok := h.QueryTable[uniqueKey]
  126. return v, ok
  127. }
  128. // Size 返回堆中元素数量
  129. // 时间复杂度: O(1)
  130. func (h *SmallHeap[T]) Size() int {
  131. return len(h.Items)
  132. }