From 80bf8317c45a91e43b08ebf49e57673ded386d4b Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 15 Jun 2024 23:06:48 -0400 Subject: [PATCH] import ArrayExtensions from .NET Classic MBS.Framework --- .../src/lib/MBS.Core/ArrayExtensions.cs | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs diff --git a/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs b/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs new file mode 100644 index 0000000..179bf3f --- /dev/null +++ b/framework-dotnet/src/lib/MBS.Core/ArrayExtensions.cs @@ -0,0 +1,104 @@ +// +// ArrayExtensions.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace MBS.Core; + +public static class ArrayExtensions +{ + /// + /// Splits the into two arrays at the specified + /// , where contains the + /// elements before and + /// contains the elements after . + /// + /// The array to split. + /// The index at which to split the array. + /// The array containing elements before the split point. + /// The array containing elements after the split point. + /// The of elements in the array. + public static void Bisect(this T[] array, int index, out T[] left, out T[] right) + { + left = new T[index]; + right = new T[array.Length - index]; + + Array.Copy(array, 0, left, 0, left.Length); + Array.Copy(array, index, right, 0, right.Length); + } + public static void Array_RemoveAt(ref T[] array, int index, int count = 1) + { + T[] old = (T[])array.Clone(); + + int start = index; + int length = count; + if (count < 0) + { + start = index + count; + length = Math.Abs(count); + } + Array.Resize(ref array, old.Length - length); + + Array.Copy(old, 0, array, 0, start); + if (array.Length - (start + length) > -1) + Array.Copy(old, start + length, array, start, array.Length - (start + length)); + } + public static void Array_Append(ref T[] destinationArray, T[] sourceArray) + { + int start = destinationArray.Length; + Array.Resize(ref destinationArray, destinationArray.Length + sourceArray.Length); + Array.Copy(sourceArray, 0, destinationArray, start, sourceArray.Length); + } + + /// + /// Determines if and + /// contain the same elements. + /// + /// + /// if both arrays contain the same elements, + /// otherwise. + /// The first array to search. + /// The second array to search. + /// The 1st type parameter. + public static bool Matches(this IList array1, IList array2) + { + if (array1.Count != array2.Count) + { + // short-circuit if arrays have different lengths + return false; + } + + for (int i = 0; i < array1.Count; i++) + { + if (!array1[i].Equals(array2[i])) + return false; + } + return true; + } + + public static T[] Concat(T[] array1, T[] array2) + { + T[] array3 = new T[array1.Length + array2.Length]; + Array.Copy(array1, 0, array3, 0, array1.Length); + Array.Copy(array2, 0, array3, array1.Length, array2.Length); + return array3; + } +}