Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

compare.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package util
  5. import "sort"
  6. // Int64Slice attaches the methods of Interface to []int64, sorting in increasing order.
  7. type Int64Slice []int64
  8. func (p Int64Slice) Len() int { return len(p) }
  9. func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] }
  10. func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  11. // IsSliceInt64Eq returns if the two slice has the same elements but different sequences.
  12. func IsSliceInt64Eq(a, b []int64) bool {
  13. if len(a) != len(b) {
  14. return false
  15. }
  16. sort.Sort(Int64Slice(a))
  17. sort.Sort(Int64Slice(b))
  18. for i := 0; i < len(a); i++ {
  19. if a[i] != b[i] {
  20. return false
  21. }
  22. }
  23. return true
  24. }
  25. // ExistsInSlice returns true if string exists in slice.
  26. func ExistsInSlice(target string, slice []string) bool {
  27. i := sort.Search(len(slice),
  28. func(i int) bool { return slice[i] == target })
  29. return i < len(slice)
  30. }
  31. // IsStringInSlice sequential searches if string exists in slice.
  32. func IsStringInSlice(target string, slice []string) bool {
  33. for i := 0; i < len(slice); i++ {
  34. if slice[i] == target {
  35. return true
  36. }
  37. }
  38. return false
  39. }
  40. // IsInt64InSlice sequential searches if int64 exists in slice.
  41. func IsInt64InSlice(target int64, slice []int64) bool {
  42. for i := 0; i < len(slice); i++ {
  43. if slice[i] == target {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. // IsEqualSlice returns true if slices are equal.
  50. func IsEqualSlice(target []string, source []string) bool {
  51. if len(target) != len(source) {
  52. return false
  53. }
  54. if (target == nil) != (source == nil) {
  55. return false
  56. }
  57. sort.Strings(target)
  58. sort.Strings(source)
  59. for i, v := range target {
  60. if v != source[i] {
  61. return false
  62. }
  63. }
  64. return true
  65. }