github.com/enetx/g@v1.0.80/int.go (about) 1 package g 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "math/big" 7 "math/bits" 8 "strconv" 9 10 "github.com/enetx/g/cmp" 11 "github.com/enetx/g/pkg/constraints" 12 "github.com/enetx/g/pkg/rand" 13 ) 14 15 // NewInt creates a new Int with the provided int value. 16 func NewInt[T constraints.Integer | rune | byte](i T) Int { return Int(i) } 17 18 // Min returns the minimum of Ints. 19 func (i Int) Min(b ...Int) Int { return cmp.Min(append(b, i)...) } 20 21 // Max returns the maximum of Ints. 22 func (i Int) Max(b ...Int) Int { return cmp.Max(append(b, i)...) } 23 24 // RandomRange returns a random Int in the range [from, to]. 25 func (i Int) RandomRange(to Int) Int { return rand.N(to-i+1) + i } 26 27 // Bytes returns the Int as a byte slice. 28 func (i Int) Bytes() Bytes { 29 buffer := make([]byte, 8) 30 binary.BigEndian.PutUint64(buffer, i.AsUInt64()) 31 32 return buffer[bits.LeadingZeros64(i.AsUInt64())>>3:] 33 } 34 35 // Abs returns the absolute value of the Int. 36 func (i Int) Abs() Int { return i.ToFloat().Abs().ToInt() } 37 38 // Add adds two Ints and returns the result. 39 func (i Int) Add(b Int) Int { return i + b } 40 41 // ToBigInt returns the Int as a *big.Int. 42 func (i Int) ToBigInt() *big.Int { return big.NewInt(i.AsInt64()) } 43 44 // Div divides two Ints and returns the result. 45 func (i Int) Div(b Int) Int { return i / b } 46 47 // Eq checks if two Ints are equal. 48 func (i Int) Eq(b Int) bool { return i == b } 49 50 // Gt checks if the Int is greater than the specified Int. 51 func (i Int) Gt(b Int) bool { return i > b } 52 53 // Gte checks if the Int is greater than or equal to the specified Int. 54 func (i Int) Gte(b Int) bool { return i >= b } 55 56 // ToFloat returns the Int as an Float. 57 func (i Int) ToFloat() Float { return Float(i) } 58 59 // ToString returns the Int as an String. 60 func (i Int) ToString() String { return String(strconv.Itoa(int(i))) } 61 62 // Std returns the Int as an int. 63 func (i Int) Std() int { return int(i) } 64 65 // Cmp compares two Ints and returns an cmp.Ordering. 66 func (i Int) Cmp(b Int) cmp.Ordering { return cmp.Cmp(i, b) } 67 68 // AsInt16 returns the Int as an int16. 69 func (i Int) AsInt16() int16 { return int16(i) } 70 71 // AsInt32 returns the Int as an int32. 72 func (i Int) AsInt32() int32 { return int32(i) } 73 74 // AsInt64 returns the Int as an int64. 75 func (i Int) AsInt64() int64 { return int64(i) } 76 77 // AsInt8 returns the Int as an int8. 78 func (i Int) AsInt8() int8 { return int8(i) } 79 80 // IsZero checks if the Int is 0. 81 func (i Int) IsZero() bool { return i.Eq(0) } 82 83 // IsNegative checks if the Int is negative. 84 func (i Int) IsNegative() bool { return i.Lt(0) } 85 86 // IsPositive checks if the Int is positive. 87 func (i Int) IsPositive() bool { return i.Gte(0) } 88 89 // Lt checks if the Int is less than the specified Int. 90 func (i Int) Lt(b Int) bool { return i < b } 91 92 // Lte checks if the Int is less than or equal to the specified Int. 93 func (i Int) Lte(b Int) bool { return i <= b } 94 95 // Mul multiplies two Ints and returns the result. 96 func (i Int) Mul(b Int) Int { return i * b } 97 98 // Ne checks if two Ints are not equal. 99 func (i Int) Ne(b Int) bool { return i != b } 100 101 // Random returns a random Int in the range [0, hi]. 102 func (i Int) Random() Int { return Int(0).RandomRange(i) } 103 104 // Rem returns the remainder of the division between the receiver and the input value. 105 func (i Int) Rem(b Int) Int { return i % b } 106 107 // Sub subtracts two Ints and returns the result. 108 func (i Int) Sub(b Int) Int { return i - b } 109 110 // ToBinary returns the Int as a binary string. 111 func (i Int) ToBinary() String { return String(fmt.Sprintf("%08b", i)) } 112 113 // ToHex returns the Int as a hexadecimal string. 114 func (i Int) ToHex() String { return String(fmt.Sprintf("%x", i)) } 115 116 // ToOctal returns the Int as an octal string. 117 func (i Int) ToOctal() String { return String(fmt.Sprintf("%o", i)) } 118 119 // AsUInt returns the Int as a uint. 120 func (i Int) AsUInt() uint { return uint(i) } 121 122 // AsUInt16 returns the Int as a uint16. 123 func (i Int) AsUInt16() uint16 { return uint16(i) } 124 125 // AsUInt32 returns the Int as a uint32. 126 func (i Int) AsUInt32() uint32 { return uint32(i) } 127 128 // AsUInt64 returns the Int as a uint64. 129 func (i Int) AsUInt64() uint64 { return uint64(i) } 130 131 // AsUInt8 returns the Int as a uint8. 132 func (i Int) AsUInt8() uint8 { return uint8(i) } 133 134 // Print prints the value of the Int to the standard output (console) 135 // and returns the Int unchanged. 136 func (i Int) Print() Int { fmt.Println(i); return i }