You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.go 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package common
  13. import (
  14. "github.com/go-resty/resty/v2"
  15. "math"
  16. "math/rand"
  17. "time"
  18. )
  19. // 求交集
  20. func Intersect(slice1, slice2 []int64) []int64 {
  21. m := make(map[int64]int)
  22. nn := make([]int64, 0)
  23. for _, v := range slice1 {
  24. m[v]++
  25. }
  26. for _, v := range slice2 {
  27. times, _ := m[v]
  28. if times == 1 {
  29. nn = append(nn, v)
  30. }
  31. }
  32. return nn
  33. }
  34. func IntersectString(slice1, slice2 []string) []string {
  35. k := make(map[string]int)
  36. for _, num := range slice1 {
  37. k[num]++
  38. }
  39. var output []string
  40. for _, num := range slice2 {
  41. if k[num] > 0 {
  42. output = append(output, num)
  43. k[num]--
  44. }
  45. }
  46. return output
  47. }
  48. func RemoveDuplicates(slc []string) []string {
  49. keys := make(map[string]bool)
  50. list := []string{}
  51. for _, entry := range slc {
  52. if _, value := keys[entry]; !value {
  53. keys[entry] = true
  54. list = append(list, entry)
  55. }
  56. }
  57. return list
  58. }
  59. func MicsSlice(origin []int64, count int) []int64 {
  60. tmpOrigin := make([]int64, len(origin))
  61. copy(tmpOrigin, origin)
  62. //一定要seed
  63. rand.Seed(time.Now().Unix())
  64. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  65. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  66. })
  67. result := make([]int64, 0, count)
  68. for index, value := range tmpOrigin {
  69. if index == count {
  70. break
  71. }
  72. result = append(result, value)
  73. }
  74. return result
  75. }
  76. func RoundFloat(val float64, precision uint) float64 {
  77. ratio := math.Pow(10, float64(precision))
  78. return math.Round(val*ratio) / ratio
  79. }
  80. func Contains(s []string, e string) bool {
  81. for _, a := range s {
  82. if a == e {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. func ConcatMultipleSlices[T any](slices [][]T) []T {
  89. var totalLen int
  90. for _, s := range slices {
  91. totalLen += len(s)
  92. }
  93. result := make([]T, totalLen)
  94. var i int
  95. for _, s := range slices {
  96. i += copy(result[i:], s)
  97. }
  98. return result
  99. }
  100. func GetRestyRequest(timeoutSeconds int64) *resty.Request {
  101. client := resty.New().SetTimeout(time.Duration(timeoutSeconds) * time.Second)
  102. request := client.R()
  103. return request
  104. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.