sstack.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. package prque
  9. // The size of a block of data
  10. const blockSize = 4096
  11. // A prioritized item in the sorted stack.
  12. type item struct {
  13. value interface{}
  14. priority float32
  15. }
  16. // Internal sortable stack data structure. Implements the Push and Pop ops for
  17. // the stack (heap) functionality and the Len, Less and Swap methods for the
  18. // sortability requirements of the heaps.
  19. type sstack struct {
  20. size int
  21. capacity int
  22. offset int
  23. blocks [][]*item
  24. active []*item
  25. }
  26. // Creates a new, empty stack.
  27. func newSstack() *sstack {
  28. result := new(sstack)
  29. result.active = make([]*item, blockSize)
  30. result.blocks = [][]*item{result.active}
  31. result.capacity = blockSize
  32. return result
  33. }
  34. // Pushes a value onto the stack, expanding it if necessary. Required by
  35. // heap.Interface.
  36. func (s *sstack) Push(data interface{}) {
  37. if s.size == s.capacity {
  38. s.active = make([]*item, blockSize)
  39. s.blocks = append(s.blocks, s.active)
  40. s.capacity += blockSize
  41. s.offset = 0
  42. } else if s.offset == blockSize {
  43. s.active = s.blocks[s.size/blockSize]
  44. s.offset = 0
  45. }
  46. s.active[s.offset] = data.(*item)
  47. s.offset++
  48. s.size++
  49. }
  50. // Pops a value off the stack and returns it. Currently no shrinking is done.
  51. // Required by heap.Interface.
  52. func (s *sstack) Pop() (res interface{}) {
  53. s.size--
  54. s.offset--
  55. if s.offset < 0 {
  56. s.offset = blockSize - 1
  57. s.active = s.blocks[s.size/blockSize]
  58. }
  59. res, s.active[s.offset] = s.active[s.offset], nil
  60. return
  61. }
  62. // Returns the length of the stack. Required by sort.Interface.
  63. func (s *sstack) Len() int {
  64. return s.size
  65. }
  66. // Compares the priority of two elements of the stack (higher is first).
  67. // Required by sort.Interface.
  68. func (s *sstack) Less(i, j int) bool {
  69. return s.blocks[i/blockSize][i%blockSize].priority > s.blocks[j/blockSize][j%blockSize].priority
  70. }
  71. // Swaps two elements in the stack. Required by sort.Interface.
  72. func (s *sstack) Swap(i, j int) {
  73. ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
  74. s.blocks[ib][io], s.blocks[jb][jo] = s.blocks[jb][jo], s.blocks[ib][io]
  75. }
  76. // Resets the stack, effectively clearing its contents.
  77. func (s *sstack) Reset() {
  78. *s = *newSstack()
  79. }