From 9a3bfca3131b7924392897b01f6a449322ca8d2e Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 2 Feb 2025 12:42:29 -0500 Subject: [PATCH] Renamed unique_append to uniqueAppend --- regex/misc.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/regex/misc.go b/regex/misc.go index 7b105bd..2d21e61 100644 --- a/regex/misc.go +++ b/regex/misc.go @@ -50,7 +50,7 @@ func isNormalChar(c rune) bool { // 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 unique_append[T comparable](slc []T, items ...T) ([]T, int) { +func uniqueAppend[T comparable](slc []T, items ...T) ([]T, int) { num_appended := 0 for _, item := range items { if !slices.Contains(slc, item) { @@ -61,6 +61,25 @@ func unique_append[T comparable](slc []T, items ...T) ([]T, int) { 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]