summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stack.go14
1 files changed, 8 insertions, 6 deletions
diff --git a/stack.go b/stack.go
index 4f9d4c0..a55edc9 100644
--- a/stack.go
+++ b/stack.go
@@ -21,24 +21,26 @@ func (s *Stack) Insert(v interface{}) {
// Pop value from tail
func (s *Stack) Pop() (v interface{}) {
- if size := len(*s); size > 0 {
- v, *s = (*s)[size-1], (*s)[:size-1]
+ if sp := len(*s) - 1; sp >= 0 {
+ v, *s = (*s)[sp], (*s)[:sp]
}
return v
}
// Peek a top value on Stack
func (s Stack) Peek() (v interface{}) {
- if size := len(s); size > 0 {
- v = s[size-1]
+ if sp := len(s) - 1; sp >= 0 {
+ v = s[sp]
}
return v
}
// Swap two top values
func (s Stack) Swap() {
- if size := len(s); size >= 2 {
- s[size-1], s[size-2] = s[size-2], s[size-1]
+ if sp := len(s); sp >= 2 {
+ a := sp - 1
+ b := sp - 2
+ s[a], s[b] = s[b], s[a]
}
}