github.com/lino-network/lino@v0.6.11/x/reputation/repv2/int.go (about)

     1  package repv2
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  )
     7  
     8  type Int struct {
     9  	*big.Int
    10  }
    11  
    12  func NewInt(v int64) Int {
    13  	return Int{
    14  		big.NewInt(v),
    15  	}
    16  }
    17  
    18  func NewIntFromBig(v *big.Int) Int {
    19  	return Int{big.NewInt(0).Set(v)}
    20  }
    21  
    22  func (i Int) Clone() Int {
    23  	return Int{big.NewInt(0).Set(i.Int)}
    24  }
    25  
    26  // a bunch of helper functions that takes two bitInt and returns
    27  // a newly allocated bigInt.
    28  func IntMax(a, b Int) Int {
    29  	if a.Int.Cmp(b.Int) > 0 {
    30  		return a.Clone()
    31  	} else {
    32  		return b.Clone()
    33  	}
    34  }
    35  
    36  func IntMin(a, b Int) Int {
    37  	if a.Int.Cmp(b.Int) < 0 {
    38  		return a.Clone()
    39  	} else {
    40  		return b.Clone()
    41  	}
    42  }
    43  
    44  func IntAdd(a, b Int) Int {
    45  	rst := big.NewInt(0)
    46  	return Int{rst.Add(a.Int, b.Int)}
    47  }
    48  
    49  func IntSub(a, b Int) Int {
    50  	rst := big.NewInt(0)
    51  	return Int{rst.Sub(a.Int, b.Int)}
    52  }
    53  
    54  func IntMul(a, b Int) Int {
    55  	rst := big.NewInt(0)
    56  	return Int{rst.Mul(a.Int, b.Int)}
    57  }
    58  
    59  func IntDiv(a, b Int) Int {
    60  	rst := big.NewInt(0)
    61  	return Int{rst.Div(a.Int, b.Int)}
    62  }
    63  
    64  func IntGreater(a, b Int) bool {
    65  	return a.Cmp(b) > 0
    66  }
    67  
    68  func IntGTE(a, b Int) bool {
    69  	return a.Cmp(b) >= 0
    70  }
    71  
    72  func IntLess(a, b Int) bool {
    73  	return a.Cmp(b) < 0
    74  }
    75  
    76  // return v / (num / denom)
    77  func IntDivFrac(v Int, num, denom int64) Int {
    78  	if num == 0 || denom == 0 {
    79  		panic("bigIntDivFrac zero num or denom")
    80  	}
    81  	return IntMulFrac(v, denom, num)
    82  }
    83  
    84  // return v * (num / denom)
    85  func IntMulFrac(v Int, num, denom int64) Int {
    86  	if denom == 0 {
    87  		panic("bigIntMulFrac zero denom")
    88  	}
    89  	return IntDiv(IntMul(v, NewInt(num)), NewInt(denom))
    90  }
    91  
    92  // func bigIntLTE(a, b *big.Int) bool {
    93  // 	return a.Cmp(b) <= 0
    94  // }
    95  
    96  func (i Int) Add(b Int) {
    97  	i.Int.Add(i.Int, b.Int)
    98  }
    99  
   100  func (i Int) Sub(b Int) {
   101  	i.Int.Sub(i.Int, b.Int)
   102  }
   103  
   104  func (i Int) Div(b Int) {
   105  	i.Int.Div(i.Int, b.Int)
   106  }
   107  
   108  func (i Int) Mul(b Int) {
   109  	i.Int.Mul(i.Int, b.Int)
   110  }
   111  
   112  func (i Int) Cmp(b Int) int {
   113  	return i.Int.Cmp(b.Int)
   114  }
   115  
   116  // MarshalAmino defines custom encoding scheme
   117  func (i Int) MarshalAmino() (string, error) {
   118  	if i.Int == nil { // Necessary since default Uint initialization has i.i as nil
   119  		i.Int = new(big.Int)
   120  	}
   121  	return marshalAmino(i.Int)
   122  }
   123  
   124  // UnmarshalAmino defines custom decoding scheme
   125  func (i *Int) UnmarshalAmino(text string) error {
   126  	if i.Int == nil { // Necessary since default Int initialization has i.i as nil
   127  		i.Int = new(big.Int)
   128  	}
   129  	return unmarshalAmino(i.Int, text)
   130  }
   131  
   132  // MarshalJSON defines custom encoding scheme
   133  func (i Int) MarshalJSON() ([]byte, error) {
   134  	if i.Int == nil { // Necessary since default Uint initialization has i.i as nil
   135  		i.Int = new(big.Int)
   136  	}
   137  	return marshalJSON(i.Int)
   138  }
   139  
   140  // UnmarshalJSON defines custom decoding scheme
   141  func (i *Int) UnmarshalJSON(bz []byte) error {
   142  	if i.Int == nil { // Necessary since default Int initialization has i.i as nil
   143  		i.Int = new(big.Int)
   144  	}
   145  	return unmarshalJSON(i.Int, bz)
   146  }
   147  
   148  // MarshalAmino for custom encoding scheme
   149  func marshalAmino(i *big.Int) (string, error) {
   150  	bz, err := i.MarshalText()
   151  	return string(bz), err
   152  }
   153  
   154  func unmarshalText(i *big.Int, text string) error {
   155  	if err := i.UnmarshalText([]byte(text)); err != nil {
   156  		return err
   157  	}
   158  	return nil
   159  }
   160  
   161  // UnmarshalAmino for custom decoding scheme
   162  func unmarshalAmino(i *big.Int, text string) (err error) {
   163  	return unmarshalText(i, text)
   164  }
   165  
   166  // MarshalJSON for custom encoding scheme
   167  // Must be encoded as a string for JSON precision
   168  func marshalJSON(i *big.Int) ([]byte, error) {
   169  	text, err := i.MarshalText()
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	return json.Marshal(string(text))
   174  }
   175  
   176  // UnmarshalJSON for custom decoding scheme
   177  // Must be encoded as a string for JSON precision
   178  func unmarshalJSON(i *big.Int, bz []byte) error {
   179  	var text string
   180  	err := json.Unmarshal(bz, &text)
   181  	if err == nil {
   182  		return unmarshalText(i, text)
   183  	}
   184  
   185  	// backward compatibility for old big int.
   186  	num := big.NewInt(0)
   187  	err = json.Unmarshal(bz, &num)
   188  	if err != nil {
   189  		return err
   190  	}
   191  	return unmarshalText(i, num.String())
   192  }