From 92aa32574a3ecd988f2a0541a179ecb87af923a3 Mon Sep 17 00:00:00 2001 From: Yami An Date: Wed, 17 Jan 2024 14:51:54 +0700 Subject: [PATCH] 240117 --- lib/YANLib/Core/YANNum.Decimal.cs | 48 ++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Double.cs | 48 ++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Float.cs | 48 ++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Int.cs | 49 +++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Long.cs | 49 +++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Nint.cs | 49 +++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Nuint.cs | 49 +++++++++++++++++++++++++++++++ lib/YANLib/Core/YANNum.Sbyte.cs | 49 +++++++++++++++++++++++++++++++ 8 files changed, 389 insertions(+) diff --git a/lib/YANLib/Core/YANNum.Decimal.cs b/lib/YANLib/Core/YANNum.Decimal.cs index 4941c6f1..e1998160 100644 --- a/lib/YANLib/Core/YANNum.Decimal.cs +++ b/lib/YANLib/Core/YANNum.Decimal.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a decimal value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a decimal. + /// + /// The object to be converted to a decimal. Can be . + /// The default value to use if conversion fails. Can be . + /// The decimal value of the converted object, or the converted default value, or the default value of a decimal if both conversions fail. public static decimal ToDecimal(this object? val, object? dfltVal = null) { try @@ -16,13 +24,53 @@ public static decimal ToDecimal(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective decimal values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a decimal value; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to decimals. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of decimals representing the converted values, or if the input collection is or empty. public static IEnumerable? ToDecimals(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective decimal values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a decimal value; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to decimals. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of decimals representing the converted values, or if the input collection is or empty. public static IEnumerable? ToDecimals(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal)); + /// + /// Converts an array of objects to their respective decimal values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a decimal value; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to decimals. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of decimals representing the converted values, or if the input array is or empty. public static IEnumerable? ToDecimals(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDecimal(dfltVal)); + /// + /// Generates a random decimal value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, the full range of decimal values is considered. + /// + /// The minimum bound for the decimal value. Can be . + /// The maximum bound for the decimal value. Can be . + /// A randomly generated decimal value within the specified bounds. public static decimal GenerateRandomDecimal(object? min = null, object? max = null) => new Random().NextDecimal(min, max); + /// + /// Generates a collection of random decimal values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the full range of decimal values. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the decimal values. Can be . + /// The maximum bound for the decimal values. Can be . + /// The number of random decimal values to generate. Can be . + /// An enumerable collection of randomly generated decimal values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomDecimals(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDecimal(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Double.cs b/lib/YANLib/Core/YANNum.Double.cs index e3d46ed8..a8fa3959 100644 --- a/lib/YANLib/Core/YANNum.Double.cs +++ b/lib/YANLib/Core/YANNum.Double.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a double value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a double. + /// + /// The object to be converted to a double. Can be . + /// The default value to use if conversion fails. Can be . + /// The double value of the converted object, or the converted default value, or the default value of a double if both conversions fail. public static double ToDouble(this object? val, object? dfltVal = null) { try @@ -16,13 +24,53 @@ public static double ToDouble(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective double values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a double value; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to doubles. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of doubles representing the converted values, or if the input collection is or empty. public static IEnumerable? ToDoubles(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective double values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a double value; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to doubles. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of doubles representing the converted values, or if the input collection is or empty. public static IEnumerable? ToDoubles(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal)); + /// + /// Converts an array of objects to their respective double values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a double value; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to doubles. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of doubles representing the converted values, or if the input array is or empty. public static IEnumerable? ToDoubles(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToDouble(dfltVal)); + /// + /// Generates a random double value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, the full range of double values is considered. + /// + /// The minimum bound for the double value. Can be . + /// The maximum bound for the double value. Can be . + /// A randomly generated double value within the specified bounds. public static double GenerateRandomDouble(object? min = null, object? max = null) => new Random().NextDouble(min, max); + /// + /// Generates a collection of random double values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the full range of double values. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the double values. Can be . + /// The maximum bound for the double values. Can be . + /// The number of random double values to generate. Can be . + /// An enumerable collection of randomly generated double values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomDoubles(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomDouble(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Float.cs b/lib/YANLib/Core/YANNum.Float.cs index 08221e1f..f2103baa 100644 --- a/lib/YANLib/Core/YANNum.Float.cs +++ b/lib/YANLib/Core/YANNum.Float.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a float value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a float. + /// + /// The object to be converted to a float. Can be . + /// The default value to use if conversion fails. Can be . + /// The float value of the converted object, or the converted default value, or the default value of a float if both conversions fail. public static float ToFloat(this object? val, object? dfltVal = null) { try @@ -16,13 +24,53 @@ public static float ToFloat(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective float values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a float value; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to floats. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of floats representing the converted values, or if the input collection is or empty. public static IEnumerable? ToFloats(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective float values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a float value; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to floats. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of floats representing the converted values, or if the input collection is or empty. public static IEnumerable? ToFloats(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal)); + /// + /// Converts an array of objects to their respective float values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a float value; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to floats. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of floats representing the converted values, or if the input array is or empty. public static IEnumerable? ToFloats(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToFloat(dfltVal)); + /// + /// Generates a random float value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, the full range of float values is considered. + /// + /// The minimum bound for the float value. Can be . + /// The maximum bound for the float value. Can be . + /// A randomly generated float value within the specified bounds. public static float GenerateRandomFloat(object? min = null, object? max = null) => new Random().NextSingle(min, max); + /// + /// Generates a collection of random float values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the full range of float values. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the float values. Can be . + /// The maximum bound for the float values. Can be . + /// The number of random float values to generate. Can be . + /// An enumerable collection of randomly generated float values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomFloats(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomFloat(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Int.cs b/lib/YANLib/Core/YANNum.Int.cs index c94c9367..366d97b4 100644 --- a/lib/YANLib/Core/YANNum.Int.cs +++ b/lib/YANLib/Core/YANNum.Int.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to an integer value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of an integer. + /// + /// The object to be converted to an integer. Can be . + /// The default value to use if conversion fails. Can be . + /// The integer value of the converted object, or the converted default value, or the default value of an integer if both conversions fail. public static int ToInt(this object? val, object? dfltVal = null) { try @@ -16,12 +24,44 @@ public static int ToInt(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective integer values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to an integer value; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToInts(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToInt(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective integer values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to an integer value; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToInts(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToInt(dfltVal)); + /// + /// Converts an array of objects to their respective integer values. + /// If the array is or empty, returns . + /// Each object in the array is converted to an integer value; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of integers representing the converted values, or if the input array is or empty. public static IEnumerable? ToInts(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToInt(dfltVal)); + /// + /// Generates a random integer value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, defaults to the minimum and maximum limits of an integer. + /// If the minimum value is greater than the maximum, returns the default integer value. + /// + /// The minimum bound for the integer value. Can be . + /// The maximum bound for the integer value. Can be . + /// A randomly generated integer value within the specified bounds, or the default value of an integer if bounds are invalid. public static int GenerateRandomInt(object? min = null, object? max = null) { var minValue = min.IsNull() ? int.MinValue : min.ToInt(); @@ -30,5 +70,14 @@ public static int GenerateRandomInt(object? min = null, object? max = null) return minValue > maxValue ? default : new Random().Next(minValue, maxValue); } + /// + /// Generates a collection of random integer values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the minimum and maximum limits of an integer. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the integer values. Can be . + /// The maximum bound for the integer values. Can be . + /// The number of random integer values to generate. Can be . + /// An enumerable collection of randomly generated integer values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomInts(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomInt(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Long.cs b/lib/YANLib/Core/YANNum.Long.cs index 018f7509..93f8a15e 100644 --- a/lib/YANLib/Core/YANNum.Long.cs +++ b/lib/YANLib/Core/YANNum.Long.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a long (Int64) value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a long. + /// + /// The object to be converted to a long. Can be . + /// The default value to use if conversion fails. Can be . + /// The long value of the converted object, or the converted default value, or the default value of a long if both conversions fail. public static long ToLong(this object? val, object? dfltVal = null) { try @@ -16,12 +24,44 @@ public static long ToLong(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective long (Int64) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a long value; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to longs. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of longs representing the converted values, or if the input collection is or empty. public static IEnumerable? ToLongs(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToLong(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective long (Int64) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a long value; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to longs. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of longs representing the converted values, or if the input collection is or empty. public static IEnumerable? ToLongs(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToLong(dfltVal)); + /// + /// Converts an array of objects to their respective long (Int64) values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a long value; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to longs. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of longs representing the converted values, or if the input array is or empty. public static IEnumerable? ToLongs(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToLong(dfltVal)); + /// + /// Generates a random long (Int64) value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, defaults to the minimum and maximum limits of a long. + /// If the minimum value is greater than the maximum, returns the default long value. + /// + /// The minimum bound for the long value. Can be . + /// The maximum bound for the long value. Can be . + /// A randomly generated long value within the specified bounds, or the default value of a long if bounds are invalid. public static long GenerateRandomLong(object? min = null, object? max = null) { var minValue = min.IsNull() ? long.MinValue : min.ToLong(); @@ -30,5 +70,14 @@ public static long GenerateRandomLong(object? min = null, object? max = null) return minValue > maxValue ? default : new Random().NextInt64(minValue, maxValue); } + /// + /// Generates a collection of random long values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the minimum and maximum limits of a long. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the long values. Can be . + /// The maximum bound for the long values. Can be . + /// The number of random long values to generate. Can be . + /// An enumerable collection of randomly generated long values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomLongs(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomLong(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Nint.cs b/lib/YANLib/Core/YANNum.Nint.cs index 0ec379e7..6817e7ac 100644 --- a/lib/YANLib/Core/YANNum.Nint.cs +++ b/lib/YANLib/Core/YANNum.Nint.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a native integer (nint) value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a native integer. + /// + /// The object to be converted to a native integer. Can be . + /// The default value to use if conversion fails. Can be . + /// The native integer value of the converted object, or the converted default value, or the default value of a native integer if both conversions fail. public static nint ToNint(this object? val, object? dfltVal = null) { try @@ -16,12 +24,44 @@ public static nint ToNint(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective native integer (nint) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a native integer; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of native integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToNints(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNint(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective native integer (nint) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a native integer; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of native integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToNints(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNint(dfltVal)); + /// + /// Converts an array of objects to their respective native integer (nint) values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a native integer; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of native integers representing the converted values, or if the input array is or empty. public static IEnumerable? ToNints(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNint(dfltVal)); + /// + /// Generates a random native integer (nint) value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, defaults to the minimum and maximum limits of a native integer. + /// If the minimum value is greater than the maximum, returns the default native integer value. + /// + /// The minimum bound for the native integer value. Can be . + /// The maximum bound for the native integer value. Can be . + /// A randomly generated native integer value within the specified bounds, or the default value of a native integer if bounds are invalid. public static nint GenerateRandomNint(object? min = null, object? max = null) { var minValue = min.IsNull() ? nint.MinValue : min.ToNint(); @@ -30,5 +70,14 @@ public static nint GenerateRandomNint(object? min = null, object? max = null) return minValue > maxValue ? default : new Random().NextInt64(minValue, maxValue).ToNint(); } + /// + /// Generates a collection of random native integer values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the minimum and maximum limits of a native integer. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the native integer values. Can be . + /// The maximum bound for the native integer values. Can be . + /// The number of random native integer values to generate. Can be . + /// An enumerable collection of randomly generated native integer values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomNints(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomNint(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Nuint.cs b/lib/YANLib/Core/YANNum.Nuint.cs index 5c81635e..a985eff9 100644 --- a/lib/YANLib/Core/YANNum.Nuint.cs +++ b/lib/YANLib/Core/YANNum.Nuint.cs @@ -5,6 +5,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to an unsigned native integer (nuint) value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of an unsigned native integer. + /// + /// The object to be converted to an unsigned native integer. Can be . + /// The default value to use if conversion fails. Can be . + /// The unsigned native integer value of the converted object, or the converted default value, or the default value of an unsigned native integer if both conversions fail. public static nuint ToNuint(this object? val, object? dfltVal = null) { try @@ -17,12 +25,44 @@ public static nuint ToNuint(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective unsigned native integer (nuint) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to an unsigned native integer; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to unsigned native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of unsigned native integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToNuints(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNuint(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective unsigned native integer (nuint) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to an unsigned native integer; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to unsigned native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of unsigned native integers representing the converted values, or if the input collection is or empty. public static IEnumerable? ToNuints(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNuint(dfltVal)); + /// + /// Converts an array of objects to their respective unsigned native integer (nuint) values. + /// If the array is or empty, returns . + /// Each object in the array is converted to an unsigned native integer; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to unsigned native integers. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of unsigned native integers representing the converted values, or if the input array is or empty. public static IEnumerable? ToNuints(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToNuint(dfltVal)); + /// + /// Generates a random unsigned native integer (nuint) value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, defaults to the minimum and maximum limits of an unsigned native integer. + /// If the minimum value is greater than the maximum, returns the default unsigned native integer value. + /// + /// The minimum bound for the unsigned native integer value. Can be . + /// The maximum bound for the unsigned native integer value. Can be . + /// A randomly generated unsigned native integer value within the specified bounds, or the default value of an unsigned native integer if bounds are invalid. public static nuint GenerateRandomNuint(object? min = null, object? max = null) { var minValue = min.IsNull() ? nuint.MinValue : min.ToNuint(); @@ -31,5 +71,14 @@ public static nuint GenerateRandomNuint(object? min = null, object? max = null) return minValue > maxValue ? default : (new Random().NextInt64(nint.MinValue, (long)(maxValue - (minValue - (BigInteger)nint.MinValue))) - nint.MinValue).ToNuint() + minValue; } + /// + /// Generates a collection of random unsigned native integer values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the minimum and maximum limits of an unsigned native integer. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the unsigned native integer values. Can be . + /// The maximum bound for the unsigned native integer values. Can be . + /// The number of random unsigned native integer values to generate. Can be . + /// An enumerable collection of randomly generated unsigned native integer values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomNuints(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomNuint(min, max)); } diff --git a/lib/YANLib/Core/YANNum.Sbyte.cs b/lib/YANLib/Core/YANNum.Sbyte.cs index 61c8464c..e10c8f36 100644 --- a/lib/YANLib/Core/YANNum.Sbyte.cs +++ b/lib/YANLib/Core/YANNum.Sbyte.cs @@ -4,6 +4,14 @@ namespace YANLib.Core; public static partial class YANNum { + /// + /// Converts the specified object to a signed byte (sbyte) value. + /// If the conversion fails, attempts to use the provided default value for conversion. + /// If both conversions fail, returns the default value of a signed byte. + /// + /// The object to be converted to a signed byte. Can be . + /// The default value to use if conversion fails. Can be . + /// The signed byte value of the converted object, or the converted default value, or the default value of a signed byte if both conversions fail. public static sbyte ToSbyte(this object? val, object? dfltVal = null) { try @@ -16,12 +24,44 @@ public static sbyte ToSbyte(this object? val, object? dfltVal = null) } } + /// + /// Converts a collection of objects to their respective signed byte (sbyte) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a signed byte; if conversion fails, uses the provided default value. + /// + /// The collection of objects to be converted to signed bytes. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of signed bytes representing the converted values, or if the input collection is or empty. public static IEnumerable? ToSbytes(this IEnumerable? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToSbyte(dfltVal)); + /// + /// Converts a collection (ICollection) of objects to their respective signed byte (sbyte) values. + /// If the collection is or empty, returns . + /// Each object in the collection is converted to a signed byte; if conversion fails, uses the provided default value. + /// + /// The ICollection of objects to be converted to signed bytes. Can be . + /// The default value to use if conversion fails. Can be . + /// An enumerable collection of signed bytes representing the converted values, or if the input collection is or empty. public static IEnumerable? ToSbytes(this ICollection? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToSbyte(dfltVal)); + /// + /// Converts an array of objects to their respective signed byte (sbyte) values. + /// If the array is or empty, returns . + /// Each object in the array is converted to a signed byte; if conversion fails, uses the provided default value. + /// + /// The array of objects to be converted to signed bytes. Can be . + /// The default value to use if conversion fails. Can be . + /// An array of signed bytes representing the converted values, or if the input array is or empty. public static IEnumerable? ToSbytes(this object?[]? vals, object? dfltVal = null) => vals.IsEmptyOrNull() ? default : vals.Select(x => x.ToSbyte(dfltVal)); + /// + /// Generates a random signed byte (sbyte) value within the specified minimum and maximum bounds. + /// If the minimum or maximum values are invalid or unspecified, defaults to the minimum and maximum limits of a signed byte. + /// If the minimum value is greater than the maximum, returns the default signed byte value. + /// + /// The minimum bound for the signed byte value. Can be . + /// The maximum bound for the signed byte value. Can be . + /// A randomly generated signed byte value within the specified bounds, or the default value of a signed byte if bounds are invalid. public static sbyte GenerateRandomSbyte(object? min = null, object? max = null) { var minValue = min.IsNull() ? sbyte.MinValue : min.ToSbyte(); @@ -30,5 +70,14 @@ public static sbyte GenerateRandomSbyte(object? min = null, object? max = null) return minValue > maxValue ? default : new Random().Next(minValue, maxValue).ToSbyte(); } + /// + /// Generates a collection of random signed byte values within specified minimum and maximum bounds, and of a specified size. + /// If the minimum or maximum values are invalid or unspecified, uses the minimum and maximum limits of a signed byte. + /// If the size is unspecified, defaults to a collection of size 0. + /// + /// The minimum bound for the signed byte values. Can be . + /// The maximum bound for the signed byte values. Can be . + /// The number of random signed byte values to generate. Can be . + /// An enumerable collection of randomly generated signed byte values within the specified bounds and of the specified size. public static IEnumerable GenerateRandomSbytes(object? min = null, object? max = null, object? size = null) => Range(0, size.ToUint().ToInt()).Select(i => GenerateRandomSbyte(min, max)); }