|
|
|
@ -48,49 +48,6 @@ func isNormalChar(c rune) bool {
|
|
|
|
|
return !slices.Contains(specialChars, c)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure that the given elements are only appended to the given slice if they
|
|
|
|
|
// don't already exist. Returns the new slice, and the number of unique items appended.
|
|
|
|
|
func uniqueAppend[T comparable](slc []T, items ...T) ([]T, int) {
|
|
|
|
|
num_appended := 0
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
if !slices.Contains(slc, item) {
|
|
|
|
|
slc = append(slc, item)
|
|
|
|
|
num_appended++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return slc, num_appended
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func uniqueAppendFunc[T any](slc []T, fn func(T, T) bool, items ...T) ([]T, int) {
|
|
|
|
|
toRet := make([]T, len(slc))
|
|
|
|
|
num_appended := 0
|
|
|
|
|
copy(toRet, slc)
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
itemExists := false
|
|
|
|
|
for _, val := range slc {
|
|
|
|
|
if fn(item, val) {
|
|
|
|
|
itemExists = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !itemExists {
|
|
|
|
|
toRet = append(toRet, item)
|
|
|
|
|
num_appended++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return toRet, num_appended
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true only if all the given elements are equal
|
|
|
|
|
func allEqual[T comparable](items ...T) bool {
|
|
|
|
|
first := items[0]
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
if item != first {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map function - convert a slice of T to a slice of V, based on a function
|
|
|
|
|
// that maps a T to a V
|
|
|
|
|
func funcMap[T, V any](slc []T, fn func(T) V) []V {
|
|
|
|
|