Skip to content

Commit

Permalink
perf: optimize listpack
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 15, 2024
1 parent 212d0d4 commit c7df670
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
3 changes: 3 additions & 0 deletions internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (lp *ListPack) Size() int {
}

func (lp *ListPack) LPush(data ...string) {
if len(data) > 1 {
slices.Reverse(data)
}
lp.Iterator().Insert(data...)
}

Expand Down
26 changes: 10 additions & 16 deletions internal/list/listpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@ func TestListpack(t *testing.T) {

t.Run("rpush", func(t *testing.T) {
lp := NewListPack()
lp.RPush("A")
lp.RPush("B", "C")
lp.RPush("A", "B", "C")
assert.Equal(lp.Size(), 3)
assert.Equal(lp2list(lp), []string{"A", "B", "C"})
})

t.Run("rpush", func(t *testing.T) {
t.Run("lpush", func(t *testing.T) {
lp := NewListPack()
lp.LPush("A")
lp.LPush("B", "C")
lp.LPush("A", "B", "C")
assert.Equal(lp.Size(), 3)
assert.Equal(lp2list(lp), []string{"B", "C", "A"})
assert.Equal(lp2list(lp), []string{"C", "B", "A"})
})

t.Run("lpop", func(t *testing.T) {
lp := NewListPack()
lp.LPush("A", "B", "C")
lp.RPush("A", "B", "C")

it := lp.Iterator()
// bound check
it.Prev()

val, ok := it.RemoveNext()
assert.Equal(val, "A")
Expand All @@ -66,11 +62,9 @@ func TestListpack(t *testing.T) {

t.Run("rpop", func(t *testing.T) {
lp := NewListPack()
lp.LPush("A", "B", "C")
lp.RPush("A", "B", "C")

it := lp.Iterator().SeekLast()
// bound check
// it.Next()

it.Prev()
val, ok := it.RemoveNext()
Expand All @@ -96,7 +90,7 @@ func TestListpack(t *testing.T) {

t.Run("removeNexts", func(t *testing.T) {
lp := NewListPack()
lp.LPush("aa", "bb", "cc", "dd", "ee")
lp.RPush("aa", "bb", "cc", "dd", "ee")

str, ok := lp.Iterator().RemoveNext()
assert.Equal(str, "aa")
Expand All @@ -115,7 +109,7 @@ func TestListpack(t *testing.T) {

t.Run("replaceNext", func(t *testing.T) {
lp := NewListPack()
lp.LPush("TEST1", "TEST2", "TEST3")
lp.RPush("TEST1", "TEST2", "TEST3")

it := lp.Iterator()
it.ReplaceNext("TEST4")
Expand All @@ -133,11 +127,11 @@ func TestListpack(t *testing.T) {

t.Run("compress", func(t *testing.T) {
lp := NewListPack()
lp.LPush("A", "B", "C", "D", "E")
lp.RPush("A", "B", "C")
lp.Compress()
lp.Compress()
lp.Decompress()
lp.Decompress()
assert.Equal(lp2list(lp), []string{"A", "B", "C", "D", "E"})
assert.Equal(lp2list(lp), []string{"A", "B", "C"})
})
}

0 comments on commit c7df670

Please sign in to comment.