github.com/dominant-strategies/go-quai@v0.28.2/common/prque/sstack.go (about)

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