Skip to content

Commit

Permalink
Refactor 4 unexported set-operation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 23, 2024
1 parent b2f66a6 commit b2a521c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions combine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func combine[E Elem[E]](
x = sets[0]
xIsSets0 := true

for _, set := range sets[1:] {
y = op(x, set, y)
for _, s := range sets[1:] {
y = op(y, x, s)

if xIsSets0 {
x = nil
Expand Down
10 changes: 6 additions & 4 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import "sort"

// Difference returns the set of elements that are in x, but not in y.
func (x Set[E]) Difference(y Set[E]) Set[E] {
return difference(x, y, nil)
return differenceInto(nil, x, y)
}

// Difference returns the set of elements that are in sets[0], but not in
// any of sets[1:]. If sets is empty, Difference returns an empty set.
func Difference[E Elem[E]](sets ...Set[E]) Set[E] {
return combine(difference, sets...)
return combine(differenceInto, sets...)
}

func difference[E Elem[E]](x, y, out Set[E]) Set[E] {
z := out[:0]
// differenceInto returns the set of elements that are in x, but not in y,
// overwriting z.
func differenceInto[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

inv := false

Expand Down
10 changes: 6 additions & 4 deletions intersection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import "sort"

// Intersection returns the set of elements that are in both x and y.
func (x Set[E]) Intersection(y Set[E]) Set[E] {
return intersection(x, y, nil)
return intersectionInto(nil, x, y)
}

// Intersection returns the set of elements that are in each of sets.
func Intersection[E Elem[E]](sets ...Set[E]) Set[E] {
return combine(intersection, sets...)
return combine(intersectionInto, sets...)
}

func intersection[E Elem[E]](x, y, out Set[E]) Set[E] {
z := out[:0]
// intersectionInto returns the set of elements that are in both x and y,
// overwriting z.
func intersectionInto[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
if len(x) < len(y) {
Expand Down
10 changes: 6 additions & 4 deletions symdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import "sort"
// SymmetricDifference returns the set of elements that are in one of x and y,
// but not in both.
func (x Set[E]) SymmetricDifference(y Set[E]) Set[E] {
return symmetricDifference(x, y, nil)
return symmetricDifferenceInto(nil, x, y)
}

// SymmetricDifference returns the set of elements that are in an odd number
// of sets.
func SymmetricDifference[E Elem[E]](sets ...Set[E]) Set[E] {
return combine(symmetricDifference, sets...)
return combine(symmetricDifferenceInto, sets...)
}

func symmetricDifference[E Elem[E]](x, y, out Set[E]) Set[E] {
z := out[:0]
// symmetricDifferenceInto returns the set of elements that are in one of x and
// y, but not in both, overwriting z.
func symmetricDifferenceInto[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
if len(x) < len(y) {
Expand Down
10 changes: 6 additions & 4 deletions union.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import "sort"

// Union returns the set of elements that are in either x, or y, or both.
func (x Set[E]) Union(y Set[E]) Set[E] {
return union(x, y, nil)
return unionInto(nil, x, y)
}

// Union returns the set of elements that are in any of sets.
func Union[E Elem[E]](sets ...Set[E]) Set[E] {
return combine(union, sets...)
return combine(unionInto, sets...)
}

func union[E Elem[E]](x, y, out Set[E]) Set[E] {
z := out[:0]
// unionInto returns the set of elements that are in either x, or y, or both,
// overwriting z.
func unionInto[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
if len(x) < len(y) {
Expand Down

0 comments on commit b2a521c

Please sign in to comment.