mirror of
https://github.com/anchore/syft
synced 2024-11-13 23:57:07 +00:00
3472b48177
* [wip] add initial poetry.lock relationship support Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * provide generic set for basic types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * dependency resolver should allow for conditional deps Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add tests for poetry lock relationship additions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update schema with python poetry dependency refs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * dep specification data structure should not be recursive in nature Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
// Set represents a generic set type.
|
|
type Set[T comparable] map[T]struct{}
|
|
|
|
// NewSet creates a new empty Set.
|
|
func NewSet[T comparable](start ...T) Set[T] {
|
|
ret := make(Set[T])
|
|
for _, v := range start {
|
|
ret.Add(v)
|
|
}
|
|
return ret
|
|
}
|
|
|
|
// Add adds elements to the set.
|
|
func (s Set[T]) Add(elements ...T) {
|
|
for _, e := range elements {
|
|
s[e] = struct{}{}
|
|
}
|
|
}
|
|
|
|
// Remove removes an element from the set.
|
|
func (s Set[T]) Remove(element T) {
|
|
delete(s, element)
|
|
}
|
|
|
|
// Contains checks if an element is in the set.
|
|
func (s Set[T]) Contains(element T) bool {
|
|
_, ok := s[element]
|
|
return ok
|
|
}
|
|
|
|
// ToSlice returns a sorted slice of elements in the set.
|
|
func (s Set[T]) ToSlice() []T {
|
|
ret := make([]T, len(s))
|
|
idx := 0
|
|
for v := range s {
|
|
ret[idx] = v
|
|
idx++
|
|
}
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
return fmt.Sprintf("%v", ret[i]) < fmt.Sprintf("%v", ret[j])
|
|
})
|
|
return ret
|
|
}
|
|
|
|
// Equals checks if two sets are equal.
|
|
func (s Set[T]) Equals(o Set[T]) bool {
|
|
if len(s) != len(o) {
|
|
return false
|
|
}
|
|
for k := range s {
|
|
if !o.Contains(k) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Empty checks if the set is empty.
|
|
func (s Set[T]) Empty() bool {
|
|
return len(s) == 0
|
|
}
|