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.

58 lines
1.4 KiB

  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. // IsEqualSlice returns true if slices are equal.
  32. func IsEqualSlice(target []string, source []string) bool {
  33. if len(target) != len(source) {
  34. return false
  35. }
  36. if (target == nil) != (source == nil) {
  37. return false
  38. }
  39. sort.Strings(target)
  40. sort.Strings(source)
  41. for i, v := range target {
  42. if v != source[i] {
  43. return false
  44. }
  45. }
  46. return true
  47. }