-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.go
84 lines (67 loc) · 1.82 KB
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package collections
import "fmt"
// Stack is an implementation of a stack using a slice.
type Stack[T any] struct {
elems []T
}
// Constructors
// NewStack makes a new Stack with the specified initial capacity.
func NewStack[T any](capacity int) *Stack[T] {
return &Stack[T]{make([]T, 0, capacity)}
}
// AsStack returns a Stack backed by the given slice.
func AsStack[T any](elems []T) *Stack[T] {
return &Stack[T]{elems}
}
// AsSlice returns the underlying slice for this Stack.
func (s *Stack[T]) AsSlice() []T {
return s.elems
}
// Basic (non-mutating) functions
// Size returns the number of elements in this Stack.
func (s *Stack[T]) Size() int {
return len(s.elems)
}
// IsEmpty returns true if this Stack is empty.
func (s *Stack[T]) IsEmpty() bool {
return s.Size() == 0
}
// Capacity returns the current capacity of this Stack.
func (s *Stack[T]) Capacity() int {
return cap(s.elems)
}
// Peek returns the top element of this Stack, without removing it from
// the Stack. It returns an error if the Stack is empty.
func (s *Stack[T]) Peek() (t T, err error) {
if s.IsEmpty() {
err = errStackEmpty
} else {
t = s.elems[len(s.elems)-1]
}
return
}
// Basic (mutating) functions
// Push adds the given element to the top of this Stack.
func (s *Stack[T]) Push(t T) {
s.elems = append(s.elems, t)
}
// Pop removes the top element of the Stack and returns it.
// It returns an error if the Stack is empty.
func (s *Stack[T]) Pop() (t T, err error) {
if s.IsEmpty() {
err = errStackEmpty
} else {
t = s.elems[len(s.elems)-1]
s.elems = s.elems[:len(s.elems)-1]
}
return
}
// Copying functions
// Copy returns a copy of the given Stack.
func (s *Stack[T]) Copy() *Stack[T] {
elemsCp := make([]T, 0, s.Size())
copy(elemsCp, s.elems)
return &Stack[T]{elemsCp}
}
// Errors
var errStackEmpty = fmt.Errorf("stack is empty")