stash/pkg/utils/int_collections.go
WithoutPants 1e04deb3d4
Data layer restructuring (#997)
* Move query builders to sqlite package
* Add transaction system
* Wrap model resolvers in transaction
* Add error return value for StringSliceToIntSlice
* Update/refactor mutation resolvers
* Convert query builders
* Remove unused join types
* Add stash id unit tests
* Use WAL journal mode
2021-01-18 12:23:20 +11:00

52 lines
1.3 KiB
Go

package utils
// IntIndex returns the first index of the provided int value in the provided
// int slice. It returns -1 if it is not found.
func IntIndex(vs []int, t int) int {
for i, v := range vs {
if v == t {
return i
}
}
return -1
}
// IntInclude returns true if the provided int value exists in the provided int
// slice.
func IntInclude(vs []int, t int) bool {
return IntIndex(vs, t) >= 0
}
// IntAppendUnique appends toAdd to the vs int slice if toAdd does not already
// exist in the slice. It returns the new or unchanged int slice.
func IntAppendUnique(vs []int, toAdd int) []int {
if IntInclude(vs, toAdd) {
return vs
}
return append(vs, toAdd)
}
// IntAppendUniques appends a slice of int values to the vs int slice. It only
// appends values that do not already exist in the slice. It returns the new or
// unchanged int slice.
func IntAppendUniques(vs []int, toAdd []int) []int {
for _, v := range toAdd {
vs = IntAppendUnique(vs, v)
}
return vs
}
// IntExclude removes all instances of any value in toExclude from the vs int
// slice. It returns the new or unchanged int slice.
func IntExclude(vs []int, toExclude []int) []int {
var ret []int
for _, v := range vs {
if !IntInclude(toExclude, v) {
ret = append(ret, v)
}
}
return ret
}