github.com/klaytn/klaytn@v1.12.1/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  // Internal sortable stack data structure. Implements the Push and Pop ops for
    26  // the stack (heap) functionality and the Len, Less and Swap methods for the
    27  // sortability requirements of the heaps.
    28  type sstack struct {
    29  	size     int
    30  	capacity int
    31  	offset   int
    32  
    33  	blocks [][]*item
    34  	active []*item
    35  }
    36  
    37  // Creates a new, empty stack.
    38  func newSstack() *sstack {
    39  	result := new(sstack)
    40  	result.active = make([]*item, blockSize)
    41  	result.blocks = [][]*item{result.active}
    42  	result.capacity = blockSize
    43  	return result
    44  }
    45  
    46  // Pushes a value onto the stack, expanding it if necessary. Required by
    47  // heap.Interface.
    48  func (s *sstack) Push(data interface{}) {
    49  	if s.size == s.capacity {
    50  		s.active = make([]*item, blockSize)
    51  		s.blocks = append(s.blocks, s.active)
    52  		s.capacity += blockSize
    53  		s.offset = 0
    54  	} else if s.offset == blockSize {
    55  		s.active = s.blocks[s.size/blockSize]
    56  		s.offset = 0
    57  	}
    58  	s.active[s.offset] = data.(*item)
    59  	s.offset++
    60  	s.size++
    61  }
    62  
    63  // Pops a value off the stack and returns it. Currently no shrinking is done.
    64  // Required by heap.Interface.
    65  func (s *sstack) Pop() (res interface{}) {
    66  	s.size--
    67  	s.offset--
    68  	if s.offset < 0 {
    69  		s.offset = blockSize - 1
    70  		s.active = s.blocks[s.size/blockSize]
    71  	}
    72  	res, s.active[s.offset] = s.active[s.offset], nil
    73  	return
    74  }
    75  
    76  // Returns the length of the stack. Required by sort.Interface.
    77  func (s *sstack) Len() int {
    78  	return s.size
    79  }
    80  
    81  // Compares the priority of two elements of the stack (higher is first).
    82  // Required by sort.Interface.
    83  func (s *sstack) Less(i, j int) bool {
    84  	return (s.blocks[i/blockSize][i%blockSize].priority - s.blocks[j/blockSize][j%blockSize].priority) > 0
    85  }
    86  
    87  // Swaps two elements in the stack. Required by sort.Interface.
    88  func (s *sstack) Swap(i, j int) {
    89  	ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize
    90  	s.blocks[ib][io], s.blocks[jb][jo] = s.blocks[jb][jo], s.blocks[ib][io]
    91  }
    92  
    93  // Resets the stack, effectively clearing its contents.
    94  func (s *sstack) Reset() {
    95  	*s = *newSstack()
    96  }