From e2e99ff6a93b7973869341774791565a69a1816f Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 6 Nov 2024 15:16:51 -0500 Subject: [PATCH] Added fnunction to generate numbers in a range; added capacity to some slices to prevent unnecessary reallocations --- misc.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/misc.go b/misc.go index 3f3ff02..282d65c 100644 --- a/misc.go +++ b/misc.go @@ -42,7 +42,7 @@ func assert(cond bool) { } func deleteFromSlice[T comparable](slc []T, val T) []T { - toReturn := make([]T, 0) + toReturn := make([]T, 0, len(slc)) for _, v := range slc { if v != val { toReturn = append(toReturn, v) @@ -77,7 +77,7 @@ func allEqual[T comparable](items ...T) bool { // Returns all elements in slice A that are NOT in slice B func setDifference[T comparable](s1 []T, s2 []T) []T { - toReturn := make([]T, 0) + toReturn := make([]T, 0, len(s1)) for _, val := range s1 { if !slices.Contains(s2, val) { toReturn = append(toReturn, val) @@ -110,3 +110,12 @@ func Reduce[T any](slc []T, fn func(T, T) T) T { } return slc[0] } + +// Generate numbers in a range - start (inclusive) to end (exclusive) +func genRange(start, end int) []int { + toRet := make([]int, end-start) + for i := start; i < end; i++ { + toRet[i-start] = i + } + return toRet +}