github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/model/stack/stack.go (about)

     1  // Ultimate Provisioner: UP cmd
     2  // Copyright (c) 2019 Stephen Cheng and contributors
     3  
     4  /* This Source Code Form is subject to the terms of the Mozilla Public
     5   * License, v. 2.0. If a copy of the MPL was not distributed with this
     6   * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     7  
     8  package stack
     9  
    10  import (
    11  	"container/list"
    12  )
    13  
    14  var (
    15  	Stacks map[string]*ExecStack = map[string]*ExecStack{}
    16  )
    17  
    18  type ExecStack struct {
    19  	Stack *list.List
    20  }
    21  
    22  func New(id string) *ExecStack {
    23  	s := &ExecStack{
    24  		Stack: list.New(),
    25  	}
    26  	Stacks[id] = s
    27  	return s
    28  }
    29  
    30  /*if you would like to use the object to be pushed and
    31  assign value to it, you must push a pointer
    32  */
    33  func (s *ExecStack) Push(v interface{}) {
    34  	s.Stack.PushFront(v)
    35  }
    36  
    37  func (s *ExecStack) Pop() interface{} {
    38  	top := s.Stack.Front()
    39  	s.Stack.Remove(top)
    40  	return top.Value
    41  }
    42  
    43  func (s *ExecStack) GetTop() interface{} {
    44  	if s.GetLen() > 0 {
    45  		top := s.Stack.Front()
    46  		return top.Value
    47  	} else {
    48  		return nil
    49  	}
    50  }
    51  
    52  func (s *ExecStack) GetLen() int {
    53  	return s.Stack.Len()
    54  }