-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.go
47 lines (40 loc) · 894 Bytes
/
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
package kiteroot
// Stack implements the stack container
type Stack []*Element
// Push adds the given element to the stack.
func (s *Stack) Push(e *Element) {
*s = append(*s, e)
}
// Top returns the last element in the stack if it is not empty.
// Otherwise, nil is returned.
func (s *Stack) Top() (e *Element) {
if s.Len() == 0 {
return nil
}
return (*s)[s.Len()-1]
}
// Pop removes top element from stack and returns it. if stack is empty,
// nil is returned.
func (s *Stack) Pop() (e *Element) {
if s.Len() > 0 {
n := s.Len() - 1
e, *s = (*s)[n], (*s)[:n]
return
}
return nil
}
// Len returns the number of element currently in the stack.
func (s *Stack) Len() int {
return len(*s)
}
func (s *Stack) existsTag(tag string) bool {
for _, e := range *s {
if e == nil {
continue
}
if e.Type == TagType && e.Content == tag {
return true
}
}
return false
}