Skip to content

Commit

Permalink
refactor: add Empty to dll
Browse files Browse the repository at this point in the history
  • Loading branch information
andy2046 committed May 8, 2020
1 parent 2924ba8 commit 2de0ba8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
24 changes: 17 additions & 7 deletions docs/dll.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -78,6 +79,15 @@ New returns an initialized list.



### <a name="List.Empty">func</a> (\*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




### <a name="List.Init">func</a> (\*List) [Init](/src/target/dll.go?s=546:573#L25)
``` go
func (l *List) Init() *List
Expand All @@ -87,7 +97,7 @@ Init initializes or clears list l.



### <a name="List.Next">func</a> (\*List) [Next](/src/target/dll.go?s=5215:5258#L246)
### <a name="List.Next">func</a> (\*List) [Next](/src/target/dll.go?s=5438:5481#L258)
``` go
func (l *List) Next(node *Element) *Element
```
Expand All @@ -96,7 +106,7 @@ Next returns the next list element or nil.



### <a name="List.PopLeft">func</a> (\*List) [PopLeft](/src/target/dll.go?s=2499:2535#L121)
### <a name="List.PopLeft">func</a> (\*List) [PopLeft](/src/target/dll.go?s=2722:2758#L133)
``` go
func (l *List) PopLeft() interface{}
```
Expand All @@ -105,7 +115,7 @@ PopLeft returns the first element of list l or nil if the list is empty.



### <a name="List.PopRight">func</a> (\*List) [PopRight](/src/target/dll.go?s=3166:3203#L152)
### <a name="List.PopRight">func</a> (\*List) [PopRight](/src/target/dll.go?s=3389:3426#L164)
``` go
func (l *List) PopRight() interface{}
```
Expand All @@ -114,7 +124,7 @@ PopRight returns the last element of list l or nil if the list is empty.



### <a name="List.Prev">func</a> (\*List) [Prev](/src/target/dll.go?s=5836:5879#L279)
### <a name="List.Prev">func</a> (\*List) [Prev](/src/target/dll.go?s=6059:6102#L291)
``` go
func (l *List) Prev(node *Element) *Element
```
Expand All @@ -123,7 +133,7 @@ Prev returns the previous list element or nil.



### <a name="List.PushLeft">func</a> (\*List) [PushLeft](/src/target/dll.go?s=3698:3745#L177)
### <a name="List.PushLeft">func</a> (\*List) [PushLeft](/src/target/dll.go?s=3921:3968#L189)
``` go
func (l *List) PushLeft(v interface{}) *Element
```
Expand All @@ -132,7 +142,7 @@ PushLeft inserts a new element e with value v at the front of list l and returns



### <a name="List.PushRight">func</a> (\*List) [PushRight](/src/target/dll.go?s=4255:4303#L202)
### <a name="List.PushRight">func</a> (\*List) [PushRight](/src/target/dll.go?s=4478:4526#L214)
``` go
func (l *List) PushRight(v interface{}) *Element
```
Expand All @@ -141,7 +151,7 @@ PushRight inserts a new element e with value v at the back of list l and returns



### <a name="List.Remove">func</a> (\*List) [Remove](/src/target/dll.go?s=1060:1105#L43)
### <a name="List.Remove">func</a> (\*List) [Remove](/src/target/dll.go?s=1283:1328#L55)
``` go
func (l *List) Remove(e *Element) interface{}
```
Expand Down
12 changes: 12 additions & 0 deletions pkg/dll/dll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
61 changes: 61 additions & 0 deletions pkg/dll/dll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2de0ba8

Please sign in to comment.