-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.go
53 lines (43 loc) · 987 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
48
49
50
51
52
53
package json_markd
import (
"errors"
"sync"
"github.com/cheekybits/genny/generic"
)
// Components from https://flaviocopes.com/golang-data-structure-stack/
// Item the type of the stack
type item generic.Type
// ItemStack the stack of Items
type itemStack struct {
items []item
lock sync.RWMutex
}
// New creates a new ItemStack
func (s *itemStack) new() *itemStack {
s.items = []item{}
return s
}
// Push adds an Item to the top of the stack
func (s *itemStack) push(t item) {
s.lock.Lock()
s.items = append(s.items, t)
s.lock.Unlock()
}
func (s *itemStack) size() int {
return len(s.items)
}
func (s *itemStack) top() *item {
item := s.items[len(s.items)-1]
return &item
}
// Pop removes an Item from the top of the stack
func (s *itemStack) pop() (*item, error) {
s.lock.Lock()
if s.size() == 0 {
return nil, errors.New(".errors.stack_empty")
}
item := s.items[len(s.items)-1]
s.items = s.items[0 : len(s.items)-1]
s.lock.Unlock()
return &item, nil
}