forked from govend/govend
/
stack.go
46 lines (38 loc) · 823 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
// Copyright 2016 govend. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package deps
type dep struct {
path string
level int
}
type stack struct {
data []dep
}
func newStack(values ...string) *stack {
limit := len(values) * 2
s := &stack{data: make([]dep, 0, limit)}
for i := len(values) - 1; i >= 0; i-- {
s.push(0, values[i])
}
return s
}
func (s stack) empty() bool {
return s.len() == 0
}
func (s stack) len() int {
return len(s.data)
}
func (s *stack) push(level int, values ...string) {
for _, val := range values {
s.data = append(s.data, dep{path: val, level: level})
}
}
func (s *stack) pop() dep {
if s.len() > 0 {
last := s.data[s.len()-1]
s.data = s.data[:s.len()-1]
return last
}
return dep{}
}