package narr import ( "git.noahlan.cn/noahlan/ntool/ndef" "git.noahlan.cn/noahlan/ntool/nmath" "reflect" "strings" ) // NotIn check the given value whether not in the list func NotIn[T ndef.ScalarType](list []T, value T) bool { return !In(list, value) } // In check the given value whether in the list func In[T ndef.ScalarType](list []T, value T) bool { for _, elem := range list { if elem == value { return true } } return false } // ContainsAll check given values is sub-list of sample list. func ContainsAll[T ndef.ScalarType](list, values []T) bool { return IsSubList(values, list) } // IsSubList check given values is sub-list of sample list. func IsSubList[T ndef.ScalarType](values, list []T) bool { for _, value := range values { if !In(list, value) { return false } } return true } // IsParent check given values is parent-list of samples. func IsParent[T ndef.ScalarType](values, list []T) bool { return IsSubList(list, values) } // StringsHas check the []string contains the given element func StringsHas(ss []string, val string) bool { return In(ss, val) } // IntsHas check the []int contains the given value func IntsHas(ints []int, val int) bool { return In(ints, val) } // Int64sHas check the []int64 contains the given value func Int64sHas(ints []int64, val int64) bool { return In(ints, val) } // HasValue check array(strings, intXs, uintXs) should be contained the given value(int(X),string). func HasValue(arr, val any) bool { return Contains(arr, val) } // Contains check slice/array(strings, intXs, uintXs) should be contained the given value(int(X),string). // // TIP: Difference the In(), Contains() will try to convert value type, // and Contains() support array type. func Contains(arr, val any) bool { if val == nil || arr == nil { return false } // if is string value if strVal, ok := val.(string); ok { if ss, ok := arr.([]string); ok { return StringsHas(ss, strVal) } rv := reflect.ValueOf(arr) if rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array { for i := 0; i < rv.Len(); i++ { if v, ok := rv.Index(i).Interface().(string); ok && strings.EqualFold(v, strVal) { return true } } } return false } // as int value intVal, err := nmath.Int64(val) if err != nil { return false } if int64s, err := ToInt64s(arr); err == nil { return Int64sHas(int64s, intVal) } return false } // NotContains check array(strings, ints, uints) should be not contains the given value. func NotContains(arr, val any) bool { return !Contains(arr, val) }