Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryPop to stack. vector, queue #91

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Test_MakeBuiltinSet2(t *testing.T) {
expectEq(t, s.IsEmpty(), true)
}

func Test_MakeBuiltinSetOf(t *testing.T) {
func Test_SetOf(t *testing.T) {
s := SetOf("hello", "world")
expectEq(t, s.Len(), 2)
}
Expand Down
24 changes: 21 additions & 3 deletions dlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,25 @@ func (l *DList[T]) PushBack(val T) {
}

// PopFront popups a element from the front of the list.
func (l *DList[T]) PopFront() (T, bool) {
func (l *DList[T]) PopFront() T {
r, ok := l.TryPopFront()
if !ok {
panic("DList.PopFront: empty list")
}
return r
}

// PopBack popups a element from the back of the list.
func (l *DList[T]) PopBack() T {
r, ok := l.TryPopBack()
if !ok {
panic("DList.PopBack: empty list")
}
return r
}

// TryPopFront tries to popup a element from the front of the list.
func (l *DList[T]) TryPopFront() (T, bool) {
var val T
if l.length == 0 {
return val, false
Expand All @@ -107,8 +125,8 @@ func (l *DList[T]) PopFront() (T, bool) {
return val, true
}

// PopBack popups a element from the back of the list.
func (l *DList[T]) PopBack() (T, bool) {
// TryPopBack tries to popup a element from the back of the list.
func (l *DList[T]) TryPopBack() (T, bool) {
var val T
if l.length == 0 {
return val, false
Expand Down
30 changes: 18 additions & 12 deletions dlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,52 @@ func Test_DList_PushBack(t *testing.T) {
}

func Test_DList_PopFront(t *testing.T) {
l := NewDList[int]()
_, ok := l.PopFront()
l := NewDListOf(1, 2)
expectEq(t, l.PopFront(), 1)
n, ok := l.TryPopFront()
expectEq(t, n, 2)
expectTrue(t, ok)
n, ok = l.TryPopFront()
expectFalse(t, ok)
expectPanic(t, func() { l.PopFront() })
}

func Test_DList_PopBack(t *testing.T) {
l := NewDList[int]()
_, ok := l.PopBack()
l := NewDListOf(1, 2)
expectEq(t, l.PopBack(), 2)
n, ok := l.TryPopBack()
expectTrue(t, ok)
expectEq(t, n, 1)
n, ok = l.TryPopBack()
expectFalse(t, ok)
expectPanic(t, func() { l.PopBack() })
}

func Test_DList_PushBack_PopFront(t *testing.T) {
l := NewDList[int]()
l.PushBack(1)
v, ok := l.PopFront()
expectTrue(t, ok)
v := l.PopFront()
expectEq(t, v, 1)
}

func Test_DList_PushBack_PopBack(t *testing.T) {
l := NewDList[int]()
l.PushBack(1)
v, ok := l.PopBack()
expectTrue(t, ok)
v := l.PopBack()
expectEq(t, v, 1)
}

func Test_DList_PushFront_PopBack(t *testing.T) {
l := NewDList[int]()
l.PushFront(1)
v, ok := l.PopBack()
expectTrue(t, ok)
v := l.PopBack()
expectEq(t, v, 1)
}

func Test_DList_PushFront_PopFront(t *testing.T) {
l := NewDList[int]()
l.PushFront(1)
v, ok := l.PopFront()
expectTrue(t, ok)
v := l.PopFront()
expectEq(t, v, 1)
}

Expand Down
Loading