1 // CookieJar - A contestant's algorithm toolbox
2 // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
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).
11 // The size of a block of data
12 const blockSize = 4096
14 // A prioritized item in the sorted stack.
20 // Internal sortable stack data structure. Implements the Push and Pop ops for
21 // the stack (heap) functionality and the Len, Less and Swap methods for the
22 // sortability requirements of the heaps.
32 // Creates a new, empty stack.
33 func newSstack() *sstack {
35 result.active = make([]*item, blockSize)
36 result.blocks = [][]*item{result.active}
37 result.capacity = blockSize
41 // Pushes a value onto the stack, expanding it if necessary. Required by
43 func (s *sstack) Push(data interface{}) {
44 if s.size == s.capacity {
45 s.active = make([]*item, blockSize)
46 s.blocks = append(s.blocks, s.active)
47 s.capacity += blockSize
49 } else if s.offset == blockSize {
50 s.active = s.blocks[s.size/blockSize]
53 s.active[s.offset] = data.(*item)
58 // Pops a value off the stack and returns it. Currently no shrinking is done.
59 // Required by heap.Interface.
60 func (s *sstack) Pop() (res interface{}) {
64 s.offset = blockSize - 1
65 s.active = s.blocks[s.size/blockSize]
67 res, s.active[s.offset] = s.active[s.offset], nil
71 // Returns the length of the stack. Required by sort.Interface.
72 func (s *sstack) Len() int {
76 // Compares the priority of two elements of the stack (higher is first).
77 // Required by sort.Interface.
78 func (s *sstack) Less(i, j int) bool {
79 return s.blocks[i/blockSize][i%blockSize].priority > s.blocks[j/blockSize][j%blockSize].priority
82 // Swaps two elements in the stack. Required by sort.Interface.
83 func (s *sstack) Swap(i, j int) {
84 ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
85 s.blocks[ib][io], s.blocks[jb][jo] = s.blocks[jb][jo], s.blocks[ib][io]
88 // Resets the stack, effectively clearing its contents.
89 func (s *sstack) Reset() {