| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 16 |  | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 17 | import ( | 
| Cole Faust | efc7012 | 2024-01-30 14:42:12 -0800 | [diff] [blame] | 18 | "cmp" | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 19 | "fmt" | 
| Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 20 | "path/filepath" | 
| Inseob Kim | 1a365c6 | 2019-06-08 15:47:51 +0900 | [diff] [blame] | 21 | "reflect" | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 22 | "regexp" | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 23 | "runtime" | 
|  | 24 | "sort" | 
|  | 25 | "strings" | 
| Colin Cross | 31a6745 | 2023-11-02 16:57:08 -0700 | [diff] [blame] | 26 | "sync" | 
| Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | "github.com/google/blueprint/proptools" | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 29 | ) | 
| Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 30 |  | 
| Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 31 | // CopyOf returns a new slice that has the same contents as s. | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 32 | func CopyOf[T any](s []T) []T { | 
| Spandan Das | cc4da76 | 2023-04-27 19:34:08 +0000 | [diff] [blame] | 33 | // If the input is nil, return nil and not an empty list | 
|  | 34 | if s == nil { | 
|  | 35 | return s | 
|  | 36 | } | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 37 | return append([]T{}, s...) | 
| Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 40 | // Concat returns a new slice concatenated from the two input slices. It does not change the input | 
| Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 41 | // slices. | 
| Wen-yi Chu | 41326c1 | 2023-09-22 03:58:59 +0000 | [diff] [blame] | 42 | func Concat[T any](s1, s2 []T) []T { | 
|  | 43 | res := make([]T, 0, len(s1)+len(s2)) | 
|  | 44 | res = append(res, s1...) | 
|  | 45 | res = append(res, s2...) | 
| Jiakai Zhang | 8fe3a41 | 2023-02-23 17:37:16 +0000 | [diff] [blame] | 46 | return res | 
|  | 47 | } | 
|  | 48 |  | 
| Joe Onorato | 2f99c47 | 2023-06-21 18:10:28 -0700 | [diff] [blame] | 49 | // JoinPathsWithPrefix converts the paths to strings, prefixes them | 
|  | 50 | // with prefix and then joins them separated by " ". | 
|  | 51 | func JoinPathsWithPrefix(paths []Path, prefix string) string { | 
|  | 52 | strs := make([]string, len(paths)) | 
|  | 53 | for i := range paths { | 
|  | 54 | strs[i] = paths[i].String() | 
|  | 55 | } | 
|  | 56 | return JoinWithPrefixAndSeparator(strs, prefix, " ") | 
|  | 57 | } | 
|  | 58 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 59 | // JoinWithPrefix prepends the prefix to each string in the list and | 
|  | 60 | // returns them joined together with " " as separator. | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 61 | func JoinWithPrefix(strs []string, prefix string) string { | 
| Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 62 | return JoinWithPrefixAndSeparator(strs, prefix, " ") | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | // JoinWithPrefixAndSeparator prepends the prefix to each string in the list and | 
|  | 66 | // returns them joined together with the given separator. | 
|  | 67 | func JoinWithPrefixAndSeparator(strs []string, prefix string, sep string) string { | 
| Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 68 | return JoinWithPrefixSuffixAndSeparator(strs, prefix, "", sep) | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | // JoinWithSuffixAndSeparator appends the suffix to each string in the list and | 
|  | 72 | // returns them joined together with the given separator. | 
|  | 73 | func JoinWithSuffixAndSeparator(strs []string, suffix string, sep string) string { | 
|  | 74 | return JoinWithPrefixSuffixAndSeparator(strs, "", suffix, sep) | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | // JoinWithPrefixSuffixAndSeparator appends the prefix/suffix to each string in the list and | 
|  | 78 | // returns them joined together with the given separator. | 
|  | 79 | func JoinWithPrefixSuffixAndSeparator(strs []string, prefix, suffix, sep string) string { | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 80 | if len(strs) == 0 { | 
|  | 81 | return "" | 
|  | 82 | } | 
|  | 83 |  | 
| Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 84 | // Pre-calculate the length of the result | 
|  | 85 | length := 0 | 
|  | 86 | for _, s := range strs { | 
|  | 87 | length += len(s) | 
|  | 88 | } | 
|  | 89 | length += (len(prefix)+len(suffix))*len(strs) + len(sep)*(len(strs)-1) | 
|  | 90 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 91 | var buf strings.Builder | 
| Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 92 | buf.Grow(length) | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 93 | buf.WriteString(prefix) | 
|  | 94 | buf.WriteString(strs[0]) | 
| Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 95 | buf.WriteString(suffix) | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 96 | for i := 1; i < len(strs); i++ { | 
| Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 97 | buf.WriteString(sep) | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 98 | buf.WriteString(prefix) | 
|  | 99 | buf.WriteString(strs[i]) | 
| Jooyung Han | b0864e0 | 2023-11-07 13:08:53 +0900 | [diff] [blame] | 100 | buf.WriteString(suffix) | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 101 | } | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 102 | return buf.String() | 
| Colin Cross | c0b06f1 | 2015-04-08 13:03:43 -0700 | [diff] [blame] | 103 | } | 
| Colin Cross | 9b6826f | 2015-04-10 15:47:33 -0700 | [diff] [blame] | 104 |  | 
| Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 105 | // SortedStringKeys returns the keys of the given map in the ascending order. | 
|  | 106 | // | 
|  | 107 | // Deprecated: Use SortedKeys instead. | 
| Cole Faust | 195c781 | 2023-03-01 14:21:30 -0800 | [diff] [blame] | 108 | func SortedStringKeys[V any](m map[string]V) []string { | 
|  | 109 | return SortedKeys(m) | 
| Colin Cross | 1f8c52b | 2015-06-16 16:38:17 -0700 | [diff] [blame] | 110 | } | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 111 |  | 
| Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 112 | // SortedKeys returns the keys of the given map in the ascending order. | 
| Cole Faust | efc7012 | 2024-01-30 14:42:12 -0800 | [diff] [blame] | 113 | func SortedKeys[T cmp.Ordered, V any](m map[T]V) []T { | 
| Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 114 | if len(m) == 0 { | 
|  | 115 | return nil | 
|  | 116 | } | 
|  | 117 | ret := make([]T, 0, len(m)) | 
|  | 118 | for k := range m { | 
|  | 119 | ret = append(ret, k) | 
|  | 120 | } | 
|  | 121 | sort.Slice(ret, func(i, j int) bool { | 
|  | 122 | return ret[i] < ret[j] | 
|  | 123 | }) | 
|  | 124 | return ret | 
|  | 125 | } | 
|  | 126 |  | 
| Colin Cross | 9eb853b | 2022-02-17 11:13:37 -0800 | [diff] [blame] | 127 | // stringValues returns the values of the given string-valued map in randomized map order. | 
|  | 128 | func stringValues(m interface{}) []string { | 
|  | 129 | v := reflect.ValueOf(m) | 
|  | 130 | if v.Kind() != reflect.Map { | 
|  | 131 | panic(fmt.Sprintf("%#v is not a map", m)) | 
|  | 132 | } | 
|  | 133 | if v.Len() == 0 { | 
|  | 134 | return nil | 
|  | 135 | } | 
|  | 136 | iter := v.MapRange() | 
|  | 137 | s := make([]string, 0, v.Len()) | 
|  | 138 | for iter.Next() { | 
|  | 139 | s = append(s, iter.Value().String()) | 
|  | 140 | } | 
|  | 141 | return s | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | // SortedStringValues returns the values of the given string-valued map in the ascending order. | 
|  | 145 | func SortedStringValues(m interface{}) []string { | 
|  | 146 | s := stringValues(m) | 
|  | 147 | sort.Strings(s) | 
|  | 148 | return s | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | // SortedUniqueStringValues returns the values of the given string-valued map in the ascending order | 
|  | 152 | // with duplicates removed. | 
|  | 153 | func SortedUniqueStringValues(m interface{}) []string { | 
|  | 154 | s := stringValues(m) | 
|  | 155 | return SortedUniqueStrings(s) | 
|  | 156 | } | 
|  | 157 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 158 | // IndexList returns the index of the first occurrence of the given string in the list or -1 | 
| Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 159 | func IndexList[T comparable](t T, list []T) int { | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 160 | for i, l := range list { | 
| Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 161 | if l == t { | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 162 | return i | 
|  | 163 | } | 
|  | 164 | } | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 165 | return -1 | 
|  | 166 | } | 
|  | 167 |  | 
| Sam Delmerico | 1717b3b | 2023-07-18 15:07:24 -0400 | [diff] [blame] | 168 | func InList[T comparable](t T, list []T) bool { | 
|  | 169 | return IndexList(t, list) != -1 | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 172 | func setFromList[T comparable](l []T) map[T]bool { | 
|  | 173 | m := make(map[T]bool, len(l)) | 
|  | 174 | for _, t := range l { | 
|  | 175 | m[t] = true | 
|  | 176 | } | 
|  | 177 | return m | 
|  | 178 | } | 
|  | 179 |  | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 180 | // ListSetDifference checks if the two lists contain the same elements. It returns | 
|  | 181 | // a boolean which is true if there is a difference, and then returns lists of elements | 
|  | 182 | // that are in l1 but not l2, and l2 but not l1. | 
|  | 183 | func ListSetDifference[T comparable](l1, l2 []T) (bool, []T, []T) { | 
|  | 184 | listsDiffer := false | 
|  | 185 | diff1 := []T{} | 
|  | 186 | diff2 := []T{} | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 187 | m1 := setFromList(l1) | 
|  | 188 | m2 := setFromList(l2) | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 189 | for t := range m1 { | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 190 | if _, ok := m2[t]; !ok { | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 191 | diff1 = append(diff1, t) | 
|  | 192 | listsDiffer = true | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 193 | } | 
|  | 194 | } | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 195 | for t := range m2 { | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 196 | if _, ok := m1[t]; !ok { | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 197 | diff2 = append(diff2, t) | 
|  | 198 | listsDiffer = true | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 199 | } | 
|  | 200 | } | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 201 | return listsDiffer, diff1, diff2 | 
| Sam Delmerico | 4e115cc | 2023-01-19 15:36:52 -0500 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
| Jihoon Kang | b7e1a48 | 2024-06-26 22:11:02 +0000 | [diff] [blame] | 204 | // Returns true if the two lists have common elements. | 
|  | 205 | func HasIntersection[T comparable](l1, l2 []T) bool { | 
|  | 206 | _, a, b := ListSetDifference(l1, l2) | 
|  | 207 | return len(a)+len(b) < len(setFromList(l1))+len(setFromList(l2)) | 
|  | 208 | } | 
|  | 209 |  | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 210 | // Returns true if the given string s is prefixed with any string in the given prefix list. | 
| Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 211 | func HasAnyPrefix(s string, prefixList []string) bool { | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 212 | for _, prefix := range prefixList { | 
|  | 213 | if strings.HasPrefix(s, prefix) { | 
|  | 214 | return true | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 | return false | 
|  | 218 | } | 
|  | 219 |  | 
| Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 220 | // Returns true if any string in the given list has the given substring. | 
|  | 221 | func SubstringInList(list []string, substr string) bool { | 
|  | 222 | for _, s := range list { | 
|  | 223 | if strings.Contains(s, substr) { | 
|  | 224 | return true | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 | return false | 
|  | 228 | } | 
|  | 229 |  | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 230 | // Returns true if any string in the given list has the given prefix. | 
| Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 231 | func PrefixInList(list []string, prefix string) bool { | 
| Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 232 | for _, s := range list { | 
| Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 233 | if strings.HasPrefix(s, prefix) { | 
|  | 234 | return true | 
|  | 235 | } | 
|  | 236 | } | 
|  | 237 | return false | 
|  | 238 | } | 
|  | 239 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 240 | // Returns true if any string in the given list has the given suffix. | 
|  | 241 | func SuffixInList(list []string, suffix string) bool { | 
|  | 242 | for _, s := range list { | 
|  | 243 | if strings.HasSuffix(s, suffix) { | 
|  | 244 | return true | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 | return false | 
|  | 248 | } | 
|  | 249 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 250 | // IndexListPred returns the index of the element which in the given `list` satisfying the predicate, or -1 if there is no such element. | 
|  | 251 | func IndexListPred(pred func(s string) bool, list []string) int { | 
|  | 252 | for i, l := range list { | 
|  | 253 | if pred(l) { | 
|  | 254 | return i | 
|  | 255 | } | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | return -1 | 
|  | 259 | } | 
|  | 260 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 261 | // FilterList divides the string list into two lists: one with the strings belonging | 
|  | 262 | // to the given filter list, and the other with the remaining ones | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 263 | func FilterList(list []string, filter []string) (remainder []string, filtered []string) { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 264 | // InList is O(n). May be worth using more efficient lookup for longer lists. | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 265 | for _, l := range list { | 
|  | 266 | if InList(l, filter) { | 
|  | 267 | filtered = append(filtered, l) | 
|  | 268 | } else { | 
|  | 269 | remainder = append(remainder, l) | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | return | 
|  | 274 | } | 
|  | 275 |  | 
| Martin Stjernholm | 1461c4d | 2021-03-27 19:04:05 +0000 | [diff] [blame] | 276 | // FilterListPred returns the elements of the given list for which the predicate | 
|  | 277 | // returns true. Order is kept. | 
|  | 278 | func FilterListPred(list []string, pred func(s string) bool) (filtered []string) { | 
|  | 279 | for _, l := range list { | 
|  | 280 | if pred(l) { | 
|  | 281 | filtered = append(filtered, l) | 
|  | 282 | } | 
|  | 283 | } | 
|  | 284 | return | 
|  | 285 | } | 
|  | 286 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 287 | // RemoveListFromList removes the strings belonging to the filter list from the | 
|  | 288 | // given list and returns the result | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 289 | func RemoveListFromList(list []string, filter_out []string) (result []string) { | 
|  | 290 | result = make([]string, 0, len(list)) | 
|  | 291 | for _, l := range list { | 
|  | 292 | if !InList(l, filter_out) { | 
|  | 293 | result = append(result, l) | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 | return | 
|  | 297 | } | 
|  | 298 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 299 | // RemoveFromList removes given string from the string list. | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 300 | func RemoveFromList(s string, list []string) (bool, []string) { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 301 | result := make([]string, 0, len(list)) | 
|  | 302 | var removed bool | 
|  | 303 | for _, item := range list { | 
|  | 304 | if item != s { | 
|  | 305 | result = append(result, item) | 
|  | 306 | } else { | 
|  | 307 | removed = true | 
| Logan Chien | 7922ab8 | 2018-03-06 18:29:27 +0800 | [diff] [blame] | 308 | } | 
|  | 309 | } | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 310 | return removed, result | 
| Colin Cross | b4330e2 | 2017-12-22 15:47:09 -0800 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
| Ivan Lozano | 0a468a4 | 2024-05-13 21:03:34 -0400 | [diff] [blame] | 313 | // FirstUniqueFunc returns all unique elements of a slice, keeping the first copy of | 
|  | 314 | // each.  It does not modify the input slice. The eq function should return true | 
|  | 315 | // if two elements can be considered equal. | 
|  | 316 | func FirstUniqueFunc[SortableList ~[]Sortable, Sortable any](list SortableList, eq func(a, b Sortable) bool) SortableList { | 
|  | 317 | k := 0 | 
|  | 318 | outer: | 
|  | 319 | for i := 0; i < len(list); i++ { | 
|  | 320 | for j := 0; j < k; j++ { | 
|  | 321 | if eq(list[i], list[j]) { | 
|  | 322 | continue outer | 
|  | 323 | } | 
|  | 324 | } | 
|  | 325 | list[k] = list[i] | 
|  | 326 | k++ | 
|  | 327 | } | 
|  | 328 | return list[:k] | 
|  | 329 | } | 
|  | 330 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 331 | // FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 332 | // each.  It does not modify the input slice. | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 333 | func FirstUniqueStrings(list []string) []string { | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 334 | return firstUnique(list) | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 337 | // firstUnique returns all unique elements of a slice, keeping the first copy of each.  It | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 338 | // does not modify the input slice. | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 339 | func firstUnique[T comparable](slice []T) []T { | 
| Colin Cross | 48016d5 | 2023-06-27 09:45:26 -0700 | [diff] [blame] | 340 | // Do not modify the input in-place, operate on a copy instead. | 
|  | 341 | slice = CopyOf(slice) | 
|  | 342 | return firstUniqueInPlace(slice) | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | // firstUniqueInPlace returns all unique elements of a slice, keeping the first copy of | 
|  | 346 | // each.  It modifies the slice contents in place, and returns a subslice of the original | 
|  | 347 | // slice. | 
|  | 348 | func firstUniqueInPlace[T comparable](slice []T) []T { | 
|  | 349 | // 128 was chosen based on BenchmarkFirstUniqueStrings results. | 
|  | 350 | if len(slice) > 128 { | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 351 | return firstUniqueMap(slice) | 
|  | 352 | } | 
|  | 353 | return firstUniqueList(slice) | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | // firstUniqueList is an implementation of firstUnique using an O(N^2) list comparison to look for | 
|  | 357 | // duplicates. | 
|  | 358 | func firstUniqueList[T any](in []T) []T { | 
|  | 359 | writeIndex := 0 | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 360 | outer: | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 361 | for readIndex := 0; readIndex < len(in); readIndex++ { | 
|  | 362 | for compareIndex := 0; compareIndex < writeIndex; compareIndex++ { | 
|  | 363 | if interface{}(in[readIndex]) == interface{}(in[compareIndex]) { | 
|  | 364 | // The value at readIndex already exists somewhere in the output region | 
|  | 365 | // of the slice before writeIndex, skip it. | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 366 | continue outer | 
|  | 367 | } | 
|  | 368 | } | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 369 | if readIndex != writeIndex { | 
|  | 370 | in[writeIndex] = in[readIndex] | 
|  | 371 | } | 
|  | 372 | writeIndex++ | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 373 | } | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 374 | return in[0:writeIndex] | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 377 | // firstUniqueMap is an implementation of firstUnique using an O(N) hash set lookup to look for | 
|  | 378 | // duplicates. | 
|  | 379 | func firstUniqueMap[T comparable](in []T) []T { | 
|  | 380 | writeIndex := 0 | 
|  | 381 | seen := make(map[T]bool, len(in)) | 
|  | 382 | for readIndex := 0; readIndex < len(in); readIndex++ { | 
|  | 383 | if _, exists := seen[in[readIndex]]; exists { | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 384 | continue | 
|  | 385 | } | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 386 | seen[in[readIndex]] = true | 
|  | 387 | if readIndex != writeIndex { | 
|  | 388 | in[writeIndex] = in[readIndex] | 
|  | 389 | } | 
|  | 390 | writeIndex++ | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 391 | } | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 392 | return in[0:writeIndex] | 
|  | 393 | } | 
|  | 394 |  | 
| Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 395 | // ReverseSliceInPlace reverses the elements of a slice in place and returns it. | 
|  | 396 | func ReverseSliceInPlace[T any](in []T) []T { | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 397 | for i, j := 0, len(in)-1; i < j; i, j = i+1, j-1 { | 
|  | 398 | in[i], in[j] = in[j], in[i] | 
|  | 399 | } | 
| Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 400 | return in | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
| Colin Cross | b5e3f7d | 2023-07-06 15:37:53 -0700 | [diff] [blame] | 403 | // ReverseSlice returns a copy of a slice in reverse order. | 
|  | 404 | func ReverseSlice[T any](in []T) []T { | 
|  | 405 | if in == nil { | 
|  | 406 | return in | 
|  | 407 | } | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 408 | out := make([]T, len(in)) | 
|  | 409 | for i := 0; i < len(in); i++ { | 
|  | 410 | out[i] = in[len(in)-1-i] | 
|  | 411 | } | 
|  | 412 | return out | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 415 | // LastUniqueStrings returns all unique elements of a slice of strings, keeping the last copy of | 
|  | 416 | // each.  It modifies the slice contents in place, and returns a subslice of the original slice. | 
|  | 417 | func LastUniqueStrings(list []string) []string { | 
|  | 418 | totalSkip := 0 | 
|  | 419 | for i := len(list) - 1; i >= totalSkip; i-- { | 
|  | 420 | skip := 0 | 
|  | 421 | for j := i - 1; j >= totalSkip; j-- { | 
|  | 422 | if list[i] == list[j] { | 
|  | 423 | skip++ | 
|  | 424 | } else { | 
|  | 425 | list[j+skip] = list[j] | 
|  | 426 | } | 
|  | 427 | } | 
|  | 428 | totalSkip += skip | 
|  | 429 | } | 
|  | 430 | return list[totalSkip:] | 
|  | 431 | } | 
|  | 432 |  | 
| Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 433 | // SortedUniqueStrings returns what the name says | 
|  | 434 | func SortedUniqueStrings(list []string) []string { | 
| Spandan Das | 8a8714c | 2023-04-25 18:03:54 +0000 | [diff] [blame] | 435 | // FirstUniqueStrings creates a copy of `list`, so the input remains untouched. | 
| Jooyung Han | e163303 | 2019-08-01 17:41:43 +0900 | [diff] [blame] | 436 | unique := FirstUniqueStrings(list) | 
|  | 437 | sort.Strings(unique) | 
|  | 438 | return unique | 
|  | 439 | } | 
|  | 440 |  | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 441 | // checkCalledFromInit panics if a Go package's init function is not on the | 
|  | 442 | // call stack. | 
|  | 443 | func checkCalledFromInit() { | 
|  | 444 | for skip := 3; ; skip++ { | 
|  | 445 | _, funcName, ok := callerName(skip) | 
|  | 446 | if !ok { | 
|  | 447 | panic("not called from an init func") | 
|  | 448 | } | 
|  | 449 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 450 | if funcName == "init" || strings.HasPrefix(funcName, "init·") || | 
|  | 451 | strings.HasPrefix(funcName, "init.") { | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 452 | return | 
|  | 453 | } | 
|  | 454 | } | 
|  | 455 | } | 
|  | 456 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 457 | // A regex to find a package path within a function name. It finds the shortest string that is | 
|  | 458 | // followed by '.' and doesn't have any '/'s left. | 
|  | 459 | var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) | 
|  | 460 |  | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 461 | // callerName returns the package path and function name of the calling | 
|  | 462 | // function.  The skip argument has the same meaning as the skip argument of | 
|  | 463 | // runtime.Callers. | 
|  | 464 | func callerName(skip int) (pkgPath, funcName string, ok bool) { | 
|  | 465 | var pc [1]uintptr | 
|  | 466 | n := runtime.Callers(skip+1, pc[:]) | 
|  | 467 | if n != 1 { | 
|  | 468 | return "", "", false | 
|  | 469 | } | 
|  | 470 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 471 | f := runtime.FuncForPC(pc[0]).Name() | 
|  | 472 | s := pkgPathRe.FindStringSubmatch(f) | 
|  | 473 | if len(s) < 3 { | 
|  | 474 | panic(fmt.Errorf("failed to extract package path and function name from %q", f)) | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 475 | } | 
|  | 476 |  | 
| Colin Cross | 3020fee | 2019-03-19 15:05:17 -0700 | [diff] [blame] | 477 | return s[1], s[2], true | 
| Dan Willemsen | b1957a5 | 2016-06-23 23:44:54 -0700 | [diff] [blame] | 478 | } | 
| Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 479 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 480 | // GetNumericSdkVersion removes the first occurrence of system_ in a string, | 
|  | 481 | // which is assumed to be something like "system_1.2.3" | 
| Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 482 | func GetNumericSdkVersion(v string) string { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 483 | return strings.Replace(v, "system_", "", 1) | 
| Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 484 | } | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 485 |  | 
|  | 486 | // copied from build/kati/strutil.go | 
|  | 487 | func substPattern(pat, repl, str string) string { | 
|  | 488 | ps := strings.SplitN(pat, "%", 2) | 
|  | 489 | if len(ps) != 2 { | 
|  | 490 | if str == pat { | 
|  | 491 | return repl | 
|  | 492 | } | 
|  | 493 | return str | 
|  | 494 | } | 
|  | 495 | in := str | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 496 | trimmed := str | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 497 | if ps[0] != "" { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 498 | trimmed = strings.TrimPrefix(in, ps[0]) | 
|  | 499 | if trimmed == in { | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 500 | return str | 
|  | 501 | } | 
|  | 502 | } | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 503 | in = trimmed | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 504 | if ps[1] != "" { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 505 | trimmed = strings.TrimSuffix(in, ps[1]) | 
|  | 506 | if trimmed == in { | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 507 | return str | 
|  | 508 | } | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | rs := strings.SplitN(repl, "%", 2) | 
|  | 512 | if len(rs) != 2 { | 
|  | 513 | return repl | 
|  | 514 | } | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 515 | return rs[0] + trimmed + rs[1] | 
| Jiyong Park | 7f67f48 | 2019-01-05 12:57:48 +0900 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
|  | 518 | // copied from build/kati/strutil.go | 
|  | 519 | func matchPattern(pat, str string) bool { | 
|  | 520 | i := strings.IndexByte(pat, '%') | 
|  | 521 | if i < 0 { | 
|  | 522 | return pat == str | 
|  | 523 | } | 
|  | 524 | return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:]) | 
|  | 525 | } | 
| Ivan Lozano | 022a73b | 2019-09-09 20:29:31 -0700 | [diff] [blame] | 526 |  | 
|  | 527 | var shlibVersionPattern = regexp.MustCompile("(?:\\.\\d+(?:svn)?)+") | 
|  | 528 |  | 
|  | 529 | // splitFileExt splits a file name into root, suffix and ext. root stands for the file name without | 
|  | 530 | // the file extension and the version number (e.g. "libexample"). suffix stands for the | 
|  | 531 | // concatenation of the file extension and the version number (e.g. ".so.1.0"). ext stands for the | 
|  | 532 | // file extension after the version numbers are trimmed (e.g. ".so"). | 
|  | 533 | func SplitFileExt(name string) (string, string, string) { | 
|  | 534 | // Extract and trim the shared lib version number if the file name ends with dot digits. | 
|  | 535 | suffix := "" | 
|  | 536 | matches := shlibVersionPattern.FindAllStringIndex(name, -1) | 
|  | 537 | if len(matches) > 0 { | 
|  | 538 | lastMatch := matches[len(matches)-1] | 
|  | 539 | if lastMatch[1] == len(name) { | 
|  | 540 | suffix = name[lastMatch[0]:lastMatch[1]] | 
|  | 541 | name = name[0:lastMatch[0]] | 
|  | 542 | } | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | // Extract the file name root and the file extension. | 
|  | 546 | ext := filepath.Ext(name) | 
|  | 547 | root := strings.TrimSuffix(name, ext) | 
|  | 548 | suffix = ext + suffix | 
|  | 549 |  | 
|  | 550 | return root, suffix, ext | 
|  | 551 | } | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 552 |  | 
| Jihoon Kang | cd5bfe2 | 2024-04-12 00:19:09 +0000 | [diff] [blame] | 553 | // ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths. | 
|  | 554 | func ShardPaths(paths Paths, shardSize int) []Paths { | 
| Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 555 | return proptools.ShardBySize(paths, shardSize) | 
| Jihoon Kang | cd5bfe2 | 2024-04-12 00:19:09 +0000 | [diff] [blame] | 556 | } | 
|  | 557 |  | 
| Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 558 | // ShardString takes a string and returns a slice of strings where the length of each one is | 
|  | 559 | // at most shardSize. | 
|  | 560 | func ShardString(s string, shardSize int) []string { | 
|  | 561 | if len(s) == 0 { | 
|  | 562 | return nil | 
|  | 563 | } | 
|  | 564 | ret := make([]string, 0, (len(s)+shardSize-1)/shardSize) | 
|  | 565 | for len(s) > shardSize { | 
|  | 566 | ret = append(ret, s[0:shardSize]) | 
|  | 567 | s = s[shardSize:] | 
|  | 568 | } | 
|  | 569 | if len(s) > 0 { | 
|  | 570 | ret = append(ret, s) | 
|  | 571 | } | 
|  | 572 | return ret | 
|  | 573 | } | 
|  | 574 |  | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 575 | // ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize | 
|  | 576 | // elements. | 
|  | 577 | func ShardStrings(s []string, shardSize int) [][]string { | 
| Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 578 | return proptools.ShardBySize(s, shardSize) | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 579 | } | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 580 |  | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 581 | // CheckDuplicate checks if there are duplicates in given string list. | 
|  | 582 | // If there are, it returns first such duplicate and true. | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 583 | func CheckDuplicate(values []string) (duplicate string, found bool) { | 
|  | 584 | seen := make(map[string]string) | 
|  | 585 | for _, v := range values { | 
|  | 586 | if duplicate, found = seen[v]; found { | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 587 | return duplicate, true | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 588 | } | 
|  | 589 | seen[v] = v | 
|  | 590 | } | 
| Sasha Smundak | 1e53392 | 2020-11-19 16:48:18 -0800 | [diff] [blame] | 591 | return "", false | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 592 | } | 
| Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 593 |  | 
|  | 594 | func AddToStringSet(set map[string]bool, items []string) { | 
|  | 595 | for _, item := range items { | 
|  | 596 | set[item] = true | 
|  | 597 | } | 
|  | 598 | } | 
| Colin Cross | 31a6745 | 2023-11-02 16:57:08 -0700 | [diff] [blame] | 599 |  | 
|  | 600 | // SyncMap is a wrapper around sync.Map that provides type safety via generics. | 
|  | 601 | type SyncMap[K comparable, V any] struct { | 
|  | 602 | sync.Map | 
|  | 603 | } | 
|  | 604 |  | 
|  | 605 | // Load returns the value stored in the map for a key, or the zero value if no | 
|  | 606 | // value is present. | 
|  | 607 | // The ok result indicates whether value was found in the map. | 
|  | 608 | func (m *SyncMap[K, V]) Load(key K) (value V, ok bool) { | 
|  | 609 | v, ok := m.Map.Load(key) | 
|  | 610 | if !ok { | 
|  | 611 | return *new(V), false | 
|  | 612 | } | 
|  | 613 | return v.(V), true | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | // Store sets the value for a key. | 
|  | 617 | func (m *SyncMap[K, V]) Store(key K, value V) { | 
|  | 618 | m.Map.Store(key, value) | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | // LoadOrStore returns the existing value for the key if present. | 
|  | 622 | // Otherwise, it stores and returns the given value. | 
|  | 623 | // The loaded result is true if the value was loaded, false if stored. | 
|  | 624 | func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { | 
|  | 625 | v, loaded := m.Map.LoadOrStore(key, value) | 
|  | 626 | return v.(V), loaded | 
|  | 627 | } |