feat: Adds Combine for arrays (#988)

This commit is contained in:
Kamron Batman 2022-04-06 15:34:47 -07:00 committed by GitHub
parent 8112602b88
commit 943b2eca36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1554,5 +1554,32 @@ namespace Server
min = date.Minute;
sec = date.Second;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Combine<T>(this IList<T> source, bool pooled = false, params IList<T>[] arrays)
{
var totalLength = source.Count;
foreach (var arr in arrays)
{
totalLength += arr.Count;
}
if (totalLength == 0)
{
return Array.Empty<T>();
}
var combined = pooled ? STArrayPool<T>.Shared.Rent(totalLength) : new T[totalLength];
source.CopyTo(combined, 0);
var position = source.Count;
foreach (var arr in arrays)
{
arr.CopyTo(combined, position);
position += arr.Count;
}
return combined;
}
}
}