-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor codebase to use generic
Contains
function
- Replace multiple specific `In*` functions with a generic `Contains` function using generics - Remove the use of the `reflect` package - Simplify and consolidate test cases to use the new `Contains` function - Update benchmark tests to measure the performance of the `Contains` function - Remove redundant and unused functions and tests Signed-off-by: appleboy <[email protected]>
- Loading branch information
Showing
2 changed files
with
73 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,19 @@ | ||
package array | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// InMap check index in Map | ||
func InMap(needle string, haystack []string) bool { | ||
newStack := map[string]struct{}{} | ||
for _, val := range haystack { | ||
newStack[val] = struct{}{} | ||
} | ||
|
||
if _, ok := newStack[needle]; ok { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// InSlice check index in slice | ||
func InSlice(needle string, haystack []string) bool { | ||
for _, val := range haystack { | ||
if val == needle { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// InSliceInt64 check index in slice int64 | ||
func InSliceInt64(needle int64, haystack []int64) bool { | ||
for _, val := range haystack { | ||
if val == needle { | ||
// Contains checks if a given key of any comparable type exists within a slice. | ||
// It returns true if the key is found, otherwise it returns false. | ||
// | ||
// Parameters: | ||
// - slice: A slice of any comparable type T. | ||
// - key: An element of type T to search for within the slice. | ||
// | ||
// Returns: | ||
// - bool: True if the key is found in the slice, false otherwise. | ||
func Contains[T comparable](slice []T, key T) bool { | ||
for _, item := range slice { | ||
if item == key { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
// InArray index in array for interface{} | ||
func InArray(val interface{}, array interface{}) (exists bool, index int) { | ||
exists = false | ||
index = -1 | ||
|
||
switch reflect.TypeOf(array).Kind() { | ||
case reflect.Slice: | ||
s := reflect.ValueOf(array) | ||
|
||
for i := 0; i < s.Len(); i++ { | ||
if reflect.DeepEqual(val, s.Index(i).Interface()) { | ||
index = i | ||
exists = true | ||
return | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
// In check string in array. | ||
func In(needle string, haystack []string) ([]string, bool) { | ||
newHaystack := make([]string, len(haystack)) | ||
copy(newHaystack, haystack) | ||
|
||
if len(newHaystack) == 0 { | ||
return newHaystack, false | ||
} | ||
|
||
for i, val := range newHaystack { | ||
if val == needle { | ||
newHaystack = append(newHaystack[:i], newHaystack[i+1:]...) | ||
return newHaystack, true | ||
} | ||
} | ||
|
||
return newHaystack, false | ||
} | ||
|
||
// Diff show difference in two array. | ||
func Diff(s, t []string) []string { | ||
slice1 := make([]string, len(s)) | ||
slice2 := make([]string, len(t)) | ||
copy(slice1, s) | ||
copy(slice2, t) | ||
v := []string{} | ||
if len(slice1) == 0 && len(slice2) == 0 { | ||
return []string{} | ||
} | ||
|
||
if len(slice1) == 0 { | ||
return slice2 | ||
} | ||
|
||
if len(slice2) == 0 { | ||
return slice1 | ||
} | ||
|
||
if len(slice1) > len(slice2) { | ||
slice1, slice2 = slice2, slice1 | ||
} | ||
|
||
for _, val := range slice1 { | ||
if newT, ok := In(val, slice2); ok { | ||
slice2 = newT | ||
continue | ||
} | ||
|
||
v = append(v, val) | ||
} | ||
|
||
if len(slice2) > 0 { | ||
v = append(v, slice2...) | ||
} | ||
|
||
return v | ||
} |
Oops, something went wrong.