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.

heap.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package collector
  15. import (
  16. "container/heap"
  17. "github.com/blevesearch/bleve/search"
  18. )
  19. type collectStoreHeap struct {
  20. heap search.DocumentMatchCollection
  21. compare collectorCompare
  22. }
  23. func newStoreHeap(cap int, compare collectorCompare) *collectStoreHeap {
  24. rv := &collectStoreHeap{
  25. heap: make(search.DocumentMatchCollection, 0, cap),
  26. compare: compare,
  27. }
  28. heap.Init(rv)
  29. return rv
  30. }
  31. func (c *collectStoreHeap) AddNotExceedingSize(doc *search.DocumentMatch,
  32. size int) *search.DocumentMatch {
  33. c.add(doc)
  34. if c.Len() > size {
  35. return c.removeLast()
  36. }
  37. return nil
  38. }
  39. func (c *collectStoreHeap) add(doc *search.DocumentMatch) {
  40. heap.Push(c, doc)
  41. }
  42. func (c *collectStoreHeap) removeLast() *search.DocumentMatch {
  43. return heap.Pop(c).(*search.DocumentMatch)
  44. }
  45. func (c *collectStoreHeap) Final(skip int, fixup collectorFixup) (search.DocumentMatchCollection, error) {
  46. count := c.Len()
  47. size := count - skip
  48. if size <= 0 {
  49. return make(search.DocumentMatchCollection, 0), nil
  50. }
  51. rv := make(search.DocumentMatchCollection, size)
  52. for i := size - 1; i >= 0; i-- {
  53. doc := heap.Pop(c).(*search.DocumentMatch)
  54. rv[i] = doc
  55. err := fixup(doc)
  56. if err != nil {
  57. return nil, err
  58. }
  59. }
  60. return rv, nil
  61. }
  62. // heap interface implementation
  63. func (c *collectStoreHeap) Len() int {
  64. return len(c.heap)
  65. }
  66. func (c *collectStoreHeap) Less(i, j int) bool {
  67. so := c.compare(c.heap[i], c.heap[j])
  68. return -so < 0
  69. }
  70. func (c *collectStoreHeap) Swap(i, j int) {
  71. c.heap[i], c.heap[j] = c.heap[j], c.heap[i]
  72. }
  73. func (c *collectStoreHeap) Push(x interface{}) {
  74. c.heap = append(c.heap, x.(*search.DocumentMatch))
  75. }
  76. func (c *collectStoreHeap) Pop() interface{} {
  77. var rv *search.DocumentMatch
  78. rv, c.heap = c.heap[len(c.heap)-1], c.heap[:len(c.heap)-1]
  79. return rv
  80. }