From 1bf1eb3f2e99b35637701342227ca8ac7511976d Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 21 Dec 2023 13:48:58 -0800 Subject: [PATCH] Rename filter funcs and clean up docs --- attribute/set.go | 24 +++++++++++++----------- attribute/set_internal_test.go | 8 ++++---- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/attribute/set.go b/attribute/set.go index 50334cc00502..0238715387cb 100644 --- a/attribute/set.go +++ b/attribute/set.go @@ -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 { @@ -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 } @@ -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 @@ -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 @@ -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:] } diff --git a/attribute/set_internal_test.go b/attribute/set_internal_test.go index 97b727411af9..c4938a25bcc0 100644 --- a/attribute/set_internal_test.go +++ b/attribute/set_internal_test.go @@ -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")