github.com/dnephin/dobi@v0.15.0/tasks/task/stack.go (about) 1 package task 2 3 import ( 4 "fmt" 5 ) 6 7 // Stack is a LIFO stack of strings 8 type Stack struct { 9 data []Name 10 } 11 12 // Contains returns true if an item is in the stack, false otherwise 13 func (s *Stack) Contains(item Name) bool { 14 for _, elem := range s.data { 15 if elem.Equal(item) { 16 return true 17 } 18 } 19 return false 20 } 21 22 // Push adds an item to the top of the stack 23 func (s *Stack) Push(item Name) { 24 s.data = append(s.data, item) 25 } 26 27 // Pop removes an item from the top of the stack 28 func (s *Stack) Pop() (Name, error) { 29 var item Name 30 last := len(s.data) - 1 31 if last < 0 { 32 return Name{}, fmt.Errorf("can't pop empty stack") 33 } 34 item, s.data = s.data[last], s.data[:last] 35 return item, nil 36 } 37 38 // Reset removes all items from the stack 39 func (s *Stack) Reset() { 40 s.data = []Name{} 41 } 42 43 // Items returns all the items in the stack in order 44 func (s *Stack) Items() []Name { 45 return s.data 46 } 47 48 // Names returns all the name of the items in the stack in order 49 func (s *Stack) Names() []string { 50 names := []string{} 51 for _, taskName := range s.data { 52 names = append(names, taskName.Name()) 53 } 54 return names 55 } 56 57 // NewStack returns a new empty stack 58 func NewStack() *Stack { 59 return &Stack{data: []Name{}} 60 }