Skip to content

Commit

Permalink
Add TryPop to stack, vector, queue (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
chen3feng authored Sep 2, 2022
1 parent ad4c1af commit a95be8a
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 81 deletions.
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

0 comments on commit a95be8a

Please sign in to comment.