Make a couple of other Set API functions nil-safe

This commit is contained in:
Kovid Goyal 2023-09-12 15:37:24 +05:30
parent 2d7bbf60cc
commit 93618842ae
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -84,7 +84,7 @@ func (self *Set[T]) Intersect(other *Set[T]) (ans *Set[T]) {
func (self *Set[T]) Subtract(other *Set[T]) (ans *Set[T]) { func (self *Set[T]) Subtract(other *Set[T]) (ans *Set[T]) {
ans = NewSet[T](self.Len()) ans = NewSet[T](self.Len())
for x := range self.items { for x := range self.items {
if !other.Has(x) { if other == nil || !other.Has(x) {
ans.items[x] = struct{}{} ans.items[x] = struct{}{}
} }
} }
@ -92,6 +92,9 @@ func (self *Set[T]) Subtract(other *Set[T]) (ans *Set[T]) {
} }
func (self *Set[T]) IsSubsetOf(other *Set[T]) bool { func (self *Set[T]) IsSubsetOf(other *Set[T]) bool {
if other == nil {
return self.Len() == 0
}
for x := range self.items { for x := range self.items {
if !other.Has(x) { if !other.Has(x) {
return false return false