-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make stack implementation more generic and move it to pkg/util
- Loading branch information
Showing
4 changed files
with
108 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package util | ||
|
||
// Stack is a generic stack implementation that supports len() and cap(). | ||
type Stack[T any] []T | ||
|
||
// Push pushes a new element on the stack. | ||
func (s *Stack[T]) Push(element T) { | ||
*s = append(*s, element) | ||
} | ||
|
||
// Peek returns the top element from the stack without removing it. If the stack is | ||
// empty the second return value is false. | ||
func (s *Stack[T]) Peek() (T, bool) { | ||
if len(*s) == 0 { | ||
var zero T | ||
return zero, false | ||
} | ||
return (*s)[len(*s)-1], true | ||
} | ||
|
||
// Pop returns the top element from the stack and removes it. If the stack is empty | ||
// the second return value is false. | ||
func (s *Stack[T]) Pop() (T, bool) { | ||
if len(*s) == 0 { | ||
var zero T | ||
return zero, false | ||
} | ||
i := len(*s) - 1 | ||
|
||
element := (*s)[i] | ||
*s = (*s)[:i] | ||
|
||
return element, true | ||
} | ||
|
||
// IsEmpty returns whether the stack contains elements | ||
func (s *Stack[T]) IsEmpty() bool { | ||
return len(*s) == 0 | ||
} | ||
|
||
// Reset empties the stack while maintaining its capacity | ||
func (s *Stack[T]) Reset() { | ||
*s = (*s)[:0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStack(t *testing.T) { | ||
type testVal struct { | ||
val int | ||
} | ||
|
||
var ( | ||
testStack Stack[*testVal] | ||
val *testVal | ||
) | ||
|
||
assert.Len(t, testStack, 0) | ||
assert.True(t, testStack.IsEmpty(), "testStack expected to be empty") | ||
|
||
_, ok := testStack.Peek() | ||
assert.False(t, ok, "testStack.Peek() expected to return false") | ||
_, ok = testStack.Pop() | ||
assert.False(t, ok, "testStack.Pop() expected to return false") | ||
|
||
testStack.Push(&testVal{1}) | ||
val, ok = testStack.Peek() | ||
require.True(t, ok) | ||
assert.Equal(t, &testVal{1}, val) | ||
|
||
testStack.Push(&testVal{2}) | ||
val, ok = testStack.Peek() | ||
require.True(t, ok) | ||
assert.Equal(t, &testVal{2}, val) | ||
|
||
val, ok = testStack.Pop() | ||
require.True(t, ok) | ||
assert.Equal(t, &testVal{2}, val) | ||
|
||
val, ok = testStack.Pop() | ||
require.True(t, ok) | ||
assert.Equal(t, &testVal{1}, val) | ||
|
||
_, ok = testStack.Pop() | ||
assert.True(t, testStack.IsEmpty(), "testStack expected to be empty") | ||
assert.False(t, ok, "testStack.Pop() expected to return false") | ||
|
||
testStack.Push(&testVal{1}) | ||
testStack.Reset() | ||
assert.True(t, testStack.IsEmpty(), "testStack expected to be empty") | ||
assert.Len(t, testStack, 0) | ||
assert.Greater(t, cap(testStack), 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters