github.com/luckypickle/go-ethereum-vet@v1.14.2/common/prque/sstack.go (about)

     1  // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
     2  
     3  package prque
     4  
     5  // The size of a block of data
     6  const blockSize = 4096
     7  
     8  // A prioritized item in the sorted stack.
     9  //
    10  // Note: priorities can "wrap around" the int64 range, a comes before b if (a.priority - b.priority) > 0.
    11  // The difference between the lowest and highest priorities in the queue at any point should be less than 2^63.
    12  type item struct {
    13  	value    interface{}
    14  	priority int64
    15  }
    16  
    17  // setIndexCallback is called when the element is moved to a new index.
    18  // Providing setIndexCallback is optional, it is needed only if the application needs
    19  // to delete elements other than the top one.
    20  type setIndexCallback func(a interface{}, i int)
    21  
    22  // Internal sortable stack data structure. Implements the Push and Pop ops for
    23  // the stack (heap) functionality and the Len, Less and Swap methods for the
    24  // sortability requirements of the heaps.
    25  type sstack struct {
    26  	setIndex setIndexCallback
    27  	size     int
    28  	capacity int
    29  	offset   int
    30  
    31  	blocks [][]*item
    32  	active []*item
    33  }
    34  
    35  // Creates a new, empty stack.
    36  func newSstack(setIndex setIndexCallback) *sstack {
    37  	result := new(sstack)
    38  	result.setIndex = setIndex
    39  	result.active = make([]*item, blockSize)
    40  	result.blocks = [][]*item{result.active}
    41  	result.capacity = blockSize
    42  	return result
    43  }
    44  
    45  // Pushes a value onto the stack, expanding it if necessary. Required by
    46  // heap.Interface.
    47  func (s *sstack) Push(data interface{}) {
    48  	if s.size == s.capacity {
    49  		s.active = make([]*item, blockSize)
    50  		s.blocks = append(s.blocks, s.active)
    51  		s.capacity += blockSize
    52  		s.offset = 0
    53  	} else if s.offset == blockSize {
    54  		s.active = s.blocks[s.size/blockSize]
    55  		s.offset = 0
    56  	}
    57  	if s.setIndex != nil {
    58  		s.setIndex(data.(*item).value, s.size)
    59  	}
    60  	s.active[s.offset] = data.(*item)
    61  	s.offset++
    62  	s.size++
    63  }
    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  
    81  // Returns the length of the stack. Required by sort.Interface.
    82  func (s *sstack) Len() int {
    83  	return s.size
    84  }
    85  
    86  // Compares the priority of two elements of the stack (higher is first).
    87  // Required by sort.Interface.
    88  func (s *sstack) Less(i, j int) bool {
    89  	return (s.blocks[i/blockSize][i%blockSize].priority - s.blocks[j/blockSize][j%blockSize].priority) > 0
    90  }
    91  
    92  // Swaps two elements in the stack. Required by sort.Interface.
    93  func (s *sstack) Swap(i, j int) {
    94  	ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
    95  	a, b := s.blocks[jb][jo], s.blocks[ib][io]
    96  	if s.setIndex != nil {
    97  		s.setIndex(a.value, i)
    98  		s.setIndex(b.value, j)
    99  	}
   100  	s.blocks[ib][io], s.blocks[jb][jo] = a, b
   101  }
   102  
   103  // Resets the stack, effectively clearing its contents.
   104  func (s *sstack) Reset() {
   105  	*s = *newSstack(s.setIndex)
   106  }