mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-02-01 23:40:35 +00:00
*: don't use CompareTo name for three-way comparison functions
"Compare" is almost a standard one now, although math/big uses Cmp for historic reasons (keys.PublicKey does so too). This also fixes Fixed8 since int64 to int conversion is lossy. Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
parent
a1a7e3d708
commit
b4e4567c2b
7 changed files with 33 additions and 32 deletions
|
@ -84,13 +84,13 @@ type Pool struct {
|
|||
|
||||
func (p items) Len() int { return len(p) }
|
||||
func (p items) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
func (p items) Less(i, j int) bool { return p[i].CompareTo(p[j]) < 0 }
|
||||
func (p items) Less(i, j int) bool { return p[i].Compare(p[j]) < 0 }
|
||||
|
||||
// CompareTo returns the difference between two items.
|
||||
// Compare returns the difference between two items.
|
||||
// difference < 0 implies p < otherP.
|
||||
// difference = 0 implies p = otherP.
|
||||
// difference > 0 implies p > otherP.
|
||||
func (p item) CompareTo(otherP item) int {
|
||||
func (p item) Compare(otherP item) int {
|
||||
pHigh := p.txn.HasAttribute(transaction.HighPriority)
|
||||
otherHigh := otherP.txn.HasAttribute(transaction.HighPriority)
|
||||
if pHigh && !otherHigh {
|
||||
|
@ -238,7 +238,7 @@ func (mp *Pool) Add(t *transaction.Transaction, fee Feer, data ...any) error {
|
|||
// transactions with the same priority and appending to the end of the
|
||||
// slice is always more efficient.
|
||||
n := sort.Search(len(mp.verifiedTxes), func(n int) bool {
|
||||
return pItem.CompareTo(mp.verifiedTxes[n]) > 0
|
||||
return pItem.Compare(mp.verifiedTxes[n]) > 0
|
||||
})
|
||||
// Changing sort.Search to slices.BinarySearchFunc() is not recommended
|
||||
// above, as of Go 1.23 this results in
|
||||
|
@ -487,14 +487,14 @@ func (mp *Pool) TryGetData(hash util.Uint256) (any, bool) {
|
|||
if tx, ok := mp.verifiedMap[hash]; ok {
|
||||
itm := item{txn: tx}
|
||||
n := sort.Search(len(mp.verifiedTxes), func(n int) bool {
|
||||
return itm.CompareTo(mp.verifiedTxes[n]) >= 0
|
||||
return itm.Compare(mp.verifiedTxes[n]) >= 0
|
||||
})
|
||||
if n < len(mp.verifiedTxes) {
|
||||
for i := n; i < len(mp.verifiedTxes); i++ { // items may have equal priority, so `n` is the left bound of the items which are as prioritized as the desired `itm`.
|
||||
if mp.verifiedTxes[i].txn.Hash() == hash {
|
||||
return mp.verifiedTxes[i].data, ok
|
||||
}
|
||||
if itm.CompareTo(mp.verifiedTxes[i]) != 0 {
|
||||
if itm.Compare(mp.verifiedTxes[i]) != 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -117,7 +117,7 @@ func TestOverCapacity(t *testing.T) {
|
|||
mp := New(mempoolSize, 0, false, nil)
|
||||
|
||||
var checkPoolIsSorted = func() {
|
||||
require.True(t, slices.IsSortedFunc(mp.verifiedTxes, func(a, b item) int { return -a.CompareTo(b) }))
|
||||
require.True(t, slices.IsSortedFunc(mp.verifiedTxes, func(a, b item) int { return -a.Compare(b) }))
|
||||
}
|
||||
|
||||
for i := 0; i < mempoolSize; i++ {
|
||||
|
@ -347,18 +347,18 @@ func TestMempoolItemsOrder(t *testing.T) {
|
|||
tx4.Signers = []transaction.Signer{{Account: sender0}}
|
||||
item4 := item{txn: tx4}
|
||||
|
||||
require.True(t, item1.CompareTo(item2) > 0)
|
||||
require.True(t, item2.CompareTo(item1) < 0)
|
||||
require.True(t, item1.CompareTo(item3) > 0)
|
||||
require.True(t, item3.CompareTo(item1) < 0)
|
||||
require.True(t, item1.CompareTo(item4) > 0)
|
||||
require.True(t, item4.CompareTo(item1) < 0)
|
||||
require.True(t, item2.CompareTo(item3) > 0)
|
||||
require.True(t, item3.CompareTo(item2) < 0)
|
||||
require.True(t, item2.CompareTo(item4) > 0)
|
||||
require.True(t, item4.CompareTo(item2) < 0)
|
||||
require.True(t, item3.CompareTo(item4) > 0)
|
||||
require.True(t, item4.CompareTo(item3) < 0)
|
||||
require.True(t, item1.Compare(item2) > 0)
|
||||
require.True(t, item2.Compare(item1) < 0)
|
||||
require.True(t, item1.Compare(item3) > 0)
|
||||
require.True(t, item3.Compare(item1) < 0)
|
||||
require.True(t, item1.Compare(item4) > 0)
|
||||
require.True(t, item4.Compare(item1) < 0)
|
||||
require.True(t, item2.Compare(item3) > 0)
|
||||
require.True(t, item3.Compare(item2) < 0)
|
||||
require.True(t, item2.Compare(item4) > 0)
|
||||
require.True(t, item4.Compare(item2) < 0)
|
||||
require.True(t, item3.Compare(item4) > 0)
|
||||
require.True(t, item4.Compare(item3) < 0)
|
||||
}
|
||||
|
||||
func TestMempoolAddRemoveOracleResponse(t *testing.T) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package fixedn
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -158,10 +159,10 @@ func (f Fixed8) Equal(g Fixed8) bool {
|
|||
return f == g
|
||||
}
|
||||
|
||||
// CompareTo returns the difference between the f and g.
|
||||
// difference < 0 implies f < g.
|
||||
// difference = 0 implies f = g.
|
||||
// difference > 0 implies f > g.
|
||||
func (f Fixed8) CompareTo(g Fixed8) int {
|
||||
return int(f - g)
|
||||
// Compare performs three-way comparison between f and g.
|
||||
// - -1 implies f < g.
|
||||
// - 0 implies f = g.
|
||||
// - 1 implies f > g.
|
||||
func (f Fixed8) Compare(g Fixed8) int {
|
||||
return cmp.Compare(f, g)
|
||||
}
|
||||
|
|
|
@ -164,8 +164,8 @@ func TestFixed8_Arith(t *testing.T) {
|
|||
assert.True(t, u1.LessThan(u2))
|
||||
assert.True(t, u2.GreaterThan(u1))
|
||||
assert.True(t, u1.Equal(u1))
|
||||
assert.NotZero(t, u1.CompareTo(u2))
|
||||
assert.Zero(t, u1.CompareTo(u1))
|
||||
assert.NotZero(t, u1.Compare(u2))
|
||||
assert.Zero(t, u1.Compare(u1))
|
||||
assert.EqualValues(t, Fixed8(2), u2.Div(3))
|
||||
}
|
||||
|
||||
|
|
|
@ -1169,7 +1169,7 @@ txloop:
|
|||
var cbList = s.txCbList.Load()
|
||||
if cbList != nil {
|
||||
var list = cbList.([]util.Uint256)
|
||||
_, found := slices.BinarySearchFunc(list, tx.Hash(), util.Uint256.CompareTo)
|
||||
_, found := slices.BinarySearchFunc(list, tx.Hash(), util.Uint256.Compare)
|
||||
if found {
|
||||
txCallback(tx)
|
||||
}
|
||||
|
@ -1484,7 +1484,7 @@ func (s *Server) RequestTx(hashes ...util.Uint256) {
|
|||
}
|
||||
|
||||
var sorted = slices.Clone(hashes)
|
||||
slices.SortFunc(sorted, util.Uint256.CompareTo)
|
||||
slices.SortFunc(sorted, util.Uint256.Compare)
|
||||
s.txCbList.Store(sorted)
|
||||
|
||||
for i := 0; i <= len(hashes)/payload.MaxHashesCount; i++ {
|
||||
|
|
|
@ -121,12 +121,12 @@ func (u Uint256) MarshalJSON() ([]byte, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
// CompareTo compares two Uint256 with each other. Possible output: 1, -1, 0
|
||||
// Compare performs three-way comparison of two Uint256. Possible output: 1, -1, 0
|
||||
//
|
||||
// 1 implies u > other.
|
||||
// -1 implies u < other.
|
||||
// 0 implies u = other.
|
||||
func (u Uint256) CompareTo(other Uint256) int { return bytes.Compare(u[:], other[:]) }
|
||||
func (u Uint256) Compare(other Uint256) int { return bytes.Compare(u[:], other[:]) }
|
||||
|
||||
// EncodeBinary implements the io.Serializable interface.
|
||||
func (u *Uint256) EncodeBinary(w *io.BinWriter) {
|
||||
|
|
|
@ -83,7 +83,7 @@ func TestUInt256Equals(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
assert.False(t, ua.Equals(ub), "%s and %s cannot be equal", ua, ub)
|
||||
assert.True(t, ua.Equals(ua), "%s and %s must be equal", ua, ua)
|
||||
assert.Zero(t, ua.CompareTo(ua), "%s and %s must be equal", ua, ua)
|
||||
assert.Zero(t, ua.Compare(ua), "%s and %s must be equal", ua, ua)
|
||||
}
|
||||
|
||||
func TestUint256_Serializable(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue