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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "math"
  15. "math/rand"
  16. "time"
  17. )
  18. // 求交集
  19. func Intersect(slice1, slice2 []int64) []int64 {
  20. m := make(map[int64]int)
  21. nn := make([]int64, 0)
  22. for _, v := range slice1 {
  23. m[v]++
  24. }
  25. for _, v := range slice2 {
  26. times, _ := m[v]
  27. if times == 1 {
  28. nn = append(nn, v)
  29. }
  30. }
  31. return nn
  32. }
  33. func IntersectString(slice1, slice2 []string) []string {
  34. k := make(map[string]int)
  35. for _, num := range slice1 {
  36. k[num]++
  37. }
  38. var output []string
  39. for _, num := range slice2 {
  40. if k[num] > 0 {
  41. output = append(output, num)
  42. k[num]--
  43. }
  44. }
  45. return output
  46. }
  47. func RemoveDuplicates(slc []string) []string {
  48. keys := make(map[string]bool)
  49. list := []string{}
  50. for _, entry := range slc {
  51. if _, value := keys[entry]; !value {
  52. keys[entry] = true
  53. list = append(list, entry)
  54. }
  55. }
  56. return list
  57. }
  58. func MicsSlice(origin []int64, count int) []int64 {
  59. tmpOrigin := make([]int64, len(origin))
  60. copy(tmpOrigin, origin)
  61. //一定要seed
  62. rand.Seed(time.Now().Unix())
  63. rand.Shuffle(len(tmpOrigin), func(i int, j int) {
  64. tmpOrigin[i], tmpOrigin[j] = tmpOrigin[j], tmpOrigin[i]
  65. })
  66. result := make([]int64, 0, count)
  67. for index, value := range tmpOrigin {
  68. if index == count {
  69. break
  70. }
  71. result = append(result, value)
  72. }
  73. return result
  74. }
  75. func RoundFloat(val float64, precision uint) float64 {
  76. ratio := math.Pow(10, float64(precision))
  77. return math.Round(val*ratio) / ratio
  78. }
  79. func Contains(s []string, e string) bool {
  80. for _, a := range s {
  81. if a == e {
  82. return true
  83. }
  84. }
  85. return false
  86. }
  87. func ConcatMultipleSlices[T any](slices [][]T) []T {
  88. var totalLen int
  89. for _, s := range slices {
  90. totalLen += len(s)
  91. }
  92. result := make([]T, totalLen)
  93. var i int
  94. for _, s := range slices {
  95. i += copy(result[i:], s)
  96. }
  97. return result
  98. }

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.