public static SimpleList <T> SimpleTakeWhile <T>(this T[] source, Func <T, bool> predicate) { if (source == null) { throw new InvalidOperationException("Invalid null object"); } var result = new T[8]; var length = 8; var count = 0; int log = IntExtensions.Log2((uint)source.Length) - 3; log = log > 2 ? (log - (log % 2)) : 2; for (int i = 0; i < source.Length; i++) { if (!predicate(source[i])) { break; } if (count >= length) { EnlargeExtensions.LogEnlargeArray(2, source.Length, ref result, ref log, out length); } result[count] = source[i]; count++; } var final = new SimpleList <T>(); final.Items = result; final.Count = count; return(final); }
public static SimpleList <T> ToSimpleList <T>(this IEnumerable <T> enumerable, EnlargingCoefficient coefficient = EnlargingCoefficient.By2) { var shift = coefficient switch { EnlargingCoefficient.By2 => 1, EnlargingCoefficient.By4 => 2, EnlargingCoefficient.By8 => 3, _ => 1 }; using var enumerator = enumerable.GetEnumerator(); var current = 0; var currentLength = 8; var result = new T[8]; while (enumerator.MoveNext()) { if (current >= currentLength) { EnlargeExtensions.LogEnlargeArray(shift, ref result, ref currentLength); } result[current] = enumerator.Current; current++; } var simpleList = new SimpleList <T>(); simpleList.Items = result; simpleList.Count = current; return(simpleList); }
public static SimpleList <TResult> SimpleOfType <TSource, TResult>(this IList <TSource> source) { if (source == null) { throw new InvalidOperationException("Invalid null object"); } var sourceCount = source.Count; var result = new TResult[8]; var length = 8; var count = 0; int log = IntExtensions.Log2((uint)sourceCount) - 3; log = log > 2 ? (log - (log % 2)) : 2; for (int i = 0; i < sourceCount; i++) { if (!(source[i] is TResult retyped)) { continue; } if (count >= length) { EnlargeExtensions.LogEnlargeArray(2, sourceCount, ref result, ref log, out length); } result[count] = retyped; count++; } var final = new SimpleList <TResult>(); final.Items = result; final.Count = count; return(final); }