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