Renamed package 'greg' to 'regex'
This commit is contained in:
28
regex/sliceQueue.go
Normal file
28
regex/sliceQueue.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package regex
|
||||
|
||||
import "errors"
|
||||
|
||||
// Helper functions for slices, to make them behave more like stacks
|
||||
func peek[T any](s []T) (T, error) {
|
||||
if len(s) < 1 {
|
||||
return *new(T), errors.New("stack empty")
|
||||
}
|
||||
return s[len(s)-1], nil
|
||||
}
|
||||
|
||||
func mustPop[T any](sp *[]T) T {
|
||||
val, err := pop(sp)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func pop[T any](sp *[]T) (T, error) {
|
||||
if len(*sp) < 1 {
|
||||
return *new(T), errors.New("stack empty")
|
||||
}
|
||||
to_return := (*sp)[len(*sp)-1]
|
||||
*sp = (*sp)[:len(*sp)-1]
|
||||
return to_return, nil
|
||||
}
|
Reference in New Issue
Block a user