From 2de0ba88fb83c3da000c77b45106d72cf00b024d Mon Sep 17 00:00:00 2001 From: andy2046 Date: Fri, 8 May 2020 20:54:50 +0800 Subject: [PATCH] refactor: add Empty to dll --- docs/dll.md | 24 ++++++++++++------ pkg/dll/dll.go | 12 +++++++++ pkg/dll/dll_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/docs/dll.md b/docs/dll.md index 0519a14..7e4e225 100644 --- a/docs/dll.md +++ b/docs/dll.md @@ -16,6 +16,7 @@ Package dll provides a lock-free implementation of doubly linked list. * [type Element](#Element) * [type List](#List) * [func New() *List](#New) + * [func (l *List) Empty() bool](#List.Empty) * [func (l *List) Init() *List](#List.Init) * [func (l *List) Next(node *Element) *Element](#List.Next) * [func (l *List) PopLeft() interface{}](#List.PopLeft) @@ -78,6 +79,15 @@ New returns an initialized list. +### func (\*List) [Empty](/src/target/dll.go?s=995:1022#L42) +``` go +func (l *List) Empty() bool +``` +Empty returns true if list l is empty, false otherwise + + + + ### func (\*List) [Init](/src/target/dll.go?s=546:573#L25) ``` go func (l *List) Init() *List @@ -87,7 +97,7 @@ Init initializes or clears list l. -### func (\*List) [Next](/src/target/dll.go?s=5215:5258#L246) +### func (\*List) [Next](/src/target/dll.go?s=5438:5481#L258) ``` go func (l *List) Next(node *Element) *Element ``` @@ -96,7 +106,7 @@ Next returns the next list element or nil. -### func (\*List) [PopLeft](/src/target/dll.go?s=2499:2535#L121) +### func (\*List) [PopLeft](/src/target/dll.go?s=2722:2758#L133) ``` go func (l *List) PopLeft() interface{} ``` @@ -105,7 +115,7 @@ PopLeft returns the first element of list l or nil if the list is empty. -### func (\*List) [PopRight](/src/target/dll.go?s=3166:3203#L152) +### func (\*List) [PopRight](/src/target/dll.go?s=3389:3426#L164) ``` go func (l *List) PopRight() interface{} ``` @@ -114,7 +124,7 @@ PopRight returns the last element of list l or nil if the list is empty. -### func (\*List) [Prev](/src/target/dll.go?s=5836:5879#L279) +### func (\*List) [Prev](/src/target/dll.go?s=6059:6102#L291) ``` go func (l *List) Prev(node *Element) *Element ``` @@ -123,7 +133,7 @@ Prev returns the previous list element or nil. -### func (\*List) [PushLeft](/src/target/dll.go?s=3698:3745#L177) +### func (\*List) [PushLeft](/src/target/dll.go?s=3921:3968#L189) ``` go func (l *List) PushLeft(v interface{}) *Element ``` @@ -132,7 +142,7 @@ PushLeft inserts a new element e with value v at the front of list l and returns -### func (\*List) [PushRight](/src/target/dll.go?s=4255:4303#L202) +### func (\*List) [PushRight](/src/target/dll.go?s=4478:4526#L214) ``` go func (l *List) PushRight(v interface{}) *Element ``` @@ -141,7 +151,7 @@ PushRight inserts a new element e with value v at the back of list l and returns -### func (\*List) [Remove](/src/target/dll.go?s=1060:1105#L43) +### func (\*List) [Remove](/src/target/dll.go?s=1283:1328#L55) ``` go func (l *List) Remove(e *Element) interface{} ``` diff --git a/pkg/dll/dll.go b/pkg/dll/dll.go index 9d3f279..8e82c37 100644 --- a/pkg/dll/dll.go +++ b/pkg/dll/dll.go @@ -38,6 +38,18 @@ func (l *List) Init() *List { return l } +// Empty returns true if list l is empty, false otherwise +func (l *List) Empty() bool { + t := l.head.next.getReference() + h := l.tail.prev.getReference() + + if t == l.tail && h == l.head { + return true + } + + return false +} + // Remove removes e from l if e is an element of list l. // It returns the element value e.Value if succeed, nil otherwise func (l *List) Remove(e *Element) interface{} { diff --git a/pkg/dll/dll_test.go b/pkg/dll/dll_test.go index 71e3eff..a843763 100644 --- a/pkg/dll/dll_test.go +++ b/pkg/dll/dll_test.go @@ -11,6 +11,51 @@ var ( lock sync.Mutex ) +func TestDListEmpty(t *testing.T) { + l := New() + if !l.Empty() { + t.Error("new list is empty") + } + + var elm *Element + + elm = l.PushLeft(1) + if l.Empty() { + t.Error("list is not empty") + } + l.Remove(elm) + if !l.Empty() { + t.Error("list is empty") + } + + elm = l.PushRight(2) + if l.Empty() { + t.Error("list is not empty") + } + l.Remove(elm) + if !l.Empty() { + t.Error("list is empty") + } + + elm = l.PushLeft(3) + if l.Empty() { + t.Error("list is not empty") + } + l.PopRight() + if !l.Empty() { + t.Error("list is empty") + } + + elm = l.PushRight(4) + if l.Empty() { + t.Error("list is not empty") + } + l.PopLeft() + if !l.Empty() { + t.Error("list is empty") + } +} + func TestDListNext(t *testing.T) { l := New() start := make(chan struct{}) @@ -58,6 +103,10 @@ func TestDListNext(t *testing.T) { } fmt.Printf("%v.", v) } + + if !l.Empty() { + t.Error("list is empty") + } } func TestDListPrev(t *testing.T) { @@ -107,6 +156,10 @@ func TestDListPrev(t *testing.T) { } fmt.Printf("%v.", v) } + + if !l.Empty() { + t.Error("list is empty") + } } func TestDListPL(t *testing.T) { @@ -161,6 +214,10 @@ func TestDListPL(t *testing.T) { } fmt.Printf("%v.", v) } + + if !l.Empty() { + t.Error("list is empty") + } } func TestDListPR(t *testing.T) { @@ -215,6 +272,10 @@ func TestDListPR(t *testing.T) { } fmt.Printf("%v.", v) } + + if !l.Empty() { + t.Error("list is empty") + } } func record(t *testing.T, v int) {