Skip to content

Commit

Permalink
refactor: chose a single name, removed aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLarson committed Feb 22, 2022
1 parent 2059cd8 commit 9ab5dbf
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 27 deletions.
11 changes: 2 additions & 9 deletions types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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); {
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions types/coin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
17 changes: 1 addition & 16 deletions types/dec_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 9ab5dbf

Please sign in to comment.