Documentation ¶
Overview ¶
Description: This package contains utility functions for working with slices.
Index ¶
- func CheckDuplicateValues[T comparable](slices ...[]T) error
- func ContainsAny(slice interface{}, element interface{}) (bool, error)
- func ContainsObjectIDMap(slice []primitive.ObjectID, element primitive.ObjectID) bool
- func ContainsStringLinear(slice []string, element string) bool
- func ContainsStringMap(slice []string, element string) bool
- func RemoveDuplicates[T comparable](slice []T) []T
- func RemoveDuplicatesObjectIDs(ids []primitive.ObjectID) []primitive.ObjectID
- func RemoveSliceElements[T comparable](slice1, slice2 []T) []T
- type SliceComparisonResult
- type SliceObjectIDComparisonResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDuplicateValues ¶ added in v1.13.0
func CheckDuplicateValues[T comparable](slices ...[]T) error
CheckDuplicateValues checks for duplicates across multiple slices of type T. Returns an error if at least one value is duplicated across a minimum of two slices or in the same slice.
func ContainsAny ¶ added in v1.11.12
ContainsAny checks if an element is present in the given slice.
This method uses reflection to work with slices of any type.
It has a time complexity of O(n), where n is the length of the slice.
func ContainsObjectIDMap ¶ added in v1.11.13
ContainsObjectIDMap checks if a slice contains a given element using a map for faster lookups.
This method has a time complexity of O(n) for creating the map, and O(1) for the lookup.
However, it has a space complexity of O(n), where n is the length of the slice.
It is suitable for large slices or when the slice is expected to contain the element.
func ContainsStringLinear ¶
ContainsStringLinear checks if a slice contains a given element using a linear search.
This method has a time complexity of O(n), where n is the length of the slice.
It is suitable for small slices or when the slice is not expected to contain the element.
func ContainsStringMap ¶
ContainsStringMap checks if a slice contains a given element using a map for faster lookups.
This method has a time complexity of O(n) for creating the map, and O(1) for the lookup.
However, it has a space complexity of O(n), where n is the length of the slice.
It is suitable for large slices or when the slice is expected to contain the element.
func RemoveDuplicates ¶ added in v1.13.0
func RemoveDuplicates[T comparable](slice []T) []T
RemoveDuplicates removes duplicates from a slice of any type using generics.
func RemoveDuplicatesObjectIDs ¶ added in v1.40.5
RemoveDuplicatesObjectIDs removes duplicates from a slice of primitive.ObjectID.
func RemoveSliceElements ¶ added in v1.13.10
func RemoveSliceElements[T comparable](slice1, slice2 []T) []T
RemoveSliceElements removes elements of the second slice from the first slice.
Types ¶
type SliceComparisonResult ¶ added in v1.40.0
type SliceComparisonResult[T comparable] struct { DifferenceA []T // Elements in sliceA but not in sliceB. DifferenceB []T // Elements in sliceB but not in sliceA. Similar []T // Elements present in both slices. }
SliceComparisonResult holds the differences and similarities between two slices.
func CompareSlices ¶ added in v1.40.0
func CompareSlices[T comparable](sliceA, sliceB []T) SliceComparisonResult[T]
CompareSlices compares two slices and returns a struct with the differences and similarities.
It works with slices of any comparable type and has a time complexity of O(n + m), where n is the length of sliceA and m is the length of sliceB.
type SliceObjectIDComparisonResult ¶ added in v1.40.0
type SliceObjectIDComparisonResult struct { DifferenceA []primitive.ObjectID // Elements in sliceA but not in sliceB. DifferenceB []primitive.ObjectID // Elements in sliceB but not in sliceA. Similar []primitive.ObjectID // Elements present in both slices. }
SliceObjectIDComparisonResult holds the differences and similarities between two slices of primitive.ObjectID.
func CompareObjectIDSlices ¶ added in v1.40.0
func CompareObjectIDSlices(sliceA, sliceB []primitive.ObjectID) SliceObjectIDComparisonResult
CompareObjectIDSlices compares two slices of primitive.ObjectID and returns a struct with the differences and similarities.