Skip to content

Commit

Permalink
Rename filter funcs and clean up docs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Dec 21, 2023
1 parent c1f1d59 commit 1bf1eb3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
24 changes: 13 additions & 11 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (S

*tmp = nil

uniq := filterBack(kvs, func() func(kv KeyValue) bool {
uniq := filteredToFront(kvs, func() func(kv KeyValue) bool {
firstCall := true
var prev Key
return func(kv KeyValue) bool {
Expand All @@ -283,7 +283,7 @@ func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (S
if filter != nil {
// Unique prior to filtering so the returned dropped attributes will
// not also include the deduplicated attributes.
k := filterFront(kvs, filter)
k := filteredToBack(kvs, filter)
if k == len(kvs) {
return Set{equivalent: computeDistinct(kvs[:k])}, nil
}
Expand All @@ -293,10 +293,11 @@ func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (S
return Set{equivalent: computeDistinct(kvs)}, nil
}

// filterFront filters the slice in-place using f. All kept KeyValues are moved
// to the front of slice (in order), and all dropped KeyValues are moved the
// end. The index for the first dropped KeyValue is returned.
func filterFront(slice []KeyValue, f Filter) int {
// filteredToBack filters slice in-place using f. All KeyValues that need to be
// removed are moved to the end. All KeyValues that need to be kept are moved
// (in-order) to the front. The index for the first KeyValue to be removed is
// returned.
func filteredToBack(slice []KeyValue, f Filter) int {
n := len(slice)
if n == 0 {
return 0
Expand All @@ -312,10 +313,11 @@ func filterFront(slice []KeyValue, f Filter) int {
return j + 1
}

// filterBack filters the slice in-place using f. All kept KeyValues are moved
// to the end of slice (in order), and all dropped KeyValues are moved the
// front. The index for the first kept KeyValue is returned.
func filterBack(slice []KeyValue, f Filter) int {
// filteredToFront filters slice in-place using f. All KeyValues that need to
// be removed are moved to the front. All KeyValues that need to be kept are
// moved (in-order) to the back. The index for the first KeyValue to be kept is
// returned.
func filteredToFront(slice []KeyValue, f Filter) int {
n := len(slice)
if n == 0 {
return 0
Expand Down Expand Up @@ -365,7 +367,7 @@ func (l *Set) Filter(re Filter) (Set, []KeyValue) {
end := len(slice) - 1
slice[start], slice[end] = slice[end], slice[start]

div := filterFront(slice[start:end], re) + start
div := filteredToBack(slice[start:end], re) + start
return Set{equivalent: computeDistinct(slice[:div])}, slice[div:]
}

Expand Down
8 changes: 4 additions & 4 deletions attribute/set_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ func TestFilters(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Run("filterFront", func(t *testing.T) {
t.Run("filteredToBack", func(t *testing.T) {
in := make([]KeyValue, len(test.in))
copy(in, test.in)

k := filterFront(in, test.fltr)
k := filteredToBack(in, test.fltr)
require.Equal(t, len(test.kept), k, "partition index")

assert.Equal(t, test.kept, in[:k], "kept")
assert.ElementsMatch(t, test.drop, in[k:], "dropped")
})

t.Run("filterBack", func(t *testing.T) {
t.Run("filteredToFront", func(t *testing.T) {
in := make([]KeyValue, len(test.in))
copy(in, test.in)

k := filterBack(in, test.fltr)
k := filteredToFront(in, test.fltr)
require.Equal(t, len(test.drop), k, "partition index")

assert.ElementsMatch(t, test.drop, in[:k], "dropped")
Expand Down

0 comments on commit 1bf1eb3

Please sign in to comment.