From f0c3b962745bf17a5de3478487ad4a57dc0d54b2 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 7 Jul 2020 01:30:36 +0800 Subject: [PATCH] fix: orderedstring add performance improvements --- pkg/util/stringutils2/sortedstrings.go | 26 ++++++++- pkg/util/stringutils2/sortedstrings_test.go | 61 +++++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/pkg/util/stringutils2/sortedstrings.go b/pkg/util/stringutils2/sortedstrings.go index 1224b4d8e2..b7637f3660 100644 --- a/pkg/util/stringutils2/sortedstrings.go +++ b/pkg/util/stringutils2/sortedstrings.go @@ -29,6 +29,11 @@ func NewSortedStrings(strs []string) SSortedStrings { } func Append(ss SSortedStrings, ele ...string) SSortedStrings { + ss = ss.Append(ele...) + return ss +} + +func (ss SSortedStrings) Append(ele ...string) SSortedStrings { if ss == nil { ss = NewSortedStrings([]string{}) } @@ -38,14 +43,29 @@ func Append(ss SSortedStrings, ele ...string) SSortedStrings { continue } ss = append(ss, e) - for i := len(ss) - 1; i > pos; i -= 1 { - ss[i] = ss[i-1] - } + copy(ss[pos+1:], ss[pos:]) ss[pos] = e } return ss } +func (ss SSortedStrings) Remove(ele ...string) SSortedStrings { + if ss == nil { + return ss + } + for _, e := range ele { + pos, find := ss.Index(e) + if !find { + continue + } + if pos < len(ss)-1 { + copy(ss[pos:], ss[pos+1:]) + } + ss = ss[:len(ss)-1] + } + return ss +} + func (ss SSortedStrings) Index(needle string) (int, bool) { i := 0 j := len(ss) - 1 diff --git a/pkg/util/stringutils2/sortedstrings_test.go b/pkg/util/stringutils2/sortedstrings_test.go index 70985479a4..aa01c6ddba 100644 --- a/pkg/util/stringutils2/sortedstrings_test.go +++ b/pkg/util/stringutils2/sortedstrings_test.go @@ -15,6 +15,7 @@ package stringutils2 import ( + "reflect" "testing" ) @@ -74,3 +75,63 @@ func TestMergeStrings(t *testing.T) { t.Logf("B: %s", ss2) t.Logf("%s", m) } + +func TestSortedStringsAppend(t *testing.T) { + cases := []struct { + in []string + ele []string + want SSortedStrings + }{ + { + in: []string{"Alpha", "Bravo", "Go"}, + ele: []string{"Go2"}, + want: []string{"Alpha", "Bravo", "Go", "Go2"}, + }, + { + in: []string{"Alpha", "Bravo", "Go2"}, + ele: []string{"Go"}, + want: []string{"Alpha", "Bravo", "Go", "Go2"}, + }, + { + in: []string{"Alpha", "Bravo", "Go2"}, + ele: []string{"Aaaa", "Go"}, + want: []string{"Aaaa", "Alpha", "Bravo", "Go", "Go2"}, + }, + } + for _, c := range cases { + got := NewSortedStrings(c.in).Append(c.ele...) + if !reflect.DeepEqual(c.want, got) { + t.Errorf("want: %s got: %s", c.want, got) + } + } +} + +func TestSortedStringsRemove(t *testing.T) { + cases := []struct { + in []string + ele []string + want SSortedStrings + }{ + { + in: []string{"Alpha", "Bravo", "Go"}, + ele: []string{"Go", "Go2"}, + want: []string{"Alpha", "Bravo"}, + }, + { + in: []string{"Alpha", "Bravo", "Go2"}, + ele: []string{"Go"}, + want: []string{"Alpha", "Bravo", "Go2"}, + }, + { + in: []string{"Alpha", "Bravo", "Go", "Go2"}, + ele: []string{"Aaaa", "Alpha"}, + want: []string{"Bravo", "Go", "Go2"}, + }, + } + for _, c := range cases { + got := NewSortedStrings(c.in).Remove(c.ele...) + if !reflect.DeepEqual(c.want, got) { + t.Errorf("want: %s got: %s", c.want, got) + } + } +}