Skip to content

Commit

Permalink
Reduce number of exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
b97tsk committed Jan 23, 2024
1 parent 04a60b3 commit 741616c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 98 deletions.
5 changes: 4 additions & 1 deletion combine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package intervals

func combine[E Elem[E]](
// Combine applies set operation op over sets, returning the end result.
// op can be any of [Difference], [Intersection], [SymmetricDifference] and
// [Union].
func Combine[E Elem[E]](
op func(z, x, y Set[E]) Set[E],
sets ...Set[E],
) Set[E] {
Expand Down
14 changes: 4 additions & 10 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ 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 DifferenceInto(nil, x, y)
return Difference(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(DifferenceInto, sets...)
}

// DifferenceInto returns the set of elements that are in x, but not in y,
// overwriting z. z must not be x or y; z must not be used after.
func DifferenceInto[E Elem[E]](z, x, y Set[E]) Set[E] {
// Difference returns the set of elements that are in x, but not in y,
// overwriting z. z must not be x or y and z must not be used after.
func Difference[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

inv := false
Expand Down
6 changes: 3 additions & 3 deletions difference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestDifference(t *testing.T) {
Set[E]{{1, 5}, {9, 13}, {17, 21}, {25, 29}}.Difference(Set[E]{{5, 25}}),
Set[E]{{1, 5}, {25, 29}},
},
{Difference[E](), Set[E]{}},
{Difference(Set[E]{}), Set[E]{}},
{Combine(Difference[E]), Set[E]{}},
{Combine(Difference, Set[E]{}), Set[E]{}},
{
func() Set[E] {
var x2, x3, x5 Set[E]
Expand All @@ -79,7 +79,7 @@ func TestDifference(t *testing.T) {
x5 = Add(x5, One(E(i)))
}

return Difference(x2, x3, x5)
return Combine(Difference, x2, x3, x5)
}(),
Set[E]{{2, 3}, {4, 5}, {8, 9}, {14, 15}, {16, 17}, {22, 23}, {26, 27}, {28, 29}},
},
Expand Down
26 changes: 1 addition & 25 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,6 @@ func fuzz(f *testing.F, ff func(t *testing.T, x, y Set[elems.Uint8])) {
func FuzzCollect(f *testing.F) {
addRandomSeed(f, 16)

f.Fuzz(func(
t *testing.T,
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 byte,
) {
xs := []byte{x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15}

var x []Interval[elems.Uint8]

for i, j := 0, len(xs); i < j; i += 2 {
x = append(x, Range(elems.Uint8(xs[i]), elems.Uint8(xs[i+1])))
}

if w, z := plainUnion(x), Collect(x...); !z.Equal(w) {
t.Logf("x = %v", x)
t.Logf("collect(x...) = %v", w)
t.Logf("collect(x...) = %v (actual)", z)
t.Fail()
}
})
}

func FuzzCollectInto(f *testing.F) {
addRandomSeed(f, 16)

f.Fuzz(func(
t *testing.T,
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 byte,
Expand All @@ -216,7 +192,7 @@ func FuzzCollectInto(f *testing.F) {

y := slices.Clone(x)

if w, z := plainUnion(x), CollectInto(y, y...); !z.Equal(w) {
if w, z := plainUnion(x), Collect(y, y...); !z.Equal(w) {
t.Logf("x = %v", x)
t.Logf("collect(x...) = %v", w)
t.Logf("collect(x...) into(y) = %v (actual)", z)
Expand Down
13 changes: 4 additions & 9 deletions intersection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ 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 IntersectionInto(nil, x, y)
return Intersection(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(IntersectionInto, sets...)
}

// IntersectionInto returns the set of elements that are in both x and y,
// overwriting z. z must not be x or y; z must not be used after.
func IntersectionInto[E Elem[E]](z, x, y Set[E]) Set[E] {
// Intersection returns the set of elements that are in both x and y,
// overwriting z. z must not be x or y and z must not be used after.
func Intersection[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
Expand Down
6 changes: 3 additions & 3 deletions intersection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestIntersection(t *testing.T) {
Set[E]{{3, 11}, {13, 21}}.Intersection(Set[E]{{1, 5}, {9, 15}, {19, 23}}),
Set[E]{{3, 5}, {9, 11}, {13, 15}, {19, 21}},
},
{Intersection[E](), Set[E]{}},
{Intersection(Set[E]{}), Set[E]{}},
{Combine(Intersection[E]), Set[E]{}},
{Combine(Intersection, Set[E]{}), Set[E]{}},
{
func() Set[E] {
var x2, x3, x5 Set[E]
Expand All @@ -43,7 +43,7 @@ func TestIntersection(t *testing.T) {
x5 = Add(x5, One(E(i)))
}

return Intersection(x2, x3, x5)
return Combine(Intersection, x2, x3, x5)
}(),
Set[E]{{30, 31}, {60, 61}, {90, 91}},
},
Expand Down
26 changes: 9 additions & 17 deletions intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,11 @@ func (r Interval[E]) Set() Set[E] {
// a Set.
type Set[E Elem[E]] []Interval[E]

// Collect returns the set of elements that are in any of s.
//
// Collect performs better if s are sorted in ascending order by the Low field.
func Collect[E Elem[E]](s ...Interval[E]) Set[E] {
return CollectInto(nil, s...)
}

// CollectInto returns the set of elements that are in any of s, overwriting x.
// Collect returns the set of elements that are in any of s, overwriting x.
// x and s can be the same slice. x must not be used after.
//
// CollectInto performs better if s are sorted in ascending order by the Low
// field.
func CollectInto[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {
// Collect performs better if s are sorted in ascending order by the Low field.
func Collect[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {
x = x[:0]

for _, r := range s {
Expand Down Expand Up @@ -105,11 +97,11 @@ func CollectInto[E Elem[E]](x Set[E], s ...Interval[E]) Set[E] {

// Add adds range [r.Low, r.High) into x, returning the modified Set.
func Add[E Elem[E]](x Set[E], r Interval[E]) Set[E] {
return AddRange(x, r.Low, r.High)
return addRange(x, r.Low, r.High)
}

// AddRange adds range [lo, hi) into x, returning the modified Set.
func AddRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
// addRange adds range [lo, hi) into x, returning the modified Set.
func addRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
i := sort.Search(len(x), func(i int) bool { return x[i].Low.Compare(lo) > 0 })
// j := sort.Search(len(x), func(i int) bool { return x[i].High.Compare(hi) > 0 })

Expand Down Expand Up @@ -174,11 +166,11 @@ func AddRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {

// Delete removes range [r.Low, r.High) from x, returning the modified Set.
func Delete[E Elem[E]](x Set[E], r Interval[E]) Set[E] {
return DeleteRange(x, r.Low, r.High)
return deleteRange(x, r.Low, r.High)
}

// DeleteRange removes range [lo, hi) from x, returning the modified Set.
func DeleteRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
// deleteRange removes range [lo, hi) from x, returning the modified Set.
func deleteRange[E Elem[E]](x Set[E], lo, hi E) Set[E] {
i := sort.Search(len(x), func(i int) bool { return x[i].High.Compare(lo) > 0 })
// j := sort.Search(len(x), func(i int) bool { return x[i].Low.Compare(hi) > 0 })

Expand Down
10 changes: 5 additions & 5 deletions intervalset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ func TestCreation(t *testing.T) {
nil,
},
{
Collect(Range[E](1, 5), Range[E](7, 11), Range[E](13, 17)),
Collect(nil, Range[E](1, 5), Range[E](7, 11), Range[E](13, 17)),
Set[E]{{1, 5}, {7, 11}, {13, 17}},
},
{
Collect(Range[E](13, 17), Range[E](7, 11), Range[E](1, 5)),
Collect(nil, Range[E](13, 17), Range[E](7, 11), Range[E](1, 5)),
Set[E]{{1, 5}, {7, 11}, {13, 17}},
},
{
Collect(Range[E](1, 7), Range[E](5, 13), Range[E](11, 17)),
Collect(nil, Range[E](1, 7), Range[E](5, 13), Range[E](11, 17)),
Set[E]{{1, 17}},
},
{
Collect[E](),
Collect[E](nil),
nil,
},
{
Collect(Range[E](5, 1), Range[E](3, 7), Range[E](11, 9), Range[E](5, 1)),
Collect(nil, Range[E](5, 1), Range[E](3, 7), Range[E](11, 9), Range[E](5, 1)),
Set[E]{{3, 7}},
},
}
Expand Down
14 changes: 4 additions & 10 deletions symdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ 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 SymmetricDifferenceInto(nil, x, y)
return SymmetricDifference(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(SymmetricDifferenceInto, sets...)
}

// SymmetricDifferenceInto returns the set of elements that are in one of x and
// y, but not in both, overwriting z. z must not be x or y; z must not be used
// SymmetricDifference returns the set of elements that are in one of x and y,
// but not in both, overwriting z. z must not be x or y and z must not be used
// after.
func SymmetricDifferenceInto[E Elem[E]](z, x, y Set[E]) Set[E] {
func SymmetricDifference[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
Expand Down
6 changes: 3 additions & 3 deletions symdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func TestSymmetricDifference(t *testing.T) {
Set[E]{{1, 5}, {9, 13}, {17, 21}, {25, 29}}.SymmetricDifference(Set[E]{{5, 25}}),
Set[E]{{1, 9}, {13, 17}, {21, 29}},
},
{SymmetricDifference[E](), Set[E]{}},
{SymmetricDifference(Set[E]{}), Set[E]{}},
{Combine(SymmetricDifference[E]), Set[E]{}},
{Combine(SymmetricDifference, Set[E]{}), Set[E]{}},
{
func() Set[E] {
var x2, x3, x5 Set[E]
Expand All @@ -79,7 +79,7 @@ func TestSymmetricDifference(t *testing.T) {
x5 = Add(x5, One(E(i)))
}

return SymmetricDifference(x2, x3, x5)
return Combine(SymmetricDifference, x2, x3, x5)
}(),
Set[E]{{2, 6}, {8, 10}, {14, 15}, {16, 17}, {21, 23}, {25, 29}, {30, 31}},
},
Expand Down
13 changes: 4 additions & 9 deletions union.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ 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 UnionInto(nil, x, y)
return Union(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(UnionInto, sets...)
}

// UnionInto returns the set of elements that are in either x, or y, or both,
// overwriting z. z must not be x or y; z must not be used after.
func UnionInto[E Elem[E]](z, x, y Set[E]) Set[E] {
// Union returns the set of elements that are in either x, or y, or both,
// overwriting z. z must not be x or y and z must not be used after.
func Union[E Elem[E]](z, x, y Set[E]) Set[E] {
z = z[:0]

for {
Expand Down
6 changes: 3 additions & 3 deletions union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestUnion(t *testing.T) {
Set[E]{{3, 11}, {13, 21}}.Union(Set[E]{{1, 5}, {9, 15}, {19, 23}}),
Set[E]{{1, 23}},
},
{Union[E](), Set[E]{}},
{Union(Set[E]{}), Set[E]{}},
{Combine(Union[E]), Set[E]{}},
{Combine(Union, Set[E]{}), Set[E]{}},
{
func() Set[E] {
var x2, x3, x5 Set[E]
Expand All @@ -51,7 +51,7 @@ func TestUnion(t *testing.T) {
x5 = Add(x5, One(E(i)))
}

return Union(x2, x3, x5)
return Combine(Union, x2, x3, x5)
}(),
Set[E]{{2, 7}, {8, 11}, {12, 13}, {14, 17}, {18, 19}, {20, 23}, {24, 29}, {30, 31}},
},
Expand Down

0 comments on commit 741616c

Please sign in to comment.