summaryrefslogtreecommitdiff
path: root/android/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'android/util.go')
-rw-r--r--android/util.go31
1 files changed, 26 insertions, 5 deletions
diff --git a/android/util.go b/android/util.go
index 3fc4608e0..8591cc63e 100644
--- a/android/util.go
+++ b/android/util.go
@@ -213,21 +213,23 @@ func PrettyConcat(list []string, quote bool, lastSep string) string {
}
// ListSetDifference checks if the two lists contain the same elements. It returns
-// a boolean which is true if there is a difference, and then returns lists of elements
+// a boolean which is true if there is a difference, and then returns lists of unique elements
// that are in l1 but not l2, and l2 but not l1.
func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) {
listsDiffer := false
+ l1 = firstUnique(l1)
+ l2 = firstUnique(l2)
diff1 := []T{}
diff2 := []T{}
m1 := setFromList(l1)
m2 := setFromList(l2)
- for t := range m1 {
+ for _, t := range l1 {
if _, ok := m2[t]; !ok {
diff1 = append(diff1, t)
listsDiffer = true
}
}
- for t := range m2 {
+ for _, t := range l2 {
if _, ok := m1[t]; !ok {
diff2 = append(diff2, t)
listsDiffer = true
@@ -238,8 +240,13 @@ func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) {
// Returns true if the two lists have common elements.
func HasIntersection[T comparable](l1, l2 []T) bool {
- _, a, b := ListSetDifference(l1, l2)
- return len(a)+len(b) < len(setFromList(l1))+len(setFromList(l2))
+ m1 := setFromList(l1)
+ for _, x := range l2 {
+ if _, ok := m1[x]; ok {
+ return true
+ }
+ }
+ return false
}
// Returns true if the given string s is prefixed with any string in the given prefix list.
@@ -308,6 +315,20 @@ func FilterList(list []string, filter []string) (remainder []string, filtered []
return
}
+// FilterListByPrefixes performs the same splitting as FilterList does, but treats the passed
+// filters as prefixes
+func FilterListByPrefix(list []string, filter []string) (remainder []string, filtered []string) {
+ for _, l := range list {
+ if HasAnyPrefix(l, filter) {
+ filtered = append(filtered, l)
+ } else {
+ remainder = append(remainder, l)
+ }
+ }
+
+ return
+}
+
// FilterListPred returns the elements of the given list for which the predicate
// returns true. Order is kept.
func FilterListPred(list []string, pred func(s string) bool) (filtered []string) {