github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/prque/sstack.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 // This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque". 19 20 package prque 21 22 // The size of a block of data 23 const blockSize = 4096 24 25 // A prioritized item in the sorted stack. 26 // 27 // Note: priorities can "wrap around" the int64 range, a comes before b if (a.priority - b.priority) > 0. 28 // The difference between the lowest and highest priorities in the queue at any point should be less than 2^63. 29 type item struct { 30 value interface{} 31 priority int64 32 } 33 34 // SetIndexCallback is called when the element is moved to a new index. 35 // Providing SetIndexCallback is optional, it is needed only if the application needs 36 // to delete elements other than the top one. 37 type SetIndexCallback func(data interface{}, index int) 38 39 // Internal sortable stack data structure. Implements the Push and Pop ops for 40 // the stack (heap) functionality and the Len, Less and Swap methods for the 41 // sortability requirements of the heaps. 42 type sstack struct { 43 setIndex SetIndexCallback 44 size int 45 capacity int 46 offset int 47 48 blocks [][]*item 49 active []*item 50 } 51 52 // Creates a new, empty stack. 53 func newSstack(setIndex SetIndexCallback) *sstack { 54 result := new(sstack) 55 result.setIndex = setIndex 56 result.active = make([]*item, blockSize) 57 result.blocks = [][]*item{result.active} 58 result.capacity = blockSize 59 return result 60 } 61 62 // Pushes a value onto the stack, expanding it if necessary. Required by 63 // heap.Interface. 64 func (s *sstack) Push(data interface{}) { 65 if s.size == s.capacity { 66 s.active = make([]*item, blockSize) 67 s.blocks = append(s.blocks, s.active) 68 s.capacity += blockSize 69 s.offset = 0 70 } else if s.offset == blockSize { 71 s.active = s.blocks[s.size/blockSize] 72 s.offset = 0 73 } 74 if s.setIndex != nil { 75 s.setIndex(data.(*item).value, s.size) 76 } 77 s.active[s.offset] = data.(*item) 78 s.offset++ 79 s.size++ 80 } 81 82 // Pops a value off the stack and returns it. Currently no shrinking is done. 83 // Required by heap.Interface. 84 func (s *sstack) Pop() (res interface{}) { 85 s.size-- 86 s.offset-- 87 if s.offset < 0 { 88 s.offset = blockSize - 1 89 s.active = s.blocks[s.size/blockSize] 90 } 91 res, s.active[s.offset] = s.active[s.offset], nil 92 if s.setIndex != nil { 93 s.setIndex(res.(*item).value, -1) 94 } 95 return 96 } 97 98 // Returns the length of the stack. Required by sort.Interface. 99 func (s *sstack) Len() int { 100 return s.size 101 } 102 103 // Compares the priority of two elements of the stack (higher is first). 104 // Required by sort.Interface. 105 func (s *sstack) Less(i, j int) bool { 106 return (s.blocks[i/blockSize][i%blockSize].priority - s.blocks[j/blockSize][j%blockSize].priority) > 0 107 } 108 109 // Swaps two elements in the stack. Required by sort.Interface. 110 func (s *sstack) Swap(i, j int) { 111 ib, io, jb, jo := i/blockSize, i%blockSize, j/blockSize, j%blockSize 112 a, b := s.blocks[jb][jo], s.blocks[ib][io] 113 if s.setIndex != nil { 114 s.setIndex(a.value, i) 115 s.setIndex(b.value, j) 116 } 117 s.blocks[ib][io], s.blocks[jb][jo] = a, b 118 } 119 120 // Resets the stack, effectively clearing its contents. 121 func (s *sstack) Reset() { 122 *s = *newSstack(s.setIndex) 123 }