-
Notifications
You must be signed in to change notification settings - Fork 841
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
RM-23161 Implemented LL based Stack & tests #177
Open
Spriithy
wants to merge
2
commits into
Workiva:master
Choose a base branch
from
Spriithy:stack
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright 2015 Workiva, LLC | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/* Package stack implements a simple linked-list based stack */ | ||
package stack | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrEmptyStack = errors.New("stack is empty") | ||
) | ||
|
||
// Stack is an immutable stack | ||
type Stack interface { | ||
// Top returns the top-most item of the stack. | ||
// If the stack is empty, the bool is set to false. | ||
Top() (interface{}, bool) | ||
|
||
// Pop returns the top-most item of the stack and removes it. | ||
// The error is set to ErrEmptyStack should the stack be empty. | ||
Pop() (interface{}, error) | ||
|
||
// Drop drops the top-most item of the stack. | ||
// Error is set to ErrEmptyStack should the stack be empty. | ||
Drop() error | ||
|
||
// Push pushes an item onto the stack. | ||
Push(interface{}) | ||
|
||
// PopWhile creates a channel of interfaces and pops items from the stack | ||
// as long as the predicate passed holds or the stack is emptied. | ||
PopWhile(func(interface{}) bool) []interface{} | ||
|
||
// DropWhile drops items from the stack as long as the predicate passed holds or the stack is emptied. | ||
DropWhile(func(interface{}) bool) | ||
|
||
// IsEmpty returns whether the stack is empty. | ||
IsEmpty() bool | ||
|
||
// Size returns the amount of items in the stack. | ||
Size() uint | ||
|
||
// Clear empties the stack | ||
Clear() | ||
} | ||
|
||
type stack struct { | ||
size uint | ||
top *item | ||
} | ||
|
||
type item struct { | ||
item interface{} | ||
next *item | ||
} | ||
|
||
// Top returns the top-most item of the stack. | ||
// If the stack is empty, the bool is set to false. | ||
func (s *stack) Top() (interface{}, bool) { | ||
if s.top == nil { | ||
return nil, false | ||
} | ||
return s.top.item, true | ||
} | ||
|
||
// Pop returns the top-most item of the stack and removes it. | ||
// The error is set to ErrEmptyStack should the stack be empty. | ||
func (s *stack) Pop() (interface{}, error) { | ||
if s.IsEmpty() { | ||
return nil, ErrEmptyStack | ||
} | ||
|
||
s.size-- | ||
top := s.top | ||
s.top = s.top.next | ||
return top.item, nil | ||
} | ||
|
||
// Drop drops the top-most item of the stack. | ||
// Error is set to ErrEmptyStack should the stack be empty. | ||
func (s *stack) Drop() error { | ||
if s.IsEmpty() { | ||
return ErrEmptyStack | ||
} | ||
|
||
s.size-- | ||
s.top = s.top.next | ||
return nil | ||
} | ||
|
||
// Push pushes an item onto the stack. | ||
func (s *stack) Push(it interface{}) { | ||
s.size++ | ||
s.top = &item{it, s.top} | ||
} | ||
|
||
// PopWhile creates a channel of interfaces and pops items from the stack | ||
// as long as the predicate passed holds or the stack is emptied. | ||
func (s *stack) PopWhile(pred func(interface{}) bool) []interface{} { | ||
its := make([]interface{}, 0) | ||
for !s.IsEmpty() { | ||
// We are sure this cannot return an error | ||
it, _ := s.Top() | ||
if pred(it) { | ||
s.Pop() | ||
its = append(its, it) | ||
continue | ||
} | ||
break | ||
} | ||
return its | ||
} | ||
|
||
// DropWhile drops items from the stack as long as the predicate passed holds or the stack is emptied. | ||
func (s *stack) DropWhile(pred func(interface{}) bool) { | ||
for !s.IsEmpty() { | ||
// We are sure this cannot return an error | ||
it, _ := s.Top() | ||
if pred(it) { | ||
s.Pop() | ||
continue | ||
} | ||
return | ||
} | ||
} | ||
|
||
// IsEmpty returns whether the stack is empty. | ||
func (s *stack) IsEmpty() bool { | ||
return s.size == 0 | ||
} | ||
|
||
// Size returns the amount of items in the stack. | ||
func (s *stack) Size() uint { | ||
return s.size | ||
} | ||
|
||
// Clear empties the stack | ||
func (s *stack) Clear() { | ||
s.size = 0 | ||
s.top = nil | ||
} | ||
|
||
// Empty returns a new empty stack | ||
func Empty() Stack { | ||
return &stack{0, nil} | ||
} |
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,89 @@ | ||
package stack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEmptyStack(t *testing.T) { | ||
assert := assert.New(t) | ||
s := Empty() | ||
top, ok := s.Top() | ||
assert.Nil(top) | ||
assert.False(ok) | ||
|
||
top, err := s.Pop() | ||
assert.Nil(top) | ||
assert.Equal(err, ErrEmptyStack) | ||
|
||
assert.True(s.IsEmpty()) | ||
} | ||
|
||
func TestPushPop(t *testing.T) { | ||
assert := assert.New(t) | ||
s := Empty() | ||
|
||
// stack [10] | ||
s.Push(10) | ||
assert.False(s.IsEmpty()) | ||
top, ok := s.Top() | ||
assert.True(ok) | ||
assert.Equal(top, 10) | ||
assert.Equal(uint(1), s.Size()) | ||
|
||
s.Push(3) | ||
// stack [3 10] | ||
assert.Equal(uint(2), s.Size()) | ||
top, err := s.Pop() | ||
assert.Nil(err) | ||
assert.Equal(top, 3) | ||
assert.Equal(uint(1), s.Size()) | ||
} | ||
|
||
func TestPopDropWhile(t *testing.T) { | ||
assert := assert.New(t) | ||
s := Empty() | ||
for i := 0; i < 11; i++ { | ||
s.Push(i * i) | ||
} | ||
assert.Equal(uint(11), s.Size()) | ||
|
||
pred := func(it interface{}) bool { | ||
return it.(int) >= 64 | ||
} | ||
|
||
its := s.PopWhile(pred) | ||
|
||
for _, it := range its { | ||
assert.True(pred(it)) | ||
} | ||
|
||
assert.Equal(uint(8), s.Size()) | ||
|
||
pred = func(it interface{}) bool { | ||
return s.Size() > 3 | ||
} | ||
|
||
s.DropWhile(pred) | ||
|
||
assert.Equal(uint(3), s.Size()) | ||
} | ||
|
||
func TestClearStack(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
s := Empty() | ||
s.Push("a") | ||
s.Push("b") | ||
s.Push("c") | ||
s.Push("d") | ||
|
||
assert.Equal(uint(4), s.Size()) | ||
top, ok := s.Top() | ||
assert.True(ok) | ||
assert.Equal(top, "d") | ||
|
||
s.Clear() | ||
assert.True(s.IsEmpty()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't persistent/immutable is it? I'm not saying it should be, but you modify the internal state in all the operations