forked from JointCloud/pcm-coordinator
118 lines
2.2 KiB
Go
118 lines
2.2 KiB
Go
/*
|
|
|
|
Copyright (c) [2023] [pcm]
|
|
[pcm-coordinator] is licensed under Mulan PSL v2.
|
|
You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
You may obtain a copy of Mulan PSL v2 at:
|
|
http://license.coscl.org.cn/MulanPSL2
|
|
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
See the Mulan PSL v2 for more details.
|
|
|
|
*/
|
|
|
|
package common
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// 求交集
|
|
func Intersect(slice1, slice2 []int64) []int64 {
|
|
m := make(map[int64]int)
|
|
nn := make([]int64, 0)
|
|
for _, v := range slice1 {
|
|
m[v]++
|
|
}
|
|
|
|
for _, v := range slice2 {
|
|
times, _ := m[v]
|
|
if times == 1 {
|
|
nn = append(nn, v)
|
|
}
|
|
}
|
|
return nn
|
|
}
|
|
|
|
func IntersectString(slice1, slice2 []string) []string {
|
|
k := make(map[string]int)
|
|
for _, num := range slice1 {
|
|
k[num]++
|
|
}
|
|
var output []string
|
|
for _, num := range slice2 {
|
|
if k[num] > 0 {
|
|
output = append(output, num)
|
|
k[num]--
|
|
}
|
|
}
|
|
return output
|
|
}
|
|
|
|
func RemoveDuplicates(slc []string) []string {
|
|
keys := make(map[string]bool)
|
|
list := []string{}
|
|
|
|
for _, entry := range slc {
|
|
if _, value := keys[entry]; !value {
|
|
keys[entry] = true
|
|
list = append(list, entry)
|
|
}
|
|
}
|
|
return list
|
|
}
|
|
|
|
func MicsSlice(origin []int64, count int) []int64 {
|
|
tmpOrigin := make([]int64, len(origin))
|
|
copy(tmpOrigin, origin)
|
|
//一定要seed
|
|
rand.Seed(time.Now().Unix())
|
|
rand.Shuffle(len(tmpOrigin), func(i int, j int) {
|
|
tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
|
|
})
|
|
|
|
result := make([]int64, 0, count)
|
|
for index, value := range tmpOrigin {
|
|
if index == count {
|
|
break
|
|
}
|
|
result = append(result, value)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func RoundFloat(val float64, precision uint) float64 {
|
|
ratio := math.Pow(10, float64(precision))
|
|
return math.Round(val*ratio) / ratio
|
|
}
|
|
|
|
func Contains(s []string, e string) bool {
|
|
for _, a := range s {
|
|
if a == e {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func ConcatMultipleSlices[T any](slices [][]T) []T {
|
|
var totalLen int
|
|
|
|
for _, s := range slices {
|
|
totalLen += len(s)
|
|
}
|
|
|
|
result := make([]T, totalLen)
|
|
|
|
var i int
|
|
|
|
for _, s := range slices {
|
|
i += copy(result[i:], s)
|
|
}
|
|
|
|
return result
|
|
}
|