Skip to content

Commit

Permalink
common/prque: remove dependency on golang.org/exp (ethereum#29314)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Dec 26, 2024
1 parent 2629302 commit 2b3d7f6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
10 changes: 5 additions & 5 deletions common/prque/lazyqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package prque

import (
"cmp"
"container/heap"
"time"

"github.com/XinFinOrg/XDPoSChain/common/mclock"
"golang.org/x/exp/constraints"
)

// LazyQueue is a priority queue data structure where priorities can change over
Expand All @@ -33,7 +33,7 @@ import (
//
// If the upper estimate is exceeded then Update should be called for that item.
// A global Refresh function should also be called periodically.
type LazyQueue[P constraints.Ordered, V any] struct {
type LazyQueue[P cmp.Ordered, V any] struct {
clock mclock.Clock
// Items are stored in one of two internal queues ordered by estimated max
// priority until the next and the next-after-next refresh. Update and Refresh
Expand All @@ -50,12 +50,12 @@ type LazyQueue[P constraints.Ordered, V any] struct {
}

type (
PriorityCallback[P constraints.Ordered, V any] func(data V) P // actual priority callback
MaxPriorityCallback[P constraints.Ordered, V any] func(data V, until mclock.AbsTime) P // estimated maximum priority callback
PriorityCallback[P cmp.Ordered, V any] func(data V) P // actual priority callback
MaxPriorityCallback[P cmp.Ordered, V any] func(data V, until mclock.AbsTime) P // estimated maximum priority callback
)

// NewLazyQueue creates a new lazy queue
func NewLazyQueue[P constraints.Ordered, V any](setIndex SetIndexCallback[V], priority PriorityCallback[P, V], maxPriority MaxPriorityCallback[P, V], clock mclock.Clock, refreshPeriod time.Duration) *LazyQueue[P, V] {
func NewLazyQueue[P cmp.Ordered, V any](setIndex SetIndexCallback[V], priority PriorityCallback[P, V], maxPriority MaxPriorityCallback[P, V], clock mclock.Clock, refreshPeriod time.Duration) *LazyQueue[P, V] {
q := &LazyQueue[P, V]{
popQueue: newSstack[P, V](nil),
setIndex: setIndex,
Expand Down
7 changes: 3 additions & 4 deletions common/prque/prque.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
package prque

import (
"cmp"
"container/heap"

"golang.org/x/exp/constraints"
)

// Priority queue data structure.
type Prque[P constraints.Ordered, V any] struct {
type Prque[P cmp.Ordered, V any] struct {
cont *sstack[P, V]
}

// New creates a new priority queue.
func New[P constraints.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] {
func New[P cmp.Ordered, V any](setIndex SetIndexCallback[V]) *Prque[P, V] {
return &Prque[P, V]{newSstack[P, V](setIndex)}
}

Expand Down
8 changes: 4 additions & 4 deletions common/prque/sstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

package prque

import "golang.org/x/exp/constraints"
import "cmp"

// The size of a block of data
const blockSize = 4096

// A prioritized item in the sorted stack.
type item[P constraints.Ordered, V any] struct {
type item[P cmp.Ordered, V any] struct {
value V
priority P
}
Expand All @@ -29,7 +29,7 @@ type SetIndexCallback[V any] func(data V, index int)
// Internal sortable stack data structure. Implements the Push and Pop ops for
// the stack (heap) functionality and the Len, Less and Swap methods for the
// sortability requirements of the heaps.
type sstack[P constraints.Ordered, V any] struct {
type sstack[P cmp.Ordered, V any] struct {
setIndex SetIndexCallback[V]
size int
capacity int
Expand All @@ -40,7 +40,7 @@ type sstack[P constraints.Ordered, V any] struct {
}

// Creates a new, empty stack.
func newSstack[P constraints.Ordered, V any](setIndex SetIndexCallback[V]) *sstack[P, V] {
func newSstack[P cmp.Ordered, V any](setIndex SetIndexCallback[V]) *sstack[P, V] {
result := new(sstack[P, V])
result.setIndex = setIndex
result.active = make([]*item[P, V], blockSize)
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ require (
github.com/deckarep/golang-set v1.8.0
github.com/dop251/goja v0.0.0-20200721192441-a695b0cdd498
github.com/ethereum/c-kzg-4844 v0.4.0
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/influxdata/influxdb-client-go/v2 v2.4.0
github.com/influxdata/influxdb1-client v0.0.0-20220302092344-a9ab5670611c
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-isatty v0.0.17
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible
github.com/urfave/cli/v2 v2.27.5
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand All @@ -68,7 +69,6 @@ require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-yaml/yaml v2.1.0+incompatible // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
Expand All @@ -78,7 +78,6 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/naoina/go-stringutil v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/protolambda/bls12-381-util v0.0.0-20220416220906-d8552aa452c7 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down

0 comments on commit 2b3d7f6

Please sign in to comment.