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