private static bool[] MergeNulls(XArray xarray, bool[] couldNotConvert, ref bool[] buffer) { // No nulls? Just return the values which didn't convert if (!xarray.HasNulls) { return(couldNotConvert); } if (couldNotConvert == null) { return(XArray.RemapNulls(xarray, ref buffer)); } // Or together the nulls and values which didn't convert if (xarray.Selector.Indices != null) { for (int i = 0; i < xarray.Count; ++i) { couldNotConvert[i] |= xarray.NullRows[xarray.Index(i)]; } } else { int start = xarray.Selector.StartIndexInclusive; for (int i = 0; i < xarray.Count; ++i) { couldNotConvert[i] |= xarray.NullRows[i + start]; } } return(couldNotConvert); }
public static Func <XArray, XArray> NegatedTryConvertToConverter(NegatedTryConvert negatedTryConvert, string errorContextMessage, ValueKinds errorOn = ValueKindsDefaults.ErrorOn, ValueKinds changeToDefault = ValueKindsDefaults.ChangeToDefault) { if (negatedTryConvert == null) { return(null); } Array result; bool[] couldNotConvert; bool[] buffer = null; if (changeToDefault == ValueKinds.None) { // Invalid/Null/Empty -> Null, so couldNotConvert becomes IsNull return((values) => { couldNotConvert = negatedTryConvert(values, out result); ErrorWhenSpecified(errorOn, values, couldNotConvert, errorContextMessage); return XArray.All(result, values.Count, MergeNulls(values, couldNotConvert, ref buffer), values.Selector.IsSingleValue); }); } else if (changeToDefault == ValueKinds.Invalid) { // Invalid -> Default, so keep nulls from source return((values) => { couldNotConvert = negatedTryConvert(values, out result); ErrorWhenSpecified(errorOn, values, couldNotConvert, errorContextMessage); return XArray.All(result, values.Count, XArray.RemapNulls(values, ref couldNotConvert), values.Selector.IsSingleValue); }); } else if (changeToDefault == ValueKinds.InvalidOrNull) { // Invalid/Null/Empty -> Default, so negate all nulls return((values) => { couldNotConvert = negatedTryConvert(values, out result); ErrorWhenSpecified(errorOn, values, couldNotConvert, errorContextMessage); return XArray.All(result, values.Count, null, values.Selector.IsSingleValue); }); } else { throw new NotImplementedException(changeToDefault.ToString()); } }
private XArray Convert(XArray xarray) { // If a single value was returned, only convert it if (xarray.Selector.IsSingleValue) { Allocator.AllocateToSize(ref _buffer, 1); _buffer[0] = _function(((T[])xarray.Array)[0]); return(XArray.Single(_buffer, xarray.Count)); } // Allocate for results Allocator.AllocateToSize(ref _buffer, xarray.Count); // Convert each non-null value T[] array = (T[])xarray.Array; for (int i = 0; i < xarray.Count; ++i) { int index = xarray.Index(i); bool rowIsNull = (xarray.HasNulls && xarray.NullRows[index]); _buffer[i] = (rowIsNull ? default(U) : _function(array[index])); } return(XArray.All(_buffer, xarray.Count, XArray.RemapNulls(xarray, ref _isNull))); }