diff --git a/types/coin.go b/types/coin.go index c8b7c77db77d..69e77cdbdab9 100644 --- a/types/coin.go +++ b/types/coin.go @@ -449,6 +449,8 @@ func (coins Coins) Max(coinsB Coins) Coins { // {1A, 3B, 2C}.Min({4A, 2B, 2C} == {1A, 2B, 2C}) // {2A, 3B}.Min({1B, 4C}) == {1B} // {1A, 2B}.Min({3C}) == empty +// +// See also DecCoins.Intersect(). func (coins Coins) Min(coinsB Coins) Coins { min := make([]Coin, 0) for indexA, indexB := 0, 0; indexA < len(coins) && indexB < len(coinsB); { @@ -473,15 +475,6 @@ func (coins Coins) Min(coinsB Coins) Coins { return NewCoins(min...) } -// Intersect will return a new set of coins which coinains the minimum Coin -// for common denoms found in both `coins` and `coinsB`. Identical to Min() -// method and provided for compatibility with DecCoins. Note that the -// corresponding Union() alias for Max() will not be provided since it is -// dangerously confusable with Add(). -func (coins Coins) Intersect(coinsB Coins) Coins { - return coins.Min(coinsB) -} - // IsAllGT returns true if for every denom in coinsB, // the denom is present at a greater amount in coins. func (coins Coins) IsAllGT(coinsB Coins) bool { diff --git a/types/coin_test.go b/types/coin_test.go index 11567bd3a5e4..f82b581aa691 100644 --- a/types/coin_test.go +++ b/types/coin_test.go @@ -679,10 +679,8 @@ func (s *coinTestSuite) TestMinMax() { for _, tc := range cases { min := tc.input1.Min(tc.input2) max := tc.input1.Max(tc.input2) - intersect := tc.input1.Intersect(tc.input2) s.Require().True(min.IsEqual(tc.min), tc.name) s.Require().True(max.IsEqual(tc.max), tc.name) - s.Require().True(intersect.IsEqual(tc.min), tc.name) } } diff --git a/types/dec_coin.go b/types/dec_coin.go index f66f5da1a807..aebfcc12192c 100644 --- a/types/dec_coin.go +++ b/types/dec_coin.go @@ -321,6 +321,7 @@ func (coins DecCoins) SafeSub(coinsB DecCoins) (DecCoins, bool) { // to both `coins` and `coinsB` the minimum is considered to be 0, thus they // are not added to the final set. In other words, trim any denom amount from // coin which exceeds that of coinB, such that (coin.Intersect(coinB)).IsLTE(coinB). +// See also Coins.Min(). func (coins DecCoins) Intersect(coinsB DecCoins) DecCoins { res := make([]DecCoin, len(coins)) for i, coin := range coins { @@ -333,22 +334,6 @@ func (coins DecCoins) Intersect(coinsB DecCoins) DecCoins { return removeZeroDecCoins(res) } -// Min returns the minimum of each denom of its inputs, including -// the implicitly-zero absent denoms. Identical to Intersect() and -// provided for compatibility with Coins. -func (coins DecCoins) Min(coinsB DecCoins) DecCoins { - return coins.Intersect(coinsB) -} - -// Max returns the maximum of each denom of its inputs, including -// the implicitly-zero absent denoms. Though this is the -// multiset union operation, that name is dangerously confusable -// with Add(), and will not be used. -func (coins DecCoins) Max(coinsB DecCoins) DecCoins { - // coins + coinsB == min(coins, coinsB) + max(coins, coinsB) - return coins.Add(coinsB...).Sub(coins.Min(coinsB)) -} - // GetDenomByIndex returns the Denom to make the findDup generic func (coins DecCoins) GetDenomByIndex(i int) string { return coins[i].Denom