Add function to subtract an entry from an IDList

This commit is contained in:
Bryan Boreham
2018-11-26 15:06:58 +00:00
parent 78fab88d3a
commit 86bcb1153b
2 changed files with 14 additions and 0 deletions

View File

@@ -36,3 +36,8 @@ func (a IDList) Contains(id string) bool {
func (a IDList) Intersection(b IDList) IDList {
return IDList(StringSet(a).Intersection(StringSet(b)))
}
// Minus returns the set with id removed
func (a IDList) Minus(id string) IDList {
return IDList(StringSet(a).Minus(id))
}

View File

@@ -50,6 +50,15 @@ func (s StringSet) Intersection(b StringSet) StringSet {
return result
}
// Minus returns the set with str removed
func (s StringSet) Minus(str string) StringSet {
i := sort.Search(len(s), func(i int) bool { return s[i] >= str })
if i < len(s) && s[i] == str {
return append(s[:i], s[i+1:]...)
}
return s
}
// Equal returns true if a and b have the same contents
func (s StringSet) Equal(b StringSet) bool {
if len(s) != len(b) {