Add SliceToSet() to sliceutils (#6054)

This commit is contained in:
Daniel McIlvaney 2023-09-18 12:03:27 -07:00 коммит произвёл GitHub
Родитель e370dd5350
Коммит 0f7148d5ff
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 59 добавлений и 37 удалений

Просмотреть файл

@ -13,6 +13,7 @@ import (
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/file"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/logger"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/pkggraph"
"github.com/microsoft/CBL-Mariner/toolkit/tools/internal/sliceutils"
"github.com/microsoft/CBL-Mariner/toolkit/tools/scheduler/schedulerutils"
"gonum.org/v1/gonum/graph"
@ -89,7 +90,7 @@ func main() {
nodeListGoal := searchForGoal(graph, goalSearchList)
nodeLists := append(nodeListPkg, append(nodeListSpec, nodeListGoal...)...)
nodeSet := removeDuplicates(nodeLists)
nodeSet := sliceutils.RemoveDuplicatesFromSlice(nodeLists)
if len(nodeSet) == 0 {
logger.Log.Panicf("Could not find any nodes matching pkgs:[%s] or specs:[%s] or goals[%s]", *pkgsToSearch, *specsToSearch, *goalsToSearch)
@ -172,18 +173,6 @@ func searchForSpec(graph *pkggraph.PkgGraph, specs []string) (list []*pkggraph.P
return
}
func removeDuplicates(nodeList []*pkggraph.PkgNode) (uniqueNodeList []*pkggraph.PkgNode) {
nodeMap := make(map[*pkggraph.PkgNode]bool)
for _, n := range nodeList {
nodeMap[n] = true
}
uniqueNodeList = make([]*pkggraph.PkgNode, 0, len(nodeMap))
for key, _ := range nodeMap {
uniqueNodeList = append(uniqueNodeList, key)
}
return
}
func buildRequiresGraph(graphIn *pkggraph.PkgGraph, nodeList []*pkggraph.PkgNode) (graphOut *pkggraph.PkgGraph, root *pkggraph.PkgNode, err error) {
// Make a copy of the graph
newGraph, err := graphIn.DeepCopy()
@ -256,10 +245,7 @@ func isFilteredFile(path, filterFile string) bool {
if err != nil {
logger.Log.Fatalf("Failed to load filter file '%s': %s", filterFile, err)
}
reservedFiles = make(map[string]bool)
for _, f := range reservedFileList {
reservedFiles[f] = true
}
reservedFiles = sliceutils.SliceToSet[string](reservedFileList)
}
base := filepath.Base(path)
@ -449,10 +435,7 @@ func printSpecs(graph *pkggraph.PkgGraph, tree, filter bool, filterFile string,
}
// Contert to list and sort
printLines := []string{}
for s := range results {
printLines = append(printLines, s)
}
printLines := sliceutils.SetToSlice[string](results)
sort.Strings(printLines)
for _, l := range printLines {
fmt.Println(l)

Просмотреть файл

@ -72,6 +72,20 @@ func SetToSlice[T comparable](inputSet map[T]bool) []T {
return outputSlice[:index]
}
// SliceToSet converts a slice of K to a map[K]bool.
func SliceToSet[K comparable](inputSlice []K) (outputSet map[K]bool) {
outputSet = make(map[K]bool, len(inputSlice))
for _, element := range inputSlice {
outputSet[element] = true
}
return outputSet
}
// RemoveDuplicatesFromSlice removes duplicate elements from a slice.
func RemoveDuplicatesFromSlice[K comparable](inputSlice []K) (outputSlice []K) {
return SetToSlice(SliceToSet(inputSlice))
}
func nilCheck(expected interface{}, given interface{}) (checkValid, checkResult bool) {
return (expected == nil || given == nil), (expected == nil && given == nil)
}

Просмотреть файл

@ -100,7 +100,7 @@ func TestStringsSetToSliceShouldCreateEmptySliceFromEmptySet(t *testing.T) {
assert.Empty(t, outputSlice)
}
func TestStringsSetToSliceShouldReturnValuesForAllTrueElementsInSet(t *testing.T) {
func TestSetToSliceShouldReturnValuesForAllTrueElementsInSet(t *testing.T) {
inputSet := map[string]bool{
"A": true,
"B": true,
@ -116,3 +116,41 @@ func TestStringsSetToSliceShouldReturnValuesForAllTrueElementsInSet(t *testing.T
assert.NotContains(t, outputSlice, "X")
assert.NotContains(t, outputSlice, "Y")
}
func TestSliceToSetShouldCreateEmptySetFromNil(t *testing.T) {
outputSet := SliceToSet[string](nil)
assert.NotNil(t, outputSet)
assert.Empty(t, outputSet)
}
func TestSliceToSetShouldCreateEmptySetFromEmptySlice(t *testing.T) {
outputSet := SliceToSet([]string{})
assert.NotNil(t, outputSet)
assert.Empty(t, outputSet)
}
func TestSliceToSetShouldReturnValuesForAllElementsInSlice(t *testing.T) {
inputSlice := []string{"A", "B", "C"}
outputSet := SliceToSet(inputSlice)
assert.NotNil(t, outputSet)
assert.Len(t, outputSet, 3)
assert.Contains(t, outputSet, "A")
assert.Contains(t, outputSet, "B")
assert.Contains(t, outputSet, "C")
assert.NotContains(t, outputSet, "X")
}
func TestShouldRemoveDuplicates(t *testing.T) {
inputSlice := []string{"A", "B", "C", "A", "B", "C"}
outputSlice := RemoveDuplicatesFromSlice(inputSlice)
assert.NotNil(t, outputSlice)
assert.Len(t, outputSlice, 3)
assert.Contains(t, outputSlice, "A")
assert.Contains(t, outputSlice, "B")
assert.Contains(t, outputSlice, "C")
assert.NotContains(t, outputSlice, "X")
}

Просмотреть файл

@ -123,7 +123,7 @@ func calculatePackagesToBuild(packagesNamesToBuild, packagesNamesToRebuild []*pk
}
packageVersToBuild = append(packageVersToBuild, packageVersFromConfig...)
packageVersToBuild = removePackageVersDuplicates(packageVersToBuild)
packageVersToBuild = sliceutils.RemoveDuplicatesFromSlice(packageVersToBuild)
return
}
@ -173,16 +173,6 @@ func filterLocalPackagesOnly(packageVersionsInConfig []*pkgjson.PackageVer, depe
return
}
func removePackageVersDuplicates(packageVers []*pkgjson.PackageVer) []*pkgjson.PackageVer {
uniquePackageVersToBuild := make(map[*pkgjson.PackageVer]bool)
for _, packageVer := range packageVers {
uniquePackageVersToBuild[packageVer] = true
}
return sliceutils.SetToSlice(uniquePackageVersToBuild)
}
// packageNamesToPackages converts the input strings to PackageVer structures that are understood by the graph.
// If a string is a spec name, it will convert it to all packages built from that spec.
// If the string is NOT a spec name, it will check, if the string is a package present in the graph.

Просмотреть файл

@ -44,10 +44,7 @@ type GraphBuildState struct {
// '0' where the subsequent nodes will no longer be rebuilt. 'maxFreshness < 0' will cause unbounded cascading rebuilds,
// while 'maxFreshness = 0' will cause no cascading rebuilds.
func NewGraphBuildState(reservedFiles []string, maxFreshness uint) (g *GraphBuildState) {
filesMap := make(map[string]bool)
for _, file := range reservedFiles {
filesMap[file] = true
}
filesMap := sliceutils.SliceToSet[string](reservedFiles)
return &GraphBuildState{
activeBuilds: make(map[int64]*BuildRequest),