mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 08:02:40 +08:00
update
This commit is contained in:
@ -5,10 +5,21 @@ import (
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
const hextable = "0123456789ABCDEF"
|
||||
const hexTable = "0123456789ABCDEF"
|
||||
|
||||
type BytesResult []byte
|
||||
|
||||
// FromHexString 从十六进制获取
|
||||
func FromHexString(s string) (BytesResult, error) {
|
||||
b, err := hex.DecodeString(s)
|
||||
return BytesResult(b), err
|
||||
}
|
||||
|
||||
func FromBase64String(s string) (BytesResult, error) {
|
||||
b, err := base64.StdEncoding.DecodeString(s)
|
||||
return BytesResult(b), err
|
||||
}
|
||||
|
||||
func (r BytesResult) Hex() string {
|
||||
return hex.EncodeToString(r)
|
||||
}
|
||||
@ -19,8 +30,8 @@ func (r BytesResult) UppercaseHex() string {
|
||||
|
||||
re := r[:]
|
||||
for _, v := range re {
|
||||
dst[j] = hextable[v>>4]
|
||||
dst[j+1] = hextable[v&0x0f]
|
||||
dst[j] = hexTable[v>>4]
|
||||
dst[j+1] = hexTable[v&0x0f]
|
||||
j += 2
|
||||
}
|
||||
|
||||
|
@ -20,3 +20,8 @@ func Decode(b []byte, out any) error {
|
||||
dec := gob.NewDecoder(buf)
|
||||
return dec.Decode(out)
|
||||
}
|
||||
|
||||
func MsgPackage() {
|
||||
|
||||
// msgpack.NewEncoder()
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
package collections
|
@ -1,11 +0,0 @@
|
||||
package collections
|
||||
|
||||
import "github.com/charlienet/go-mixed/locker"
|
||||
|
||||
type options struct {
|
||||
mu locker.RWLocker
|
||||
}
|
||||
|
||||
func emptyLocker() locker.RWLocker {
|
||||
return locker.EmptyLocker
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package collections
|
||||
|
||||
import "sync"
|
||||
|
||||
type rw_queue[T any] struct {
|
||||
q Queue[T]
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (q *rw_queue[T]) Push(v T) {
|
||||
q.mu.Lock()
|
||||
q.q.Put(v)
|
||||
q.mu.Unlock()
|
||||
}
|
||||
|
||||
func (q *rw_queue[T]) Pop() T {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
return q.q.Poll()
|
||||
}
|
||||
|
||||
func (q *rw_queue[T]) Peek() T {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
return q.q.Peek()
|
||||
}
|
||||
|
||||
func (q *rw_queue[T]) Size() int {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
return q.q.Size()
|
||||
}
|
||||
|
||||
func (q *rw_queue[T]) IsEmpty() bool {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
return q.q.IsEmpty()
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
package collections
|
||||
|
||||
import "sync"
|
||||
|
||||
var _ Stack[string] = &ArrayStack[string]{}
|
||||
|
||||
type Stack[T any] interface {
|
||||
Push(T)
|
||||
Pop() T
|
||||
}
|
||||
|
||||
type ArrayStack[T any] struct {
|
||||
array []T // 底层切片
|
||||
size int // 栈的元素数量
|
||||
lock sync.Mutex // 为了并发安全使用的锁
|
||||
}
|
||||
|
||||
// 初始化堆栈
|
||||
func NewArrayStack[T any]() *ArrayStack[T] {
|
||||
return &ArrayStack[T]{}
|
||||
}
|
||||
|
||||
// 入栈
|
||||
func (s *ArrayStack[T]) Push(v T) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
// 放入切片中,后进的元素放在数组最后面
|
||||
s.array = append(s.array, v)
|
||||
|
||||
// 栈中元素数量+1
|
||||
s.size = s.size + 1
|
||||
}
|
||||
|
||||
func (s *ArrayStack[T]) Pop() T {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
if s.size == 0 {
|
||||
panic("empty")
|
||||
}
|
||||
|
||||
// 栈顶元素
|
||||
v := s.array[s.size-1]
|
||||
|
||||
// 切片收缩,但可能占用空间越来越大
|
||||
//stack.array = stack.array[0 : stack.size-1]
|
||||
|
||||
// 创建新的数组,空间占用不会越来越大,但可能移动元素次数过多
|
||||
newArray := make([]T, s.size-1, s.size-1)
|
||||
for i := 0; i < s.size-1; i++ {
|
||||
newArray[i] = s.array[i]
|
||||
}
|
||||
s.array = newArray
|
||||
|
||||
// 栈中元素数量-1
|
||||
s.size = s.size - 1
|
||||
return v
|
||||
}
|
||||
|
||||
// 获取栈顶元素
|
||||
func (s *ArrayStack[T]) Peek() T {
|
||||
// 栈中元素已空
|
||||
if s.size == 0 {
|
||||
panic("empty")
|
||||
}
|
||||
|
||||
// 栈顶元素值
|
||||
v := s.array[s.size-1]
|
||||
return v
|
||||
}
|
||||
|
||||
// 栈大小
|
||||
func (s *ArrayStack[T]) Size() int {
|
||||
return s.size
|
||||
}
|
||||
|
||||
// 栈是否为空
|
||||
func (s *ArrayStack[T]) IsEmpty() bool {
|
||||
return s.size == 0
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package collections_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/charlienet/go-mixed/collections"
|
||||
)
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
arrayStack := collections.NewArrayStack[string]()
|
||||
arrayStack.Push("cat")
|
||||
arrayStack.Push("dog")
|
||||
arrayStack.Push("hen")
|
||||
|
||||
t.Log("size:", arrayStack.Size())
|
||||
t.Log("pop:", arrayStack.Pop())
|
||||
t.Log("pop:", arrayStack.Pop())
|
||||
t.Log("size:", arrayStack.Size())
|
||||
arrayStack.Push("drag")
|
||||
t.Log("pop:", arrayStack.Pop())
|
||||
arrayStack.Push("test")
|
||||
s := arrayStack.Pop()
|
||||
t.Log(s)
|
||||
}
|
6
go.mod
6
go.mod
@ -13,6 +13,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
|
||||
@ -25,11 +26,15 @@ require (
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/tebeka/strftime v0.1.5 // indirect
|
||||
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alicebob/miniredis/v2 v2.23.0
|
||||
github.com/allegro/bigcache/v3 v3.0.2
|
||||
github.com/alphadose/haxmap v1.0.2
|
||||
github.com/antonfisher/nested-logrus-formatter v1.3.1
|
||||
github.com/coocood/freecache v1.2.2
|
||||
github.com/dlclark/regexp2 v1.7.0
|
||||
@ -38,6 +43,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/stretchr/testify v1.8.0
|
||||
github.com/vmihailenco/go-tinylfu v0.2.2
|
||||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
|
||||
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
|
||||
|
20
go.sum
20
go.sum
@ -1,19 +1,27 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk=
|
||||
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc=
|
||||
github.com/alicebob/miniredis/v2 v2.23.0 h1:+lwAJYjvvdIVg6doFHuotFjueJ/7KY10xo/vm3X3Scw=
|
||||
github.com/alicebob/miniredis/v2 v2.23.0/go.mod h1:XNqvJdQJv5mSuVMc0ynneafpnL/zv52acZ6kqeS0t88=
|
||||
github.com/allegro/bigcache/v3 v3.0.2 h1:AKZCw+5eAaVyNTBmI2fgyPVJhHkdWder3O9IrprcQfI=
|
||||
github.com/allegro/bigcache/v3 v3.0.2/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
|
||||
github.com/alphadose/haxmap v1.0.2 h1:ZZwFf15DcsAz4O+SyqrpH/xeO5Plh7mNRXDM9QIcWQQ=
|
||||
github.com/alphadose/haxmap v1.0.2/go.mod h1:Pq2IXbl9/ytYHfrIAd7rIVtZQ2ezdIhZfvdqOizDeWY=
|
||||
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
|
||||
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
|
||||
github.com/bits-and-blooms/bitset v1.3.3 h1:R1XWiopGiXf66xygsiLpzLo67xEYvMkHw3w+rCOSAwg=
|
||||
github.com/bits-and-blooms/bitset v1.3.3/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/coocood/freecache v1.2.1 h1:/v1CqMq45NFH9mp/Pt142reundeBM0dVUD3osQBeu/U=
|
||||
github.com/coocood/freecache v1.2.1/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
|
||||
github.com/coocood/freecache v1.2.2 h1:UPkJCxhRujykq1jXuwxAPgDHnm6lKGrLZPnuHzgWRtE=
|
||||
github.com/coocood/freecache v1.2.2/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@ -93,6 +101,10 @@ github.com/tebeka/strftime v0.1.5 h1:1NQKN1NiQgkqd/2moD6ySP/5CoZQsKa1d3ZhJ44Jpmg
|
||||
github.com/tebeka/strftime v0.1.5/go.mod h1:29/OidkoWHdEKZqzyDLUyC+LmgDgdHo4WAFCDT7D/Ig=
|
||||
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
|
||||
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
|
||||
github.com/vmihailenco/go-tinylfu v0.2.2 h1:H1eiG6HM36iniK6+21n9LLpzx1G9R3DJa2UjUjbynsI=
|
||||
github.com/vmihailenco/go-tinylfu v0.2.2/go.mod h1:CutYi2Q9puTxfcolkliPq4npPuofg9N9t8JVrjzwa3Q=
|
||||
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 h1:k/gmLsJDWwWqbLCur2yWnJzwQEKRcAHXo6seXGuSwWw=
|
||||
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@ -116,6 +128,7 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@ -124,8 +137,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9w
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
|
@ -43,7 +43,7 @@ func (r *rangeSegment) Contains(ip netip.Addr) bool {
|
||||
return ip.Compare(r.start) >= 0 && ip.Compare(r.end) <= 0
|
||||
}
|
||||
|
||||
// IP范围判断,支持以下规则:
|
||||
// NewRange IP范围判断,支持以下规则:
|
||||
// 单IP地址,如 192.168.100.2
|
||||
// IP范围, 如 192.168.100.120-192.168.100.150
|
||||
// 掩码模式,如 192.168.2.0/24
|
||||
|
@ -2,12 +2,13 @@ package json
|
||||
|
||||
import "github.com/charlienet/go-mixed/bytesconv"
|
||||
|
||||
// StructToJsonIndent 结构转换为带格式字符串
|
||||
func StructToJsonIndent(obj any) string {
|
||||
b, _ := MarshalIndent(obj, "", " ")
|
||||
return bytesconv.BytesToString(b)
|
||||
}
|
||||
|
||||
// 结构转换为json字符串
|
||||
// StructToJson 结构转换为json字符串
|
||||
func StructToJson(obj any) string {
|
||||
b, _ := Marshal(obj)
|
||||
return bytesconv.BytesToString(b)
|
||||
|
@ -11,7 +11,7 @@ type countLocker struct {
|
||||
Count int32
|
||||
}
|
||||
|
||||
// 资源锁
|
||||
// SourceLocker 资源锁
|
||||
type SourceLocker struct {
|
||||
m RWLocker
|
||||
locks map[string]*countLocker
|
||||
@ -43,13 +43,13 @@ func (s *SourceLocker) Lock(key string) {
|
||||
l2.Lock()
|
||||
fmt.Println("二次检查加锁")
|
||||
} else {
|
||||
new := NewLocker()
|
||||
s.locks[key] = &countLocker{Locker: new, Count: 1}
|
||||
n := NewLocker()
|
||||
s.locks[key] = &countLocker{Locker: n, Count: 1}
|
||||
|
||||
s.m.Unlock()
|
||||
|
||||
fmt.Printf("新锁准备加锁:%p\n", new)
|
||||
new.Lock()
|
||||
fmt.Printf("新锁准备加锁:%p\n", n)
|
||||
n.Lock()
|
||||
|
||||
fmt.Println("初始加锁")
|
||||
}
|
||||
@ -85,10 +85,10 @@ func (s *SourceLocker) TryLock(key string) bool {
|
||||
s.m.RUnlock()
|
||||
|
||||
s.m.Lock()
|
||||
new := NewLocker()
|
||||
s.locks[key] = &countLocker{Locker: new, Count: 1}
|
||||
n := NewLocker()
|
||||
s.locks[key] = &countLocker{Locker: n, Count: 1}
|
||||
s.m.Unlock()
|
||||
|
||||
return new.TryLock()
|
||||
return n.TryLock()
|
||||
}
|
||||
}
|
||||
|
18
mathx/int.go
18
mathx/int.go
@ -3,18 +3,32 @@ package mathx
|
||||
import (
|
||||
"github.com/charlienet/go-mixed/expr"
|
||||
"golang.org/x/exp/constraints"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// MaxInt returns the larger one of v1 and v2.
|
||||
// Max returns the larger one of v1 and v2.
|
||||
func Max[T constraints.Ordered](v1, v2 T) T {
|
||||
return expr.Ternary(v1 > v2, v1, v2)
|
||||
}
|
||||
|
||||
// MinInt returns the smaller one of v1 and v2.
|
||||
// Min returns the smaller one of v1 and v2.
|
||||
func Min[T constraints.Ordered](v1, v2 T) T {
|
||||
return expr.Ternary(v1 < v2, v1, v2)
|
||||
}
|
||||
|
||||
func Abs1[T constraints.Signed](n T) T {
|
||||
shift := 63
|
||||
switch unsafe.Sizeof(n) {
|
||||
case 1:
|
||||
shift = 7
|
||||
case 4:
|
||||
shift = 31
|
||||
}
|
||||
|
||||
y := n >> shift
|
||||
return T((n ^ y) - y)
|
||||
}
|
||||
|
||||
func Abs(n int64) int64 {
|
||||
y := n >> 63
|
||||
return (n ^ y) - y
|
||||
|
27
mathx/int_test.go
Normal file
27
mathx/int_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
package mathx_test
|
||||
|
||||
import (
|
||||
"github.com/charlienet/go-mixed/mathx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMin(t *testing.T) {
|
||||
assert.Equal(t, 1, mathx.Min(1, 3))
|
||||
assert.Equal(t, 2, mathx.Min(66, 2))
|
||||
}
|
||||
|
||||
func TestMax(t *testing.T) {
|
||||
assert.Equal(t, 3, mathx.Max(1, 3))
|
||||
assert.Equal(t, 66, mathx.Max(66, 2))
|
||||
}
|
||||
|
||||
func TestAbs(t *testing.T) {
|
||||
assert.Equal(t, 23, mathx.Abs1(23))
|
||||
assert.Equal(t, 23, mathx.Abs1(-23))
|
||||
assert.Equal(t, 0, mathx.Abs1(0))
|
||||
|
||||
var u int8 = -127
|
||||
var exp int8 = 127
|
||||
assert.Equal(t, exp, mathx.Abs1(u))
|
||||
}
|
@ -27,7 +27,7 @@ func NewHashSet[T constraints.Ordered](values ...T) *hash_set[T] {
|
||||
return &set
|
||||
}
|
||||
|
||||
func (s *hash_set[T]) WithSync() *hash_set[T] {
|
||||
func (s *hash_set[T]) Sync() *hash_set[T] {
|
||||
s.lock = locker.NewRWLocker()
|
||||
return s
|
||||
}
|
||||
@ -99,6 +99,10 @@ func (s hash_set[T]) copyToSorted() Set[T] {
|
||||
return orderd
|
||||
}
|
||||
|
||||
func (s *hash_set[T]) Shrink() *hash_set[T] {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *hash_set[T]) Clone() *hash_set[T] {
|
||||
set := NewHashSet[T]()
|
||||
set.Add(s.ToSlice()...)
|
||||
|
@ -29,7 +29,7 @@ func TestContainsAll(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestContainsAny(t *testing.T) {
|
||||
|
||||
sets.NewHashSet("1", "2").Sync()
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
|
@ -47,7 +47,7 @@ func BenchmarkGetId(b *testing.B) {
|
||||
|
||||
func BenchmarkMutiGetId(b *testing.B) {
|
||||
s := CreateSnowflake(11)
|
||||
set := sets.NewHashSet[int64]().WithSync()
|
||||
set := sets.NewHashSet[int64]().Sync()
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for i := 0; p.Next(); i++ {
|
||||
id := s.GetId()
|
||||
|
Reference in New Issue
Block a user