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

compare.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // IsEqualSlice returns true if slices are equal.
  41. func IsEqualSlice(target []string, source []string) bool {
  42. if len(target) != len(source) {
  43. return false
  44. }
  45. if (target == nil) != (source == nil) {
  46. return false
  47. }
  48. sort.Strings(target)
  49. sort.Strings(source)
  50. for i, v := range target {
  51. if v != source[i] {
  52. return false
  53. }
  54. }
  55. return true
  56. }