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