Commit d7645d43 by zhengqiuyun86

数组差集算法

parent 9482dd4e
package conf package conf
import "git.168cad.top/zhengqiuyun/rym-util/util" import (
"git.168cad.top/zhengqiuyun/rym-util/exception"
"git.168cad.top/zhengqiuyun/rym-util/util"
)
type Config struct { type Config struct {
ServerName string ServerName string
...@@ -20,7 +23,7 @@ type Config struct { ...@@ -20,7 +23,7 @@ type Config struct {
func BaseConfig(conf Config) { func BaseConfig(conf Config) {
if util.IsEmpty(&conf.ServerName) { if util.IsEmpty(&conf.ServerName) {
panic("ServerName error") exception.AssertThrowable("ServerName error")
} else { } else {
ServerName = conf.ServerName ServerName = conf.ServerName
} }
...@@ -48,33 +51,33 @@ func BaseConfig(conf Config) { ...@@ -48,33 +51,33 @@ func BaseConfig(conf Config) {
MySql = conf.Mysql MySql = conf.Mysql
} }
//Base conf // Base conf
var ServerName string var ServerName string
var Env string var Env string
var HttpPort int var HttpPort int
//log conf // log conf
var LogLevel = "DEBUG" var LogLevel = "DEBUG"
var LogColorful = true var LogColorful = true
var LogResponseLength int var LogResponseLength int
var LogShowSql = true var LogShowSql = true
var LogIndexNamePrefix = "" var LogIndexNamePrefix = ""
//Redis conf // Redis conf
type RedisConf struct { type RedisConf struct {
RedisHost string RedisHost string
RedisPort int RedisPort int
RedisPwd string RedisPwd string
RedisDb int RedisDb int
ReadTimeout int //取超时间 单位秒 默认值为3秒 ReadTimeout int //取超时间 单位秒 默认值为3秒
WriteTimeout int // 写入超时时间 单位秒 默认与读超时时间一致 WriteTimeout int // 写入超时时间 单位秒 默认与读超时时间一致
PoolSize int //最大连接数 默认cpu数*10 PoolSize int //最大连接数 默认cpu数*10
MinIdleConns int //最小空闲连接数 MinIdleConns int //最小空闲连接数
} }
var Redis = RedisConf{} var Redis = RedisConf{}
//ES conf // ES conf
type EsConf struct { type EsConf struct {
ServerHost string ServerHost string
ServerPort int ServerPort int
...@@ -83,7 +86,7 @@ type EsConf struct { ...@@ -83,7 +86,7 @@ type EsConf struct {
var Es = EsConf{} var Es = EsConf{}
//Mysql conf // Mysql conf
type MysqlConf struct { type MysqlConf struct {
Host string Host string
Port int Port int
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
) )
func ThrowsErr(err error) { func ThrowsErr(err error) {
panic(err) panic[error](err)
} }
func ThrowsValid(err error, r interface{}) { func ThrowsValid(err error, r interface{}) {
...@@ -16,10 +16,10 @@ func ThrowsValid(err error, r interface{}) { ...@@ -16,10 +16,10 @@ func ThrowsValid(err error, r interface{}) {
case validator.ValidationErrors: case validator.ValidationErrors:
e := (err).(validator.ValidationErrors) e := (err).(validator.ValidationErrors)
errMsg := getError(e, r) errMsg := getError(e, r)
panic(DError{Code: "REQUEST_PARAMS_ERR", Err: errMsg}) panic[DError](DError{Code: "REQUEST_PARAMS_ERR", Err: errMsg})
break break
default: default:
panic(err) ThrowsErr(err)
break break
} }
} }
...@@ -53,7 +53,7 @@ type DError struct { ...@@ -53,7 +53,7 @@ type DError struct {
} }
func ThrowsErrS(code, err string) { func ThrowsErrS(code, err string) {
panic(DError{Code: code, Err: err}) panic[DError](DError{Code: code, Err: err})
} }
func AssertThrowableErr(throwable bool, err error) { func AssertThrowableErr(throwable bool, err error) {
......
module git.168cad.top/zhengqiuyun/rym-util module git.168cad.top/zhengqiuyun/rym-util
go 1.16 go 1.18
require ( require (
github.com/gin-gonic/gin v1.8.1 github.com/gin-gonic/gin v1.8.1
...@@ -12,6 +12,42 @@ require ( ...@@ -12,6 +12,42 @@ require (
) )
require ( require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/swaggo/swag v1.8.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
require (
github.com/gin-contrib/cors v1.4.0 github.com/gin-contrib/cors v1.4.0
github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.0 // indirect github.com/onsi/gomega v1.20.0 // indirect
......
package util package util
import "time" func Addr[T any](p T) *T {
func Strings(p string) *string {
return &p
}
func Uint64s(p uint64) *uint64 {
return &p
}
func Uint32s(p uint32) *uint32 {
return &p
}
func Uint16s(p uint16) *uint16 {
return &p
}
func Uint8s(p uint8) *uint8 {
return &p
}
func Int64s(p int64) *int64 {
return &p
}
func Int32s(p int32) *int32 {
return &p
}
func Int16s(p int16) *int16 {
return &p
}
func Int8s(p int8) *int8 {
return &p
}
func Ints(p int) *int {
return &p
}
func Float64s(p float64) *float64 {
return &p
}
func Float32s(p float32) *float32 {
return &p
}
func Bytes(p byte) *byte {
return &p
}
func Times(p time.Time) *time.Time {
return &p return &p
} }
package util package util
import ( func ArrayContains[T comparable](array []T, e T) bool {
"time"
)
func ArrayContains(array []string, e string) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsUint64(array []uint64, e uint64) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsUint32(array []uint32, e uint32) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsUint16(array []uint16, e uint16) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsUint8(array []uint8, e uint8) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsInt64(array []int64, e int64) bool {
for _, v := range array { for _, v := range array {
if v == e { if v == e {
return true return true
...@@ -52,147 +8,17 @@ func ArrayContainsInt64(array []int64, e int64) bool { ...@@ -52,147 +8,17 @@ func ArrayContainsInt64(array []int64, e int64) bool {
} }
return false return false
} }
func ArrayContainsInt32(array []int32, e int32) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsInt16(array []int16, e int16) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsInt8(array []int8, e int8) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsInt(array []int, e int) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsFloat64(array []float64, e float64) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsFloat32(array []float32, e float32) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsByte(array []byte, e byte) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func ArrayContainsTime(array []time.Time, e time.Time) bool {
for _, v := range array {
if v == e {
return true
}
}
return false
}
func DifferenceSetInt64(a1, a2 []int64) (d1, d2 []int64) {
var vMap = map[int64]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSetInt32(a1, a2 []int32) (d1, d2 []int32) {
var vMap = map[int32]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSetInt(a1, a2 []int) (d1, d2 []int) {
var vMap = map[int]*int{}
for _, v1 := range a1 {
vMap[v1] = Ints(1)
}
for _, v2 := range a2 {
if vMap[v2] == nil {
vMap[v2] = Ints(2)
} else {
vMap[v2] = Ints(0)
}
}
for k, v := range vMap {
if *v == 1 {
d1 = append(d1, k)
} else if *v == 2 {
d2 = append(d2, k)
}
}
return d1, d2
}
func DifferenceSet(a1, a2 []string) (d1, d2 []string) { func DifferenceSet[K comparable](a1, a2 []K) (d1, d2 []K) {
var vMap = map[string]*int{} var vMap = map[K]*int{}
for _, v1 := range a1 { for _, v1 := range a1 {
vMap[v1] = Ints(1) vMap[v1] = Addr[int](1)
} }
for _, v2 := range a2 { for _, v2 := range a2 {
if vMap[v2] == nil { if vMap[v2] == nil {
vMap[v2] = Ints(2) vMap[v2] = Addr[int](2)
} else { } else {
vMap[v2] = Ints(0) vMap[v2] = Addr[int](0)
} }
} }
for k, v := range vMap { for k, v := range vMap {
......
...@@ -12,13 +12,13 @@ func TestArrayContainsInterface(t *testing.T) { ...@@ -12,13 +12,13 @@ func TestArrayContainsInterface(t *testing.T) {
areaIds = append(areaIds, PareDateTime("2022-02-09 22:09:12")) areaIds = append(areaIds, PareDateTime("2022-02-09 22:09:12"))
areaIds = append(areaIds, PareDateTime("2022-03-09 22:09:12")) areaIds = append(areaIds, PareDateTime("2022-03-09 22:09:12"))
areaIds = append(areaIds, PareDateTime("2022-12-09 22:09:12")) areaIds = append(areaIds, PareDateTime("2022-12-09 22:09:12"))
fmt.Println(ArrayContainsTime(areaIds, PareDateTime("2022-02-10 22:09:12"))) fmt.Println(ArrayContains[time.Time](areaIds, PareDateTime("2022-02-10 22:09:12")))
} }
func TestDifference(t *testing.T) { func TestDifference(t *testing.T) {
a1 := []int64{1, 5, 2, 3} a1 := []int64{1, 5, 2, 3}
a2 := []int64{2, 1, 34, 3, 3, 4} a2 := []int64{2, 1, 34, 3, 3, 4}
d1, d2 := DifferenceSetInt64(a1, a2) d1, d2 := DifferenceSet[int64](a1, a2)
fmt.Println("a1与a2的差集分别是:", d1, d2) fmt.Println("a1与a2的差集分别是:", d1, d2)
} }
......
...@@ -2,102 +2,18 @@ package util ...@@ -2,102 +2,18 @@ package util
import ( import (
"bytes" "bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/exception"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"strconv"
"strings" "strings"
) )
func ParamInt64(c *gin.Context, key string) int64 { func ParamNumber[T any](c *gin.Context, key string) *T {
return int64(ParamInt(c, key)) if val, ok := c.Get(key); ok && val != nil {
} i, _ := val.(T)
return &i
func ParamInt32(c *gin.Context, key string) int32 {
return int32(ParamInt(c, key))
}
func ParamInt16(c *gin.Context, key string) int16 {
return int16(ParamInt(c, key))
}
func ParamInt8(c *gin.Context, key string) int8 {
return int8(ParamInt(c, key))
}
func ParamUint64(c *gin.Context, key string) uint64 {
return uint64(ParamInt(c, key))
}
func ParamUint32(c *gin.Context, key string) uint32 {
return uint32(ParamInt(c, key))
}
func ParamUint16(c *gin.Context, key string) uint16 {
return uint16(ParamInt(c, key))
}
func ParamUint8(c *gin.Context, key string) uint8 {
return uint8(ParamInt(c, key))
}
func ParamInt(c *gin.Context, key string) int {
v := c.Query(key)
if v == "" {
return 0
}
i, err := strconv.Atoi(v)
if err != nil {
exception.ThrowsErrS("PARAMS_ERR", "请传入正确的数字")
} }
return i return nil
}
func ParamFloat64(c *gin.Context, key string) float64 {
v := c.Query(key)
if v == "" {
return 0
}
i, err := strconv.ParseFloat(v, 2)
if err != nil {
exception.ThrowsErrS("PARAMS_ERR", "请传入正确的数字")
}
return i
}
func ParamFloat32(c *gin.Context, key string) float32 {
return float32(ParamFloat64(c, key))
}
func ParamFloat64B(c *gin.Context, key string, bitSize int) float64 {
v := c.Query(key)
if v == "" {
return 0
}
i, err := strconv.ParseFloat(v, bitSize)
if err != nil {
exception.ThrowsErrS("PARAMS_ERR", "请传入正确的数字")
}
return i
}
func ParamFloat32B(c *gin.Context, key string, bitSize int) float32 {
return float32(ParamFloat64B(c, key, bitSize))
}
func Param(c *gin.Context, key string) string {
v, ok := c.GetQuery(key)
if !ok || v == "" {
exception.ThrowsErrS("PARAMS_ERR", fmt.Sprintf("%s不能为空", key))
}
return v
}
func ParamB(c *gin.Context, key string) string {
v := c.Query(key)
return v
} }
const ( const (
......
...@@ -90,11 +90,12 @@ func StrToMapStr(obj string) map[string]string { ...@@ -90,11 +90,12 @@ func StrToMapStr(obj string) map[string]string {
return m return m
} }
func ToObject(txt string, p interface{}) { func ToObject(txt string, p interface{}) error {
err := json.Unmarshal([]byte(txt), p) err := json.Unmarshal([]byte(txt), p)
if err != nil { if err != nil {
panic(err) return err
} }
return nil
} }
func SortKeys(orgMap map[string]interface{}) []string { func SortKeys(orgMap map[string]interface{}) []string {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment