From 943b2eca3661679bc2f6f973ad18c10c7097816f Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Wed, 6 Apr 2022 15:34:47 -0700 Subject: [PATCH] feat: Adds Combine for arrays (#988) --- Projects/Server/Utilities/Utility.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Projects/Server/Utilities/Utility.cs b/Projects/Server/Utilities/Utility.cs index 30f25b577..6881fb03d 100644 --- a/Projects/Server/Utilities/Utility.cs +++ b/Projects/Server/Utilities/Utility.cs @@ -1554,5 +1554,32 @@ namespace Server min = date.Minute; sec = date.Second; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static T[] Combine(this IList source, bool pooled = false, params IList[] arrays) + { + var totalLength = source.Count; + foreach (var arr in arrays) + { + totalLength += arr.Count; + } + + if (totalLength == 0) + { + return Array.Empty(); + } + + var combined = pooled ? STArrayPool.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; + } } }