You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

priorityqueue.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package roaring
  2. import "container/heap"
  3. /////////////
  4. // The priorityQueue is used to keep Bitmaps sorted.
  5. ////////////
  6. type item struct {
  7. value *Bitmap
  8. index int
  9. }
  10. type priorityQueue []*item
  11. func (pq priorityQueue) Len() int { return len(pq) }
  12. func (pq priorityQueue) Less(i, j int) bool {
  13. return pq[i].value.GetSizeInBytes() < pq[j].value.GetSizeInBytes()
  14. }
  15. func (pq priorityQueue) Swap(i, j int) {
  16. pq[i], pq[j] = pq[j], pq[i]
  17. pq[i].index = i
  18. pq[j].index = j
  19. }
  20. func (pq *priorityQueue) Push(x interface{}) {
  21. n := len(*pq)
  22. item := x.(*item)
  23. item.index = n
  24. *pq = append(*pq, item)
  25. }
  26. func (pq *priorityQueue) Pop() interface{} {
  27. old := *pq
  28. n := len(old)
  29. item := old[n-1]
  30. item.index = -1 // for safety
  31. *pq = old[0 : n-1]
  32. return item
  33. }
  34. func (pq *priorityQueue) update(item *item, value *Bitmap) {
  35. item.value = value
  36. heap.Fix(pq, item.index)
  37. }
  38. /////////////
  39. // The containerPriorityQueue is used to keep the containers of various Bitmaps sorted.
  40. ////////////
  41. type containeritem struct {
  42. value *Bitmap
  43. keyindex int
  44. index int
  45. }
  46. type containerPriorityQueue []*containeritem
  47. func (pq containerPriorityQueue) Len() int { return len(pq) }
  48. func (pq containerPriorityQueue) Less(i, j int) bool {
  49. k1 := pq[i].value.highlowcontainer.getKeyAtIndex(pq[i].keyindex)
  50. k2 := pq[j].value.highlowcontainer.getKeyAtIndex(pq[j].keyindex)
  51. if k1 != k2 {
  52. return k1 < k2
  53. }
  54. c1 := pq[i].value.highlowcontainer.getContainerAtIndex(pq[i].keyindex)
  55. c2 := pq[j].value.highlowcontainer.getContainerAtIndex(pq[j].keyindex)
  56. return c1.getCardinality() > c2.getCardinality()
  57. }
  58. func (pq containerPriorityQueue) Swap(i, j int) {
  59. pq[i], pq[j] = pq[j], pq[i]
  60. pq[i].index = i
  61. pq[j].index = j
  62. }
  63. func (pq *containerPriorityQueue) Push(x interface{}) {
  64. n := len(*pq)
  65. item := x.(*containeritem)
  66. item.index = n
  67. *pq = append(*pq, item)
  68. }
  69. func (pq *containerPriorityQueue) Pop() interface{} {
  70. old := *pq
  71. n := len(old)
  72. item := old[n-1]
  73. item.index = -1 // for safety
  74. *pq = old[0 : n-1]
  75. return item
  76. }
  77. //func (pq *containerPriorityQueue) update(item *containeritem, value *Bitmap, keyindex int) {
  78. // item.value = value
  79. // item.keyindex = keyindex
  80. // heap.Fix(pq, item.index)
  81. //}