sstack.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // CookieJar - A contestant's algorithm toolbox
  2. // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
  3. //
  4. // CookieJar is dual licensed: use of this source code is governed by a BSD
  5. // license that can be found in the LICENSE file. Alternatively, the CookieJar
  6. // toolbox may be used in accordance with the terms and conditions contained
  7. // in a signed written agreement between you and the author(s).
  8. // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
  9. package prque
  10. // The size of a block of data
  11. const blockSize = 4096
  12. // A prioritized item in the sorted stack.
  13. //
  14. // Note: priorities can "wrap around" the int64 range, a comes before b if (a.priority - b.priority) > 0.
  15. // The difference between the lowest and highest priorities in the queue at any point should be less than 2^63.
  16. type item struct {
  17. value interface{}
  18. priority int64
  19. }
  20. // SetIndexCallback is called when the element is moved to a new index.
  21. // Providing SetIndexCallback is optional, it is needed only if the application needs
  22. // to delete elements other than the top one.
  23. type SetIndexCallback func(data interface{}, index int)
  24. // Internal sortable stack data structure. Implements the Push and Pop ops for
  25. // the stack (heap) functionality and the Len, Less and Swap methods for the
  26. // sortability requirements of the heaps.
  27. type sstack struct {
  28. setIndex SetIndexCallback
  29. size int
  30. capacity int
  31. offset int
  32. wrapAround bool
  33. blocks [][]*item
  34. active []*item
  35. }
  36. // Creates a new, empty stack.
  37. func newSstack(setIndex SetIndexCallback, wrapAround bool) *sstack {
  38. result := new(sstack)
  39. result.setIndex = setIndex
  40. result.active = make([]*item, blockSize)
  41. result.blocks = [][]*item{result.active}
  42. result.capacity = blockSize
  43. result.wrapAround = wrapAround
  44. return result
  45. }
  46. // Pushes a value onto the stack, expanding it if necessary. Required by
  47. // heap.Interface.
  48. func (s *sstack) Push(data interface{}) {
  49. if s.size == s.capacity {
  50. s.active = make([]*item, blockSize)
  51. s.blocks = append(s.blocks, s.active)
  52. s.capacity += blockSize
  53. s.offset = 0
  54. } else if s.offset == blockSize {
  55. s.active = s.blocks[s.size/blockSize]
  56. s.offset = 0
  57. }
  58. if s.setIndex != nil {
  59. s.setIndex(data.(*item).value, s.size)
  60. }
  61. s.active[s.offset] = data.(*item)
  62. s.offset++
  63. s.size++
  64. }
  65. // Pops a value off the stack and returns it. Currently no shrinking is done.
  66. // Required by heap.Interface.
  67. func (s *sstack) Pop() (res interface{}) {
  68. s.size--
  69. s.offset--
  70. if s.offset < 0 {
  71. s.offset = blockSize - 1
  72. s.active = s.blocks[s.size/blockSize]
  73. }
  74. res, s.active[s.offset] = s.active[s.offset], nil
  75. if s.setIndex != nil {
  76. s.setIndex(res.(*item).value, -1)
  77. }
  78. return
  79. }
  80. // Returns the length of the stack. Required by sort.Interface.
  81. func (s *sstack) Len() int {
  82. return s.size
  83. }
  84. // Compares the priority of two elements of the stack (higher is first).
  85. // Required by sort.Interface.
  86. func (s *sstack) Less(i, j int) bool {
  87. a, b := s.blocks[i/blockSize][i%blockSize].priority, s.blocks[j/blockSize][j%blockSize].priority
  88. if s.wrapAround {
  89. return a-b > 0
  90. }
  91. return a > b
  92. }
  93. // Swaps two elements in the stack. Required by sort.Interface.
  94. func (s *sstack) Swap(i, j int) {
  95. ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
  96. a, b := s.blocks[jb][jo], s.blocks[ib][io]
  97. if s.setIndex != nil {
  98. s.setIndex(a.value, i)
  99. s.setIndex(b.value, j)
  100. }
  101. s.blocks[ib][io], s.blocks[jb][jo] = a, b
  102. }
  103. // Resets the stack, effectively clearing its contents.
  104. func (s *sstack) Reset() {
  105. *s = *newSstack(s.setIndex, false)
  106. }