Unify the behaviors of Shard*(...) utility functions
This change introduces a generic version of Shard*(...) utility
functions to prevent future digressions of the methods.
Test: m nothing --no-skip-soong-tests
Change-Id: I825214f0d79fb0549573ce01f298eea5eb87cf17
diff --git a/android/util.go b/android/util.go
index 363b31c..698a856 100644
--- a/android/util.go
+++ b/android/util.go
@@ -524,22 +524,27 @@
return root, suffix, ext
}
-// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
-func ShardPaths(paths Paths, shardSize int) []Paths {
- if len(paths) == 0 {
+func shard[T ~[]E, E any](toShard T, shardSize int) []T {
+ if len(toShard) == 0 {
return nil
}
- ret := make([]Paths, 0, (len(paths)+shardSize-1)/shardSize)
- for len(paths) > shardSize {
- ret = append(ret, paths[0:shardSize])
- paths = paths[shardSize:]
+
+ ret := make([]T, 0, (len(toShard)+shardSize-1)/shardSize)
+ for len(toShard) > shardSize {
+ ret = append(ret, toShard[0:shardSize])
+ toShard = toShard[shardSize:]
}
- if len(paths) > 0 {
- ret = append(ret, paths)
+ if len(toShard) > 0 {
+ ret = append(ret, toShard)
}
return ret
}
+// ShardPaths takes a Paths, and returns a slice of Paths where each one has at most shardSize paths.
+func ShardPaths(paths Paths, shardSize int) []Paths {
+ return shard(paths, shardSize)
+}
+
// ShardString takes a string and returns a slice of strings where the length of each one is
// at most shardSize.
func ShardString(s string, shardSize int) []string {
@@ -560,18 +565,7 @@
// ShardStrings takes a slice of strings, and returns a slice of slices of strings where each one has at most shardSize
// elements.
func ShardStrings(s []string, shardSize int) [][]string {
- if len(s) == 0 {
- return nil
- }
- ret := make([][]string, 0, (len(s)+shardSize-1)/shardSize)
- for len(s) > shardSize {
- ret = append(ret, s[0:shardSize])
- s = s[shardSize:]
- }
- if len(s) > 0 {
- ret = append(ret, s)
- }
- return ret
+ return shard(s, shardSize)
}
// CheckDuplicate checks if there are duplicates in given string list.