12345678910111213141516171819202122232425262728293031323334353637 |
- package node
- // Delete an item from an array, returning item and true on success.
- func arrayDelete[T any](stack *[]T, pos int) (T, bool) {
- // log.Printf("Array %d, %d\n", len(*stack), cap(*stack))
- var result T
- /*
- https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
- https://github.com/golang/go/wiki/SliceTricks
- */
- if pos < 0 || pos > len(*stack) {
- return result, false
- }
- result = (*stack)[pos]
- copy((*stack)[pos:], (*stack)[pos+1:])
- // var temp T
- // (*stack)[len(*stack)-1] = temp
- *stack = (*stack)[:len(*stack)-1]
- return result, true
- }
- // Pop items from head of array, return true on success.
- func arrayPop[T any](stack *[]T, count int) bool {
- /*
- https://stackoverflow.com/questions/33834742/remove-and-adding-elements-to-array-in-go-lang
- https://github.com/golang/go/wiki/SliceTricks
- */
- if count < 0 || count > len(*stack) {
- return false
- }
- copy((*stack)[0:], (*stack)[count:])
- // var temp T
- // (*stack)[len(*stack)-1] = temp
- *stack = (*stack)[:len(*stack)-count]
- return true
- }
|