github.com/aergoio/aergo@v1.3.1/state/util.go (about)

     1  package state
     2  
     3  type stack []int
     4  
     5  func newStack() *stack {
     6  	return &stack{}
     7  }
     8  func (stk *stack) last() int {
     9  	return len(*stk) - 1
    10  }
    11  func (stk *stack) peek() int {
    12  	if stk == nil || len(*stk) == 0 {
    13  		return -1
    14  	}
    15  	return (*stk)[stk.last()]
    16  }
    17  func (stk *stack) pop() int {
    18  	if stk == nil || len(*stk) == 0 {
    19  		return -1
    20  	}
    21  	lst := stk.last()
    22  	top := (*stk)[lst]
    23  	*stk = (*stk)[:lst]
    24  	return top
    25  }
    26  func (stk *stack) push(args ...int) *stack {
    27  	if stk == nil {
    28  		stk = &stack{}
    29  	}
    30  	*stk = append(*stk, args...)
    31  	return stk
    32  }
    33  func (stk *stack) export() []int {
    34  	size := len(*stk)
    35  	if size <= 0 {
    36  		return nil
    37  	}
    38  	buf := make([]int, size)
    39  	for i, v := range *stk {
    40  		buf[size-i-1] = v
    41  	}
    42  	return buf
    43  }
    44  func (stk *stack) get(index int) int {
    45  	if index < 0 {
    46  		return -1
    47  	}
    48  	return (*stk)[index]
    49  }
    50  func (stk *stack) iter() func() int {
    51  	next := stk.last()
    52  	return func() int {
    53  		if next < 0 {
    54  			return -1
    55  		}
    56  		idx := next
    57  		next--
    58  		return (*stk)[idx]
    59  	}
    60  }